2 * Code for the controls page.
7 mctx.control.api = mctx.api + 'control'
8 mctx.control.states = {
15 mctx.control.state = null;
17 function toggleControls(running) {
19 $("#experiment-stop").show();
20 $("#pressure-widget").show();
21 $("#start-widget").hide();
23 $("#start-widget").show();
24 $("#experiment-stop").hide();
25 $("#pressure-widget").hide();
29 $.fn.setStatusUpdater = function () {
32 var updater = function () {
34 url : mctx.control.api,
35 data : {'action' : 'identify'}
36 }).done(function (data) {
37 if (!result.checkStatus(data)) {
38 $(result).parent().addClass("fail");
39 setTimeout(updater, 4000);
46 switch (data.control_state_id) {
47 case mctx.control.states.start:
48 text = "Experiment started - '" + data.control_experiment_name +
49 "' by " + data.control_user_name;
52 case mctx.control.states.pause:
53 text = "Experiment paused - '" + data.control_experiment_name +
54 "' by " + data.control_user_name;
57 case mctx.control.states.stop:
58 text = "No experiment running.";
60 case mctx.control.states.emergency:
61 text = "Emergency mode - '" + data.control_experiment_name +
62 "' by " + data.control_user_name;
66 text = "Unknown mode: " + data.control_state_id;
70 if (data.control_state_id !== mctx.control.state) {
71 toggleControls(running);
74 $(result).parent().addClass("fail");
76 $(result).parent().addClass("pass");
79 mctx.control.state = data.control_state_id;
82 setTimeout(updater, 2000);
85 $(result).text("Connection failed.").parent().addClass("fail");
86 setTimeout(updater, 4000);
94 $.fn.startExperiment = function (group, experiment, force, result) {
95 $(group).attr('disabled', 'disabled');
97 if (!experiment || !experiment.match(/^[a-zA-Z0-9_-]+$/)) {
98 result.text("Experiment names must be composed of alphanumeric characters" +
99 " or the characters -_-").addClass("fail");
100 $(group).removeAttr('disabled');
104 var data = {action : "start", name : experiment};
110 url : mctx.control.api,
112 }).done(function (data) {
113 if (!result.checkStatus(data)) {
116 result.html(" ");
117 toggleControls(true);
118 }).always(function () {
119 $(group).removeAttr('disabled');
123 $.fn.stopExperiment = function (result) {
125 stop.attr('disabled', 'disabled');
126 result.text("Stopping the experiment...");
129 url : mctx.control.api,
130 data : {action : "stop"}
131 }).done(function (data) {
132 if (!result.checkStatus(data)) {
135 result.html(" ");
136 toggleControls(false);
137 }).always(function () {
138 stop.removeAttr('disabled');
142 $.fn.setPressure = function(pressure, result) {
143 result.html(" ");
145 for (var k in pressure) {
146 var n = Number(pressure[k]);
147 if (isNaN(n) || n < 0) {
148 result.text("You must give positive numeric values.").addClass("fail");
154 var set = pressure['set'] + "," + pressure['wait'] + ","
155 pressure['size'] + "," + pressure['count'];
157 url : mctx.api + "actuators",
158 data : {id : mctx.actuator.pressure_regulator, set : set}
159 }).done(function (data) {
160 if (!result.checkStatus(data)) {
164 result.text("Set ok!").removeClass("fail").addClass("pass");