X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=testing%2FMCTXWeb%2Fpublic_html%2Fstatic%2Fmctx.gui.js;h=773fa2335bf48b79e05749595542dcd22b407a91;hb=df1a9751a2d23cc9d43bf49c069ac9effc2a752f;hp=180c3951818d4a59b5c68ebc37637d30f95e0dc6;hpb=25ecb8cc15ad229de75d7bda20d7d36003544b77;p=matches%2FMCTX3420.git diff --git a/testing/MCTXWeb/public_html/static/mctx.gui.js b/testing/MCTXWeb/public_html/static/mctx.gui.js index 180c395..773fa23 100644 --- a/testing/MCTXWeb/public_html/static/mctx.gui.js +++ b/testing/MCTXWeb/public_html/static/mctx.gui.js @@ -1,14 +1,28 @@ /** * MCTX3420 2013 GUI stuff. + * Coding style: + * - Always end statements with semicolons + * - Egyptian brackets are highly recommended (*cough*). + * - Don't use synchronous stuff - hook events into callbacks + * - $.fn functions should return either themselves or some useful object + * to allow for chaining of method calls */ mctx = {}; -mctx.api = location.protocol + "//" + location.host + "/api/"; +//Don't use this in the final version +mctx.location = window.location.pathname; +mctx.location = mctx.location.substring(0, mctx.location.lastIndexOf('/')) + "/"; +//mctx.location = location.protocol + "//" + location.host + "/"; +mctx.api = location.protocol + "//" + location.host + "/" + "api/"; mctx.expected_api_version = 0; -mctx.key = undefined; mctx.has_control = false; +mctx.debug = true; -mctx.return_codes = { +mctx.statusCodes = { + STATUS_OK : 1 +}; + +mctx.statusCodesDescription = { "1" : "Ok", "-1" : "General error", "-2" : "Unauthorized", @@ -22,7 +36,8 @@ mctx.sensors = { 2 : {name : "Strain gauge 3"}, 3 : {name : "Strain gauge 4"}, 4 : {name : "Pressure sensor 1"}, - 5 : {name : "Pressure sensor 2"} + 5 : {name : "Pressure sensor 2"}, + 6 : {name : "Pressure sensor 3"} }; mctx.actuators = { @@ -36,13 +51,68 @@ mctx.strain_gauges = {}; mctx.strain_gauges.ids = [0, 1, 2, 3]; mctx.strain_gauges.time_limit = 20; +/** + * Logs a message if mctx.debug is enabled. This function takes + * a variable number of arguments and passes them + * to alert or console.log (based on browser support). + * @returns {undefined} + */ +function debugLog () { + if (mctx.debug) { + if (typeof console === "undefined" || typeof console.log === "undefined") { + for (var i = 0; i < arguments.length; i++) { + alert(arguments[i]); + } + } else { + console.log.apply(this, arguments); + } + } +} + /** * Writes the current date to wherever it's called. */ -function getDate(){ +function getDate() { document.write((new Date()).toDateString()); } +/** + * Should be run before the load of any GUI page. + * To hook events to be called after this function runs, + * use the 'always' method, e.g runBeforeLoad().always(function() {my stuff}); + * @param {type} isLoginPage + * @returns The return value of calling $.ajax + */ +function runBeforeLoad(isLoginPage) { + return $.ajax({ + url : mctx.api + "identify" + }).done(function (data) { + if (data.logged_in && isLoginPage) { + if (mctx.debug) { + debugLog("Redirect disabled!"); + } else { + window.location = mctx.location; + } + } else if (!data.logged_in && !isLoginPage) { + if (mctx.debug) { + debugLog("Redirect disabled!"); + } else { + //Note: this only clears the nameless cookie + document.cookie = ""; + window.location = mctx.location + "login.html"; + } + } else { + mctx.friendlyName = data.friendly_name; + } + }).fail(function (jqHXR) { + if (mctx.debug) { + debugLog("Failed to ident server. Is API running?") + } else if (!isLoginPage) { + window.location = mctx.location + "login.html"; + } + }); +} + /** * Populates a submenu of the navigation bar * @param {string} header The header @@ -81,14 +151,15 @@ $.fn.populateNavbar = function () { return {text : items[item].name, href : href}; }; - menu.populateSubmenu("Sensor data dump", mctx.sensors, sensorTranslator); - menu.populateSubmenu("Actuator data dump", mctx.actuators, actuatorTranslator); + menu.populateSubmenu("Sensor data", mctx.sensors, sensorTranslator); + menu.populateSubmenu("Actuator data", mctx.actuators, actuatorTranslator); menu.appendTo(this); return this; } /** * Sets the camera autoupdater + * Obsolete? * @returns {$.fn} */ $.fn.setCamera = function () { @@ -118,6 +189,10 @@ $.fn.setCamera = function () { return this; }; +/** + * Sets the strain graphs to graph stuff. Obsolete? + * @returns {$.fn} + */ $.fn.setStrainGraphs = function () { var sensor_url = mctx.api + "sensors"; var graphdiv = this; @@ -143,43 +218,68 @@ $.fn.setStrainGraphs = function () { } $.plot(graphdiv, data); setTimeout(updater, 500); - }, function () {alert("It crashed");}); + }, function () {debugLog("It crashed");}); }; updater(); return this; }; +/** + * Performs a login attempt. + * @returns The AJAX object of the login request */ $.fn.login = function () { var username = this.find("input[name='username']").val(); var password = this.find("input[name='pass']").val(); - var force = this.find("input[name='force']").is(":checked"); - var url = mctx.api + "control"; - - var authFunc = function(xhr) { - xhr.setRequestHeader("Authorization", - "Basic " + base64.encode(username + ":" + password)); + var out = this.find("#result"); + var redirect = function () { + window.location.href = mctx.location; }; - - $.ajax({ - url : url, - data : {action : "lock", force : (force ? true : undefined)}, - beforeSend : authFunc + + out.removeAttr("class"); + out.text("Logging in..."); + + return $.ajax({ + url : mctx.api + "bind", + data : {user: username, pass : password} }).done(function (data) { - mctx.key = data.key; if (data.status < 0) { - alert("no - " + data.description); + mctx.has_control = false; + out.attr("class", "fail"); + out.text("Login failed: " + data.description); } else { + //todo: error check mctx.has_control = true; - alert("yes - " + mctx.key); + out.attr("class", "pass"); + out.text("Login ok!"); + setTimeout(redirect, 800); } }).fail(function (jqXHR) { - mctx.key = undefined; mctx.has_control = false; - alert("no"); + out.attr("class", "fail"); + out.text("Login request failed - connection issues.") }); }; +/** + * Performs a logout request. The nameless cookie is + * always cleared and the browser redirected to the login page, + * independent of whether or not logout succeeded. + * @returns The AJAX object of the logout request. + */ +$.fn.logout = function () { + return $.ajax({ + url : mctx.api + "unbind" + }).always(function () { + //Note: this only clears the nameless cookie + document.cookie = ""; + window.location = mctx.location + "login.html"; + }); +}; + +/** + * Sets the error log to continuously update. + * @returns itself */ $.fn.setErrorLog = function () { var url = mctx.api + "errorlog"; var outdiv = this; @@ -187,11 +287,22 @@ $.fn.setErrorLog = function () { var updater = function () { $.ajax({url : url}).done(function (data) { outdiv.text(data); - setTimeout(updater, 1000); + outdiv.scrollTop( + outdiv[0].scrollHeight - outdiv.height() + ); + setTimeout(updater, 2000); }).fail(function (jqXHR) { - outdiv.text("Failed to retrieve the error log."); + if (jqXHR.status === 502 || jqXHR.status === 0) { + outdiv.text("Failed to retrieve the error log."); + } + setTimeout(updater, 4000); }); }; updater(); -}; \ No newline at end of file + return this; +}; + +$(document).ajaxError(function (event, jqXHR) { + //console.log("AJAX query failed with: " + jqXHR.status + " (" + jqXHR.statusText + ")"); +}); \ No newline at end of file