view static/iwws.js @ 10:15fed86d9fac

Fix example to be correct.
author Daniel O'Connor <darius@dons.net.au>
date Fri, 03 Feb 2012 23:47:25 +1030
parents 6edb93c20971
children bfe6ed563ffc
line wrap: on
line source

/*
 * Copyright 2011 Daniel O'Connor <darius@dons.net.au>
 *
 *  Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright notice, this list of
 *      conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright notice, this list
 *      of conditions and the following disclaimer in the documentation and/or other materials
 *      provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of Daniel O'Connor.
 */

var timer = null;
var datacache = null;

$.jQTouch({
    icon: 'icon.png',
    startupScreen: 'img/startup.png'
});

function draw_graph() {
    var temp_out = [];
    var dewpt = [];
    var wavg = [];
    var wgust = [];
    var rain = [];
    var pressure = [];
    var i, mint = 5, maxt = 35;
    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 = datacache['idx'][i] - tzofs;
	if (datacache['temp_out'][i] < mint)
	    mint = datacache['temp_out']
	if (datacache['temp_out'][i] > maxt)
	    maxt = datacache['temp_out'][i]
	temp_out.push([t * 1000.0, datacache['temp_out'][i]]);
	if (datacache['hum_out'][i] != null && datacache['temp_out'][i] != null) {
	    dewpt.push([t * 1000.0, hum2dp(datacache['hum_out'][i], datacache['temp_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])]);
	pressure.push([t * 1000.0, datacache['abs_pressure'][i]]);
	if (datacache['rain'][i] > 0)
	    rain.push([t * 1000.0, datacache['rain'][i]]);
    }

    $.plot($("#graph1"), [
	{ data : pressure, label: "Baro", yaxis : 1, points : { show : true }, lines : { show : true } },
	{ data : temp_out, label: "Temp.", yaxis : 2, points : { show : true }, lines : { show : true } },
	{ data : dewpt, label: "Dew point", yaxis : 2, points : { show : true }, lines : { show : true } },
    ],
	   { xaxis : { mode : 'time' },
	     legend : { backgroundOpacity : 0, position : 'nw' },
	     yaxis : { min : 950, max : 1050, tickFormatter : baroFormatter },
	     y2axis : { min : mint, max : maxt, tickFormatter : degCFormatter },
	   });
    $.plot($("#graph2"), [
	{ data : wavg, label: "Wind (Avg)", yaxis : 1, points : { show : true }, lines : { show : true }, direction : true },
	{ data : wgust, label: "Wind (Gust)", yaxis : 1, points : { show : true }, lines : { show : true }, direction : true },
	{ data : rain, label: "Rain", yaxis : 2, bars : { show : true, barWidth : 0.5 * 60 * 60 * 1000, align : "centre" } },
    ],
	   { xaxis : { mode : 'time' },
	     legend : { backgroundOpacity : 0, position : 'nw' },
	     yaxis : { tickFormatter : spdFormatter },
	     y2axis : { min : 0, tickFormatter : mmFormatter }
	   });
}

function wind2angle(dir) {
    var a = dir * (360.0 / 16.0);
    return a;
}

// Formula from http://www.paroscientific.com/dewpoint.htm
function alpha(temp, rh, a, b) {
    return (((a * temp) / (b + temp)) + Math.log(rh / 100.0));
}

function hum2dp(rh, temp) {
    var a = 17.27;
    var b = 237.7;
    var Td = (b * alpha(temp, rh, a, b)) / (a - alpha(temp, rh, a, b));
    return Td;
}

function degCFormatter(v, axis) {
    return v.toFixed(axis.tickDecimals) +"°C";
}

function pctFormatter(v, axis) {
    return v.toFixed(axis.tickDecimals) +"%";
}

function spdFormatter(v, axis) {
    return v.toFixed(axis.tickDecimals) + "kph";
}

function mmFormatter(v, axis) {
    return v.toFixed(axis.tickDecimals) + "mm";
}

function baroFormatter(v, axis) {
    return v.toFixed(axis.tickDecimals) + "hPa";
}

function got_data(data, status) {
    if (status != "success") {
	$.log("Couldn't load data. status = " + status);
	return;
    }

    datacache = data;
    draw_graph();
}

function update_data() {
    /* 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();
    });
});