3 * @purpose GUI in JavaScript using jQuery and jQuery Flot libraries
7 $(document).ready(function()
14 $.fn.pruneSensorData = function(id)
16 var sensor = g_sensors[id]
17 // remove unwanted values
18 if (g_storeTime[id] != null)
20 var last_time = sensor[sensor.length-1][0]
21 //console.log("Last time: " + String(last_time))
23 for (back_index = sensor.length-1; back_index > 0; --back_index)
25 if ((last_time - sensor[back_index][0]) > g_storeTime[id])
30 //console.log("Keep point " + String(sensor[back_index][0]) + "s ago")
35 //console.log("Delete " +String(back_index))
36 sensor.splice(0, back_index)
43 $.fn.updateSensor = function(json)
45 //console.log(json.data)
46 var sensor = g_sensors[json.id]
47 var most_recent = null
48 if (sensor.length > 0)
49 most_recent = sensor[sensor.length-1][0]
51 for (var i=0; i < json.data.length; ++i)
53 if (json.data[i][0] > most_recent || most_recent == null)
55 sensor.push(json.data[i])
56 most_recent = json.data[i][0]
60 $(this).pruneSensorData(json.id)
62 //console.log("Plot:")
63 //console.log(g_sensors[json.id])
64 $.plot("#sensor"+String(json.id)+"_plot", [g_sensors[json.id]])
65 $.ajax({url : "/api/sensors", data : {id : json.id}, success : function(data) {$(this).updateSensor(data);}});
70 $.fn.reloadSensor = function(id)
72 g_storeTime[id] = Number($("#sensor"+String(id)+"_settings").find("#storeTime").val())
73 console.log(String($("#sensor"+String(id)+"_settings").find("#storeTime").val()))
77 $(this).pruneSensorData(id)
78 $.plot("#sensor"+String(id)+"_plot", [g_sensors[id]])
81 $.fn.LoadGUI = function()
83 //TODO: Get rid of g_numSensors; query the server for sensors?
87 for (var i = 0; i < g_numSensors; ++i)
89 g_sensors.push([new Array()])
91 //plotsHTML += "<div id=sensor"+String(i)+"\n"
92 plotsHTML += "<h2 id=sensor"+String(i)+"_name>Sensor: "+String(i)+"</h2>\n"
93 plotsHTML += "<div id=sensor"+String(i)+"_plot class=\"plot\" onclick=\"window.open('/api/sensors?id="+String(i)+"&dump')\"> </div>\n"
94 plotsHTML += "<div id=sensor"+String(i)+"_settings\n"
95 plotsHTML += "<p> Plot since <input type=text id=storeTime value=\""+String(g_storeTime[i])+"\"/> seconds ago\n"
96 plotsHTML += "<button id=update onclick=$(document).reloadSensor("+String(i)+")>Update</button></p>\n"
97 plotsHTML += "<button id=dump onclick=\"window.open('/api/sensors?id="+String(i)+"&dump')\">Dump All Data\n"
98 plotsHTML += "</div>\n" // end settings
99 //plotsHTML += "</div>\n" //
102 $("#plots").html(plotsHTML)
112 for (var i = 0; i < g_numSensors; ++i)
114 $.ajax({url : "/api/sensors", data : {id : i}, success : function(data) {$(this).updateSensor(data);}})