| author | Nicolas Martin <joliclic@gmail.com> |
| Thu, 18 May 2017 15:05:29 +0200 | |
| changeset 3 | 874a53e7b615 |
| parent 2 | 1425d4030bea |
| permissions | -rw-r--r-- |
|
1
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
1 |
'use strict'; |
|
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
2 |
|
| 2 | 3 |
var gServers = [ |
|
1
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
4 |
{ |
|
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
5 |
name: 'bird', |
| 2 | 6 |
url: 'ws:/192.168.0.10', |
7 |
websocket: null, |
|
|
1
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
8 |
}, |
|
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
9 |
//{ |
|
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
10 |
// name: "fox", |
|
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
11 |
// url: "ws://192.168.0.11", |
|
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
12 |
// , websserver: null |
|
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
13 |
//}, |
|
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
14 |
|
|
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
15 |
]; |
|
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
16 |
|
| 2 | 17 |
//var gButtons = [ |
18 |
// { |
|
19 |
// id: "bt_black", |
|
20 |
// command: "black" |
|
21 |
// } |
|
22 |
//]; |
|
23 |
||
24 |
||
25 |
function $(expr) { |
|
26 |
return document.querySelector(expr); |
|
27 |
} |
|
28 |
||
29 |
function $$(expr) { |
|
30 |
return Array.from(document.querySelectorAll(expr)); |
|
31 |
} |
|
32 |
||
33 |
function sendCommand(str) { |
|
34 |
gServers.forEach(function(server) { |
|
35 |
if (server.websocket && server.websocket.readyState == WebSocket.OPEN) { |
|
36 |
server.websocket.send(str) |
|
37 |
} else { |
|
38 |
updateStatus(); |
|
39 |
} |
|
40 |
}); |
|
41 |
} |
|
42 |
||
43 |
function updateStatus() { |
|
44 |
var nb_servers = gServers.length; |
|
45 |
var nb_servers_ok = 0; |
|
46 |
||
47 |
gServers.forEach(function(server) { |
|
48 |
if (server.websocket && server.websocket.readyState == WebSocket.OPEN) { |
|
49 |
nb_servers_ok++; |
|
50 |
if (server.status_box) { |
|
51 |
server.status_box.innerHTML = |
|
52 |
'<span class="status-txt status-ok">OK</span>'; |
|
53 |
} |
|
54 |
} else if (server.status_box) { |
|
55 |
server.status_box.innerHTML = |
|
56 |
'<span class="status-txt status-alert">KO</span>'; |
|
57 |
} |
|
58 |
}); |
|
59 |
||
60 |
var main_status_box = $('#main-status-box'); |
|
61 |
if (!main_status_box) |
|
62 |
return; |
|
63 |
||
64 |
if (nb_servers_ok === 0) { |
|
65 |
main_status_box.innerHTML = |
|
66 |
'<big class="status-txt status-alert">! KO !</big>'; |
|
67 |
} else if (nb_servers === nb_servers_ok) { |
|
68 |
main_status_box.innerHTML = |
|
69 |
'<big class="status-txt status-ok">OK</big>'; |
|
70 |
} else if (nb_servers_ok < nb_servers) { |
|
71 |
main_status_box.innerHTML = |
|
72 |
'<big class="status-txt status-warning">warning !</big>'; |
|
|
1
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
73 |
} |
| 2 | 74 |
} |
|
1
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
75 |
|
|
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
76 |
function init() { |
| 2 | 77 |
updateStatus(); |
78 |
||
79 |
var substatus_box = $('#substatus-box'); |
|
80 |
gServers.forEach(function(server) { |
|
81 |
if (substatus_box) { |
|
82 |
server.status_box = document.createElement('div'); |
|
83 |
server.status_box.className = 'substatus'; |
|
84 |
substatus_box.appendChild(server.status_box); |
|
85 |
} |
|
86 |
server.websocket = new WebSocket(server.url); |
|
87 |
server.websocket.addEventListener('open', function (evt) { |
|
88 |
updateStatus(); |
|
89 |
}); |
|
90 |
}); |
|
91 |
||
92 |
var mutable_bt = $('#mutable-cmd-bt'); |
|
93 |
var mutable_input = $('#mutable-cmd-input'); |
|
94 |
if (mutable_bt && mutable_input) { |
|
95 |
mutable_bt.addEventListener('click', function(evt) { |
|
96 |
cmd = mutable_input.value; |
|
97 |
||
98 |
if (cmd) |
|
99 |
sendCommand(cmd); |
|
100 |
}); |
|
101 |
} |
|
102 |
||
103 |
$$('.cmd-bt').forEach(function(elt) { |
|
104 |
elt.addEventListener('click', function(evt) { |
|
105 |
cmd = elt.getAttribute('data-cmd'); |
|
106 |
if (cmd) |
|
107 |
sendCommand(cmd); |
|
108 |
}); |
|
109 |
}); |
|
110 |
||
|
1
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
111 |
|
|
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
112 |
} |
|
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
113 |
|
|
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
114 |
|
| 2 | 115 |
document.addEventListener('DOMContentLoaded', function(e) { |
|
1
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
116 |
//console.log("DOM loaded"); |
| 2 | 117 |
init(); |
|
1
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
118 |
}); |
|
8ad95a9cc477
main files for the web control
Nicolas Martin <joliclic@gmail.com>
parents:
diff
changeset
|
119 |