#!/bin/sh

#logger "button: ${BUTTON}; action: ${ACTION}; last seen: ${SEEN}ms"
#exit 0
PATH=/root/bin:/bin:/sbin:/usr/bin:/usr/sbin

[ "BTN_0" == "${BUTTON}" ] && {
#    echo 1 > /sys/class/gpio/gpio0/value
    logger "button: ${BUTTON}; action: ${ACTION}; last seen: ${SEEN}ms"
#    echo 0 > /sys/class/gpio/gpio0/value

    exit 0
}

[ "elec_meter" == "${BUTTON}" ] && {
# log the power consumption
    diff=${SEEN}

#    echo 0 > /sys/class/gpio/gpio8/value

    acc=`cat /tmp/www/power/accumul`
    acc=$(( ${acc} + 1 ))
    echo ${acc} > /tmp/www/power/accumul

    qc=`echo "3600000 / ${SEEN}"|bc -l`
    qc=${qc:0:8}
#    qp=`echo "$qc / 0.022419"|bc -l`
#    qp=${qp:0:8}

    if [ ${diff} -gt 1000 -o "pressed" == "${ACTION}" ]; then
        pidof rrdtool || /usr/bin/rrdtool update /tmp/power.rrd N:${qc} &
    fi

#    logger "${qc}W (`hrtime ${diff}`) *" &
#    kacc=`echo "${acc} / 1000"|bc -l`
    kacc=$(( ${acc} / 1000 ))
    logger "${qc}W (${kacc}kWh)" &
#    logger "${qc}W (`hrtime ${diff}`)" &
#    logger "${qc}W" &

    echo "${qc}" >> /tmp/power.log &

    #blink if the power exceedes 10kW and show kW
    if [ ${SEEN} -lt 360 ]; then
	if [ "${ACTION}" == "pressed" ]; then
	    qc=`echo "${qc} / 1000"|bc -l`
	    #qc=$(( ${qc} / 1000 ))
	else
	    qc="    ";
	fi
    fi
    /usr/sbin/saa ${qc} &

#    echo 1 > /sys/class/gpio/gpio8/value

    /root/bin/odometer.sh /tmp/www/power/accumul 5 "kWh" > /tmp/www/power/counter.html &

    exit 0
}

[ "water_meter" == "${BUTTON}" ] && {
# log the water consumption
#    echo 0 > /sys/class/gpio/gpio11/value

    # debounce workaround
    [ ${SEEN} -lt 400 ] && exit 0

    if [ "pressed" == "${ACTION}" ]; then
	echo "${SEEN}" > /tmp/www/water/high
	diff=$(( `cat /tmp/www/water/low` + ${SEEN} ))
    else
	echo "${SEEN}" > /tmp/www/water/low
	diff=$(( `cat /tmp/www/water/high` + ${SEEN} ))
    fi

    qc=`echo "3600000 / ${diff}"|bc -l`

    [ ${qc:0:1} == "." ] && qc="0${qc}"

    acc=`cat /tmp/www/water/accumul`
    cnt=`cat /tmp/www/water/counter`

    ACCUM_LOG="/tmp/www/water/log/accumul.`date +%h-%Y`.log"
    ln -s ${ACCUM_LOG} /tmp/www/water/accumul.log

    # 5 minutes gap
    if [ ${SEEN} -gt 300000 -a ${acc} -gt 0 ]; then
	logger "accumulated ${acc} liter(s)" &
	begin=`date +%s -r ${ACCUM_LOG}`
	echo -en `date -r /tmp/www/water/accumul` >> ${ACCUM_LOG}
	echo " : ${acc} liters (${cnt})" >> ${ACCUM_LOG}
	touch /tmp/www/water/begin_timestamp
	echo 0 > /tmp/www/water/accumul
	acc=0
	/root/bin/odometer.sh /tmp/www/water/counter 5 "m3" > /tmp/www/water/counter.html &
    fi

    if [ "pressed" == "${ACTION}" ]; then
	logger "${qc:0:6}l/h (`hrtime ${diff}`) v ($acc)" &
    else
	logger "${qc:0:6}l/h (`hrtime ${diff}`) ^ ($acc)" &
    fi

    pidof rrdtool || /usr/bin/rrdtool update /tmp/water.rrd N:${qc}
#    echo 1 > /sys/class/gpio/gpio11/value

    if [ "pressed" == "${ACTION}" ]; then
	acc=$(( ${acc} + 1 ))
	echo "${acc}" > /tmp/www/water/accumul
	cnt=$(( ${cnt} + 1 ))
	echo "${cnt}" > /tmp/www/water/counter
    fi

    exit 0
}

logger "button: ${BUTTON}; action: ${ACTION}; last seen: `hrtime ${SEEN}`"
