2 * MCTX3420 2013 GUI stuff.
6 mctx.api = location.protocol + "//" + location.host + "/api/";
7 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"}
29 0 : {name : "Solenoid 1"},
30 1 : {name : "Solenoid 2"},
31 2 : {name : "Solenoid 3"},
32 3 : {name : "Pressure regulator"}
35 mctx.strain_gauges = {};
36 mctx.strain_gauges.ids = [0, 1, 2, 3];
37 mctx.strain_gauges.time_limit = 20;
40 * Writes the current date to wherever it's called.
43 document.write((new Date()).toDateString());
47 * Populates a submenu of the navigation bar
48 * @param {string} header The header
49 * @param {object} items An object representing the submenu items
50 * @param {function} translator A function that translates an object item
51 * into a text and href.
52 * @returns {$.fn} Itself
54 $.fn.populateSubmenu = function(header, items, translator) {
55 var submenuHeader = $("<li/>").append($("<a/>", {text : header, href : "#"}));
56 var submenu = $("<ul/>", {"class" : "submenu"});
59 var info = translator(item, items);
60 submenu.append($("<li/>").append(
61 $("<a/>", {text : info.text,
62 href : info.href, target : "_blank"})
66 this.append(submenuHeader.append(submenu));
71 * Populates the navigation bar
73 $.fn.populateNavbar = function () {
74 var menu = $("<ul/>", {"class" : "menu"});
75 var sensorTranslator = function(item, items) {
76 var href = mctx.api + "sensors?start_time=0&format=tsv&id=" + item;
77 return {text : items[item].name, href : href};
79 var actuatorTranslator = function(item, items) {
80 var href = mctx.api + "actuators?start_time=0&format=tsv&id=" + item;
81 return {text : items[item].name, href : href};
84 menu.populateSubmenu("Sensor data", mctx.sensors, sensorTranslator);
85 menu.populateSubmenu("Actuator data", mctx.actuators, actuatorTranslator);
91 * Sets the camera autoupdater
94 $.fn.setCamera = function () {
95 var url = mctx.api + "image";
98 //Stop updating if we can't retrieve an image!
99 this.error(function() {
105 var updater = function() {
108 parent.attr("src", "");
112 parent.attr("src", url + "#" + (new Date()).getTime());
114 setTimeout(updater, 1000);
121 $.fn.setStrainGraphs = function () {
122 var sensor_url = mctx.api + "sensors";
125 var updater = function () {
126 var time_limit = mctx.strain_gauges.time_limit;
127 var responses = new Array(mctx.strain_gauges.ids.length);
129 for (var i = 0; i < mctx.strain_gauges.ids.length; i++) {
130 var parameters = {id : i, start_time: -time_limit};
131 responses[i] = $.ajax({url : sensor_url, data : parameters});
134 $.when.apply(this, responses).then(function () {
135 var data = new Array(arguments.length);
136 for (var i = 0; i < arguments.length; i++) {
137 var raw_data = arguments[i][0].data;
138 var pruned_data = [];
139 var step = ~~(raw_data.length/100);
140 for (var j = 0; j < raw_data.length; j += step)
141 pruned_data.push(raw_data[j]);
142 data[i] = pruned_data;
144 $.plot(graphdiv, data);
145 setTimeout(updater, 500);
146 }, function () {alert("It crashed");});
153 $.fn.login = function () {
154 var username = this.find("input[name='username']").val();
155 var password = this.find("input[name='pass']").val();
156 var force = this.find("input[name='force']").is(":checked");
157 var url = mctx.api + "control";
159 var authFunc = function(xhr) {
160 xhr.setRequestHeader("Authorization",
161 "Basic " + base64.encode(username + ":" + password));
166 data : {action : "lock", force : (force ? true : undefined)},
167 beforeSend : authFunc
168 }).done(function (data) {
170 if (data.status < 0) {
171 alert("no - " + data.description);
173 mctx.has_control = true;
174 alert("yes - " + mctx.key);
176 }).fail(function (jqXHR) {
177 mctx.key = undefined;
178 mctx.has_control = false;