+ //Stop updating if we can't retrieve an image!
+ this.error(function() {
+ update = false;
+ });
+
+ var parent = this;
+
+ var updater = function() {
+ if (!update) {
+ alert("Cam fail");
+ parent.attr("src", "");
+ return;
+ }
+
+ parent.attr("src", url + "#" + (new Date()).getTime());
+
+ setTimeout(updater, 10000);
+ };
+
+ updater();
+ return this;
+};
+
+/**
+* Sets the strain graphs to graph stuff. Obsolete?
+* @returns {$.fn}
+*/
+$.fn.setStrainGraphs = function () {
+ var sensor_url = mctx.api + "sensors";
+ var graphdiv = this;
+
+ var updater = function () {
+ var time_limit = mctx.strain_gauges.time_limit;
+ var responses = new Array(mctx.strain_gauges.ids.length);
+
+ for (var i = 0; i < mctx.strain_gauges.ids.length; i++) {
+ var parameters = {id : i, start_time: -time_limit};
+ responses[i] = $.ajax({url : sensor_url, data : parameters});
+ }
+
+ $.when.apply(this, responses).then(function () {
+ var data = new Array(arguments.length);
+ for (var i = 0; i < arguments.length; i++) {
+ var raw_data = arguments[i][0].data;
+ var pruned_data = [];
+ var step = ~~(raw_data.length/100);
+ for (var j = 0; j < raw_data.length; j += step)
+ pruned_data.push(raw_data[j]);
+ data[i] = pruned_data;
+ }
+ $.plot(graphdiv, data);
+ setTimeout(updater, 1000);
+ }, 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 out = this.find("#result");
+ var redirect = function () {
+ window.location.href = mctx.location;
+ };
+
+ out.removeAttr("class");
+ out.text("Logging in...");
+
+ return $.ajax({
+ url : mctx.api + "bind",
+ data : {user: username, pass : password}
+ }).done(function (data) {
+ if (data.status < 0) {
+ mctx.has_control = false;
+ out.attr("class", "fail");
+ out.text("Login failed: " + data.description);
+ } else {
+ //todo: error check
+ mctx.has_control = true;
+ out.attr("class", "pass");
+ out.text("Login ok!");
+ setTimeout(redirect, 800);
+ }
+ }).fail(function (jqXHR) {
+ mctx.has_control = false;
+ 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;
+
+ if ($(this).length <= 0) {
+ //No error log, so do nothing.