2 * MCTX3420 2013 GUI stuff.
6 //Don't use this in the final version
7 mctx.location = window.location.pathname;
8 mctx.location = mctx.location.substring(0, mctx.location.lastIndexOf('/')) + "/";
9 //mctx.location = location.protocol + "//" + location.host + "/";
10 mctx.api = location.protocol + "//" + location.host + "/" + "api/";
11 mctx.expected_api_version = 0;
12 mctx.has_control = false;
19 mctx.statusCodesDescription = {
21 "-1" : "General error",
22 "-2" : "Unauthorized",
24 "-4" : "Already exists"
28 0 : {name : "Strain gauge 1"},
29 1 : {name : "Strain gauge 2"},
30 2 : {name : "Strain gauge 3"},
31 3 : {name : "Strain gauge 4"},
32 4 : {name : "Pressure sensor 1"},
33 5 : {name : "Pressure sensor 2"},
34 6 : {name : "Pressure sensor 3"}
38 0 : {name : "Solenoid 1"},
39 1 : {name : "Solenoid 2"},
40 2 : {name : "Solenoid 3"},
41 3 : {name : "Pressure regulator"}
44 mctx.strain_gauges = {};
45 mctx.strain_gauges.ids = [0, 1, 2, 3];
46 mctx.strain_gauges.time_limit = 20;
48 function debugLog (msg) {
49 if (typeof console === "undefined" || typeof console.log === "undefined") {
57 * Writes the current date to wherever it's called.
60 document.write((new Date()).toDateString());
64 * Should be run before the load of any GUI page.
65 * To hook events to be called after this function runs,
66 * use the 'always' method, e.g runBeforeLoad().always(function() {my stuff});
67 * @param {type} isLoginPage
68 * @returns The return value of calling $.ajax
70 function runBeforeLoad(isLoginPage) {
72 url : mctx.api + "identify"
73 }).done(function (data) {
74 if (data.logged_in && isLoginPage) {
76 debugLog("Redirect disabled!");
78 window.location = mctx.location;
80 } else if (!data.logged_in && !isLoginPage) {
82 debugLog("Redirect disabled!");
84 //Note: this only clears the nameless cookie
86 window.location = mctx.location + "login.html";
89 mctx.friendlyName = data.friendly_name;
91 }).fail(function (jqHXR) {
93 debugLog("Failed to ident server. Is API running?")
94 } else if (!isLoginPage) {
95 window.location = mctx.location + "login.html";
101 * Populates a submenu of the navigation bar
102 * @param {string} header The header
103 * @param {object} items An object representing the submenu items
104 * @param {function} translator A function that translates an object item
105 * into a text and href.
106 * @returns {$.fn} Itself
108 $.fn.populateSubmenu = function(header, items, translator) {
109 var submenuHeader = $("<li/>").append($("<a/>", {text : header, href : "#"}));
110 var submenu = $("<ul/>", {"class" : "submenu"});
112 for (var item in items) {
113 var info = translator(item, items);
114 submenu.append($("<li/>").append(
115 $("<a/>", {text : info.text,
116 href : info.href, target : "_blank"})
120 this.append(submenuHeader.append(submenu));
125 * Populates the navigation bar
127 $.fn.populateNavbar = function () {
128 var menu = $("<ul/>", {"class" : "menu"});
129 var sensorTranslator = function(item, items) {
130 var href = mctx.api + "sensors?start_time=0&format=tsv&id=" + item;
131 return {text : items[item].name, href : href};
133 var actuatorTranslator = function(item, items) {
134 var href = mctx.api + "actuators?start_time=0&format=tsv&id=" + item;
135 return {text : items[item].name, href : href};
138 menu.populateSubmenu("Sensor data", mctx.sensors, sensorTranslator);
139 menu.populateSubmenu("Actuator data", mctx.actuators, actuatorTranslator);
145 * Sets the camera autoupdater
148 $.fn.setCamera = function () {
149 var url = mctx.api + "image"; //http://beaglebone/api/image
152 //Stop updating if we can't retrieve an image!
153 this.error(function() {
159 var updater = function() {
162 parent.attr("src", "");
166 parent.attr("src", url + "#" + (new Date()).getTime());
168 setTimeout(updater, 1000);
175 $.fn.setStrainGraphs = function () {
176 var sensor_url = mctx.api + "sensors";
179 var updater = function () {
180 var time_limit = mctx.strain_gauges.time_limit;
181 var responses = new Array(mctx.strain_gauges.ids.length);
183 for (var i = 0; i < mctx.strain_gauges.ids.length; i++) {
184 var parameters = {id : i, start_time: -time_limit};
185 responses[i] = $.ajax({url : sensor_url, data : parameters});
188 $.when.apply(this, responses).then(function () {
189 var data = new Array(arguments.length);
190 for (var i = 0; i < arguments.length; i++) {
191 var raw_data = arguments[i][0].data;
192 var pruned_data = [];
193 var step = ~~(raw_data.length/100);
194 for (var j = 0; j < raw_data.length; j += step)
195 pruned_data.push(raw_data[j]);
196 data[i] = pruned_data;
198 $.plot(graphdiv, data);
199 setTimeout(updater, 500);
200 }, function () {alert("It crashed");});
207 $.fn.login = function () {
208 var username = this.find("input[name='username']").val();
209 var password = this.find("input[name='pass']").val();
210 var out = this.find("#result");
211 var redirect = function () {
212 window.location.href = mctx.location;
215 out.removeAttr("class");
216 out.text("Logging in...");
219 url : mctx.api + "bind",
220 data : {user: username, pass : password}
221 }).done(function (data) {
222 if (data.status < 0) {
223 mctx.has_control = false;
224 out.attr("class", "fail");
225 out.text("Login failed: " + data.description);
228 mctx.has_control = true;
229 out.attr("class", "pass");
230 out.text("Login ok!");
231 setTimeout(redirect, 800);
233 }).fail(function (jqXHR) {
234 mctx.has_control = false;
235 out.attr("class", "fail");
236 out.text("Login request failed - connection issues.")
240 $.fn.logout = function () {
242 url : mctx.api + "unbind"
243 }).always(function () {
244 //Note: this only clears the nameless cookie
245 document.cookie = "";
246 window.location = mctx.location + "login.html";
250 $.fn.setErrorLog = function () {
251 var url = mctx.api + "errorlog";
254 var updater = function () {
255 $.ajax({url : url}).done(function (data) {
258 outdiv[0].scrollHeight - outdiv.height()
260 setTimeout(updater, 1000);
261 }).fail(function (jqXHR) {
262 if (jqXHR.status === 502 || jqXHR.status === 0) {
263 outdiv.text("Failed to retrieve the error log.");
265 setTimeout(updater, 1500);
272 $(document).ajaxError(function (event, jqXHR) {
273 //console.log("AJAX query failed with: " + jqXHR.status + " (" + jqXHR.statusText + ")");