-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
111 lines (89 loc) · 2.07 KB
/
client.js
File metadata and controls
111 lines (89 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* HW only supports 1-8 relays */
ALL_RELAYS = 0;
MAX_RELAYS = 8;
var App = App || {};
App.config = {
};
function clearField(input) {
input.value = "";
}
/* Send button click handler */
$('#sendButton').live('click', function(e) {
var command = $('#cmdInput').attr('value');
console.log(command);
/* Validate the input */
if (command.length > 50) {
alert('command too long. 50 characters max');
return false;
}
/* Split the command */
var strArr = command.split(" ");
/* We don't expect more than 2 args */
/* If length == 1 only TEMP and STATUS accepted*/
if (strArr.length > 2 ||
(strArr.length == 1 &&
strArr[0] != 'TEMP' &&
strArr[0] != 'STATUS' &&
strArr[0] != 'ON' &&
strArr[0] != 'OFF')) {
alert('unexpected command');
return false;
}
/* If length == 2 only ON/OFF accepted */
if (strArr.length == 2) {
if (strArr[0] != 'ON' &&
strArr[0] != 'OFF' ) {
alert('unexpected command 2');
return false;
}
/* Check that second arg is a number */
if (/[^\0-9]/.test(strArr[1])) {
alert('Bad character found. Only numbers are allowed');
return false;
}
/* Check that the number is smaller than MAX_RELAYS */
if (parseInt(strArr[1]) > MAX_RELAYS ||
parseInt(strArr[1]) < ALL_RELAYS) {
alert('Check the relay number being used.');
return false;
}
}
$.ajax({
cache: false,
type: 'GET',
url: '/command',
data: {
cmd: strArr[0],
arg: strArr[1]
},
error: function() {
alert('Error connecting to server');
},
success: App.onResponse
});
return false;
});
App.onResponse = function (response) {
if (response.error) {
alert ("ERROR: " + response.error);
return;
}
console.log(response);
$('#resVal').text('Response: ' + response.data.toString());
App.showResponse();
}
/* Show Command input div */
App.showCmdInput = function() {
$('#execute').show();
$('#response').hide();
$('#cmdInput').focus();
};
/* Show response div */
App.showResponse = function() {
$('#execute').show();
$('#response').show();
};
/* Main */
$(function() {
App.showCmdInput();
});