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) {
26 for (var i = 0; i < dependent.length-1; ++i) {
27 var start = dependent[i][0];
28 var end = dependent[i+1][0];
29 var average = 0, n = 0;
30 for (; j < independent.length; ++j) {
31 if (independent[j][0] < start)
33 else if (independent[j][0] >= end)
35 average += independent[j][1];
40 result.push([dependent[i][1], average]);
47 * Helper function adds the sensors and actuators to a form
48 * @param input_type is it a radio? or is it a checkbox?
49 * @param check_first determines whether the first item is checked or not
50 * @param group which group this input belongs to (name field)
52 $.fn.deployDevices = function(input_type, check_first, group) {
54 var apply = function(dict, prefix) {
55 $.each(dict, function(key, val) {
57 'type' : input_type, 'value' : key, 'alt' : val,
58 'class' : prefix, 'name' : group,
59 'id' : prefix + '_' + val //Unique id (name mangling)
61 var entry = $("<input/>", attributes);
62 var label = $("<label/>", {'for' : prefix + '_' + val, 'text' : val});
63 entry.prop("checked", check_first);
65 container.append(entry).append(label);
69 apply(mctx.sensors, 'sensors');
70 apply(mctx.actuators, 'actuators');
74 * Identify sensors/actuators
75 * @returns itself (Is this right?)
77 $.fn.setDevices = function() {
78 // Query for sensors and actuators
80 url : mctx.api + 'identify',
81 data : {'sensors' : 1, 'actuators' : 1}
82 }).done(function (data) {
83 mctx.sensors = $.extend(mctx.sensors, data.sensors);
84 mctx.actuators = $.extend(mctx.actuators, data.actuators);
86 $("#xaxis").deployDevices("radio", false, 'xaxis');
87 $("#yaxis").deployDevices("checkbox", true, 'yaxis');
88 $("#current_time").val(data.running_time);
94 * Sets the graphs to graph stuff.
97 $.fn.setGraph = function () {
98 clearTimeout(mctx.graph.timer);
100 'sensors' : mctx.graph.api.sensors,
101 'actuators' : mctx.graph.api.actuators
104 var updateData = function(json, data) {
105 for (var i = 0; i < json.data.length; ++i)
106 data.push(json.data[i]);
111 // Determine which actuator/sensors to plot
113 var xaxis = $("#xaxis input:checked");
114 var yaxis = $("#yaxis input:checked");
115 var start_time = $("#start_time").val();
116 var end_time = $("#end_time").val();
117 if (!$.isNumeric(start_time)) {
120 if (!$.isNumeric(end_time)) {
125 var populateDict = function () {
127 dict['urltype'] = $(this).attr("class");
128 dict['id'] = $(this).attr("value");
130 dict['start_time'] = start_time;
131 dict['end_time'] = end_time;
132 devices[$(this).attr("alt")] = dict;
134 xaxis.each(populateDict);
135 yaxis.each(populateDict);
138 yaxis.each(function() {
140 devices[$(this).attr("alt")] = {};
141 devices[$(this).attr("alt")]["url"] = mctx.api + $(this).attr("class");
142 devices[$(this).attr("alt")]["data"] = [];
143 devices[$(this).attr("alt")]["start_time"] = start_time;
144 devices[$(this).attr("alt")]["end_time"] = end_time;
148 var updater = function () {
151 var ctime = $("#current_time");
153 $.each(devices, function(key, val) {
155 if (val.urltype in urls) {
156 var parameters = {id : val.id};
157 if (start_time != null) {
158 parameters.start_time = start_time;
160 if (end_time != null) {
161 parameters.end_time = end_time;
163 responses.push($.ajax({url : urls[val.urltype], data : parameters})
164 .done(function(json) {
165 //alert("Hi from " + json.name);
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?) yup
179 $.when.apply(this, responses).then(function () {
181 yaxis.each(function() {
182 //alert("Add " + $(this).val() + " to plot");
183 if (xaxis.attr("alt") === "time") {
184 //alert("Against time");
185 plot_data.push(devices[$(this).attr("alt")].data);
189 dataMerge(devices[xaxis.attr("alt")].data, devices[$(this).attr("alt")].data, result);
192 for (var i = 0; i < result.length; ++i)
193 astr += "[" + result[i][0] + "," + result[i][1] + "]" + ",";
197 plot_data.push(result);
201 //alert(plot_data + "");
202 //alert("Plot happened");
203 $.plot("#graph", plot_data);
204 mctx.graph.timer = setTimeout(updater, 1000);
205 }, function () {alert("Graph crashed");});
212 $.fn.runButton = function() {
213 //alert($(this).val());
214 if ($(this).val() === "Run") {
215 $("#graph").setGraph();
216 $(this).val("Pause");
219 clearTimeout(mctx.graph.timer);