summaryrefslogtreecommitdiff
path: root/functions.sh
blob: b24f80bdc9b4daab020d31cb9ec4e3536cc90432 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env bash
function flogin() {
	rm -f cookiejar.txt csrftoken.txt
	local authtoken
	authtoken="$(curl -s https://www.e-thermostaat.nl | grep -o -m 1 '<input name="authenticity_token.*\?/>' | sed 's/.*value="//g' | sed 's/".*$//g' | sed 's/=/%3D/g')"
	declare -a creds
	creds=($(fgetcredentials))
	curl -s -c cookiejar.txt 'https://www.e-thermostaat.nl/process_login?locale=nl&secure=true' -d "utf8=%E2%9C%93&authenticity_token=$authtoken&login=${creds[0]}&password=${creds[1]}&commit=Inloggen" >/dev/null
	fgetcsrftoken >csrftoken.txt
}

function flogout() {
	curl -s -c cookiejar.txt -b cookiejar.txt 'https://www.e-thermostaat.nl/logout' >/dev/null
	rm -f cookiejar.txt csrftoken.txt
}

function fgetcredentials() {
	#credentials.txt should be something like "username password".
	#code currently can't handle spaces in either.
	cat credentials.txt
}

function fensurelogin() {
	# fgetthermpage >/dev/null  #STUB
	curl -s -I -c cookiejar.txt -b cookiejar.txt 'https://www.e-thermostaat.nl/pages/index' | head -n 1 | grep -m 1 200 >/dev/null
	if test $? -ne 0; then
		flogin
	fi
}

function fgetthermpage() {
	local page
	while true; do
		page="$(curl -s -c cookiejar.txt -b cookiejar.txt 'https://www.e-thermostaat.nl/pages/thermostat')"
		if test -z "$(grep -o -m 1 redirected <<<"$page")"; then
			break
		fi
		flogin
	done
	cat <<<"$page"
}

function fgetcsrftoken() {
	fgetthermpage | grep '<meta' | grep 'csrf-token' | sed 's/.*content="\([^"]*\)".*/\1/'
}

function fcsrftoken() {
	if ! test -e csrftoken.txt; then
		flogin
	fi
	cat csrftoken.txt
}

function fcurlwithtoken() {
	curl -s -b cookiejar.txt -c cookiejar.txt -H "X-CSRF-token: $(fcsrftoken)" -e 'https://www.e-thermostaat.nl/pages/index' $@
}

#returns currenttemp targettemp.
#capture with declare -a temp; temp=($(fgettemp)); echo ${temp[0]} ${temp[1]}
function fgettemp() {
	local page images templateimgs tempcontext tempimgs tempsrcs tempstr currenttemp targettemp
	page="$(fgetthermpage)"
	images="$(grep -o "<img.*\?/>" <<<"$page")"
	templateimgs="$(grep 'height="1"' <<<"$images")"
	declare -A srcalt  #associative array
	for templateimg in $(tr -d ' ' <<<"$templateimgs"); do
		src="$(sed 's/.*src="\([^"]*\)".*/\1/' <<<"$templateimg")"
		alt="$(sed 's/.*alt="\([^"]*\)".*/\1/' <<<"$templateimg" | tr , .)"
		srcalt["$src"]="$alt"
	done
	tempcontext="$(grep -C 3 '<div \+id *= *"temperature">' <<<"$page")"
	tempimgs="$(grep -o '<img[^>]*>' <<<"$tempcontext" | grep -v "Thermostaat mode" | grep 'height="35"')"
	tempsrcs="$(sed 's/.*src="\([^"]*\)".*/\1/' <<<"$tempimgs")"
	tempstr=""
	for tempsrc in $tempsrcs; do
		tempstr+="${srcalt["$tempsrc"]}"
	done
	currenttemp="${tempstr:0:4}"
	targettemp="${tempstr:4:4}"
	echo "$currenttemp" "$targettemp"
}

#takes one argument, the temperature increment in floating point format.
#the system only accepts multiples of 0.5, though
function finctemp() {
	if ! [[ "$1" =~ ^-?[0-9]*(\.[05])?$ ]]; then
		return 1
	fi
	fensurelogin
	fcurlwithtoken -d "increment=$1" 'https://www.e-thermostaat.nl/processing/temperature' >/dev/null
}

#returns number in [1-4], the selected state
function fgetstate() {
	local page linklist selectedstate
	page="$(fgetthermpage)"
	linklist="$(grep -m 1 -C 4 '<div id="termostat-states">' <<<"$page" | tail -n 4)"
	selectedstate="$(sed 's/.*class="\([^"]*\)".*/\1/' <<<"$linklist" | grep -nm 1 selected | cut -d: -f1)"
	echo $selectedstate
}

#returns 4 words, the names of the 4 states
function fgetstatenames() {
	local page linklist statenames
	page="$(fgetthermpage)"
	linklist="$(grep -m 1 -C 4 '<div id="termostat-states">' <<<"$page" | tail -n 4)"
	statenames="$(sed 's/.*setState(\(&quot;\)\?\([^)&]*\)\(&quot;\)\?).*/\2/' <<<"$linklist")"
	echo $statenames
}

function fgetprogress() {
	fensurelogin
	json="$(fcurlwithtoken 'https://www.e-thermostaat.nl/pages/temperature_progress' | grep -m 1 'var json_data =' | sed 's/[^=]*= *\({.*}\);.*/\1/')"
	echo $json
}


function fissourced() {
	test ${BASH_SOURCE[0]} != $0
}

if ! fissourced; then
	if test $# -eq 0; then
		echo "functions.sh is meant to be sourced (or called with parameters, but that's only for advanced people)"
	else
		eval $@
	fi
fi