#!/bin/sh
# Monitors state of grid-tie PV.

# Writes simple HTML file to stdout with the 'live' PV stats.

# Also copies files (etc) to make data available live.
# None of this data is sensitive or have security implications.

# Logging directory.
LOGDIR=/var/log/SunnyBeam
# Last data: not guaranteed valid.
# Format:
#    20140722T14:09Z 2810
LASTDATA=${LOGDIR}/LASTDATA.flag
LASTSAMPLEDATA="`cat $LASTDATA`"

# Compute UTC YYYYMMDD date to match data logging.
UTCDATE="`date -u +%Y%m%d`"
# Yesterday in same format as UTCDATE (if easy to compute), else blank.
#YESTERDAY="`echo $UTCDATE | awk '$3%100 != 01 { printf("%8.8d", $1-1); }'`"
YESTERDAY="`date -u --date yesterday +%Y%m%d`"
# What should be today's log file.
LOGFILE=${LOGDIR}/${UTCDATE}.log
# Yesterday's log file, if it exists, else blank.
YLOGFILE=""
POSSIBLEYLF=${LOGDIR}/${YESTERDAY}.log
if [ -e ${POSSIBLEYLF} ]; then YLOGFILE=${POSSIBLEYLF}; fi


# Generate HTML header in simplified form...
# DHD20160620: now 'html' ie HTML5; was <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
# Title of this page.
TITLE="Grid-tie PV Power For 16WW"
# Series title text.
SERIESTITLE="Earth Notes"
# Home link to the index page.
cat << EOHEADER
<!DOCTYPE html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>$TITLE - $SERIESTITLE</title>
<meta name=viewport content="width=device-width,initial-scale=1">
<meta property="og:description" name=description content="Live solar PV grid-tied microgeneration at 16WW from 4 x 1.3kWp arrays 2xE and 2xW facing.">
<link href=https://www.earth.org.uk/_live-grid-tie-stats.html rel=canonical>
<style>@media print{.noprint{display:none!important}}</style>
</head>
<body>
<h1 style="font-family:sans-serif;margin:0;padding:0"><span style="color:green">$SERIESTITLE</span>: $TITLE</h1>
<p>(See <a href="_off-grid-stats.html">off-grid PV stats/graphs</a>.)</p>
EOHEADER

echo "<p>Flags/status:</p><ul>"
echo "<li>The last sample time was "`echo ${LASTSAMPLEDATA} | awk '{print $1}' `".</li>"
echo "<li>The last sampled power was "`echo ${LASTSAMPLEDATA} | awk '{print $2}' `"W.</li>"
#echo "<li>Samples so far today (UTC): `wc -l < ${LOGFILE}`</li>"
#if [ ! -z "${YLOGFILE}" -a -e "${YLOGFILE}" ]; then
#    echo "<li>Samples yesterday (UTC): `wc -l < ${YLOGFILE}`</li>"
#fi
echo "</ul>"

# Insert graph of grid-tie power output (if present).
if [ -f out/hourly/gridTiePower.png ]; then
cat << EOGRAPH
<p style="text-align:center"><img src="out/hourly/gridTiePower.png" width="640" height="480" style="max-width:100%;height:auto" alt="daily grid-tie generation graph" /></p>
EOGRAPH
else
    echo "<p>No graph available...</p>"
fi

# Insert calendar-month graph (if present).
if [ -f out/daily/gridTiePowercm.png ]; then
cat << EOGRAPH2
<p style="text-align:center"><img src="out/daily/gridTiePowercm.png" width="640" height="240" style="max-width:100%;height:auto" alt="monthly grid-tie generation graph" /></p>
EOGRAPH2
else
    echo "<p>No calendar-month graph available...</p>"
fi

# Insert thumbnail and link to all generation.
if [ -f out/monthly/Eall.png -a out/monthly/Eall-tn.png ]; then
cat << EOGRAPH3
<p style="text-align:center">Daily PV Output Since 2008: <a href="out/monthly/Eall.png"><img src="out/monthly/Eall-tn.png" alt="Daily PV Output Since 2008" title="Daily PV Output Since 2008" width="80" height="60" style="vertical-align:middle" /></a></p>
EOGRAPH3
fi


# Start (non-printing) footer.
echo '<footer class=noprint>'

echo '<p>See <a href="https://enlighten.enphaseenergy.com/pv/public_systems/9wmb1445397/overview">Enlighten public view of system c/o Enphase/Envoy monitoring</a>.</p>'

# Do atomic update of visible copy of live data and link to it.
COPYDIR=/rw/docs-public/www.hd.org/Damon/Env/data/SunnyBeam/live
if [ -e ${LOGFILE} ];  then
    cp ${LOGFILE} ${COPYDIR}/${UTCDATE}.log.tmp
    mv -f ${COPYDIR}/${UTCDATE}.log.tmp ${COPYDIR}/${UTCDATE}.log
    chmod a+r ${COPYDIR}/${UTCDATE}.log
    echo "<p>Current day <a href="data/SunnyBeam/live/${UTCDATE}.log">data</a>.</p>"
fi

# Link to historical by-day generation data, from 2008.
YEAR="`date -u +%Y`"
echo "<p>Historic by-day generation data:</p><ul>"
y=2008
while [ $y -le $YEAR ]; do
    CSV=data/WW-PV-roof/E${y}.csv
    if [ -e "$CSV" -a "`wc -l < $CSV`" -gt 2 ]; then
        echo "<li>$y <a href=\""$CSV"\">CSV</a> and <a href=\""out/yearly/E${y}.png"\">.png chart</a></li>"
    fi
    y=`expr 1 + $y`
done
echo "</ul>"
echo "<p>"
echo "<a href=\"out/monthly/Eall.png\">Chart for the whole data set</a>, and collated <a href=\"out/daily/PVEalldailykWh+smoothed.dat\">daily kWh generation records with smoothing</a>."
echo "</p>"

cat << RAW
<p>
See also
<a href="data/WW-PV-roof/raw/">raw historic intraday generation data</a>
and at <code><a href="data/SunnyBeam/live/">data/SunnyBeam/live</a>/YYYYMMDD.log</code> from 20140721.
</p>
RAW

# End (non-printing) footer.
echo '</footer>'

# Wrap up the HTML page.
cat << FOOTER
<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--2019. [<a href="/">home</a>]<br />
</small></p></footer></body></html>
FOOTER

exit 0
