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 = 40, 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(lo, hi) {
32 var comb = [], lb = [];
33 var trange = tmax - tmin;
34 var inc, inc2, base, t, dt;
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 base = (Math.floor(tmin / inc) + 1) * inc + 60 * dt.getTimezoneOffset();
66 for (t = base; t < tmax; t += inc)
68 base = (Math.floor(tmin / inc2) + 1) * inc2 + 60 * dt.getTimezoneOffset();
69 for (t = base; t < tmax; t += inc2)
70 lb.push([t, label(t)]);
71 //dbg.innerHTML = "ord=" + ord + "<br>inc=" + inc + "<br>"
72 // + comb + "<br>" + lb;
77 var comb = getcombx(0, hmax);
81 for (i = 0; comb[0][i]; i++) {
82 ctx.moveTo(px(comb[0][i]), py(0) + 5);
83 ctx.lineTo(px(comb[0][i]), py(0));
85 ctx.strokeStyle = "gray";
89 ctx.moveTo(px(tmin), py(0));
90 ctx.lineTo(px(tmax), py(0));
91 ctx.strokeStyle = "black";
94 ctx.fillStyle = "black";
95 ctx.font = "bold 16px Courier";
96 ctx.textAlign = "center";
98 for (i = 0; comb[1][i]; i++) {
99 ctx.fillText(comb[1][i][1], px(comb[1][i][0]), py(0) + 20);
100 ctx.moveTo(px(comb[1][i][0]), py(0) + 5);
101 ctx.lineTo(px(comb[1][i][0]), py(0));
103 ctx.strokeStyle = "black";
106 ctx.fillStyle = "black";
107 ctx.textAlign = "left";
108 ctx.fillText(showdate(tmin), px(tmin), py(0) + 40);
109 ctx.textAlign = "right";
110 ctx.fillText(showdate(tmax), px(tmax), py(0) + 40);
113 function getcomby(lo, hi) {
114 var comb = [], lb = [];
116 var ord = Math.pow(10, Math.floor(Math.log10(d)));
117 var scl = Math.floor(d / ord);
118 var inc, inc2, first, x, lb;
120 if (scl < 2) { inc = 0.1; inc2 = 0.2; }
121 else if (scl < 5) { inc = 0.1; inc2 = 0.5; }
122 else { inc = 0.5; inc2 = 1; }
125 first = (Math.floor(lo / inc) + 1) * inc;
126 for (x = 0; x < (d / inc) - 1.2; x++)
127 comb.push(first + inc * x);
128 first = (Math.floor(lo / inc2) + 1) * inc2;
129 for (x = 0; x < (d / inc2) - 1.2; x++)
130 lb.push(first + inc2 * x);
131 //dbg.innerHTML = "ord=" + ord + "<br>inc=" + inc + "<br>"
132 // + comb + "<br>" + lb;
137 var comb = getcomby(0, hmax);
141 for (i = 0; comb[0][i]; i++) {
142 ctx.moveTo(px(tmin) - 5, py(comb[0][i]));
143 ctx.lineTo(px(tmax), py(comb[0][i]));
145 ctx.strokeStyle = "lightgray";
149 ctx.moveTo(px(tmin), py(0));
150 ctx.lineTo(px(tmin), py(hmax));
151 ctx.strokeStyle = "black";
154 ctx.fillStyle = "black";
155 ctx.font = "bold 16px Courier";
156 ctx.textAlign = "right";
157 ctx.fillText(0, px(tmin) - 6, py(0));
159 for (i = 0; comb[1][i]; i++) {
160 ctx.fillText(comb[1][i].toFixed(1), px(tmin) - 6, py(comb[1][i]));
161 ctx.moveTo(px(tmin) - 5, py(comb[1][i]));
162 ctx.lineTo(px(tmax), py(comb[1][i]));
164 ctx.strokeStyle = "gray";
167 ctx.textAlign = "left";
168 ctx.fillText("l/min", px(tmin) + 4, py(hmax) + 12);
171 /* @ updates global var `hmax` */
172 function differentiate(times) {
176 for (i = 0; i < times.length - 1; i++) {
177 dv = times[i+1][1] - times[i][1];
178 dt = times[i+1][0] - times[i][0];
179 if (dt != 0 && dv != 0) {
180 v = (dv / dt) * 600 ; /* Litres per min */
181 if (hmax < v) hmax = v;
182 res.push([times[i][0], v]);
185 if (i) res.push([times[i][0], v]);
190 function drawplot(data, color) {
194 ctx.moveTo(px(data[0][0]), py(data[0][1]));
195 for (i = 1; i < data.length; i++) {
196 ctx.lineTo(px(data[i][0]), py(data[i - 1][1]));
197 ctx.lineTo(px(data[i][0]), py(data[i][1]));
199 ctx.strokeStyle = color;
203 function showloading() {
204 ctx.fillStyle = "green";
205 ctx.font = "bold 16px Courier";
206 ctx.textAlign="center";
207 ctx.fillText("...loading...", (ww / 2) , (wh / 2) + 8);
210 function showempty() {
211 ctx.fillStyle = "red";
212 ctx.font = "bold 24px Courier";
213 ctx.textAlign="center";
214 ctx.fillText("No data for the requested time interval",
215 (ww / 2) , (wh / 2) + 8);
218 function clearplot() {
219 ctx.clearRect(0, 0, ww, wh);
223 errordiv.style.visibility = "hidden";
224 errordiv.innerHTML = "";
226 if (cold_d.length || hot_d.length) {
227 tfact = (ww - xzero) / (tmax - tmin);
228 hfact = (wh - yzero) / hmax;
231 drawplot(cold_d, "blue");
232 drawplot(hot_d, "red");
238 function gotdata(data) {
239 document.getElementById("curcold").innerHTML =
240 (data.current.cold / 100).toFixed(2);
241 document.getElementById("curhot").innerHTML =
242 (data.current.hot / 100).toFixed(2);
244 document.getElementById("totcold").innerHTML =
245 ((data.cold[data.cold.length - 1][1] - data.cold[0][1]) * 10);
246 document.getElementById("tothot").innerHTML =
247 ((data.hot[data.hot.length - 1][1] - data.hot[0][1]) * 10);
249 tmin = data.range.lo;
250 tmax = data.range.hi;
251 //dbg.innerHTML = "from " + tmin + " to " + tmax
252 // + "<br>from " + showdate(tmin) + " to " + showdate(tmax);
253 /* differetiate() updates hmax */
255 cold_d = differentiate(data.cold);
256 hot_d = differentiate(data.hot);
257 //dbg.innerHTML = "hmax=" + hmax + " hfact=" + hfact + "<br>"
258 // + cold_d + "<br>" + hot_d;
262 function sendreq(qstr) {
263 var url = "query.cgi" + qstr;
264 var xmlhttp = new XMLHttpRequest();
266 //dbg.innerHTML = url;
267 xmlhttp.onreadystatechange = function() {
268 if (xmlhttp.readyState == 4)
269 if (xmlhttp.status == 200) {
270 // dbg.innerHTML = xmlhttp.responseText;
271 var myData = JSON.parse(xmlhttp.responseText);
274 errordiv.style.visibility = "visible";
275 errordiv.style.display = "block";
276 errordiv.innerHTML = xmlhttp.responseText;
279 xmlhttp.open("GET", url, true);
285 function iso2qu(idate) {
286 return idate.replace("T", "+").replace("0Z", "");
289 function sendquery(lo, hi) {
290 return sendreq("?lo=" + iso2qu(lo) + "&hi=" + iso2qu(hi));
294 ww = window.innerWidth - 4;
295 if (ww > window.innerHeight) ww = window.innerHeight;
299 canvas.style.width = ww + "px";
300 canvas.style.height = wh + "px";
304 function daystart(date) {
305 date.setMilliseconds(0);
312 function prevweek() {
313 var tdy = daystart(new Date());
314 var dow = tdy.getDay();
317 wstart = new Date(1*tdy - 86400000 * (dow + 7));
318 wend = new Date(1*wstart + 86400000 * 7);
319 sendquery(wstart.toISOString(), wend.toISOString());
322 function thisweek() {
323 var tdy = daystart(new Date());
324 var dow = tdy.getDay();
327 wstart = new Date(1*tdy - 86400000 * dow);
328 wend = new Date(1*wstart + 86400000 * 7);
329 sendquery(wstart.toISOString(), wend.toISOString());
332 function beforeyesterday() {
333 var tdy = daystart(new Date());
334 var ytd = new Date(1*tdy - 86400000);
335 var byd = new Date(1*ytd - 86400000);
336 sendquery(byd.toISOString(), ytd.toISOString());
339 function yesterday() {
340 var tdy = daystart(new Date());
341 var ytd = new Date(1*tdy - 86400000);
342 sendquery(ytd.toISOString(), tdy.toISOString());
346 var tdy = daystart(new Date());
347 var tmr = new Date(1*tdy + 86400000);
348 sendquery(tdy.toISOString(), tmr.toISOString());
351 function initialize() {
352 var qstr = window.location.search;
354 dbg = document.getElementById("debug");
355 errordiv = document.getElementById("errormsg");
356 canvas = document.getElementById("plot");
357 ctx = canvas.getContext("2d");
359 if (qstr) sendreq(qstr);
362 document.getElementById("today").onclick = today;
363 document.getElementById("yesterday").onclick = yesterday;
364 document.getElementById("beforeyesterday").onclick = beforeyesterday;
365 document.getElementById("thisweek").onclick = thisweek;
366 document.getElementById("prevweek").onclick = prevweek;
370 window.onload = initialize;
371 window.onresize = resize;
375 font-family: PipeDream;
376 src: url('PIPED.TTF') format('truetype');
377 /* Free to use font from http://www.mlink.net/~paterson/jpfonts.htm */
382 font-family: PipeDream;
385 background-color: lightgray;
409 border: solid 1px black;
414 background-color: #d0e0ff;
418 background-color: #ffd0e0;
426 /* border: solid 1px black; */
440 display: inline-block;
444 border: solid 1px black;
446 background-color: lightgray;
454 transform: translate(0, -50%);
460 <title>Water Meters</title>
462 <h1>WATER METERS</h1>
463 <div id="currentvals">
464 Current Readings (m<sup>3</sup>)
465 <div class="current cold" id="curcold">cold</div>
466 <div class="current hot" id="curhot">hot</div>
469 <canvas id="plot" width="640" height = "320"></canvas>
471 <div id="errormsg"></div>
474 Total for the period (l)
475 <div class="current cold" id="totcold">cold</div>
476 <div class="current hot" id="tothot">hot</div>
480 <div class="query" id="prevweek"><div class="label">PREVIOUS WEEK</div></div>
481 <div class="query" id="beforeyesterday"><div class="label">DAY
482 BEFORE YESTERDAY</div></div>
483 <div class="query" id="yesterday"><div class="label">YESTERDAY</div></div>
484 <div class="query" id="today"><div class="label">TODAY</div></div>
485 <div class="query" id="thisweek"><div class="label">THIS WEEK</div></div>
488 <div id="debug"></div>