X-Git-Url: http://www.average.org/gitweb/?p=pulsecounter.git;a=blobdiff_plain;f=web%2Findex.html;h=490e21f6c273d6010d1668a310eb774b623a07fa;hp=e08a07a9f0211d995eb08d711d39c4e21367c280;hb=16c5c402a89494b17377d09dee6c97685ce1bdb1;hpb=f221f12f7259cd64fdf09494ed61926322448ae8 diff --git a/web/index.html b/web/index.html index e08a07a..490e21f 100644 --- a/web/index.html +++ b/web/index.html @@ -10,29 +10,89 @@ var ww, wh; var hmax, hfact; var tmin, tmax, tfact; - var xzero = 20, yzero = 20; + var xzero = 36, yzero = 24; var cold_d = [], hot_d = []; + function showdate(utime) { + var dt = new Date(utime*1000); + return dt.toLocaleDateString() + " " + dt.toLocaleTimeString(); + } + + function getcomb(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.2; } + 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 + "
inc=" + inc + "
" + // + comb + "
" + lb; + return [comb, lb]; + } + function px(x) { return xzero + ((x - tmin) * tfact); } + function py(y) { return wh - yzero - (y * hfact); } + function xaxis() { 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 12px Courier"; + ctx.textAlign = "left"; + ctx.fillText(showdate(tmin), px(tmin), py(0) + 20); + ctx.textAlign = "right"; + ctx.fillText(showdate(tmax), px(tmax), py(0) + 20); } + function yaxis() { + var comb = getcomb(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 12px Courier"; + ctx.textAlign = "right"; + ctx.fillText(0, px(tmin) - 6, py(0)); + for (i = 0; comb[1][i]; i++) { + ctx.fillText(comb[1][i].toFixed(1), px(tmin) - 6, py(comb[1][i])); + } + ctx.textAlign = "left"; + ctx.fillText("l/h", px(tmin) + 4, py(hmax) + 8); } + /* @ updates global var `hmax` */ function differentiate(times) { var res = []; @@ -42,15 +102,16 @@ dv = times[i+1][1] - times[i][1]; dt = times[i+1][0] - times[i][0]; if (dt != 0 && dv != 0) { - v = dv / dt; + v = (dv / dt) * 360 ; /* Litres per hour */ if (hmax < v) hmax = v; res.push([times[i][0], v]); } } - res.push([times[i][0], v]); + if (i) res.push([times[i][0], v]); return res; } + function drawplot(data, color) { var i; @@ -63,14 +124,37 @@ 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() { - if (cold_d.length && hot_d.length) { - drawplot(cold_d, "blue"); - drawplot(hot_d, "red"); + 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 { - /* load animation here */ ; + showempty(); } } @@ -81,21 +165,25 @@ (data.current.hot / 100).toFixed(2); tmin = data.range.lo; tmax = data.range.hi; - tfact = (ww - xzero) / (tmax - tmin); + //dbg.innerHTML = "from " + tmin + " to " + tmax + // + "
from " + showdate(tmin) + " to " + showdate(tmax); /* differetiate() updates hmax */ hmax = 0; cold_d = differentiate(data.cold); hot_d = differentiate(data.hot); - hfact = (wh - yzero) / hmax; - //document.getElementById("debug").innerHTML = cold_d + "
" + hot_d; + //dbg.innerHTML = "hmax=" + hmax + " hfact=" + hfact + "
" + // + cold_d + "
" + hot_d; redraw(); } + function iso2qu(idate) { + return idate.replace("T", "+").replace("0Z", ""); + } + function sendquery(lo, hi) { - var url = "query.cgi"; + var url = "query.cgi" + "?lo=" + iso2qu(lo) + "&hi=" + iso2qu(hi); - if (lo && hi) url += "?lo=" + lo + "&" + hi; - else url = "query.cgi?lo=2015-12-19+00:00:00&hi=2015-12-20+00:00:00"; //FIX + //dbg.innerHTML = url; xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { // dbg.innerHTML = xmlhttp.responseText; @@ -104,11 +192,13 @@ } } xmlhttp.open("GET", url, true); + clearplot(); + showloading(); xmlhttp.send(); } function resize() { - ww = window.innerWidth; + ww = window.innerWidth - 4; if (ww > window.innerHeight) ww = window.innerHeight; wh = ww / 2; canvas.width = ww; @@ -118,13 +208,70 @@ 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); + xmlhttp = new XMLHttpRequest(); + 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); + xmlhttp = new XMLHttpRequest(); + 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); + xmlhttp = new XMLHttpRequest(); + sendquery(byd.toISOString(), ytd.toISOString()); + } + + function yesterday() { + var tdy = daystart(new Date()); + var ytd = new Date(1*tdy - 86400000); + xmlhttp = new XMLHttpRequest(); + sendquery(ytd.toISOString(), tdy.toISOString()); + } + + function today() { + var tdy = daystart(new Date()); + var tmr = new Date(1*tdy + 86400000); + xmlhttp = new XMLHttpRequest(); + sendquery(tdy.toISOString(), tmr.toISOString()); + } + function initialize() { dbg = document.getElementById("debug"); canvas = document.getElementById("plot"); ctx = canvas.getContext("2d"); - xmlhttp = new XMLHttpRequest(); resize(); - sendquery(); + 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 */ @@ -137,14 +284,24 @@ } Water Meters -

Water Meters

+

WATER METERS

Current Values
cold
@@ -187,6 +369,14 @@ canvas#plot {

-
+
+
PREVIOUS WEEK
+
DAY + BEFORE YESTERDAY
+
YESTERDAY
+
TODAY
+
THIS WEEK
+
+