#!/bin/sh
# Capture battery/power levels for sensors at 16WW, at least daily.
# Writes HTML (which should be mobile-friendly) to stdout.
# $1 is the directory in which ????????.json log files can be found,
# from which battery voltages are to be extracted and listed.

cat << HEADER
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>16WW Sensor Battery Levels</title>
<meta name="viewport" content="width=device-width,initial-scale=1"/>
<meta property="og:description" name="description" content="Monitoring battery levels across sensors and actuators at 16WW."/>
</head>
<h1>16WW Sensor Battery Levels</h1>
HEADER

# Sort into order and kill some duplicates early.
LOGFILES=`find $1 -name '????????.json' -mtime -2 | sort -u | tail -2`

# Output of filterJSON is of form:
#2015-10-04T17:17:38Z 0d49 2584
#2015-10-04T17:17:38Z 0d49 2584
#2015-10-04T17:18:39Z 3015 2499
#2015-10-04T17:19:02Z f1c6 2312
#2015-10-04T17:25:38Z 0d49 2584
#2015-10-04T17:25:38Z 0d49 2584
#2015-10-04T17:26:39Z 3015 2499

echo "<pre>"
#cat $LOGFILES /dev/null | OpenTRV/scripts/filterJSON 'B|mV' | \
#    awk '{ mV[$2] = $3; ts[$2] = $1; } \
#        END { for(i in ts) { print ts[i], i, mV[i]/1000; } }' | \
#    sort
#echo
cat $LOGFILES /dev/null | OpenTRV/scripts/filterJSON 'B|cV' | \
    awk '{ cV[$2] = $3; ts[$2] = $1; } \
        END { for(i in ts) { print ts[i], i, cV[i]/100; } }' | \
    sort
echo "</pre>"

cat << IMG
<p style="text-align:center"><img src="/out/weekly/16WWSensorPower.png" width="1280" height="480" style="max-width:100%;height:auto" alt="sensor battery voltage"/></p>
IMG

cat << FOOTER
<p><small>
This page was automatically generated on ${UTCDATE} UTC (`date -u`).<br />
Copyright &copy; <a href="http://d.hd.org/">Damon Hart-Davis</a> 2009--2017.
[<a href="/">home</a>]<br />
</small></p></body></html>
FOOTER

exit 0
