From 8c9214a60db100a206496d1c417c08a20ea2693e Mon Sep 17 00:00:00 2001 From: Jeremy Tan Date: Sat, 19 Oct 2013 21:19:00 +0800 Subject: [PATCH] Fix another graph bug + some more work on control page --- testing/MCTXWeb/public_html/control.html | 9 +- testing/MCTXWeb/public_html/static/base64.js | 178 ------------------ .../public_html/static/mctx.control.js | 60 +++++- .../MCTXWeb/public_html/static/mctx.graph.js | 55 +++--- .../MCTXWeb/public_html/static/mctx.gui.js | 16 +- testing/MCTXWeb/public_html/static/style.css | 13 +- 6 files changed, 118 insertions(+), 213 deletions(-) delete mode 100644 testing/MCTXWeb/public_html/static/base64.js diff --git a/testing/MCTXWeb/public_html/control.html b/testing/MCTXWeb/public_html/control.html index 61157b6..fdd2649 100644 --- a/testing/MCTXWeb/public_html/control.html +++ b/testing/MCTXWeb/public_html/control.html @@ -16,8 +16,13 @@ @@ -85,7 +90,7 @@ - + diff --git a/testing/MCTXWeb/public_html/static/base64.js b/testing/MCTXWeb/public_html/static/base64.js deleted file mode 100644 index 72bced4..0000000 --- a/testing/MCTXWeb/public_html/static/base64.js +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Copyright (c) 2010 Nick Galbreath - * http://code.google.com/p/stringencoders/source/browse/#svn/trunk/javascript - * - * Permission is hereby granted, free of charge, to any person - * obtaining a copy of this software and associated documentation - * files (the "Software"), to deal in the Software without - * restriction, including without limitation the rights to use, - * copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following - * conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES - * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT - * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - */ - -/* base64 encode/decode compatible with window.btoa/atob - * - * window.atob/btoa is a Firefox extension to convert binary data (the "b") - * to base64 (ascii, the "a"). - * - * It is also found in Safari and Chrome. It is not available in IE. - * - * if (!window.btoa) window.btoa = base64.encode - * if (!window.atob) window.atob = base64.decode - * - * The original spec's for atob/btoa are a bit lacking - * https://developer.mozilla.org/en/DOM/window.atob - * https://developer.mozilla.org/en/DOM/window.btoa - * - * window.btoa and base64.encode takes a string where charCodeAt is [0,255] - * If any character is not [0,255], then an DOMException(5) is thrown. - * - * window.atob and base64.decode take a base64-encoded string - * If the input length is not a multiple of 4, or contains invalid characters - * then an DOMException(5) is thrown. - */ -var base64 = {}; -base64.PADCHAR = '='; -base64.ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; - -base64.makeDOMException = function() { - // sadly in FF,Safari,Chrome you can't make a DOMException - var e, tmp; - - try { - return new DOMException(DOMException.INVALID_CHARACTER_ERR); - } catch (tmp) { - // not available, just passback a duck-typed equiv - // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Error - // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Error/prototype - var ex = new Error("DOM Exception 5"); - - // ex.number and ex.description is IE-specific. - ex.code = ex.number = 5; - ex.name = ex.description = "INVALID_CHARACTER_ERR"; - - // Safari/Chrome output format - ex.toString = function() { return 'Error: ' + ex.name + ': ' + ex.message; }; - return ex; - } -} - -base64.getbyte64 = function(s,i) { - // This is oddly fast, except on Chrome/V8. - // Minimal or no improvement in performance by using a - // object with properties mapping chars to value (eg. 'A': 0) - var idx = base64.ALPHA.indexOf(s.charAt(i)); - if (idx === -1) { - throw base64.makeDOMException(); - } - return idx; -} - -base64.decode = function(s) { - // convert to string - s = '' + s; - var getbyte64 = base64.getbyte64; - var pads, i, b10; - var imax = s.length - if (imax === 0) { - return s; - } - - if (imax % 4 !== 0) { - throw base64.makeDOMException(); - } - - pads = 0 - if (s.charAt(imax - 1) === base64.PADCHAR) { - pads = 1; - if (s.charAt(imax - 2) === base64.PADCHAR) { - pads = 2; - } - // either way, we want to ignore this last block - imax -= 4; - } - - var x = []; - for (i = 0; i < imax; i += 4) { - b10 = (getbyte64(s,i) << 18) | (getbyte64(s,i+1) << 12) | - (getbyte64(s,i+2) << 6) | getbyte64(s,i+3); - x.push(String.fromCharCode(b10 >> 16, (b10 >> 8) & 0xff, b10 & 0xff)); - } - - switch (pads) { - case 1: - b10 = (getbyte64(s,i) << 18) | (getbyte64(s,i+1) << 12) | (getbyte64(s,i+2) << 6); - x.push(String.fromCharCode(b10 >> 16, (b10 >> 8) & 0xff)); - break; - case 2: - b10 = (getbyte64(s,i) << 18) | (getbyte64(s,i+1) << 12); - x.push(String.fromCharCode(b10 >> 16)); - break; - } - return x.join(''); -} - -base64.getbyte = function(s,i) { - var x = s.charCodeAt(i); - if (x > 255) { - throw base64.makeDOMException(); - } - return x; -} - -base64.encode = function(s) { - if (arguments.length !== 1) { - throw new SyntaxError("Not enough arguments"); - } - var padchar = base64.PADCHAR; - var alpha = base64.ALPHA; - var getbyte = base64.getbyte; - - var i, b10; - var x = []; - - // convert to string - s = '' + s; - - var imax = s.length - s.length % 3; - - if (s.length === 0) { - return s; - } - for (i = 0; i < imax; i += 3) { - b10 = (getbyte(s,i) << 16) | (getbyte(s,i+1) << 8) | getbyte(s,i+2); - x.push(alpha.charAt(b10 >> 18)); - x.push(alpha.charAt((b10 >> 12) & 0x3F)); - x.push(alpha.charAt((b10 >> 6) & 0x3f)); - x.push(alpha.charAt(b10 & 0x3f)); - } - switch (s.length - imax) { - case 1: - b10 = getbyte(s,i) << 16; - x.push(alpha.charAt(b10 >> 18) + alpha.charAt((b10 >> 12) & 0x3F) + - padchar + padchar); - break; - case 2: - b10 = (getbyte(s,i) << 16) | (getbyte(s,i+1) << 8); - x.push(alpha.charAt(b10 >> 18) + alpha.charAt((b10 >> 12) & 0x3F) + - alpha.charAt((b10 >> 6) & 0x3f) + padchar); - break; - } - return x.join(''); -} - - diff --git a/testing/MCTXWeb/public_html/static/mctx.control.js b/testing/MCTXWeb/public_html/static/mctx.control.js index cf67629..63932b7 100644 --- a/testing/MCTXWeb/public_html/static/mctx.control.js +++ b/testing/MCTXWeb/public_html/static/mctx.control.js @@ -5,13 +5,65 @@ mctx.control = {}; mctx.control.api = mctx.api + 'control' +mctx.control.states = { + start : 0, + pause : 1, + resume : 2, + stop : 3, + emergency : 4 +}; -$(document).ready(function () { +$.fn.initialiseControls = function () { + var result = this; + $.ajax({ url : mctx.control.api, data : {'action' : 'identify'} - }).done(function () { + }).done(function (data) { + if (!result.checkStatus(data)) { + $(result).parent().addClass("fail"); + return; + } + var text; + var running = false; + var fail = false; + switch (data.control_state_id) { + case mctx.control.states.start: + text = "Experiment started - '" + data.control_experiment_name + + "' by " + data.control_user_name; + running = true; + break; + case mctx.control.states.pause: + text = "Experiment paused - '" + data.control_experiment_name + + "' by " + data.control_user_name; + running = true; + break; + case mctx.control.states.stop: + text = "No experiment running."; + break; + case mctx.control.states.emergency: + text = "Emergency mode - '" + data.control_experiment_name + + "' by " + data.control_user_name; + running = true; + fail = true; + default: + text = "Unknown mode: " + data.control_state_id; + fail = true; + } + + if (running) { + $("#experiment-stop").show(); + $("#pressure-widget").show(); + } else { + $("#start-widget").show(); + } + + $(result).text(text); + if (fail) { + $(result).parent().addClass("fail"); + } else { + $(result).parent().addClass("pass"); + } }); -}); - +}; diff --git a/testing/MCTXWeb/public_html/static/mctx.graph.js b/testing/MCTXWeb/public_html/static/mctx.graph.js index 4bb3e06..57c8841 100644 --- a/testing/MCTXWeb/public_html/static/mctx.graph.js +++ b/testing/MCTXWeb/public_html/static/mctx.graph.js @@ -106,19 +106,19 @@ $.fn.setDevices = function() { }); }; -function setGraphStatus(on, failText) { +function setGraphStatus(on, failText, keep) { if (on) { mctx.graph.running = true; $("#status-text").html(" "); - $("#graph-run").text("Pause"); + $("#graph-run").prop("value", "Pause"); } else { mctx.graph.running = false; if (failText) { $("#status-text").text(failText).addClass("fail"); - } else { + } else if (!keep) { $("#status-text").text("Graph stopped").removeClass("fail"); } - $("#graph-run").text("Run"); + $("#graph-run").prop("value", "Run"); } } @@ -155,6 +155,11 @@ function graphUpdater() { responses.push($.ajax({url : urls[val.urltype], data : parameters}) .done(function(json) { //alert("Hi from " + json.name); + if (!$("#status-text").checkStatus(json)) { + setGraphStatus(false, null, true); + return; + } + var dev = val.data; for (var i = 0; i < json.data.length; ++i) { if (dev.length <= 0 || json.data[i][0] > dev[dev.length-1][0]) { @@ -169,29 +174,29 @@ function graphUpdater() { //... When the response is received, then() will happen (I think?) $.when.apply(this, responses).then(function () { - var plot_data = []; - yaxis.each(function() { - //alert("Add " + $(this).val() + " to plot"); - if (xaxis.attr("alt") === "time") { - //alert("Against time"); - plot_data.push(devices[$(this).attr("alt")].data); + if (mctx.graph.running) { + var plot_data = []; + + yaxis.each(function() { + //alert("Add " + $(this).val() + " to plot"); + if (xaxis.attr("alt") === "time") { + //alert("Against time"); + plot_data.push(devices[$(this).attr("alt")].data); + } else { + var result = [] + dataMerge(devices[xaxis.attr("alt")].data, + devices[$(this).attr("alt")].data, result); + plot_data.push(result); + } + }); + + if (mctx.graph.chart !== null) { + mctx.graph.chart.setData(plot_data); + mctx.graph.chart.setupGrid(); + mctx.graph.chart.draw(); } else { - var result = [] - dataMerge(devices[xaxis.attr("alt")].data, - devices[$(this).attr("alt")].data, result); - plot_data.push(result); + mctx.graph.chart = $.plot("#graph", plot_data); } - }); - - //$.plot("#graph", plot_data); - if (mctx.graph.chart !== null) { - mctx.graph.chart.setData(plot_data); - mctx.graph.chart.setupGrid(); - mctx.graph.chart.draw(); - } else { - mctx.graph.chart = $.plot("#graph", plot_data); - } - if (mctx.graph.running) { mctx.graph.timer = setTimeout(updater, 1000); } }, function () { diff --git a/testing/MCTXWeb/public_html/static/mctx.gui.js b/testing/MCTXWeb/public_html/static/mctx.gui.js index 3198055..3130a18 100644 --- a/testing/MCTXWeb/public_html/static/mctx.gui.js +++ b/testing/MCTXWeb/public_html/static/mctx.gui.js @@ -25,8 +25,12 @@ mctx.menu = [ {'text' : 'Help', href : mctx.location + 'help.html'} ]; -mctx.statusCodes = { - STATUS_OK : 1 +mctx.status = { + OK : 1, + ERROR : -1, + UNAUTHORIZED : -2, + NOTRUNNING : -3, + ALREADYEXISTS : -4 }; mctx.statusCodesDescription = { @@ -296,6 +300,14 @@ $.fn.setErrorLog = function () { return this; }; +$.fn.checkStatus = function(data) { + if (data.status !== mctx.status.OK) { + $(this).text(data.description).addClass("fail"); + return false; + } + return true; +}; + $(document).ready(function () { //Show the content! $("#content").css("display", "block"); diff --git a/testing/MCTXWeb/public_html/static/style.css b/testing/MCTXWeb/public_html/static/style.css index a3f0c09..f282aba 100644 --- a/testing/MCTXWeb/public_html/static/style.css +++ b/testing/MCTXWeb/public_html/static/style.css @@ -118,10 +118,10 @@ tr.pass { font-weight: inherit; } -tr.fail { +tr.fail, tr.fail .fail { color: #000; background-color: #E30909; - font-weight: bold + font-weight: inherit; } table.centre { @@ -434,4 +434,13 @@ form.controls { #experiment-stop { padding: 0.2em 0.4em; + display: none; +} + +#start-widget { + display: none; +} + +#pressure-widget { + display: none; } \ No newline at end of file -- 2.20.1
Experiment statesdgfsdsses