#!/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 '/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 '" <<<"$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 '
' <<<"$page")" tempimgs="$(grep -o ']*>' <<<"$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 '
' <<<"$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 '
' <<<"$page" | tail -n 4)" statenames="$(sed 's/.*setState(\("\)\?\([^)&]*\)\("\)\?).*/\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 | node -e 's="";process.stdin.on("data",function(d){s+=d.toString();});process.stdin.on("end",function(){o=JSON.parse(s);for(k in o){if(isNaN(+k))continue;for(K in o[k])console.log(("00"+k).slice(-2)+":"+("00"+K).slice(-2)+" "+o[k][K].M+" "+o[k][K].W);}})' } 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