]> www.average.org Git - pulsecounter.git/blob - web/index.html
use Pipe-Dream font by Jonathan Paterson
[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
60   function yaxis() {
61     var comb = getcomb(0, hmax);
62     var i;
63
64     ctx.beginPath();
65     for (i = 0; comb[0][i]; i++) {
66       ctx.moveTo(px(tmin) - 5, py(comb[0][i]));
67       ctx.lineTo(px(tmax), py(comb[0][i]));
68     }
69     ctx.strokeStyle = "lightgray";
70     ctx.stroke();
71
72     ctx.beginPath();
73     ctx.moveTo(px(tmin), py(0));
74     ctx.lineTo(px(tmin), py(hmax));
75     ctx.strokeStyle = "black";
76     ctx.stroke();
77
78     ctx.fillStyle = "black";
79     ctx.font = "bold 8px Courier";
80     ctx.fillText(0, px(tmin) - 8, py(0));
81     for (i = 0; comb[1][i]; i++) {
82       ctx.fillText(comb[1][i], px(tmin) - xzero + 2, py(comb[1][i]));
83     }
84     ctx.fillText("l/h", px(tmin) + 2, py(hmax) + 8);
85   }
86
87   /* @ updates global var `hmax` */
88   function differentiate(times) {
89     var res = [];
90     var dv, dt, v;
91
92     for (i = 0; i < times.length - 1; i++) {
93       dv = times[i+1][1] - times[i][1];
94       dt = times[i+1][0] - times[i][0];
95       if (dt != 0 && dv != 0) {
96         v = (dv / dt) * 360 ; /* Litres per hour */
97         if (hmax < v) hmax = v;
98         res.push([times[i][0], v]);
99       }
100     }
101     res.push([times[i][0], v]);
102
103     return res;
104   }
105
106   function drawplot(data, color) {
107     var i;
108
109     ctx.beginPath();
110     ctx.moveTo(px(data[0][0]), py(data[0][1]));
111     for (i = 1; i < data.length; i++) {
112       ctx.lineTo(px(data[i][0]), py(data[i - 1][1]));
113       ctx.lineTo(px(data[i][0]), py(data[i][1]));
114     }
115     ctx.strokeStyle = color;
116     ctx.stroke();
117   }
118
119   function showloading() {
120     ctx.fillStyle = "green";
121     ctx.font = "bold 16px Courier";
122     ctx.fillText("loading", (ww / 2) - 40 , (wh / 2) + 8);
123   }
124
125   function clearplot() {
126     ctx.clearRect(0, 0, ww, wh);
127   }
128
129   function redraw() {
130     if (cold_d.length && hot_d.length) {
131       tfact = (ww - xzero) / (tmax - tmin);
132       hfact = (wh - yzero) / hmax;
133       clearplot();
134       xaxis();
135       yaxis();
136       drawplot(cold_d, "blue");
137       drawplot(hot_d, "red");
138     } else {
139       showloading();
140     }
141   }
142
143   function gotdata(data) {
144     document.getElementById("cold").innerHTML =
145       (data.current.cold / 100).toFixed(2);
146     document.getElementById("hot").innerHTML =
147       (data.current.hot / 100).toFixed(2);
148     tmin = data.range.lo;
149     tmax = data.range.hi;
150     dbg.innerHTML = "from " + tmin + " to " + tmax
151                   + "<br>from " + showdate(tmin) + " to " + showdate(tmax);
152     /* differetiate() updates hmax */
153     hmax = 0;
154     cold_d = differentiate(data.cold);
155     hot_d = differentiate(data.hot);
156     //dbg.innerHTML = "hmax=" + hmax + " hfact=" + hfact + "<br>"
157     //              + cold_d + "<br>" + hot_d;
158     redraw();
159   }
160
161   function iso2qu(idate) {
162     return idate.replace("T", "+").replace("0Z", "");
163   }
164
165   function sendquery(lo, hi) {
166     var url = "query.cgi" + "?lo=" + iso2qu(lo) + "&hi=" + iso2qu(hi);
167
168     //dbg.innerHTML = url;
169     xmlhttp.onreadystatechange = function() {
170       if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
171         // dbg.innerHTML = xmlhttp.responseText;
172         var myData = JSON.parse(xmlhttp.responseText);
173         gotdata(myData);
174       }
175     }
176     xmlhttp.open("GET", url, true);
177     xmlhttp.send();
178   }
179
180   function resize() {
181     ww = window.innerWidth - 4;
182     if (ww > window.innerHeight) ww = window.innerHeight;
183     wh = ww / 2;
184     canvas.width = ww;
185     canvas.height = wh;
186     canvas.style.width = ww + "px";
187     canvas.style.height = wh + "px";
188     redraw();
189   }
190
191   function initialize() {
192     var tdy = new Date();
193     var tmr;
194
195     tdy.setMilliseconds(0);
196     tdy.setSeconds(0);
197     tdy.setMinutes(0);
198     tdy.setHours(0);
199     tmr = new Date(1*tdy + 86400000);
200     dbg = document.getElementById("debug");
201     canvas = document.getElementById("plot");
202     ctx = canvas.getContext("2d");
203     resize();
204     
205     xmlhttp = new XMLHttpRequest();
206     sendquery(tdy.toISOString(), tmr.toISOString());
207   }
208
209   /* Set up */
210   if(window.attachEvent) {
211     window.attachEvent('onload', initialize);
212     window.attachEvent('onresize', resize);
213   } else {
214     window.onload = initialize;
215     window.onresize = resize;
216   }
217 </script>
218 <style>
219 @font-face {
220   font-family: PipeDream;
221   src: url('PIPED.TTF') format('truetype');
222   /* Free to use font from http://www.mlink.net/~paterson/jpfonts.htm */
223 }
224 h1 {
225   margin-top: 5px;
226   text-align: center;
227   font-family: PipeDream;
228   font-size: 64px;
229   font-weight: normal;
230   background-color: lightgray;
231 }
232 br {
233   clear: both;
234 }
235 div#currentvals {
236   width: 18em;
237   margin-left: auto;
238   margin-right: auto;
239   margin-bottom: 10px;
240   text-align: center;
241   font-size: 150%;
242 }
243 div.current {
244   position: relative;
245   padding: 0.2em;
246   border: solid 1px black;
247   margin: 0.2em;
248 }
249 div#cold {
250   float: left;
251   background-color: #d0e0ff;
252 }
253 div#hot {
254   float: right;
255   background-color: #ffd0e0;
256 }
257 canvas#plot {
258   padding: 0px;
259   margin: auto;
260   display: block;
261   width: 640px;
262   height: 320px;
263   border: solid 1px black;
264 }
265 body {
266   margin: 0px;
267 }
268 </style>
269 <title>Water Meters</title>
270 </head><body>
271 <h1>WATER METERS</h1>
272 <div id="currentvals">
273   Current Values
274   <div class="current" id="cold">cold</div>
275   <div class="current" id="hot">hot</div>
276 </div>
277 <br />
278 <canvas id="plot" width="640" height = "320"></canvas>
279 <br />
280 <div id=debug>
281 </div>
282 </body>