#!/bin/sh
# png update script for temperature/humidity
# 30.09.2015 Jens Vehlhaber jens@eisfair.org
# Released under the GPL 

RRD_DATA_DIR="/etc/rrdtoolstemp"

. /etc/rrdtoolstemp/objects.conf

count="1"
while [ "$count" -le "0${OBJECTS_N}" ]
do
	eval sensor='$SENSOR_'${count}
	eval objectname='$OBJECTNAME_'${count}
	eval filename='$FILENAME_'${count}
	eval webdir='$WEBDIR_'${count}
	# set default if value empty
	[ -z "$objectname" ] && "Test"
	[ -z "$filename" ]   && "testfile"
	[ -z "$webdir" ]     && webdir="/var/www/localhost/htdocs"
	count=`expr ${count} + 1`
	[ "$sensor" != "TH" ] && continue

	# 1 Tag
	/usr/bin/rrdtool graph ${webdir}/${filename}-d.png \
	--end now --start end-24h \
	-w 740 -h 280 -a PNG \
	-l -0 -u 100 \
	--font DEFAULT:9: \
	--slope-mode \
	-t "$objectname - 1 Tag" \
	--vertical-label "Temp (°C) / Feuchte (%)" \
	DEF:temp=${RRD_DATA_DIR}/${filename}.rrd:temp:AVERAGE \
	DEF:tmin=${RRD_DATA_DIR}/${filename}.rrd:temp:MIN \
	DEF:tmax=${RRD_DATA_DIR}/${filename}.rrd:temp:MAX \
	DEF:humi=${RRD_DATA_DIR}/${filename}.rrd:humi:AVERAGE  \
	DEF:fmin=${RRD_DATA_DIR}/${filename}.rrd:humi:MIN \
	DEF:fmax=${RRD_DATA_DIR}/${filename}.rrd:humi:MAX \
	VDEF:gtakt=temp,LAST \
	VDEF:gtmin=tmin,MINIMUM \
	VDEF:gtavr=tmin,AVERAGE \
	VDEF:gtmax=tmax,MAXIMUM \
	VDEF:gfakt=humi,LAST \
	VDEF:gfmin=fmin,MINIMUM \
	VDEF:gfavr=fmin,AVERAGE \
	VDEF:gfmax=fmax,MAXIMUM \
	LINE1:temp#ff0000:"Temperatur °C\t\t\t" \
	LINE2:humi#0000ff:"Feuchte %\n" \
	GPRINT:gtakt:"Temp aktuell\: %10.2lf °C\t" \
	GPRINT:gfakt:"Feuchte aktuell\: %10.2lf %%\n" \
	GPRINT:gtavr:"Temp schnitt\: %10.2lf °C\t" \
	GPRINT:gfavr:"Feuchte schnitt\: %10.2lf %%\n" \
	GPRINT:gtmin:"Temp min\:     %10.2lf °C\t" \
	GPRINT:gfmin:"Feuchte min\:     %10.2lf %%\n" \
	GPRINT:gtmax:"Temp max\:     %10.2lf °C\t" \
	GPRINT:gfmax:"Feuchte max\:     %10.2lf %%\n" >/dev/null
done

exit 0