]> www.average.org Git - pulsecounter.git/blob - web/index.html
320921ec9d65445ddad0b42f63d7950ffba20e90
[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;
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 redraw() {
67     if (cold_d.length && hot_d.length) {
68       drawplot(cold_d, "blue");
69       drawplot(hot_d, "red");
70       xaxis();
71       yaxis();
72     } else {
73       /* load animation here */ ;
74     }
75   }
76
77   function gotdata(data) {
78     document.getElementById("cold").innerHTML =
79       (data.current.cold / 100).toFixed(2);
80     document.getElementById("hot").innerHTML =
81       (data.current.hot / 100).toFixed(2);
82     tmin = data.range.lo;
83     tmax = data.range.hi;
84     tfact = (ww - xzero) / (tmax - tmin);
85     /* differetiate() updates hmax */
86     hmax = 0;
87     cold_d = differentiate(data.cold);
88     hot_d = differentiate(data.hot);
89     hfact = (wh - yzero) / hmax;
90     //document.getElementById("debug").innerHTML = cold_d + "<br>" + hot_d;
91     redraw();
92   }
93
94   function sendquery(lo, hi) {
95     var url = "query.cgi";
96
97     if (lo && hi) url += "?lo=" + lo + "&" + hi;
98     else url = "query.cgi?lo=2015-12-19+00:00:00&hi=2015-12-20+00:00:00"; //FIX
99     xmlhttp.onreadystatechange = function() {
100       if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
101         // dbg.innerHTML = xmlhttp.responseText;
102         var myData = JSON.parse(xmlhttp.responseText);
103         gotdata(myData);
104       }
105     }
106     xmlhttp.open("GET", url, true);
107     xmlhttp.send();
108   }
109
110   function resize() {
111     ww = window.innerWidth;
112     if (ww > window.innerHeight) ww = window.innerHeight;
113     wh = ww / 2;
114     canvas.width = ww;
115     canvas.height = wh;
116     canvas.style.width = ww + "px";
117     canvas.style.height = wh + "px";
118     redraw();
119   }
120
121   function initialize() {
122     dbg = document.getElementById("debug");
123     canvas = document.getElementById("plot");
124     ctx = canvas.getContext("2d");
125     xmlhttp = new XMLHttpRequest();
126     resize();
127     sendquery();
128   }
129
130   /* Set up */
131   if(window.attachEvent) {
132     window.attachEvent('onload', initialize);
133     window.attachEvent('onresize', resize);
134   } else {
135     window.onload = initialize;
136     window.onresize = resize;
137   }
138 </script>
139 <style>
140 h1 {
141   text-align: center;
142 }
143 br {
144   clear: both;
145 }
146 div#currentvals {
147   width: 14em;
148   margin-left: auto;
149   margin-right: auto;
150   margin-bottom: 10px;
151   text-align: center;
152   font-size: 150%;
153 }
154 div.current {
155   position: relative;
156   padding: 0.2em;
157   border: solid 1px black;
158   margin: 0.2em;
159 }
160 div#cold {
161   float: left;
162   background-color: #d0e0ff;
163 }
164 div#hot {
165   float: right;
166   background-color: #ffd0e0;
167 }
168 canvas#plot {
169   padding: 0px;
170   margin: auto;
171   display: block;
172   width: 640px;
173   height: 320px;
174   border: solid 1px black;
175 }
176 body {
177   margin: 0px;
178 }
179 </style>
180 <title>Water Meters</title>
181 </head><body>
182 <h1>Water Meters</h1>
183 <div id="currentvals">
184   Current Values
185   <div class="current" id="cold">cold</div>
186   <div class="current" id="hot">hot</div>
187 </div>
188 <br />
189 <canvas id="plot" width="640" height = "320"></canvas>
190 <br />
191 <div id=debug>
192 </div>
193 </body>