
// 
// Our model is the settings object.
//
// (c) 2014 Edouard Lafargue, ed@lafargue.name

define(function(require) {
    "use strict";
    
    var $       = require('jquery'),
        _       = require('underscore'),
        Backbone = require('backbone'),
        tpl     = require('text!tpl/instruments/W433NumView.html'),
                template = null;
    
        try {
            template =  _.template(tpl);
        } catch (e) {
            // Will happen if we are packaged in a Chrome app
            template = require('js/tpl/instruments/W433NumView.js', function(){} , function(err) {
                            console.log("Compiled JS preloading error callback.");
                            });
        }

    return Backbone.View.extend({

        initialize:function (options) {
            linkManager.on('input', this.showInput, this);
            this.sensors = {};

            // Start a watchdog every minute to go over the sensors and find out
            // which ones are stale/lost:
            _.bindAll(this,"refreshSensors");
            this.watchdog = setInterval(this.refreshSensors, 60000);

        },

        render:function () {
            var self = this;
            console.log('Main render of W433 numeric view');
            $(this.el).html(template());
            return this;
        },

        onClose: function() {
            console.log("W433 numeric view closing...");
            linkManager.off('input', this.showInput, this);
            clearInterval(this.watchdog);

        },

        showInput: function(data) {
            var stamp = new Date().getTime();
            // Get sensor info: if we know it, update the value & label
            // if we don't, add it
            var sensor =data.sensor_name + " - " + data.reading_type;
            var sensordata = this.sensors[sensor];
            if (sensordata == undefined) {
                $('#sensorlist',this.el).append('<li id="' + sensor.replace(/ /g, '_') + '">' +
                                                '<span class="label label-success">&nbsp;</span>&nbsp;' +
                                                sensor + ':&nbsp;' + data.value + '</li>');
            } else {
                $('#' + sensor.replace(/ /g, '_'), this.el).html('<span class="label label-success">&nbsp;</span>&nbsp;' +
                                                sensor + ':&nbsp;' + data.value);
            }
            this.sensors[sensor] = { stamp: stamp};


        },

        refreshSensors: function() {
            var self = this;
            console.log("Refresh Sensor labels in num view");
            var stamp = Date.now();
            _.each(this.sensors,function(value,key) {
                if (stamp - value.stamp > 300000) { // 5 minutes, lost
                    $('#' + key.replace(/ /g, '_'), self.el).find('.label').removeClass('label-warning').addClass('label-danger');

                } else if (stamp - value.stamp > 180000) { // 3 minutes, stale
                    $('#' + key.replace(/ /g, '_'), self.el).find('.label').removeClass('label-success').addClass('label-warning');
                }
            });
        }

    });
});
@@ -1753,16 +1753,17 @@
 sensor =
+ 
 data.sen
@@ -2164,27 +2164,248 @@
 nbsp;' +
- data.value
+%0A                                                ((data.reading_type == 'wind' %7C%7C%0A                                                  data.reading_type == 'wind-gust') ? data.value.dir + '%C2%B0 - ' + data.value.speed + 'knt' : data.value)
  + '%3C/li
@@ -2622,26 +2622,209 @@
 bsp;' + 
-data.value
+((data.reading_type == 'wind' %7C%7C%0A                                                  data.reading_type == 'wind-gust') ? data.value.dir + '%C2%B0 - ' + data.value.speed + 'knt' : data.value) + '%3C/li%3E'
 );%0A     

Support wind in numeric display on W433