]> www.average.org Git - pulsecounter.git/blobdiff - web/index.html
wrong conversion. l/min, not /hr
[pulsecounter.git] / web / index.html
index ea05899a87cb3a4a974dc730ef881d99acd74a17..debb7d8b73aa02dcdb50c35a8292e4891625ebe7 100644 (file)
 <html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <script>
-  var canvas;
-  var context;
-  function drawplot(times, color) {
-    var tmin = times[0][0];
-    var tfact = 640/(times[times.length-1][0] - tmin);
+  var dbg;
+  var canvas, ctx;
+  var ww, wh;
+  var hmax, hfact;
+  var tmin, tmax, tfact;
+  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 + "<br>inc=" + inc + "<br>"
+    //              + comb + "<br>" + 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/min", px(tmin) + 4, py(hmax) + 8);
+  }
+
+  /* @ 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 = 480 / hmax;
-    context.beginPath();
-    context.moveTo(0, 480 - height[1]*hfact);
-    for (i = 1; i < times.length; i++) {
-      context.lineTo((times[i][0] - tmin)*tfact,480 - height[i]*hfact);
-      context.lineTo((times[i][0] - tmin)*tfact,480 - 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() {
+    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) {
+    document.getElementById("cold").innerHTML =
+      (data.current.cold / 100).toFixed(2);
+    document.getElementById("hot").innerHTML =
+      (data.current.hot / 100).toFixed(2);
+    tmin = data.range.lo;
+    tmax = data.range.hi;
+    //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 && xmlhttp.status == 200) {
+        // dbg.innerHTML = xmlhttp.responseText;
+        var myData = JSON.parse(xmlhttp.responseText);
+        gotdata(myData);
+      }
+    }
+    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");
     canvas = document.getElementById("plot");
-    context = canvas.getContext("2d");
-    drawplot(data.cold, "blue");
-    drawplot(data.hot, "red");
+    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: both;
+}
+div#currentvals {
+  width: 18em;
+  margin-left: auto;
+  margin-right: auto;
+  margin-bottom: 10px;
+  text-align: center;
+  font-size: 150%;
+}
+div.current {
+  position: relative;
+  padding: 0.2em;
+  border: solid 1px black;
+  margin: 0.2em;
+}
+div#cold {
+  float: left;
+  background-color: #d0e0ff;
+}
+div#hot {
+  float: right;
+  background-color: #ffd0e0;
+}
+canvas#plot {
+  padding: 0px;
+  margin: auto;
+  display: block;
+  width: 640px;
+  height: 320px;
+  /* border: solid 1px black; */
+}
+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>
-<canvas id="plot" width="640" height = "480"
-        style="border:solid 1px #000000;"></canvas>
-<hr/>
-<div id=debug>
+<h1>WATER METERS</h1>
+<div id="currentvals">
+  Current Values
+  <div class="current" id="cold">cold</div>
+  <div class="current" id="hot">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 />
+<canvas id="plot" width="640" height = "320"></canvas>
+<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>