Mercurial > ~darius > hgwebdir.cgi > iwws
annotate static/iwws.js @ 7:24e9b717722d
- Update every 10 minutes.
- Redraw the graph on orientation change.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Tue, 16 Aug 2011 22:34:30 +0930 |
parents | 2d9ee2b3ae82 |
children | 4b9d1b47ca17 |
rev | line source |
---|---|
0 | 1 /* |
2 * Copyright 2011 Daniel O'Connor <darius@dons.net.au> | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without modification, are | |
5 * permitted provided that the following conditions are met: | |
6 * | |
7 * 1. Redistributions of source code must retain the above copyright notice, this list of | |
8 * conditions and the following disclaimer. | |
9 * | |
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list | |
11 * of conditions and the following disclaimer in the documentation and/or other materials | |
12 * provided with the distribution. | |
13 * | |
14 * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED | |
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR | |
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
23 * | |
24 * The views and conclusions contained in the software and documentation are those of the | |
25 * authors and should not be interpreted as representing official policies, either expressed | |
26 * or implied, of Daniel O'Connor. | |
27 */ | |
28 | |
7
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
29 var timer = null; |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
30 var datacache = null; |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
31 |
0 | 32 $.jQTouch({ |
33 icon: 'icon.png', | |
34 startupScreen: 'img/startup.png' | |
35 }); | |
36 | |
7
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
37 function draw_graph() { |
0 | 38 var temp_out = []; |
39 var hum_out = []; | |
40 var wavg = []; | |
41 var wgust = []; | |
42 var rain = []; | |
43 var i, mint = 5, maxt = 35; | |
7
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
44 var l = datacache['idx'].length - 1; |
0 | 45 var d = new Date(); |
46 var tzofs = d.getTimezoneOffset() * 60; | |
47 for (i = 0; i < l; i++) { | |
48 // Convert time from UTC to browser LT | |
7
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
49 t = datacache['idx'][i] - tzofs; |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
50 if (datacache['temp_out'] < mint) |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
51 mint = datacache['temp_out'] |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
52 if (datacache['temp_out'] < maxt) |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
53 maxt = datacache['temp_out'] |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
54 temp_out.push([t * 1000.0, datacache['temp_out'][i]]); |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
55 hum_out.push([t * 1000.0, datacache['hum_out'][i]]); |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
56 wavg.push([t * 1000.0, datacache['wind_ave'][i], wind2angle(datacache['wind_dir'][i])]); |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
57 wgust.push([t * 1000.0, datacache['wind_gust'][i], wind2angle(datacache['wind_dir'][i])]); |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
58 if (datacache['rain'][i] > 0) |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
59 rain.push([t * 1000.0, datacache['rain'][i]]); |
0 | 60 } |
61 $.plot($("#graph1"), [ | |
62 { data : temp_out, label: "Temp.", yaxis : 1, points : { show : true }, lines : { show : true } }, | |
63 { data : hum_out, label: "RH", yaxis : 2, points : { show : true }, lines : { show : true } } | |
64 ], | |
65 { xaxis : { mode : 'time' }, | |
66 legend : { backgroundOpacity : 0, position : 'nw' }, | |
67 yaxis : { min : mint, max : maxt, tickFormatter : degCFormatter }, | |
68 y2axis : { min : 0, max : 100, tickFormatter : pctFormatter } | |
69 }); | |
70 $.plot($("#graph2"), [ | |
71 { data : wavg, label: "Wind (Avg)", yaxis : 1, points : { show : true }, lines : { show : true }, direction : true }, | |
72 { data : wgust, label: "Wind (Gust)", yaxis : 1, points : { show : true }, lines : { show : true }, direction : true }, | |
73 { data : rain, label: "Rain", yaxis : 2, bars : { show : true, barWidth : 0.5 * 60 * 60 * 1000, align : "centre" } } | |
74 ], | |
75 { xaxis : { mode : 'time' }, | |
76 legend : { backgroundOpacity : 0, position : 'nw' }, | |
77 yaxis : { tickFormatter : spdFormatter }, | |
78 y2axis : { min : 0, tickFormatter : mmFormatter } | |
79 }); | |
80 } | |
81 | |
82 function wind2angle(dir) { | |
83 var a = dir * (360.0 / 16.0); | |
84 return a; | |
85 } | |
86 | |
87 function degCFormatter(v, axis) { | |
88 return v.toFixed(axis.tickDecimals) +"°C"; | |
89 } | |
90 | |
91 function pctFormatter(v, axis) { | |
92 return v.toFixed(axis.tickDecimals) +"%"; | |
93 } | |
94 | |
95 function spdFormatter(v, axis) { | |
96 return v.toFixed(axis.tickDecimals) + "kph"; | |
97 } | |
98 | |
99 function mmFormatter(v, axis) { | |
100 return v.toFixed(axis.tickDecimals) + "mm"; | |
101 } | |
102 | |
7
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
103 function got_data(data, status) { |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
104 if (status != "success") { |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
105 $.log("Couldn't load data. status = " + status); |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
106 return; |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
107 } |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
108 |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
109 datacache = data; |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
110 draw_graph(); |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
111 } |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
112 |
0 | 113 function update_data() { |
7
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
114 /* Cancel any pending timeout (eg if the user pressed update) */ |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
115 if (timer != null) |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
116 clearTimeout(timeout); |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
117 jQuery.getJSON('iwws/getdata.json', got_data); |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
118 /* Set to refresh in 10 minutes */ |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
119 timeout = setTimeout(update_data, 10 * 60 * 60); |
0 | 120 } |
121 | |
122 $(document).ready(function(){ | |
123 update_data(); | |
7
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
124 |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
125 $('body').bind('turn', function(event, info) { |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
126 draw_graph(); |
24e9b717722d
- Update every 10 minutes.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
127 }); |
0 | 128 }); |