<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
- var canvas;
- var context;
- var ww;
- var wh;
- var tmin;
- var tmax;
- function drawplot(times, color) {
- var tfact = ww / (tmax - tmin);
+ var dbg, errordiv;
+ var canvas, ctx;
+ var ww, wh;
+ var hmax, hfact;
+ var tmin, tmax, tfact;
+ var xzero = 60, yzero = 48;
+ var cold_d = [], hot_d = [];
+
+ function showdate(utime) {
+ var dt = new Date(utime*1000);
+ return dt.toLocaleDateString() + " " + dt.toLocaleTimeString();
+ }
+
+ function px(x) {
+ return xzero + ((x - tmin) * tfact);
+ }
+
+ function py(y) { return wh - yzero - (y * hfact);
+ }
+
+ const dow = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
+ const mn = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
+ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
+
+ function getcombx(tmin, tmax) {
+ var comb = [], lb = [];
+ var trange = tmax - tmin;
+ var inc, inc2, base, base2, t, dt, zoff;
+ var label = function(t) { return "<<" + t.toFixed(1) + ">>"; }
+ function tohour(t) { var dt = new Date(t*1000); return dt.getHours(); }
+ function todow(t) { var dt = new Date(t*1000); return dow[dt.getDay()]; }
+ function todom(t) { var dt = new Date(t*1000); return dt.getDate(); }
+ function tomonth(t) { var dt = new Date(t*1000); return mn[dt.getMonth()]; }
+
+ if (trange < 172800) { /* two days -> one hour */
+ inc = 3600;
+ inc2 = 21600;
+ label = tohour;
+ }
+ else if (trange < 864000) { /* 10 days -> six hours */
+ inc = 21600;
+ inc2 = 86400;
+ label = todow;
+ }
+ else if (trange < 2678400) { /* 31 days -> 1 day */
+ inc = 86400;
+ inc2 = 86400;
+ label = todom;
+ }
+ else { /* ~ one month */
+ /* TODO: make this a separate case with loop over months rather than
+ fixed number of seconds. */
+ inc = 86400;
+ inc2 = 2592000;
+ label = tomonth;
+ }
+
+ dt = new Date(tmin*1000);
+ zoff = 60 * dt.getTimezoneOffset();
+ base = (Math.floor((tmin - zoff - 1) / inc) + 1) * inc + zoff;
+ for (t = base; t < tmax; t += inc)
+ comb.push(t);
+ base2 = (Math.floor((tmin - zoff - 1) / inc2) + 1) * inc2 + zoff;
+ for (t = base2; t < tmax; t += inc2)
+ lb.push([t, label(t)]);
+ //dbg.innerHTML = "inc=" + inc + "<br>inc2=" + inc2 + "<br>"
+ // + "tmin=" + tmin + " tmax=" + tmax + "<br>"
+ // + "base=" + base + " base2=" + base2 + "<br>"
+ // + comb + "<br>" + lb;
+ return [comb, lb];
+ }
+
+ function xaxis() {
+ var comb = getcombx(tmin, tmax);
+ var i;
+
+ ctx.beginPath();
+ for (i = 0; comb[0][i]; i++) {
+ ctx.moveTo(px(comb[0][i]), py(0) + 5);
+ ctx.lineTo(px(comb[0][i]), py(0));
+ }
+ ctx.strokeStyle = "gray";
+ ctx.stroke();
+
+ ctx.beginPath();
+ ctx.moveTo(px(tmin), py(0));
+ ctx.lineTo(px(tmax), py(0));
+ ctx.strokeStyle = "black";
+ ctx.stroke();
+
+ ctx.fillStyle = "black";
+ ctx.font = "bold 16px Courier";
+ ctx.textAlign = "center";
+ ctx.beginPath();
+ for (i = 0; comb[1][i]; i++) {
+ ctx.fillText(comb[1][i][1], px(comb[1][i][0]), py(0) + 20);
+ ctx.moveTo(px(comb[1][i][0]), py(0) + 5);
+ ctx.lineTo(px(comb[1][i][0]), py(0));
+ }
+ ctx.strokeStyle = "black";
+ ctx.stroke();
+
+ ctx.fillStyle = "black";
+ ctx.textAlign = "left";
+ ctx.fillText(showdate(tmin), px(tmin), py(0) + 40);
+ ctx.textAlign = "right";
+ ctx.fillText(showdate(tmax), px(tmax), py(0) + 40);
+ }
+
+ function getcomby(lo, hi) {
+ var comb = [], lb = [];
+ var d = hi - lo;
+ var ord = Math.pow(10, Math.floor(Math.log10(d)));
+ var scl = Math.floor(d / ord);
+ var inc, inc2, first, x, lb;
+
+ if (scl < 2) { inc = 0.1; inc2 = 0.2; }
+ else if (scl < 5) { inc = 0.1; inc2 = 0.5; }
+ else { inc = 0.5; inc2 = 1; }
+ inc *= ord;
+ inc2 *= ord;
+ first = (Math.floor(lo / inc) + 1) * inc;
+ for (x = 0; x < (d / inc) - 1.2; x++)
+ comb.push(first + inc * x);
+ first = (Math.floor(lo / inc2) + 1) * inc2;
+ for (x = 0; x < (d / inc2) - 1.2; x++)
+ lb.push(first + inc2 * x);
+ //dbg.innerHTML = "ord=" + ord + "<br>inc=" + inc + "<br>"
+ // + comb + "<br>" + lb;
+ return [comb, lb];
+ }
+
+ function yaxis() {
+ var comb = getcomby(0, hmax);
+ var i;
+
+ ctx.beginPath();
+ for (i = 0; comb[0][i]; i++) {
+ ctx.moveTo(px(tmin) - 5, py(comb[0][i]));
+ ctx.lineTo(px(tmax), py(comb[0][i]));
+ }
+ ctx.strokeStyle = "lightgray";
+ ctx.stroke();
+
+ ctx.beginPath();
+ ctx.moveTo(px(tmin), py(0));
+ ctx.lineTo(px(tmin), py(hmax));
+ ctx.strokeStyle = "black";
+ ctx.stroke();
+
+ ctx.fillStyle = "black";
+ ctx.font = "bold 16px Courier";
+ ctx.textAlign = "right";
+ ctx.fillText(0, px(tmin) - 6, py(0));
+ ctx.beginPath();
+ for (i = 0; comb[1][i]; i++) {
+ ctx.fillText(comb[1][i].toFixed(1), px(tmin) - 6, py(comb[1][i]));
+ ctx.moveTo(px(tmin) - 5, py(comb[1][i]));
+ ctx.lineTo(px(tmax), py(comb[1][i]));
+ }
+ ctx.strokeStyle = "gray";
+ ctx.stroke();
+
+ ctx.textAlign = "left";
+ ctx.fillText("l/min", px(tmin) + 4, py(hmax) + 12);
+ }
+
+ /* @ updates global var `hmax` */
+ function differentiate(times) {
+ var res = [];
+ var dv, dt, v;
+
+ for (i = 0; i < times.length - 1; i++) {
+ dv = times[i+1][1] - times[i][1];
+ dt = times[i+1][0] - times[i][0];
+ if (dt != 0 && dv != 0) {
+ v = (dv / dt) * 600 ; /* Litres per min */
+ if (hmax < v) hmax = v;
+ res.push([times[i][0], v]);
+ }
+ }
+ if (i) res.push([times[i][0], v]);
+
+ return res;
+ }
+
+ function drawplot(data, color) {
var i;
- var height = [];
- var hmax = 0;
- var hfact;
- for (i = 1; i < times.length; i++) {
- height[i] = 1 / (times[i][0] - times[i-1][0]);
- if (hmax < height[i]) hmax = height[i];
+ ctx.beginPath();
+ ctx.moveTo(px(data[0][0]), py(data[0][1]));
+ for (i = 1; i < data.length; i++) {
+ ctx.lineTo(px(data[i][0]), py(data[i - 1][1]));
+ ctx.lineTo(px(data[i][0]), py(data[i][1]));
}
- height[i+1] = height[i];
- hfact = wh / hmax;
- context.beginPath();
- context.moveTo(0, wh - height[1]*hfact);
- for (i = 1; i < times.length; i++) {
- context.lineTo((times[i][0] - tmin)*tfact,wh - height[i]*hfact);
- context.lineTo((times[i][0] - tmin)*tfact,wh - height[i+1]*hfact);
+ ctx.strokeStyle = color;
+ ctx.stroke();
+ }
+
+ function showloading() {
+ ctx.fillStyle = "green";
+ ctx.font = "bold 16px Courier";
+ ctx.textAlign="center";
+ ctx.fillText("...loading...", (ww / 2) , (wh / 2) + 8);
+ }
+
+ function showempty() {
+ ctx.fillStyle = "red";
+ ctx.font = "bold 24px Courier";
+ ctx.textAlign="center";
+ ctx.fillText("No data for the requested time interval",
+ (ww / 2) , (wh / 2) + 8);
+ }
+
+ function clearplot() {
+ ctx.clearRect(0, 0, ww, wh);
+ }
+
+ function redraw() {
+ errordiv.style.visibility = "hidden";
+ errordiv.innerHTML = "";
+ clearplot();
+ if (cold_d.length || hot_d.length) {
+ tfact = (ww - xzero) / (tmax - tmin);
+ hfact = (wh - yzero) / hmax;
+ xaxis();
+ yaxis();
+ drawplot(cold_d, "blue");
+ drawplot(hot_d, "red");
+ } else {
+ showempty();
}
- context.strokeStyle = color;
- context.stroke();
}
+
function gotdata(data) {
- canvas = document.getElementById("plot");
- context = canvas.getContext("2d");
- ww = canvas.offsetWidth;
- wh = canvas.offsetHeight;
+ document.getElementById("curcold").innerHTML =
+ (data.current.cold / 100).toFixed(2);
+ document.getElementById("curhot").innerHTML =
+ (data.current.hot / 100).toFixed(2);
+
+ if (data.cold.length)
+ document.getElementById("totcold").innerHTML =
+ ((data.cold[data.cold.length - 1][1] - data.cold[0][1]) * 10);
+ else document.getElementById("totcold").innerHTML = "0";
+ if (data.hot.length)
+ document.getElementById("tothot").innerHTML =
+ ((data.hot[data.hot.length - 1][1] - data.hot[0][1]) * 10);
+ else document.getElementById("tothot").innerHTML = "0";
+
tmin = data.range.lo;
tmax = data.range.hi;
- document.getElementById("cold").innerHTML = data.current.cold;
- document.getElementById("hot").innerHTML = data.current.hot;
- drawplot(data.cold, "blue");
- drawplot(data.hot, "red");
+ //dbg.innerHTML = "from " + tmin + " to " + tmax
+ // + "<br>from " + showdate(tmin) + " to " + showdate(tmax);
+ /* differetiate() updates hmax */
+ hmax = 0;
+ cold_d = differentiate(data.cold);
+ hot_d = differentiate(data.hot);
+ //dbg.innerHTML = "hmax=" + hmax + " hfact=" + hfact + "<br>"
+ // + cold_d + "<br>" + hot_d;
+ redraw();
+ }
+
+ function sendreq(qstr) {
+ var url = "query.cgi" + qstr;
+ var xmlhttp = new XMLHttpRequest();
+
+ //dbg.innerHTML = url;
+ xmlhttp.onreadystatechange = function() {
+ if (xmlhttp.readyState == 4)
+ if (xmlhttp.status == 200) {
+ // dbg.innerHTML = xmlhttp.responseText;
+ var myData = JSON.parse(xmlhttp.responseText);
+ gotdata(myData);
+ } else {
+ errordiv.style.visibility = "visible";
+ errordiv.style.display = "block";
+ errordiv.innerHTML = xmlhttp.responseText;
+ }
+ }
+ xmlhttp.open("GET", url, true);
+ clearplot();
+ showloading();
+ xmlhttp.send();
+ }
+
+ function iso2qu(idate) {
+ return idate.replace("T", "+").replace("0Z", "");
+ }
+
+ function sendquery(lo, hi) {
+ return sendreq("?lo=" + iso2qu(lo) + "&hi=" + iso2qu(hi));
+ }
+
+ function resize() {
+ ww = window.innerWidth - 4;
+ if (ww > window.innerHeight) ww = window.innerHeight;
+ wh = ww / 2;
+ canvas.width = ww;
+ canvas.height = wh;
+ canvas.style.width = ww + "px";
+ canvas.style.height = wh + "px";
+ redraw();
+ }
+
+ function daystart(date) {
+ date.setMilliseconds(0);
+ date.setSeconds(0);
+ date.setMinutes(0);
+ date.setHours(0);
+ return date;
+ }
+
+ function prevweek() {
+ var tdy = daystart(new Date());
+ var dow = tdy.getDay();
+ var wstart, wend;
+
+ wstart = new Date(1*tdy - 86400000 * (dow + 7));
+ wend = new Date(1*wstart + 86400000 * 7);
+ sendquery(wstart.toISOString(), wend.toISOString());
+ }
+
+ function thisweek() {
+ var tdy = daystart(new Date());
+ var dow = tdy.getDay();
+ var wstart, wend;
+
+ wstart = new Date(1*tdy - 86400000 * dow);
+ wend = new Date(1*wstart + 86400000 * 7);
+ sendquery(wstart.toISOString(), wend.toISOString());
+ }
+
+ function beforeyesterday() {
+ var tdy = daystart(new Date());
+ var ytd = new Date(1*tdy - 86400000);
+ var byd = new Date(1*ytd - 86400000);
+ sendquery(byd.toISOString(), ytd.toISOString());
+ }
+
+ function yesterday() {
+ var tdy = daystart(new Date());
+ var ytd = new Date(1*tdy - 86400000);
+ sendquery(ytd.toISOString(), tdy.toISOString());
+ }
+
+ function today() {
+ var tdy = daystart(new Date());
+ var tmr = new Date(1*tdy + 86400000);
+ sendquery(tdy.toISOString(), tmr.toISOString());
}
+
+ function initialize() {
+ var qstr = window.location.search;
+
+ dbg = document.getElementById("debug");
+ errordiv = document.getElementById("errormsg");
+ canvas = document.getElementById("plot");
+ ctx = canvas.getContext("2d");
+ resize();
+ if (qstr) sendreq(qstr);
+ else today();
+
+ document.getElementById("today").onclick = today;
+ document.getElementById("yesterday").onclick = yesterday;
+ document.getElementById("beforeyesterday").onclick = beforeyesterday;
+ document.getElementById("thisweek").onclick = thisweek;
+ document.getElementById("prevweek").onclick = prevweek;
+ }
+
+ /* Set up */
+ window.onload = initialize;
+ window.onresize = resize;
</script>
<style>
+@font-face {
+ font-family: PipeDream;
+ src: url('PIPED.TTF') format('truetype');
+ /* Free to use font from http://www.mlink.net/~paterson/jpfonts.htm */
+}
h1 {
+ margin-top: 5px;
text-align: center;
+ font-family: PipeDream;
+ font-size: 64px;
+ font-weight: normal;
+ background-color: lightgray;
}
br {
- clear: all;
+ clear: both;
}
div#currentvals {
- width: 16em;
+ width: 18em;
margin-left: auto;
margin-right: auto;
+ margin-bottom: 10px;
text-align: center;
font-size: 150%;
}
+div#totalvals {
+ width: 18em;
+ margin-left: auto;
+ margin-right: auto;
+ margin-bottom: 10px;
+ text-align: center;
+ font-size: 100%;
+}
div.current {
position: relative;
padding: 0.2em;
border: solid 1px black;
- margin: 1em;
+ margin: 0.2em;
}
-div#cold {
+div.cold {
float: left;
background-color: #d0e0ff;
}
-div#hot {
+div.hot {
float: right;
background-color: #ffd0e0;
}
canvas#plot {
+ padding: 0px;
+ margin: auto;
+ display: block;
+ width: 640px;
+ height: 320px;
+ /* border: solid 1px black; */
+}
+div#errormsg {
+ visibility: hidden;
+ color: red;
+ text-align: center;
+}
+div#queries {
margin-left: auto;
margin-right: auto;
+ margin-bottom: 10px;
+ text-align: center;
+}
+div.query {
+ display: inline-block;
+ position: relative;
+ width: 8em;
+ height: 8em;
border: solid 1px black;
+ border-radius: 1em;
+ background-color: lightgray;
+ cursor: pointer;
+}
+div.label {
+ display: block;
+ width: 100%;
+ position: absolute;
+ top: 50%;
+ transform: translate(0, -50%);
+}
+body {
+ margin: 0px;
}
</style>
<title>Water Meters</title>
</head><body>
-<h1>Water Meters</h1>
+<h1>WATER METERS</h1>
<div id="currentvals">
- Current Values
- <div class="current" id="cold">cold</div>
- <div class="current" id="hot">hot</div>
+ Current Readings (m<sup>3</sup>)
+ <div class="current cold" id="curcold">cold</div>
+ <div class="current hot" id="curhot">hot</div>
</div>
<br />
-<canvas id="plot" width="640" height = "480"></canvas>
+<canvas id="plot" width="640" height = "320"></canvas>
+<br />
+<div id="errormsg"></div>
<br />
-<div id=debug>
+<div id="totalvals">
+ Total for the period (l)
+ <div class="current cold" id="totcold">cold</div>
+ <div class="current hot" id="tothot">hot</div>
</div>
-<script>
- var xmlhttp = new XMLHttpRequest();
- var url = "query.cgi";
-
- xmlhttp.onreadystatechange = function() {
- if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
- document.getElementById("debug").innerHTML = xmlhttp.responseText;
- var myData = JSON.parse(xmlhttp.responseText);
- gotdata(myData);
- }
- }
- xmlhttp.open("GET", url, true);
- xmlhttp.send();
-</script>
+<br />
+<div id="queries">
+ <div class="query" id="prevweek"><div class="label">PREVIOUS WEEK</div></div>
+ <div class="query" id="beforeyesterday"><div class="label">DAY
+ BEFORE YESTERDAY</div></div>
+ <div class="query" id="yesterday"><div class="label">YESTERDAY</div></div>
+ <div class="query" id="today"><div class="label">TODAY</div></div>
+ <div class="query" id="thisweek"><div class="label">THIS WEEK</div></div>
+</div>
+<br />
+<div id="debug"></div>
</body>