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());
63 function runBeforeLoad(isLoginPage) {
65 url : mctx.api + "identify"
66 }).done(function (data) {
68 debugLog("Redirect disabled!");
69 } else if (data.logged_in && isLoginPage) {
70 window.location = mctx.location;
71 } else if (!data.logged_in && !isLoginPage) {
72 //Note: this only clears the nameless cookie
74 window.location = mctx.location + "login.html";
76 mctx.friendlyName = data.friendly_name;
77 $("#content").css("display", "block");
79 }).fail(function (jqHXR) {
81 window.location = mctx.location + "login.html";
83 debugLog("Failed to ident server. Is API running?")
89 * Populates a submenu of the navigation bar
90 * @param {string} header The header
91 * @param {object} items An object representing the submenu items
92 * @param {function} translator A function that translates an object item
93 * into a text and href.
94 * @returns {$.fn} Itself
96 $.fn.populateSubmenu = function(header, items, translator) {
97 var submenuHeader = $("<li/>").append($("<a/>", {text : header, href : "#"}));
98 var submenu = $("<ul/>", {"class" : "submenu"});
100 for (var item in items) {
101 var info = translator(item, items);
102 submenu.append($("<li/>").append(
103 $("<a/>", {text : info.text,
104 href : info.href, target : "_blank"})
108 this.append(submenuHeader.append(submenu));
113 * Populates the navigation bar
115 $.fn.populateNavbar = function () {
116 var menu = $("<ul/>", {"class" : "menu"});
117 var sensorTranslator = function(item, items) {
118 var href = mctx.api + "sensors?start_time=0&format=tsv&id=" + item;
119 return {text : items[item].name, href : href};
121 var actuatorTranslator = function(item, items) {
122 var href = mctx.api + "actuators?start_time=0&format=tsv&id=" + item;
123 return {text : items[item].name, href : href};
126 menu.populateSubmenu("Sensor data", mctx.sensors, sensorTranslator);
127 menu.populateSubmenu("Actuator data", mctx.actuators, actuatorTranslator);
133 * Sets the camera autoupdater
136 $.fn.setCamera = function () {
137 var url = mctx.api + "image"; //http://beaglebone/api/image
140 //Stop updating if we can't retrieve an image!
141 this.error(function() {
147 var updater = function() {
150 parent.attr("src", "");
154 parent.attr("src", url + "#" + (new Date()).getTime());
156 setTimeout(updater, 1000);
163 $.fn.setStrainGraphs = function () {
164 var sensor_url = mctx.api + "sensors";
167 var updater = function () {
168 var time_limit = mctx.strain_gauges.time_limit;
169 var responses = new Array(mctx.strain_gauges.ids.length);
171 for (var i = 0; i < mctx.strain_gauges.ids.length; i++) {
172 var parameters = {id : i, start_time: -time_limit};
173 responses[i] = $.ajax({url : sensor_url, data : parameters});
176 $.when.apply(this, responses).then(function () {
177 var data = new Array(arguments.length);
178 for (var i = 0; i < arguments.length; i++) {
179 var raw_data = arguments[i][0].data;
180 var pruned_data = [];
181 var step = ~~(raw_data.length/100);
182 for (var j = 0; j < raw_data.length; j += step)
183 pruned_data.push(raw_data[j]);
184 data[i] = pruned_data;
186 $.plot(graphdiv, data);
187 setTimeout(updater, 500);
188 }, function () {alert("It crashed");});
195 $.fn.login = function () {
196 var username = this.find("input[name='username']").val();
197 var password = this.find("input[name='pass']").val();
198 var out = this.find("#result");
199 var redirect = function () {
200 window.location.href = mctx.location;
203 out.removeAttr("class");
204 out.text("Logging in...");
207 url : mctx.api + "bind",
208 data : {user: username, pass : password}
209 }).done(function (data) {
210 if (data.status < 0) {
211 mctx.has_control = false;
212 out.attr("class", "fail");
213 out.text("Login failed: " + data.description);
216 mctx.has_control = true;
217 out.attr("class", "pass");
218 out.text("Login ok!");
219 setTimeout(redirect, 800);
221 }).fail(function (jqXHR) {
222 mctx.has_control = false;
223 out.attr("class", "fail");
224 out.text("Login request failed - connection issues.")
228 $.fn.logout = function () {
230 url : mctx.api + "unbind"
231 }).always(function () {
232 //Note: this only clears the nameless cookie
233 document.cookie = "";
234 window.location = mctx.location + "login.html";
238 $.fn.setErrorLog = function () {
239 var url = mctx.api + "errorlog";
242 var updater = function () {
243 $.ajax({url : url}).done(function (data) {
246 outdiv[0].scrollHeight - outdiv.height()
248 setTimeout(updater, 1000);
249 }).fail(function (jqXHR) {
250 if (jqXHR.status === 502 || jqXHR.status === 0) {
251 outdiv.text("Failed to retrieve the error log.");
253 setTimeout(updater, 1500);
260 $(document).ajaxError(function (event, jqXHR) {
261 //console.log("AJAX query failed with: " + jqXHR.status + " (" + jqXHR.statusText + ")");