1 <?xml version="1.0" encoding="utf-8"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
11 var tmin, tmax, tfact;
12 var xzero = 60, yzero = 48;
13 var cold_d = [], hot_d = [];
15 function showdate(utime) {
16 var dt = new Date(utime*1000);
17 return dt.toLocaleDateString() + " " + dt.toLocaleTimeString();
21 return xzero + ((x - tmin) * tfact);
24 function py(y) { return wh - yzero - (y * hfact);
27 const dow = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
28 const mn = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
29 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
31 function getcombx(tmin, tmax) {
32 var comb = [], lb = [];
33 var trange = tmax - tmin;
34 var inc, inc2, base, base2, t, dt, zoff;
35 var label = function(t) { return "<<" + t.toFixed(1) + ">>"; }
36 function tohour(t) { var dt = new Date(t*1000); return dt.getHours(); }
37 function todow(t) { var dt = new Date(t*1000); return dow[dt.getDay()]; }
38 function todom(t) { var dt = new Date(t*1000); return dt.getDate(); }
39 function tomonth(t) { var dt = new Date(t*1000); return mn[dt.getMonth()]; }
41 if (trange < 172800) { /* two days -> one hour */
46 else if (trange < 864000) { /* 10 days -> six hours */
51 else if (trange < 2678400) { /* 31 days -> 1 day */
56 else { /* ~ one month */
57 /* TODO: make this a separate case with loop over months rather than
58 fixed number of seconds. */
64 dt = new Date(tmin*1000);
65 zoff = 60 * dt.getTimezoneOffset();
66 base = (Math.floor((tmin - zoff - 1) / inc) + 1) * inc + zoff;
67 for (t = base; t < tmax; t += inc)
69 base2 = (Math.floor((tmin - zoff - 1) / inc2) + 1) * inc2 + zoff;
70 for (t = base2; t < tmax; t += inc2)
71 lb.push([t, label(t)]);
72 //dbg.innerHTML = "inc=" + inc + "<br>inc2=" + inc2 + "<br>"
73 // + "tmin=" + tmin + " tmax=" + tmax + "<br>"
74 // + "base=" + base + " base2=" + base2 + "<br>"
75 // + comb + "<br>" + lb;
80 var comb = getcombx(tmin, tmax);
84 for (i = 0; comb[0][i]; i++) {
85 ctx.moveTo(px(comb[0][i]), py(0) + 5);
86 ctx.lineTo(px(comb[0][i]), py(0));
88 ctx.strokeStyle = "gray";
92 ctx.moveTo(px(tmin), py(0));
93 ctx.lineTo(px(tmax), py(0));
94 ctx.strokeStyle = "black";
97 ctx.fillStyle = "black";
98 ctx.font = "bold 16px Courier";
99 ctx.textAlign = "center";
101 for (i = 0; comb[1][i]; i++) {
102 ctx.fillText(comb[1][i][1], px(comb[1][i][0]), py(0) + 20);
103 ctx.moveTo(px(comb[1][i][0]), py(0) + 5);
104 ctx.lineTo(px(comb[1][i][0]), py(0));
106 ctx.strokeStyle = "black";
109 ctx.fillStyle = "black";
110 ctx.textAlign = "left";
111 ctx.fillText(showdate(tmin), px(tmin), py(0) + 40);
112 ctx.textAlign = "right";
113 ctx.fillText(showdate(tmax), px(tmax), py(0) + 40);
116 function getcomby(lo, hi) {
117 var comb = [], lb = [];
119 var ord = Math.pow(10, Math.floor(Math.log10(d)));
120 var scl = Math.floor(d / ord);
121 var inc, inc2, first, x, lb;
123 if (scl < 2) { inc = 0.1; inc2 = 0.2; }
124 else if (scl < 5) { inc = 0.1; inc2 = 0.5; }
125 else { inc = 0.5; inc2 = 1; }
128 first = (Math.floor(lo / inc) + 1) * inc;
129 for (x = 0; x < (d / inc) - 1.2; x++)
130 comb.push(first + inc * x);
131 first = (Math.floor(lo / inc2) + 1) * inc2;
132 for (x = 0; x < (d / inc2) - 1.2; x++)
133 lb.push(first + inc2 * x);
134 //dbg.innerHTML = "ord=" + ord + "<br>inc=" + inc + "<br>"
135 // + comb + "<br>" + lb;
140 var comb = getcomby(0, hmax);
144 for (i = 0; comb[0][i]; i++) {
145 ctx.moveTo(px(tmin) - 5, py(comb[0][i]));
146 ctx.lineTo(px(tmax), py(comb[0][i]));
148 ctx.strokeStyle = "lightgray";
152 ctx.moveTo(px(tmin), py(0));
153 ctx.lineTo(px(tmin), py(hmax));
154 ctx.strokeStyle = "black";
157 ctx.fillStyle = "black";
158 ctx.font = "bold 16px Courier";
159 ctx.textAlign = "right";
160 ctx.fillText(0, px(tmin) - 6, py(0));
162 for (i = 0; comb[1][i]; i++) {
163 ctx.fillText(comb[1][i].toFixed(1), px(tmin) - 6, py(comb[1][i]));
164 ctx.moveTo(px(tmin) - 5, py(comb[1][i]));
165 ctx.lineTo(px(tmax), py(comb[1][i]));
167 ctx.strokeStyle = "gray";
170 ctx.textAlign = "left";
171 ctx.fillText("l/min", px(tmin) + 4, py(hmax) + 12);
174 /* @ updates global var `hmax` */
175 function differentiate(times) {
179 for (i = 0; i < times.length - 1; i++) {
180 dv = times[i+1][1] - times[i][1];
181 dt = times[i+1][0] - times[i][0];
182 if (dt != 0 && dv != 0) {
183 v = (dv / dt) * 600 ; /* Litres per min */
184 if (hmax < v) hmax = v;
185 res.push([times[i][0], v]);
188 if (i) res.push([times[i][0], v]);
193 function drawplot(data, color) {
198 ctx.moveTo(px(data[0][0]), py(data[0][1]));
199 for (i = 1; i < data.length; i++) {
200 ctx.lineTo(px(data[i][0]), py(data[i - 1][1]));
201 ctx.lineTo(px(data[i][0]), py(data[i][1]));
203 ctx.strokeStyle = color;
208 function showloading() {
209 ctx.fillStyle = "green";
210 ctx.font = "bold 16px Courier";
211 ctx.textAlign="center";
212 ctx.fillText("...loading...", (ww / 2) , (wh / 2) + 8);
215 function showempty() {
216 ctx.fillStyle = "red";
217 ctx.font = "bold 24px Courier";
218 ctx.textAlign="center";
219 ctx.fillText("No data for the requested time interval",
220 (ww / 2) , (wh / 2) + 8);
223 function clearplot() {
224 ctx.clearRect(0, 0, ww, wh);
228 errordiv.style.visibility = "hidden";
229 errordiv.innerHTML = "";
231 if (cold_d.length || hot_d.length) {
232 tfact = (ww - xzero) / (tmax - tmin);
233 hfact = (wh - yzero) / hmax;
236 drawplot(cold_d, "blue");
237 drawplot(hot_d, "red");
243 function gotdata(data) {
244 document.getElementById("curcold").innerHTML =
245 (data.current.cold / 100).toFixed(2);
246 document.getElementById("curhot").innerHTML =
247 (data.current.hot / 100).toFixed(2);
249 if (data.cold.length)
250 document.getElementById("totcold").innerHTML =
251 ((data.cold[data.cold.length - 1][1] - data.cold[0][1]) * 10);
252 else document.getElementById("totcold").innerHTML = "0";
254 document.getElementById("tothot").innerHTML =
255 ((data.hot[data.hot.length - 1][1] - data.hot[0][1]) * 10);
256 else document.getElementById("tothot").innerHTML = "0";
258 tmin = data.range.lo;
259 tmax = data.range.hi;
260 //dbg.innerHTML = "from " + tmin + " to " + tmax
261 // + "<br>from " + showdate(tmin) + " to " + showdate(tmax);
262 /* differetiate() updates hmax */
264 cold_d = differentiate(data.cold);
265 hot_d = differentiate(data.hot);
266 //dbg.innerHTML = "hmax=" + hmax + " hfact=" + hfact + "<br>"
267 // + cold_d + "<br>" + hot_d;
271 function sendreq(qstr) {
272 var url = "query.cgi" + qstr;
273 var xmlhttp = new XMLHttpRequest();
275 //dbg.innerHTML = url;
276 xmlhttp.onreadystatechange = function() {
277 if (xmlhttp.readyState == 4)
278 if (xmlhttp.status == 200) {
279 // dbg.innerHTML = xmlhttp.responseText;
280 var myData = JSON.parse(xmlhttp.responseText);
283 errordiv.style.visibility = "visible";
284 errordiv.style.display = "block";
285 errordiv.innerHTML = xmlhttp.responseText;
288 xmlhttp.open("GET", url, true);
294 function iso2qu(idate) {
295 return idate.replace("T", "+").replace("0Z", "");
298 function sendquery(lo, hi) {
299 return sendreq("?lo=" + iso2qu(lo) + "&hi=" + iso2qu(hi));
303 ww = window.innerWidth - 4;
304 if (ww > window.innerHeight) ww = window.innerHeight;
308 canvas.style.width = ww + "px";
309 canvas.style.height = wh + "px";
313 function daystart(date) {
314 date.setMilliseconds(0);
321 function prevweek() {
322 var tdy = daystart(new Date());
323 var dow = tdy.getDay();
326 wstart = new Date(1*tdy - 86400000 * (dow + 7));
327 wend = new Date(1*wstart + 86400000 * 7);
328 sendquery(wstart.toISOString(), wend.toISOString());
331 function thisweek() {
332 var tdy = daystart(new Date());
333 var dow = tdy.getDay();
336 wstart = new Date(1*tdy - 86400000 * dow);
337 wend = new Date(1*wstart + 86400000 * 7);
338 sendquery(wstart.toISOString(), wend.toISOString());
341 function beforeyesterday() {
342 var tdy = daystart(new Date());
343 var ytd = new Date(1*tdy - 86400000);
344 var byd = new Date(1*ytd - 86400000);
345 sendquery(byd.toISOString(), ytd.toISOString());
348 function yesterday() {
349 var tdy = daystart(new Date());
350 var ytd = new Date(1*tdy - 86400000);
351 sendquery(ytd.toISOString(), tdy.toISOString());
355 var tdy = daystart(new Date());
356 var tmr = new Date(1*tdy + 86400000);
357 sendquery(tdy.toISOString(), tmr.toISOString());
360 function initialize() {
361 var qstr = window.location.search;
363 dbg = document.getElementById("debug");
364 errordiv = document.getElementById("errormsg");
365 canvas = document.getElementById("plot");
366 ctx = canvas.getContext("2d");
368 if (qstr) sendreq(qstr);
371 document.getElementById("today").onclick = today;
372 document.getElementById("yesterday").onclick = yesterday;
373 document.getElementById("beforeyesterday").onclick = beforeyesterday;
374 document.getElementById("thisweek").onclick = thisweek;
375 document.getElementById("prevweek").onclick = prevweek;
379 window.onload = initialize;
380 window.onresize = resize;
384 font-family: PipeDream;
385 src: url('PIPED.TTF') format('truetype');
386 /* Free to use font from http://www.mlink.net/~paterson/jpfonts.htm */
391 font-family: PipeDream;
394 background-color: lightgray;
418 border: solid 1px black;
423 background-color: #d0e0ff;
427 background-color: #ffd0e0;
435 /* border: solid 1px black; */
449 display: inline-block;
453 border: solid 1px black;
455 background-color: lightgray;
463 transform: translate(0, -50%);
469 <title>Water Meters</title>
471 <h1>WATER METERS</h1>
472 <div id="currentvals">
473 Current Readings (m<sup>3</sup>)
474 <div class="current cold" id="curcold">cold</div>
475 <div class="current hot" id="curhot">hot</div>
478 <canvas id="plot" width="640" height = "320"></canvas>
480 <div id="errormsg"></div>
483 Total for the period (l)
484 <div class="current cold" id="totcold">cold</div>
485 <div class="current hot" id="tothot">hot</div>
489 <div class="query" id="prevweek"><div class="label">PREVIOUS WEEK</div></div>
490 <div class="query" id="beforeyesterday"><div class="label">DAY
491 BEFORE YESTERDAY</div></div>
492 <div class="query" id="yesterday"><div class="label">YESTERDAY</div></div>
493 <div class="query" id="today"><div class="label">TODAY</div></div>
494 <div class="query" id="thisweek"><div class="label">THIS WEEK</div></div>
497 <div id="debug"></div>