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";
15 mctx.graph.devices = {};
16 mctx.graph.dependent = null;
17 mctx.graph.independent = null;
18 mctx.graph.timer = null;
19 mctx.graph.running = false;
20 mctx.graph.chart = null;
23 * Helper - Calculate pairs of (dependent, independent) values
24 * Given input as (time, value) pairs for dependent and independent
25 * Appends each value pair to the result
29 * Helper - Calculate pairs of (dependent, independent) values
30 * Given input as (time, value) pairs for dependent and independent
31 * Appends each value pair to the result
32 * @param {array[][]} dependent Dependent data to be correlated with independent
33 * @param {array[][]} independent Independent data
34 * @param {array[][]} result Storage location
35 * @returns {dataMerge.result}
37 function dataMerge(dependent, independent, result) {
39 for (var i = 0; i < dependent.length-1; ++i) {
40 var start = dependent[i][0];
41 var end = dependent[i+1][0];
42 var average = 0, n = 0;
43 for (; j < independent.length; ++j) {
44 if (independent[j][0] < start)
46 else if (independent[j][0] >= end)
48 average += independent[j][1];
53 result.push([dependent[i][1], average]);
60 * Helper function adds the sensors and actuators to a form
61 * @param input_type is it a radio? or is it a checkbox?
62 * @param check_first determines whether the first item is checked or not
63 * @param group which group this input belongs to (name field)
65 $.fn.deployDevices = function(input_type, check_first, group) {
67 var apply = function(dict, prefix) {
68 $.each(dict, function(key, val) {
70 'type' : input_type, 'value' : key, 'alt' : val,
71 'class' : prefix, 'name' : group,
72 'id' : prefix + '_' + val //Unique id (name mangling)
75 var entry = $("<input/>", attributes);
76 var label = $("<label/>", {'for' : prefix + '_' + val, 'text' : val});
77 entry.prop("checked", check_first);
79 container.append(entry).append(label);
83 apply(mctx.sensors, 'sensors');
84 apply(mctx.actuators, 'actuators');
88 * Identify sensors/actuators
89 * @returns itself (Is this right?)
91 $.fn.setDevices = function() {
92 // Query for sensors and actuators
94 url : mctx.api + 'identify',
95 data : {'sensors' : 1, 'actuators' : 1}
96 }).done(function (data) {
97 mctx.sensors = $.extend(mctx.sensors, data.sensors);
98 mctx.actuators = $.extend(mctx.actuators, data.actuators);
100 //Always set the 'time' option to be checked
101 $("#xaxis input").prop('checked', true);
102 $("#xaxis").deployDevices("radio", false, 'xaxis');
103 $("#yaxis").deployDevices("checkbox", true, 'yaxis');
104 $("#current_time").val(data.running_time);
105 //Add event listeners for when the
106 $(".change input").change(function () {
107 $("#graph").setGraph();
112 function setGraphStatus(on, failText, keep) {
114 mctx.graph.running = true;
115 $("#status-text").html(" ");
116 $("#graph-run").prop("value", "Pause");
118 mctx.graph.running = false;
120 $("#status-text").text(failText).addClass("fail");
122 $("#status-text").text("Graph stopped").removeClass("fail");
124 $("#graph-run").prop("value", "Run");
128 function graphUpdater() {
130 'sensors' : mctx.graph.api.sensors,
131 'actuators' : mctx.graph.api.actuators
134 var updater = function () {
136 var ctime = $("#current_time");
138 var xaxis = mctx.graph.xaxis;
139 var yaxis = mctx.graph.yaxis;
140 var start_time = mctx.graph.start_time;
141 var end_time = mctx.graph.end_time;
142 var devices = mctx.graph.devices;
144 if (xaxis.size() < 1 || yaxis.size() < 1) {
145 setGraphStatus(false, "No x or y axis selected.");
149 $.each(devices, function(key, val) {
150 if (val.urltype in urls) {
151 var parameters = {id : val.id};
152 if (start_time !== null) {
153 parameters.start_time = start_time;
155 if (end_time !== null) {
156 parameters.end_time = end_time;
158 responses.push($.ajax({url : urls[val.urltype], data : parameters})
159 .done(function(json) {
160 //alert("Hi from " + json.name);
161 if (!$("#status-text").checkStatus(json)) {
162 setGraphStatus(false, null, true); //Don't reset text, checkstatus just set it.
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 () {
180 if (mctx.graph.running) {
183 yaxis.each(function() {
185 series.label = $(this).attr("alt");
187 //alert("Add " + $(this).val() + " to plot");
188 if (xaxis.attr("alt") === "time") {
189 //alert("Against time");
190 series.data = devices[$(this).attr("alt")].data;
193 dataMerge(devices[xaxis.attr("alt")].data,
194 devices[$(this).attr("alt")].data, result);
195 series.data = result;
197 plot_data.push(series);
200 if (mctx.graph.chart !== null) {
201 mctx.graph.chart.setData(plot_data);
202 mctx.graph.chart.setupGrid();
203 mctx.graph.chart.draw();
207 container : "#graph-legend"
210 mctx.graph.chart = $.plot("#graph", plot_data, options);
212 mctx.graph.timer = setTimeout(updater, 1000);
215 setGraphStatus("Connection issue - graph stopped.");
216 //This will always happen when a user closes the page
217 //alert("Graph crashed");
221 setGraphStatus(true);
227 * Sets the graphs to graph stuff.
230 $.fn.setGraph = function () {
231 // Determine which actuator/sensors to plot
232 var xaxis = $("#xaxis input[name=xaxis]:checked");
233 var yaxis = $("#yaxis input[name=yaxis]:checked");
234 if (xaxis.size() < 1 || yaxis.size() < 1) {
236 setGraphStatus(false, "No x or y axis selected.");
240 var start_time = $("#start_time").val();
241 var end_time = $("#end_time").val();
242 if (!$.isNumeric(start_time)) {
245 if (!$.isNumeric(end_time)) {
250 var populateDict = function () {
252 dict['urltype'] = $(this).attr("class");
253 dict['id'] = $(this).attr("value");
255 dict['start_time'] = start_time;
256 dict['end_time'] = end_time;
257 devices[$(this).attr("alt")] = dict;
259 xaxis.each(populateDict);
260 yaxis.each(populateDict);
262 mctx.graph.xaxis = xaxis;
263 mctx.graph.yaxis = yaxis;
264 mctx.graph.start_time = start_time;
265 mctx.graph.end_time = end_time;
266 mctx.graph.devices = devices;
268 if (!mctx.graph.running) {
269 $("#graph-run").val("Pause");
270 $("#status-text").text("")
277 $.fn.runButton = function() {
278 if (mctx.graph.running) {
279 setGraphStatus(false);
280 clearTimeout(mctx.graph.timer);
282 $("#graph").setGraph();