2 * Graph sensor and/or actuator values
5 //TODO: Clean this file up, I bow to Jeremy's superior JavaScript knowledge
10 mctx.graph.api.sensors = mctx.api + "sensors";
11 mctx.graph.api.actuators = mctx.api + "actuators";
14 mctx.graph.dependent = null;
15 mctx.graph.independent = null;
16 mctx.graph.timer = null;
19 * Helper - Calculate pairs of (dependent, independent) values
20 * Given input as (time, value) pairs for dependent and independent
21 * Appends each value pair to the result
24 function dataMerge(dependent, independent, result) {
27 for (var i = 0; i < dependent.length-1; ++i) {
28 var start = dependent[i][0];
29 var end = dependent[i+1][0];
30 var average = 0; var n = 0;
31 for (; j < independent.length; ++j) {
32 if (independent[j][0] < start)
34 else if (independent[j][0] >= end)
36 average += independent[j][1];
41 result.push([dependent[i][1], average]);
48 * Helper function adds the sensors and actuators to a form
50 $.fn.deployDevices = function(input_type, check_first) {
51 var formhtml = $(this).html();
52 var formname = $(this).attr("id");
53 // formhtml += "<i> Sensors </i>"
54 var checked = "checked";
57 $.each(mctx.sensors, function(key, val) {
58 formhtml += "<input type=\""+input_type+"\" value=\""+val+"\" id=\"sensors\" name=\""+formname+"\""+checked+">" + val + "</input>";
61 // formhtml += "<i> Actuators </i>"
62 $.each(mctx.actuators, function(key, val) {
63 formhtml += "<input type=\""+input_type+"\" value=\""+val+"\" id=\"actuators\" name=\""+formname+"\""+checked+">" + val + "</input>";
66 $(this).html(formhtml);
70 * Identify sensors/actuators
71 * @returns itself (Is this right?)
73 $.fn.setDevices = function() {
74 // Query for sensors and actuators
75 var sensor_curtime = 0;
76 var actuator_curtime = 0;
78 $.ajax({url : mctx.api + "?sensors"}).done(function(data) {
79 mctx.sensors = $.extend(mctx.sensors, data.sensors);
80 sensor_curtime = data.running_time;
82 $.ajax({url : mctx.api + "?actuators"}).done(function(data) {
83 mctx.actuators = $.extend(mctx.actuators, data.actuators);
84 actuator_curtime = data.running_time;
87 $("#xaxis").deployDevices("radio", false);
88 $("#yaxis").deployDevices("checkbox", true);
89 var c = Math.max(actuator_curtime, sensor_curtime);
90 $("input[name=current_time]", "#time_range").val(c);
100 * Sets the graphs to graph stuff.
103 $.fn.setGraph = function () {
104 clearTimeout(mctx.graph.timer);
105 var sensor_url = mctx.api + "sensors";
106 var actuator_url = mctx.api + "actuators";
108 var updateData = function(json, data) {
109 for (var i = 0; i < json.data.length; ++i)
110 data.push(json.data[i]);
116 // Determine which actuator/sensors to plot
118 var xaxis = $("input[name=xaxis]:checked", "#xaxis");
119 var yaxis = $("input[name=yaxis]:checked", "#yaxis");
120 var start_time = $("#start_time").val();
121 var end_time = $("#end_time").val();
122 if (!$.isNumeric(start_time)) {
125 if (!$.isNumeric(end_time)) {
130 xaxis.each(function() {
131 devices[$(this).val()] = {};
132 devices[$(this).val()]["url"] = mctx.api + $(this).attr("id");
133 devices[$(this).val()]["data"] = [];
134 devices[$(this).val()]["start_time"] = start_time;
135 devices[$(this).val()]["end_time"] = end_time;
137 yaxis.each(function() {
138 devices[$(this).val()] = {};
139 devices[$(this).val()]["url"] = mctx.api + $(this).attr("id");
140 devices[$(this).val()]["data"] = [];
141 devices[$(this).val()]["start_time"] = start_time;
142 devices[$(this).val()]["end_time"] = end_time;
146 var updater = function () {
149 var ctime = $("#current_time");
152 $.each(devices, function(key, val) {
153 if (devices[key].url === sensor_url || devices[key].url === actuator_url) {
156 //alert(devices[key].url);
157 parameters = {name : key};
158 if (start_time != null) {
159 //alert("start_time = " + start_time);
160 parameters = $.extend(parameters, {start_time : start_time});
162 if (end_time != null)
163 parameters = $.extend(parameters, {end_time : end_time});
164 responses.push($.ajax({url : devices[key].url, data : parameters}).done(function(json) {
165 //alert("Hi from " + json.name);
166 var dev = devices[json.name].data;
167 for (var i = 0; i < json.data.length; ++i) {
168 if (dev.length <= 0 || json.data[i][0] > dev[dev.length-1][0]) {
169 dev.push(json.data[i]);
172 ctime.val(json.running_time);
173 //alert(devices[json.name].data);
178 //... When the response is received, then() will happen (I think?)
179 $.when.apply(this, responses).then(function () {
182 yaxis.each(function() {
183 //alert("Add " + $(this).val() + " to plot");
184 if (xaxis.val() === "time") {
185 //alert("Against time");
186 plot_data.push(devices[$(this).val()].data);
190 dataMerge(devices[xaxis.val()].data, devices[$(this).val()].data, result);
193 for (var i = 0; i < result.length; ++i)
194 astr += "[" + result[i][0] + "," + result[i][1] + "]" + ",";
198 plot_data.push(result);
202 //alert(plot_data + "");
203 //alert("Plot happened");
204 $.plot("#graph", plot_data);
205 mctx.graph.timer = setTimeout(updater, 1000);
206 }, function () {alert("Graph crashed");});
213 $.fn.runButton = function() {
214 //alert($(this).val());
215 if ($(this).val() === "Run") {
216 $("#graph").setGraph();
217 $(this).val("Pause");
220 clearTimeout(mctx.graph.timer);