]> www.average.org Git - pulsecounter.git/blob - web/index.html
24d58a725208a8e0de61b5c883d97417ef42d95f
[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 showdate(utime) {
17     var dt = new Date(utime*1000);
18     return dt.toLocaleDateString() + " " + dt.toLocaleTimeString();
19   }
20
21   function getcomb(lo, hi) {
22     var comb = [], lb = [];
23     var d = hi - lo;
24     var ord = Math.pow(10, Math.floor(Math.log10(d)));
25     var scl = Math.floor(d / ord);
26     var inc, inc2, first, x, lb;
27
28     if (scl < 2) { inc = 0.1; inc2 = 0.5; }
29     else if (scl < 5) { inc = 0.2; inc2 = 1; }
30     else { inc = 0.5; inc2 = 2; }
31     inc *= ord;
32     inc2 *= ord;
33     first = (Math.floor(lo / inc) + 1) * inc;
34     for (x = 0; x < (d / inc) - 1.5; x++)
35       comb.push(first + inc * x);
36     first = (Math.floor(lo / inc2) + 1) * inc2;
37     for (x = 0; x < (d / inc2) - 1.5; x++)
38       lb.push(first + inc2 * x);
39     //dbg.innerHTML = "ord=" + ord + "<br>inc=" + inc + "<br>"
40     //              + comb + "<br>" + lb;
41     return [comb, lb];
42   }
43
44   function px(x) {
45     return xzero + ((x - tmin) * tfact);
46   }
47
48   function py(y) {
49     return wh - yzero - (y * hfact);
50   }
51
52   function xaxis() {
53     ctx.beginPath();
54     ctx.moveTo(px(tmin), py(0));
55     ctx.lineTo(px(tmax), py(0));
56     ctx.strokeStyle = "black";
57     ctx.stroke();
58
59     ctx.fillStyle = "black";
60     ctx.font = "bold 8px Courier";
61     ctx.textAlign = "left";
62     ctx.fillText(showdate(tmin), px(tmin), py(0) + 16);
63     ctx.textAlign = "right";
64     ctx.fillText(showdate(tmax), px(tmax), py(0) + 16);
65   }
66
67   function yaxis() {
68     var comb = getcomb(0, hmax);
69     var i;
70
71     ctx.beginPath();
72     for (i = 0; comb[0][i]; i++) {
73       ctx.moveTo(px(tmin) - 5, py(comb[0][i]));
74       ctx.lineTo(px(tmax), py(comb[0][i]));
75     }
76     ctx.strokeStyle = "lightgray";
77     ctx.stroke();
78
79     ctx.beginPath();
80     ctx.moveTo(px(tmin), py(0));
81     ctx.lineTo(px(tmin), py(hmax));
82     ctx.strokeStyle = "black";
83     ctx.stroke();
84
85     ctx.fillStyle = "black";
86     ctx.font = "bold 8px Courier";
87     ctx.textAlign = "right";
88     ctx.fillText(0, px(tmin) - 2, py(0));
89     for (i = 0; comb[1][i]; i++) {
90       ctx.fillText(comb[1][i], px(tmin) - 2, py(comb[1][i]));
91     }
92     ctx.textAlign = "left";
93     ctx.fillText("l/h", px(tmin) + 2, py(hmax) + 8);
94   }
95
96   /* @ updates global var `hmax` */
97   function differentiate(times) {
98     var res = [];
99     var dv, dt, v;
100
101     for (i = 0; i < times.length - 1; i++) {
102       dv = times[i+1][1] - times[i][1];
103       dt = times[i+1][0] - times[i][0];
104       if (dt != 0 && dv != 0) {
105         v = (dv / dt) * 360 ; /* Litres per hour */
106         if (hmax < v) hmax = v;
107         res.push([times[i][0], v]);
108       }
109     }
110     if (i) res.push([times[i][0], v]);
111
112     return res;
113   }
114
115   function drawplot(data, color) {
116     var i;
117
118     ctx.beginPath();
119     ctx.moveTo(px(data[0][0]), py(data[0][1]));
120     for (i = 1; i < data.length; i++) {
121       ctx.lineTo(px(data[i][0]), py(data[i - 1][1]));
122       ctx.lineTo(px(data[i][0]), py(data[i][1]));
123     }
124     ctx.strokeStyle = color;
125     ctx.stroke();
126   }
127
128   function showloading() {
129     ctx.fillStyle = "green";
130     ctx.font = "bold 16px Courier";
131     ctx.textAlign="center";
132     ctx.fillText("...loading...", (ww / 2) , (wh / 2) + 8);
133   }
134
135   function showempty() {
136     ctx.fillStyle = "red";
137     ctx.font = "bold 24px Courier";
138     ctx.textAlign="center";
139     ctx.fillText("No data for the requested time interval",
140                  (ww / 2) , (wh / 2) + 8);
141   }
142
143   function clearplot() {
144     ctx.clearRect(0, 0, ww, wh);
145   }
146
147   function redraw() {
148     clearplot();
149     if (cold_d.length || hot_d.length) {
150       tfact = (ww - xzero) / (tmax - tmin);
151       hfact = (wh - yzero) / hmax;
152       xaxis();
153       yaxis();
154       drawplot(cold_d, "blue");
155       drawplot(hot_d, "red");
156     } else {
157       showempty();
158     }
159   }
160
161   function gotdata(data) {
162     document.getElementById("cold").innerHTML =
163       (data.current.cold / 100).toFixed(2);
164     document.getElementById("hot").innerHTML =
165       (data.current.hot / 100).toFixed(2);
166     tmin = data.range.lo;
167     tmax = data.range.hi;
168     //dbg.innerHTML = "from " + tmin + " to " + tmax
169     //              + "<br>from " + showdate(tmin) + " to " + showdate(tmax);
170     /* differetiate() updates hmax */
171     hmax = 0;
172     cold_d = differentiate(data.cold);
173     hot_d = differentiate(data.hot);
174     //dbg.innerHTML = "hmax=" + hmax + " hfact=" + hfact + "<br>"
175     //              + cold_d + "<br>" + hot_d;
176     redraw();
177   }
178
179   function iso2qu(idate) {
180     return idate.replace("T", "+").replace("0Z", "");
181   }
182
183   function sendquery(lo, hi) {
184     var url = "query.cgi" + "?lo=" + iso2qu(lo) + "&hi=" + iso2qu(hi);
185
186     //dbg.innerHTML = url;
187     xmlhttp.onreadystatechange = function() {
188       if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
189         // dbg.innerHTML = xmlhttp.responseText;
190         var myData = JSON.parse(xmlhttp.responseText);
191         gotdata(myData);
192       }
193     }
194     xmlhttp.open("GET", url, true);
195     clearplot();
196     showloading();
197     xmlhttp.send();
198   }
199
200   function resize() {
201     ww = window.innerWidth - 4;
202     if (ww > window.innerHeight) ww = window.innerHeight;
203     wh = ww / 2;
204     canvas.width = ww;
205     canvas.height = wh;
206     canvas.style.width = ww + "px";
207     canvas.style.height = wh + "px";
208     redraw();
209   }
210
211   function daystart(date) {
212     date.setMilliseconds(0);
213     date.setSeconds(0);
214     date.setMinutes(0);
215     date.setHours(0);
216     return date;
217   }
218
219   function beforeyesterday() {
220     var tdy = daystart(new Date());
221     var ytd = new Date(1*tdy - 86400000);
222     var byd = new Date(1*ytd - 86400000);
223     xmlhttp = new XMLHttpRequest();
224     sendquery(byd.toISOString(), ytd.toISOString());
225   }
226
227   function yesterday() {
228     var tdy = daystart(new Date());
229     var ytd = new Date(1*tdy - 86400000);
230     xmlhttp = new XMLHttpRequest();
231     sendquery(ytd.toISOString(), tdy.toISOString());
232   }
233
234   function today() {
235     var tdy = daystart(new Date());
236     var tmr = new Date(1*tdy + 86400000);
237     xmlhttp = new XMLHttpRequest();
238     sendquery(tdy.toISOString(), tmr.toISOString());
239   }
240
241   function initialize() {
242     dbg = document.getElementById("debug");
243     canvas = document.getElementById("plot");
244     ctx = canvas.getContext("2d");
245     resize();
246     today();
247     
248     document.getElementById("today").onclick = today;
249     document.getElementById("yesterday").onclick = yesterday;
250     document.getElementById("beforeyesterday").onclick = beforeyesterday;
251     //document.getElementById("thisweek").onclick = thisweek;
252     //document.getElementById("lastweek").onclick = lastweek;
253   }
254
255   /* Set up */
256   if(window.attachEvent) {
257     window.attachEvent('onload', initialize);
258     window.attachEvent('onresize', resize);
259   } else {
260     window.onload = initialize;
261     window.onresize = resize;
262   }
263 </script>
264 <style>
265 @font-face {
266   font-family: PipeDream;
267   src: url('PIPED.TTF') format('truetype');
268   /* Free to use font from http://www.mlink.net/~paterson/jpfonts.htm */
269 }
270 h1 {
271   margin-top: 5px;
272   text-align: center;
273   font-family: PipeDream;
274   font-size: 64px;
275   font-weight: normal;
276   background-color: lightgray;
277 }
278 br {
279   clear: both;
280 }
281 div#currentvals {
282   width: 18em;
283   margin-left: auto;
284   margin-right: auto;
285   margin-bottom: 10px;
286   text-align: center;
287   font-size: 150%;
288 }
289 div.current {
290   position: relative;
291   padding: 0.2em;
292   border: solid 1px black;
293   margin: 0.2em;
294 }
295 div#cold {
296   float: left;
297   background-color: #d0e0ff;
298 }
299 div#hot {
300   float: right;
301   background-color: #ffd0e0;
302 }
303 canvas#plot {
304   padding: 0px;
305   margin: auto;
306   display: block;
307   width: 640px;
308   height: 320px;
309   border: solid 1px black;
310 }
311 div#queries {
312   margin-left: auto;
313   margin-right: auto;
314   margin-bottom: 10px;
315   text-align: center;
316 }
317 div.query {
318   display: inline-block;
319   width: 8em;
320   height: 8em;
321   border: solid 1px black;
322   background-color: lightgray;
323   cursor: pointer;
324   vertical-align: middle;
325 }
326 span.query {
327   display: inline-block;
328   vertical-align: middle;   /* does not work for some reason */
329 }
330 body {
331   margin: 0px;
332 }
333 </style>
334 <title>Water Meters</title>
335 </head><body>
336 <h1>WATER METERS</h1>
337 <div id="currentvals">
338   Current Values
339   <div class="current" id="cold">cold</div>
340   <div class="current" id="hot">hot</div>
341 </div>
342 <br />
343 <canvas id="plot" width="640" height = "320"></canvas>
344 <br />
345 <div id="queries">
346  <div class="query" id="prevweek"><span class="query">PREV WEEK</span></div>
347  <div class="query" id="beforeyesterday"><span class="query">DAY
348    BEFORE YESTERDAY</span></div>
349  <div class="query" id="yesterday"><span class="query">YESTERDAY</span></div>
350  <div class="query" id="today"><span class="query">TODAY</span></div>
351  <div class="query" id="thisweek"><span class="query">THIS WEEK</span></div>
352 </div>
353 <br />
354 <div id="debug"></div>
355 </body>