#!/bin/bash -x
#
# shell> sudo apt-get install rddtool lftp
# shell> sudo crontab -e
#        0 * * * * /data/wekly/froling_w.sh

DEVICE="/dev/ttyUSB0"
DEVICEOPT="57600 cs8 -parenb -cstopb -hupcl"

FTPURL="jesch70.tipido.net"
FTPLOGIN=
FTPPASS=
FTPDIR="htdocs/weekly"

RRDTOOLDB="/data/rrdtool_db/weekly"
HISTORY="weekly" # daily, weekly, yearly
WIDTH="650"
HEIGHT="100"

PUPLIC="/data/public_html/weekly"
ANIME="/data/anime"

VARS=(\
	'zustand:1:0' \
	'kesseltemp:2:1:3:C°' \
	'abgastemp:3:1:3:C°' \
	'kesselstrg:4:1:3:%' \
	'saugzug:10:1:3:U/min'
	'pufferoben:20:1:3:C°'
	'pufferunten:21:1:3:C°' \
	'pufferpumpe:22:1:3:%' \
	'boiler:23:1:3:C°' \
	'vorlauf:24:1:3:C°' \
	'hkpumpe:26:3' \
	'aussentemp:28:1:3:C°'\
	'betriebsstunden:30:1:3:h' \
	'fehler:99:1' \
     )
	 

stty -F $DEVICE sane
stty -F $DEVICE $DEVICEOPT
cat $DEVICE ${DEVICEOPT// /,} > /tmp/logfile &

PID=$!

sleep 15s

mkdir -p "$RRDTOOLDB"

case $HISTORY in
   d*) hist="-24h"; trend="32200"; trendstart="-2 days" ;;
   w*) hist="-144h"; trend="86400"; trendstart="-8 days" ;;
   y*) hist="-8760h"; trend="1724000"; trendstart="-365 days" ;;
    *) hist="-24h"; trend="12h"; trendstart="-2 days" ;;
esac

