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