From 150f751aba6fb7b6697a65441d10ba29b96df021 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Wed, 13 Jan 2016 14:19:08 +0100 Subject: Initial --- .gitignore | 3 ++ functions.sh | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ gettemp.sh | 7 ++++ inctemp.sh | 3 ++ login.sh | 3 ++ logout.sh | 3 ++ 6 files changed, 123 insertions(+) create mode 100644 .gitignore create mode 100755 functions.sh create mode 100755 gettemp.sh create mode 100755 inctemp.sh create mode 100755 login.sh create mode 100755 logout.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..168ee3e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +credentials.txt +cookiejar.txt +csrftoken.txt diff --git a/functions.sh b/functions.sh new file mode 100755 index 0000000..857ae55 --- /dev/null +++ b/functions.sh @@ -0,0 +1,104 @@ +#!/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 +} + + +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 diff --git a/gettemp.sh b/gettemp.sh new file mode 100755 index 0000000..6283876 --- /dev/null +++ b/gettemp.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +source functions.sh + +declare -a temp +temp=($(fgettemp)) + +echo "current: ${temp[0]}; target: ${temp[1]}" diff --git a/inctemp.sh b/inctemp.sh new file mode 100755 index 0000000..e139e4d --- /dev/null +++ b/inctemp.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +source functions.sh +finctemp $@ || exit 1 diff --git a/login.sh b/login.sh new file mode 100755 index 0000000..69ad12b --- /dev/null +++ b/login.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +source functions.sh +flogin diff --git a/logout.sh b/logout.sh new file mode 100755 index 0000000..aeed34b --- /dev/null +++ b/logout.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +source functions.sh +flogout -- cgit v1.2.3-54-g00ecf