Somewhat done control page
[matches/MCTX3420.git] / testing / MCTXWeb / public_html / static / mctx.control.js
1 /**
2  * Code for the controls page.
3  * @date 19-10-2013
4  */
5
6 mctx.control = {};
7 mctx.control.api = mctx.api + 'control'
8 mctx.control.states = {
9   start : 0,
10   pause : 1,
11   resume : 2,
12   stop : 3,
13   emergency : 4
14 };
15 mctx.control.state = null;
16
17 function toggleControls(running) {
18   if (running) {
19     $("#experiment-stop").show();
20     $("#pressure-widget").show();
21     $("#start-widget").hide();
22   } else {
23     $("#start-widget").show();
24     $("#experiment-stop").hide();
25     $("#pressure-widget").hide();
26   }
27 }
28
29 $.fn.setStatusUpdater = function () {
30   var result = this;
31   
32   var updater = function () {
33     $.ajax({
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);
40         return;
41       }
42
43       var text;
44       var running = false;
45       var fail = false;
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;
50           running = true;
51         break;
52         case mctx.control.states.pause:
53           text = "Experiment paused - '" + data.control_experiment_name +
54                  "' by " + data.control_user_name;
55           running = true;
56         break;
57         case mctx.control.states.stop:
58           text = "No experiment running.";
59         break;
60         case mctx.control.states.emergency:
61           text = "Emergency mode - '" + data.control_experiment_name +
62                  "' by " + data.control_user_name;
63           running = true;
64           fail = true;
65         default:
66           text = "Unknown mode: " + data.control_state_id;
67           fail = true;
68       }
69       
70       if (data.control_state_id !== mctx.control.state) {      
71         toggleControls(running);
72         $(result).text(text);
73         if (fail) {
74           $(result).parent().addClass("fail");
75         } else {
76           $(result).parent().addClass("pass");
77         }
78         
79         mctx.control.state = data.control_state_id;
80       }
81       
82       setTimeout(updater, 2000);
83     })
84    .fail(function () {
85      $(result).text("Connection failed.").parent().addClass("fail");
86      setTimeout(updater, 4000);
87    });
88   };
89   
90   updater();
91 };
92
93
94 $.fn.startExperiment = function (group, experiment, force, result) {
95  $(group).attr('disabled', 'disabled');
96  
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');
101    return;
102  } 
103  
104  var data = {action : "start", name : experiment};
105  if (force) {
106    data.force = 1;
107  }
108  
109  $.ajax({
110    url : mctx.control.api,
111    data : data
112  }).done(function (data) {
113    if (!result.checkStatus(data)) {
114      return;
115    }
116    result.html(" ");
117    toggleControls(true);
118  }).always(function () {
119    $(group).removeAttr('disabled');
120  });
121 };
122
123 $.fn.stopExperiment = function (result) {
124   var stop = this;
125   stop.attr('disabled', 'disabled');
126   result.text("Stopping the experiment...");
127   
128   $.ajax({
129     url : mctx.control.api,
130     data : {action : "stop"}
131   }).done(function (data) {
132     if (!result.checkStatus(data)) {
133       return;
134     }
135     result.html(" ");
136     toggleControls(false);
137   }).always(function () {
138     stop.removeAttr('disabled');
139   });
140 };
141
142 $.fn.setPressure = function(pressure, result) {
143   result.html(" ");
144   
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");
149       return;
150     }
151     pressure[k] = n;
152   }
153   
154   var set = pressure['set'] + "," + pressure['wait'] + ","
155             pressure['size'] + "," + pressure['count'];
156   $.ajax({
157     url : mctx.api + "actuators",
158     data : {id : mctx.actuator.pressure_regulator, set : set}
159   }).done(function (data) {
160     if (!result.checkStatus(data)) {
161       return;
162     }
163     
164     result.text("Set ok!").removeClass("fail").addClass("pass");
165   });
166 };

UCC git Repository :: git.ucc.asn.au