for x in "${VARS[@]}" ; do
    IFS=":" y=( $x )
    IFS=";" z=( $(grep -m1 -ai ";${y[1]};[0-9]" /tmp/logfile) )
    export ${y[0]}="${z[${y[2]}]}"
    if [ -z "${y[3]}" ] ; then
	echo ${z[${y[2]}]} | sed 's/^. //;s/^... //' > $PUPLIC/${y[0]}.txt
    else
	value=$(echo ${z[${y[2]}]} ${z[${y[3]}]} | awk '{ print $1 / $2 }')
	echo $value ${y[4]} > $PUPLIC/${y[0]}.txt
	if test -e "$RRDTOOLDB/${y[0]}.rrd" ; then
	    rrdtool update "$RRDTOOLDB/${y[0]}.rrd" N:$value
	else
	    rrdtool create "$RRDTOOLDB/${y[0]}.rrd" --step 300 \
	    DS:${y[0]}:GAUGE:600:U:U \
	    RRA:AVERAGE:0.5:1:2880 \
	    RRA:AVERAGE:0.5:60:700 \
	    RRA:AVERAGE:0.5:240:720 \
	    RRA:AVERAGE:0.5:1440:730 \
	    RRA:MAX:0.5:1:2880 \
	    RRA:MAX:0.5:60:700 \
	    RRA:MAX:0.5:240:720 \
	    RRA:MAX:0.5:1440:730 \
	    RRA:MIN:0.5:1:2880 \
	    RRA:MIN:0.5:60:700 \
	    RRA:MIN:0.5:240:720 \
	    RRA:MIN:0.5:1440:730
	fi

	rrdtool graph "$PUPLIC/${y[0]}.png" \
	--start $hist --end now \
	--font 'TITLE:12:DejaVuSans Bold' \
	--title="$(echo ${y[0]} | sed 's/\(^.\)/\U\1/') (${y[4]})" \
	--width $WIDTH \
	--height $HEIGHT \
	--watermark "last updated: $(date)" \
	--disable-rrdtool-tag \
	--slope-mode \
	--color "BACK#000000" \
	--color "SHADEA#000000" \
	--color "SHADEB#000000" \
	--color "FONT#DDDDDD" \
	--color "CANVAS#202020" \
	--color "GRID#666666" \
	--color "MGRID#AAAAAA" \
	--color "FRAME#202020" \
	--color "ARROW#FFFFFF" \
	"DEF:${y[0]}avg=$RRDTOOLDB/${y[0]}.rrd:${y[0]}:AVERAGE:start=$trendstart" \
	"DEF:${y[0]}max=$RRDTOOLDB/${y[0]}.rrd:${y[0]}:MAX" \
	"DEF:${y[0]}min=$RRDTOOLDB/${y[0]}.rrd:${y[0]}:MIN" \
	"CDEF:${y[0]}trend=${y[0]}avg,$trend,TREND" \
	"GPRINT:${y[0]}avg:LAST:current\: %2.1lf ${y[4]/\%/%%}" \
	"GPRINT:${y[0]}avg:MIN:min\: %2.1lf ${y[4]/\%/%%}" \
	"GPRINT:${y[0]}avg:MAX:max\: %2.1lf ${y[4]/\%/%%}" \
	"AREA:${y[0]}avg#FF9900:average" \
	"CDEF:s10=${y[0]}avg,0.98,*" "AREA:s10#E10000" \
	"CDEF:s15=${y[0]}avg,0.85,*" "AREA:s15#D20000" \
	"CDEF:s20=${y[0]}avg,0.80,*" "AREA:s20#C30000" \
	"CDEF:s25=${y[0]}avg,0.75,*" "AREA:s25#B40000" \
	"CDEF:s30=${y[0]}avg,0.70,*" "AREA:s30#A50000" \
	"CDEF:s35=${y[0]}avg,0.65,*" "AREA:s35#960000" \
	"CDEF:s40=${y[0]}avg,0.60,*" "AREA:s40#870000" \
	"CDEF:s45=${y[0]}avg,0.55,*" "AREA:s45#780000" \
	"CDEF:s50=${y[0]}avg,0.50,*" "AREA:s50#690000" \
	"CDEF:s55=${y[0]}avg,0.45,*" "AREA:s55#5A0000" \
	"CDEF:s60=${y[0]}avg,0.40,*" "AREA:s60#4B0000" \
	"CDEF:s65=${y[0]}avg,0.35,*" "AREA:s65#3C0000" \
	"CDEF:s70=${y[0]}avg,0.30,*" "AREA:s70#2D0000" \
	"CDEF:s75=${y[0]}avg,0.25,*" "AREA:s75#180000" \
	"CDEF:s80=${y[0]}avg,0.20,*" "AREA:s80#0F0000" \
	"CDEF:s85=${y[0]}avg,0.15,*" "AREA:s85#000000" \
	"LINE1.3:${y[0]}trend#00D600:trend"
    fi
done

kill -9 $PID

[ "$hkpumpe" = 0 ] && \
cp "$ANIME"/ani-pumpe-green-off.gif "$PUPLIC"/ani-pumpe-green.gif || cp "$ANIME"/ani-pumpe-green.gif "$PUPLIC"

[ "$pufferpumpe" = 0000 ] && \
cp "$ANIME"/ani-pumpe-black-off.gif "$PUPLIC"/ani-pumpe-black.gif || cp "$ANIME"/ani-pumpe-black.gif "$PUPLIC"

[ "$saugzug" = 0000 ] && \
cp "$ANIME"/ani-luefter-off.gif "$PUPLIC"/ani-luefter.gif || cp "$ANIME"/ani-luefter.gif "$PUPLIC"

date > $PUPLIC/timestamp.txt

echo "${saugzug/0000/0} U/min ($((${saugzug:-0}/29)) %)" > "$PUPLIC"/saugzug.txt

if [ "$FTPPASS" ] ; then
lftp <<EOF
set ftp:ssl-allow no
set ftp:passive-mode true
set ftp:list-options -a
set net:timeout 10
open -u $FTPLOGIN,$FTPPASS $FTPURL
mirror --reverse --only-newer $PUPLIC/ /$FTPDIR
EOF
fi
