<script type="text/javascript">
runBeforeLoad().done(function () {
$(document).ready(function () {
- $("#state-exp").initialiseControls();
+ $("form").submit(function () { //Prevent form submit globally
+ return false;
+ })
+
+ //Set the status updated
+ $("#state-exp").setStatusUpdater();
+
+ //Set the logic for the start controls
+ $("#start-controls").submit(function () {
+ var start = $("#start-controls input[type='button']");
+ var force = $("#start-controls input[name='start_force']");
+
+ $(this).startExperiment(start, $("#experiment_name").val(),
+ force.is(":checked"), $("#start-result"));
+ force.prop("checked", false);
+ });
+
+ //Set the logic for the stop button
+ $("#experiment-stop").click(function () {
+ $(this).stopExperiment($("#stop-status"));
+ });
+
+ //Set the logic for the pressure controls
+ $("#pressure-controls").submit(function () {
+ var pressure = {
+ set : $("#pressure-set").val(),
+ step : $("#pressure-stepsize").val(),
+ wait : $("#pressure-stepwait").val(),
+ count : $("#pressure-stepcount").val()
+ };
+ $(this).setPressure(pressure, $("#pressure-result"));
+ });
});
}).fail(function () {
$(document).ready(function () {
</td>
</tr>
</table>
+ <div id="stop-status">
+
+ </div>
<div class="sub-title">Error and warning messages</div>
<textarea id="errorlog" wrap="off" rows="4" cols="30" readonly>
<p>
<label for="experiment_name">Experiment name</label>
<input id="experiment_name" type="text">
+
+ <label for="start_force">Overwrite existing</label>
+ <input type="checkbox" name="start_force" id="start_force">
</p>
<p id="start-result">
</p>
<p class="centre">
- <input type="button" name="start_strain" value="Strain test">
- <input type="button" name="start_strain" value="Explode test">
+ <input type="submit" name="start_strain" value="Strain test">
+ <input type="submit" name="start_explode" value="Explode test">
</p>
</form>
</div>
stop : 3,
emergency : 4
};
+mctx.control.state = null;
-$.fn.initialiseControls = function () {
+function toggleControls(running) {
+ if (running) {
+ $("#experiment-stop").show();
+ $("#pressure-widget").show();
+ $("#start-widget").hide();
+ } else {
+ $("#start-widget").show();
+ $("#experiment-stop").hide();
+ $("#pressure-widget").hide();
+ }
+}
+
+$.fn.setStatusUpdater = function () {
var result = this;
+ var updater = function () {
+ $.ajax({
+ url : mctx.control.api,
+ data : {'action' : 'identify'}
+ }).done(function (data) {
+ if (!result.checkStatus(data)) {
+ $(result).parent().addClass("fail");
+ setTimeout(updater, 4000);
+ 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 (data.control_state_id !== mctx.control.state) {
+ toggleControls(running);
+ $(result).text(text);
+ if (fail) {
+ $(result).parent().addClass("fail");
+ } else {
+ $(result).parent().addClass("pass");
+ }
+
+ mctx.control.state = data.control_state_id;
+ }
+
+ setTimeout(updater, 2000);
+ })
+ .fail(function () {
+ $(result).text("Connection failed.").parent().addClass("fail");
+ setTimeout(updater, 4000);
+ });
+ };
+
+ updater();
+};
+
+
+$.fn.startExperiment = function (group, experiment, force, result) {
+ $(group).attr('disabled', 'disabled');
+
+ if (!experiment || !experiment.match(/^[a-zA-Z0-9_-]+$/)) {
+ result.text("Experiment names must be composed of alphanumeric characters" +
+ " or the characters -_-").addClass("fail");
+ $(group).removeAttr('disabled');
+ return;
+ }
+
+ var data = {action : "start", name : experiment};
+ if (force) {
+ data.force = 1;
+ }
+
+ $.ajax({
+ url : mctx.control.api,
+ data : data
+ }).done(function (data) {
+ if (!result.checkStatus(data)) {
+ return;
+ }
+ result.html(" ");
+ toggleControls(true);
+ }).always(function () {
+ $(group).removeAttr('disabled');
+ });
+};
+
+$.fn.stopExperiment = function (result) {
+ var stop = this;
+ stop.attr('disabled', 'disabled');
+ result.text("Stopping the experiment...");
+
$.ajax({
url : mctx.control.api,
- data : {'action' : 'identify'}
+ data : {action : "stop"}
}).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;
+ result.html(" ");
+ toggleControls(false);
+ }).always(function () {
+ stop.removeAttr('disabled');
+ });
+};
+
+$.fn.setPressure = function(pressure, result) {
+ result.html(" ");
+
+ for (var k in pressure) {
+ var n = Number(pressure[k]);
+ if (isNaN(n) || n < 0) {
+ result.text("You must give positive numeric values.").addClass("fail");
+ return;
}
-
- if (running) {
- $("#experiment-stop").show();
- $("#pressure-widget").show();
- } else {
- $("#start-widget").show();
+ pressure[k] = n;
+ }
+
+ var set = pressure['set'] + "," + pressure['wait'] + ","
+ pressure['size'] + "," + pressure['count'];
+ $.ajax({
+ url : mctx.api + "actuators",
+ data : {id : mctx.actuator.pressure_regulator, set : set}
+ }).done(function (data) {
+ if (!result.checkStatus(data)) {
+ return;
}
- $(result).text(text);
- if (fail) {
- $(result).parent().addClass("fail");
- } else {
- $(result).parent().addClass("pass");
- }
+ result.text("Set ok!").removeClass("fail").addClass("pass");
});
-};
+};
\ No newline at end of file