forked from adcomp/piface-python-websocket-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
piface_ws.js
55 lines (46 loc) · 1.17 KB
/
piface_ws.js
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
var ws = null;
function connect() {
var host = $("#host").val();
ws = new WebSocket("ws://" + host + ":8888/piface");
ws.onmessage = function(evt) {
// console.log(evt.data);
var piface = JSON.parse(evt.data);
update(piface);
};
ws.onclose = function(evt) {
console.log("Connection close ..");
$("#host").css("background", "#bd1143");
$('#input').css('display', 'none')
$('#output').css('display', 'none')
$('#connect').show()
};
ws.onopen = function(evt) {
console.log("WebSocket open ..")
$("#host").css("background", "#7eb52b");
$('#input').css('display', 'block')
$('#output').css('display', 'block')
$('#connect').hide()
};
}
function update(piface) {
for (var i=0; i<8; i++) {
// input
if (piface['in'][i] == '0') {
$('#in_' + i).css('background', '#333');
$('#in_' + i + '_state').html('Off');
}
else {
$('#in_' + i).css('background', '#159');
$('#in_' + i + '_state').html('On');
}
// output
if (piface['out'][i] == '0') {
$('#out_' + i).css('background', '#333');
$('#out_' + i + '_state').html('Off');
}
else {
$('#out_' + i).css('background', '#bd1143');
$('#out_' + i + '_state').html('On');
}
}
}