2 * MCTX3420 2013 GUI stuff.
6 mctx.location = location.protocol + "//" + location.host + "/";
7 mctx.api = mctx.location + "/api/";
8 mctx.expected_api_version = 0;
9 mctx.has_control = false;
13 "-1" : "General error",
14 "-2" : "Unauthorized",
16 "-4" : "Already exists"
20 0 : {name : "Strain gauge 1"},
21 1 : {name : "Strain gauge 2"},
22 2 : {name : "Strain gauge 3"},
23 3 : {name : "Strain gauge 4"},
24 4 : {name : "Pressure sensor 1"},
25 5 : {name : "Pressure sensor 2"},
26 6 : {name : "Pressure sensor 3"}
30 0 : {name : "Solenoid 1"},
31 1 : {name : "Solenoid 2"},
32 2 : {name : "Solenoid 3"},
33 3 : {name : "Pressure regulator"}
36 mctx.strain_gauges = {};
37 mctx.strain_gauges.ids = [0, 1, 2, 3];
38 mctx.strain_gauges.time_limit = 20;
40 function debugLog (msg) {
41 if (typeof console === "undefined" || typeof console.log === "undefined") {
49 * Writes the current date to wherever it's called.
52 document.write((new Date()).toDateString());
55 function runBeforeLoad(isLoginPage) {
57 url : mctx.api + "identify"
58 }).done(function (data) {
59 if (data.logged_in && isLoginPage) {
60 window.location = mctx.location;
61 } else if (!data.logged_in && !isLoginPage) {
62 //Note: this only clears the nameless cookie
64 window.location = mctx.location + "login.html";
66 mctx.friendlyName = data.friendly_name;
68 }).fail(function (jqHXR) {
70 window.location = mctx.location + "login.html";
72 debugLog("Failed to ident server. Is API running?")
78 * Populates a submenu of the navigation bar
79 * @param {string} header The header
80 * @param {object} items An object representing the submenu items
81 * @param {function} translator A function that translates an object item
82 * into a text and href.
83 * @returns {$.fn} Itself
85 $.fn.populateSubmenu = function(header, items, translator) {
86 var submenuHeader = $("<li/>").append($("<a/>", {text : header, href : "#"}));
87 var submenu = $("<ul/>", {"class" : "submenu"});
89 for (var item in items) {
90 var info = translator(item, items);
91 submenu.append($("<li/>").append(
92 $("<a/>", {text : info.text,
93 href : info.href, target : "_blank"})
97 this.append(submenuHeader.append(submenu));
102 * Populates the navigation bar
104 $.fn.populateNavbar = function () {
105 var menu = $("<ul/>", {"class" : "menu"});
106 var sensorTranslator = function(item, items) {
107 var href = mctx.api + "sensors?start_time=0&format=tsv&id=" + item;
108 return {text : items[item].name, href : href};
110 var actuatorTranslator = function(item, items) {
111 var href = mctx.api + "actuators?start_time=0&format=tsv&id=" + item;
112 return {text : items[item].name, href : href};
115 menu.populateSubmenu("Sensor data", mctx.sensors, sensorTranslator);
116 menu.populateSubmenu("Actuator data", mctx.actuators, actuatorTranslator);
122 * Sets the camera autoupdater
125 $.fn.setCamera = function () {
126 var url = mctx.api + "image"; //http://beaglebone/api/image
129 //Stop updating if we can't retrieve an image!
130 this.error(function() {
136 var updater = function() {
139 parent.attr("src", "");
143 parent.attr("src", url + "#" + (new Date()).getTime());
145 setTimeout(updater, 1000);
152 $.fn.setStrainGraphs = function () {
153 var sensor_url = mctx.api + "sensors";
156 var updater = function () {
157 var time_limit = mctx.strain_gauges.time_limit;
158 var responses = new Array(mctx.strain_gauges.ids.length);
160 for (var i = 0; i < mctx.strain_gauges.ids.length; i++) {
161 var parameters = {id : i, start_time: -time_limit};
162 responses[i] = $.ajax({url : sensor_url, data : parameters});
165 $.when.apply(this, responses).then(function () {
166 var data = new Array(arguments.length);
167 for (var i = 0; i < arguments.length; i++) {
168 var raw_data = arguments[i][0].data;
169 var pruned_data = [];
170 var step = ~~(raw_data.length/100);
171 for (var j = 0; j < raw_data.length; j += step)
172 pruned_data.push(raw_data[j]);
173 data[i] = pruned_data;
175 $.plot(graphdiv, data);
176 setTimeout(updater, 500);
177 }, function () {alert("It crashed");});
184 $.fn.login = function () {
185 var username = this.find("input[name='username']").val();
186 var password = this.find("input[name='pass']").val();
187 var out = this.find("#result");
188 var redirect = function () {
189 window.location.href = mctx.location;
192 out.removeAttr("class");
193 out.text("Logging in...");
196 url : mctx.api + "bind",
197 data : {user: username, pass : password}
198 }).done(function (data) {
200 mctx.has_control = true;
201 out.attr("class", "pass");
202 out.text("Login ok!");
203 setTimeout(redirect, 1000);
204 }).fail(function (jqXHR) {
205 mctx.has_control = false;
206 out.attr("class", "fail");
207 out.text("Login request failed - connection issues.")
211 $.fn.logout = function () {
213 url : mctx.api + "unbind"
214 }).always(function () {
215 //Note: this only clears the nameless cookie
216 document.cookie = "";
217 window.location = mctx.location + "login.html";
221 $.fn.setErrorLog = function () {
222 var url = mctx.api + "errorlog";
225 var updater = function () {
226 $.ajax({url : url}).done(function (data) {
229 outdiv[0].scrollHeight - outdiv.height()
231 setTimeout(updater, 1000);
232 }).fail(function (jqXHR) {
233 if (jqXHR.status === 502 || jqXHR.status === 0) {
234 outdiv.text("Failed to retrieve the error log.");
236 setTimeout(updater, 1500);
243 $(document).ajaxError(function (event, jqXHR) {
244 //console.log("AJAX query failed with: " + jqXHR.status + " (" + jqXHR.statusText + ")");