]> www.average.org Git - pulsecounter.git/blob - web/index.html
9e459a20450ddcba8543270404b1fdbe25633d8f
[pulsecounter.git] / web / index.html
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">
5 <head>
6 <script>
7   var dbg;
8   var xmlhttp;
9   var canvas, ctx;
10   var ww, wh;
11   var hmax, hfact;
12   var tmin, tmax, tfact;
13   var xzero = 20, yzero = 20;
14   var cold_d = [], hot_d = [];
15
16   function px(x) {
17     return xzero + ((x - tmin) * tfact);
18   }
19   function py(y) {
20     return wh - yzero - (y * hfact);
21   }
22   function xaxis() {
23     ctx.beginPath();
24     ctx.moveTo(px(tmin), py(0));
25     ctx.lineTo(px(tmax), py(0));
26     ctx.strokeStyle = "black";
27     ctx.stroke();
28   }
29   function yaxis() {
30     ctx.beginPath();
31     ctx.moveTo(px(tmin), py(0));
32     ctx.lineTo(px(tmin), py(hmax));
33     ctx.strokeStyle = "black";
34     ctx.stroke();
35   }
36   /* @ updates global var `hmax` */
37   function differentiate(times) {
38     var res = [];
39     var dv, dt, v;
40
41     for (i = 0; i < times.length - 1; i++) {
42       dv = times[i+1][1] - times[i][1];
43       dt = times[i+1][0] - times[i][0];
44       if (dt != 0 && dv != 0) {
45         v = (dv / dt) * 360 ; /* Litres per hour */
46         if (hmax < v) hmax = v;
47         res.push([times[i][0], v]);
48       }
49     }
50     res.push([times[i][0], v]);
51
52     return res;
53   }
54   function drawplot(data, color) {
55     var i;
56
57     ctx.beginPath();
58     ctx.moveTo(px(data[0][0]), py(data[0][1]));
59     for (i = 1; i < data.length; i++) {
60       ctx.lineTo(px(data[i][0]), py(data[i - 1][1]));
61       ctx.lineTo(px(data[i][0]), py(data[i][1]));
62     }
63     ctx.strokeStyle = color;
64     ctx.stroke();
65   }
66   function showloading() {
67     ctx.fillStyle = "green";
68     ctx.font = "bold 16px Courier";
69     ctx.fillText("loading", (ww / 2) - 40 , (wh / 2) + 8);
70   }
71   function clearplot() {
72     ctx.clearRect(0, 0, ww, wh);
73   }
74   function redraw() {
75     if (cold_d.length && hot_d.length) {
76       clearplot();
77       drawplot(cold_d, "blue");
78       drawplot(hot_d, "red");
79       xaxis();
80       yaxis();
81     } else {
82       showloading();
83     }
84   }
85
86   function gotdata(data) {
87     document.getElementById("cold").innerHTML =
88       (data.current.cold / 100).toFixed(2);
89     document.getElementById("hot").innerHTML =
90       (data.current.hot / 100).toFixed(2);
91     tmin = data.range.lo;
92     tmax = data.range.hi;
93     tfact = (ww - xzero) / (tmax - tmin);
94     /* differetiate() updates hmax */
95     hmax = 0;
96     cold_d = differentiate(data.cold);
97     hot_d = differentiate(data.hot);
98     hfact = (wh - yzero) / hmax;
99     //dbg.innerHTML = "hmax=" + hmax + " hfact=" + hfact + "<br>"
100     //              + cold_d + "<br>" + hot_d;
101     redraw();
102   }
103
104   function sendquery(lo, hi) {
105     var url = "query.cgi";
106
107     if (lo && hi) url += "?lo=" + lo + "&" + hi;
108     else url = "query.cgi?lo=2015-12-19+00:00:00&hi=2015-12-20+00:00:00"; //FIX
109     xmlhttp.onreadystatechange = function() {
110       if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
111         // dbg.innerHTML = xmlhttp.responseText;
112         var myData = JSON.parse(xmlhttp.responseText);
113         gotdata(myData);
114       }
115     }
116     xmlhttp.open("GET", url, true);
117     xmlhttp.send();
118   }
119
120   function resize() {
121     ww = window.innerWidth - 4;
122     if (ww > window.innerHeight) ww = window.innerHeight;
123     wh = ww / 2;
124     canvas.width = ww;
125     canvas.height = wh;
126     canvas.style.width = ww + "px";
127     canvas.style.height = wh + "px";
128     redraw();
129   }
130
131   function initialize() {
132     dbg = document.getElementById("debug");
133     canvas = document.getElementById("plot");
134     ctx = canvas.getContext("2d");
135     xmlhttp = new XMLHttpRequest();
136     resize();
137     sendquery();
138   }
139
140   /* Set up */
141   if(window.attachEvent) {
142     window.attachEvent('onload', initialize);
143     window.attachEvent('onresize', resize);
144   } else {
145     window.onload = initialize;
146     window.onresize = resize;
147   }
148 </script>
149 <style>
150 h1 {
151   text-align: center;
152 }
153 br {
154   clear: both;
155 }
156 div#currentvals {
157   width: 18em;
158   margin-left: auto;
159   margin-right: auto;
160   margin-bottom: 10px;
161   text-align: center;
162   font-size: 150%;
163 }
164 div.current {
165   position: relative;
166   padding: 0.2em;
167   border: solid 1px black;
168   margin: 0.2em;
169 }
170 div#cold {
171   float: left;
172   background-color: #d0e0ff;
173 }
174 div#hot {
175   float: right;
176   background-color: #ffd0e0;
177 }
178 canvas#plot {
179   padding: 0px;
180   margin: auto;
181   display: block;
182   width: 640px;
183   height: 320px;
184   border: solid 1px black;
185 }
186 body {
187   margin: 0px;
188 }
189 </style>
190 <title>Water Meters</title>
191 </head><body>
192 <h1>Water Meters</h1>
193 <div id="currentvals">
194   Current Values
195   <div class="current" id="cold">cold</div>
196   <div class="current" id="hot">hot</div>
197 </div>
198 <br />
199 <canvas id="plot" width="640" height = "320"></canvas>
200 <br />
201 <div id=debug>
202 </div>
203 </body>