kuhinja 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.92: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//'`
35
36 #       echo "# $topic [$rids] -> $file"
37         cat <<__SHELL__ >> /dev/shm/curl
38 curl -G 'http://10.60.0.92: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' AND ( $rids )" | sed -e 's/^.*"values":\[\[//' -e 's/\],\[/\n/g' -e 's/,/ /g' -e 's/]].*$//' > /dev/shm/data.$file
39 __SHELL__
40 }
41
42 influx_tele() {
43         topic=$1
44         t_col=$2
45         h_col=$3
46         file=$4
47
48         cat <<__SHELL__ >> /dev/shm/curl
49 curl -G 'http://10.60.0.92:8086/query' --data-urlencode "db=telegraf" --data-urlencode "epoch=s" --data-urlencode "q=SELECT \"$t_col\",\"$h_col\" FROM \"mqtt_tele_sensor\" WHERE time > now() - $INTERVAL and topic='$topic'" | sed -e 's/^.*"values":\[\[//' -e 's/\],\[/\n/g' -e 's/,/ /g' -e 's/]].*$//' > /dev/shm/data.$file
50 __SHELL__
51 }
52
53 curl -G 'http://10.60.0.92:8086/query' --data-urlencode "db=rtl_433" --data-urlencode "q=SELECT time,id,temperature_C,humidity FROM \"Prologue-TH\" WHERE time > now() - $INTERVAL" -H "Accept: application/csv" --data-urlencode "epoch=s" \
54         | tee /dev/shm/rtl_433.th \
55         | awk -F, '{print $3" "$5" "$6 > "/dev/shm/id."$4 }'
56
57 psql_subvision() {
58         to=$1
59         echo "select extract(epoch from time), temperature, humidity from s_1 where time > now() - interval '$INTERVAL' order by time" | \
60                 ssh subvision psql -t -A eg5120 | sed 's/|/ /g' > /dev/shm/data.$to
61 }
62
63
64 rename_ids() {
65         to=$1
66         shift
67         while [ ! -z "$1" ] ; do
68                 if [ -e /dev/shm/id.$1 ] ; then
69                         cat /dev/shm/id.$1 > /dev/shm/data.$to
70                         rm  /dev/shm/id.$1
71                 else
72                         rm /dev/shm/data.$to
73                 fi
74                 shift
75         done
76 }
77
78 rename_ids      'vani-t-h' 156 # sunce
79 rename_ids      'kutija-t-h' 138 # hlad
80 #rename_ids     'kupaona-t-h' 78 15
81 psql_subvision  'kupaona-t-h'
82 rename_ids      'kuhinja-t-h' 144
83 rename_ids      'soba-t-h' 251
84 rename_ids      'hodnik-t-h' 234
85
86
87 influx_mqtt     'stat/boiler/DS18B20/temperature'       'DS18B20-t'
88 influx_mqtt     'stat/boiler/DHT22/temperature'         'DHT22-t'
89 influx_mqtt     'stat/boiler/DHT22/humidity'            'DHT22-h'
90
91 #influx_tele    'tele/ir/SENSOR' 'AM2301_Temperature' 'AM2301_Humidity' 'ir-dht22-t-h'
92 #influx_tele    'tele/dht11-ir-hdmi/SENSOR' 'DHT11_Temperature' 'DHT11_Humidity' 'stol-dht11-t-h'
93
94 influx_mqtt     'stat/boiler/d'                         'boiler-d'
95
96 #influx_rtl     'vani-t-h' 0 165 234
97 #influx_rtl     'kutija-t-h' 104 228 241 # hlad
98 #influx_rtl     'kupaona-t-h' 114 98
99 #influx_rtl     'kuhinja-t-h' 199 27
100 #influx_rtl     'soba-t-h' 59 252
101
102 cat /dev/shm/curl | parallel
103
104 GRAY=`echo $QUERY_STRING | grep -i gray | wc -l`
105
106 tz_secs=`date +%:z | cut -d: -f1 | sed -e 's/^+//' -e 's/$/ * 3600/' | bc`
107
108 cd /home/pi/vaillant-thermostat/
109 gnuplot -e "gray=$GRAY ; lines=$LINES ; interval=\"$INTERVAL\" ; tz_secs=$tz_secs" temp3.gnuplot
110
111 if [ "$GRAY" = 1 ] ; then
112         convert /dev/shm/temp3.png -gravity center -extent 600x800 -colorspace Gray /dev/shm/temp3-gray.png
113         GRAY="-gray"
114 else
115         GRAY=""
116 fi
117
118 set > /dev/shm/temp3.cgi.vars
119
120 lines_checked=""
121 test "$LINES" = 1 && lines_checked="checked"
122
123 if echo $HTTP_USER_AGENT | grep Mozilla > /dev/null ; then
124         echo "Content-type: text/html\r\n\r\n"
125         cat << __HTML__
126 <body>
127 <form method="get" action="/temp3.sh" style="position: fixed; top: 0; left: 0">
128 <input type=submit name="t" value="48h">
129 <input type=submit name="t" value="24h">
130 <input type=submit name="t" value="12h">
131 <input type=submit name="t" value="6h">
132 <input type=submit name="t" value="1h">
133 <input type=checkbox name="lines" $lines_checked> <!-- $LINES -->
134 </form>
135 <img src="/temp3.png">
136 </body>
137 __HTML__
138         exit 0
139 fi
140
141 echo "Location: $REQUEST_SCHEME://$SERVER_NAME:$SERVER_PORT/temp3$GRAY.png"
142 echo
143
144 exit 0
145