diff 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
line wrap: on
line diff
--- a/static/iwws.js	Mon Aug 15 23:07:52 2011 +0930
+++ b/static/iwws.js	Tue Aug 16 22:34:30 2011 +0930
@@ -26,39 +26,37 @@
  * or implied, of Daniel O'Connor.
  */
 
+var timer = null;
+var datacache = null;
+
 $.jQTouch({
     icon: 'icon.png',
     startupScreen: 'img/startup.png'
 });
 
-function draw_graph(data, status) {
-    if (status != "success") {
-	$.log("Couldn't load data. status = %s", status);
-	return;
-    }
-
+function draw_graph() {
     var temp_out = [];
     var hum_out = [];
     var wavg = [];
     var wgust = [];
     var rain = [];
     var i, mint = 5, maxt = 35;
-    var l = data['idx'].length - 1;
+    var l = datacache['idx'].length - 1;
     var d = new Date();
     var tzofs = d.getTimezoneOffset() * 60;
     for (i = 0; i < l; i++) {
 	// Convert time from UTC to browser LT
-	t = data['idx'][i] - tzofs;
-	if (data['temp_out'] < mint)
-	    mint = data['temp_out']
-	if (data['temp_out'] < maxt)
-	    maxt = data['temp_out']
-	temp_out.push([t * 1000.0, data['temp_out'][i]]);
-	hum_out.push([t * 1000.0, data['hum_out'][i]]);
-	wavg.push([t * 1000.0, data['wind_ave'][i], wind2angle(data['wind_dir'][i])]);
-	wgust.push([t * 1000.0, data['wind_gust'][i], wind2angle(data['wind_dir'][i])]);
-	if (data['rain'][i] > 0)
-	    rain.push([t * 1000.0, data['rain'][i]]);
+	t = datacache['idx'][i] - tzofs;
+	if (datacache['temp_out'] < mint)
+	    mint = datacache['temp_out']
+	if (datacache['temp_out'] < maxt)
+	    maxt = datacache['temp_out']
+	temp_out.push([t * 1000.0, datacache['temp_out'][i]]);
+	hum_out.push([t * 1000.0, datacache['hum_out'][i]]);
+	wavg.push([t * 1000.0, datacache['wind_ave'][i], wind2angle(datacache['wind_dir'][i])]);
+	wgust.push([t * 1000.0, datacache['wind_gust'][i], wind2angle(datacache['wind_dir'][i])]);
+	if (datacache['rain'][i] > 0)
+	    rain.push([t * 1000.0, datacache['rain'][i]]);
     }
     $.plot($("#graph1"), [
 	{ data : temp_out, label: "Temp.", yaxis : 1, points : { show : true }, lines : { show : true } },
@@ -102,10 +100,29 @@
     return v.toFixed(axis.tickDecimals) + "mm";
 }
 
+function got_data(data, status) {
+    if (status != "success") {
+	$.log("Couldn't load data. status = " + status);
+	return;
+    }
+
+    datacache = data;
+    draw_graph();
+}
+
 function update_data() {
-    jQuery.getJSON('iwws/getdata.json', draw_graph);
+    /* Cancel any pending timeout (eg if the user pressed update) */
+    if (timer != null)
+	clearTimeout(timeout);
+    jQuery.getJSON('iwws/getdata.json', got_data);
+    /* Set to refresh in 10 minutes */
+    timeout = setTimeout(update_data, 10 * 60 * 60);
 }
 
 $(document).ready(function(){
     update_data();
+
+    $('body').bind('turn', function(event, info) {
+	draw_graph();
+    });
 });