#!/bin/sh -x
# sudo apt-get install jq
+# ln -s `pwd`/temp2.sh /var/www/html/temp2.sh
+# ln -s /dev/shm/temp2.png /var/www/html/temp2.png
+# ln -s /dev/shm/temp2-gray.png /var/www/html/temp2-gray.png
#
-# install as cgi-script and invoke to get png
-
-#ssh rpi 'cat `ls -t /dev/shm/vaillant.* | head -1` | sed "s/ .*| */ /"' > /dev/shm/temp.tsv
-#tail -4320 `ls -t /dev/shm/vaillant.* | head -1` | sed "s/ .*| */ /" > /dev/shm/temp.tsv
+# install as cgi-script and invoke to get png use http://localhost/temp2.sh?gray for kindle grayscale output
curl -G 'http://10.60.0.89:8086/query' --data-urlencode "db=telegraf" --data-urlencode "epoch=s" --data-urlencode "q=SELECT \"value\" FROM \"mqtt_consumer\" WHERE time > now() - 48h AND \"topic\"='stat/boiler/DS18B20/temperature'" | sed -e 's/^.*"values":\[\[//' -e 's/\],\[/\n/g' -e 's/,/ /g' -e 's/]].*$//' > /dev/shm/DS18B20-temperature
curl -G 'http://10.60.0.89:8086/query' --data-urlencode "db=telegraf" --data-urlencode "epoch=s" --data-urlencode "q=SELECT \"value\" FROM \"mqtt_consumer\" WHERE time > now() - 48h AND \"topic\"='stat/boiler/DHT22/temperature'" | sed -e 's/^.*"values":\[\[//' -e 's/\],\[/\n/g' -e 's/,/ /g' -e 's/]].*$//' > /dev/shm/DHT22-temperature
curl -G 'http://10.60.0.89:8086/query' --data-urlencode "db=telegraf" --data-urlencode "epoch=s" --data-urlencode "q=SELECT \"humidity\" FROM \"rtl_433\" WHERE time > now() - 48h AND \"topic\"='rpi2/rtl_433/influx' AND \"id\" = '5'" | sed -e 's/^.*"values":\[\[//' -e 's/\],\[/\n/g' -e 's/,/ /g' -e 's/]].*$//' > /dev/shm/rtl_433-humidity
+
cd /home/pi/vaillant-thermostat/
gnuplot -e "t_mean=`head -1 /dev/shm/temperature.mean.spread`;t_spread=`tail -1 /dev/shm/temperature.mean.spread`;h_mean=`head -1 /dev/shm/humidity.mean.spread` ; h_spread=`tail -1 /dev/shm/humidity.mean.spread`" temp2.gnuplot
-convert /dev/shm/temp2.png -gravity center -extent 600x800 -colorspace Gray /dev/shm/temp2-gray.png
-# ln -s /dev/shm/temp2-gray.png /var/www/html/temp2.png
-echo "Location: $REQUEST_SCHEME://$SERVER_NAME:$SERVER_PORT/temp2.png"
+GRAY=`echo $QUERY_STRING | grep -i gray`
+if [ ! -z "$GRAY" ] ; then
+ convert /dev/shm/temp2.png -gravity center -extent 600x800 -colorspace Gray /dev/shm/temp2-gray.png
+ GRAY="-gray"
+fi
+
+set > /dev/shm/temp2.cgi.vars
+
+echo "Location: $REQUEST_SCHEME://$SERVER_NAME:$SERVER_PORT/temp2$GRAY.png"
echo
exit 0