vani battery change
[vaillant-thermostat] / temp3.sh
1 #!/bin/sh
2
3 # sudo apt-get install jq
4 # ln -s `pwd`/temp3.sh /var/www/html/temp3.sh 
5 # ln -s /dev/shm/temp3.png /var/www/html/temp3.png 
6 # ln -s /dev/shm/temp3-gray.png /var/www/html/temp3-gray.png 
7 #
8 # install as cgi-script and invoke to get png use http://localhost/temp3.sh?gray for kindle grayscale output
9
10 # use as http://localhost/temp3.sh?t=6h to show last 6h instead of default 48h
11
12 INTERVAL=`echo $QUERY_STRING | grep -i 't=[0-9]*h' | sed 's/.*t=\([0-9]*h\).*/\1/'`
13 test -z "$INTERVAL" && INTERVAL=48h
14
15 LINES=0
16 echo $QUERY_STRING | grep -i 'lines=' >/dev/null && LINES=1
17
18 : >/dev/shm/curl
19
20 influx_mqtt() {
21         topic=$1
22         shift
23         file=$1
24
25 #       echo "# $topic [$rids] -> $file"
26         cat <<__SHELL__ >> /dev/shm/curl
27 curl -G 'http://10.60.0.89:8086/query' --data-urlencode "db=telegraf" --data-urlencode "epoch=s" --data-urlencode "q=SELECT \"value\" FROM \"mqtt_stat\" WHERE time > now() - $INTERVAL AND \"topic\"='$topic' " | sed -e 's/^.*"values":\[\[//' -e 's/\],\[/\n/g' -e 's/,/ /g' -e 's/]].*$//' > /dev/shm/data.$file
28 __SHELL__
29 }
30
31 influx_rtl() {
32         file=$1
33         shift;
34         rids=`echo $* | sed -e 's/\([0-9][0-9]*\)/ or "id" = '"'"'\1'"'"'/g' -e 's/^ or/ AND /'`
35
36 #       echo "# $topic [$rids] -> $file"
37         cat <<__SHELL__ >> /dev/shm/curl
38 curl -G 'http://10.60.0.89:8086/query' --data-urlencode "db=telegraf" --data-urlencode "epoch=s" --data-urlencode "q=SELECT \"temperature_C\",\"humidity\" FROM \"mqtt_rtl_433\" WHERE time > now() - $INTERVAL and model="Prologue-TH" $rids" | sed -e 's/^.*"values":\[\[//' -e 's/\],\[/\n/g' -e 's/,/ /g' -e 's/]].*$//' > /dev/shm/data.$file
39 __SHELL__
40 }
41
42 influx_mqtt     'stat/boiler/DS18B20/temperature'       'DS18B20-t'
43 influx_mqtt     'stat/boiler/DHT22/temperature'         'DHT22-t'
44 influx_mqtt     'stat/boiler/DHT22/humidity'            'DHT22-h'
45
46 influx_mqtt     'stat/boiler/d'                         'boiler-d'
47
48 influx_rtl      'vani-t-h' 40 0
49 influx_rtl      'kutija-t-h' 59
50 influx_rtl      'kupaona-t-h' 125 148
51 influx_rtl      'kuhinja-t-h' 175 239
52 influx_rtl      'soba-t-h' 95
53
54 cat /dev/shm/curl | parallel
55
56
57 GRAY=`echo $QUERY_STRING | grep -i gray | wc -l`
58
59 tz_secs=`date +%:z | cut -d: -f1 | sed -e 's/^+//' -e 's/$/ * 3600/' | bc`
60
61 cd /home/pi/vaillant-thermostat/
62 gnuplot -e "gray=$GRAY ; lines=$LINES ; interval=\"$INTERVAL\" ; tz_secs=$tz_secs" temp3.gnuplot
63
64 if [ "$GRAY" = 1 ] ; then
65         convert /dev/shm/temp3.png -gravity center -extent 600x800 -colorspace Gray /dev/shm/temp3-gray.png
66         GRAY="-gray"
67 else
68         GRAY=""
69 fi
70
71 set > /dev/shm/temp3.cgi.vars
72
73 lines_checked=""
74 test "$LINES" = 1 && lines_checked="checked"
75
76 if echo $HTTP_USER_AGENT | grep Mozilla > /dev/null ; then
77         echo "Content-type: text/html\r\n\r\n"
78         cat << __HTML__
79 <body>
80 <form method="get" action="/temp3.sh" style="position: fixed; top: 0; left: 0">
81 <input type=submit name="t" value="48h">
82 <input type=submit name="t" value="24h">
83 <input type=submit name="t" value="12h">
84 <input type=submit name="t" value="6h">
85 <input type=submit name="t" value="1h">
86 <input type=checkbox name="lines" $lines_checked> <!-- $LINES -->
87 </form>
88 <img src="/temp3.png">
89 </body>
90 __HTML__
91         exit 0
92 fi
93
94 echo "Location: $REQUEST_SCHEME://$SERVER_NAME:$SERVER_PORT/temp3$GRAY.png"
95 echo
96
97 exit 0
98