-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·312 lines (275 loc) · 7.56 KB
/
server.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/local/bin/node
const express = require('express');
const app = express();
const http = require('http').Server(app);
const net = require('net');
const io = require('socket.io')(http);
let numMes = 1;
app.use(express.static(__dirname + '/dist'));
const socket = new net.Socket();
socket.setEncoding('hex');
const ipSuperviseur = 'localhost';
const port = process.env.port || 3000;
let dataInReception = '';
let lastRequest = '';
let dateOrigin;
const connections = [];
/** DECLARATION FONCTIONS */
/*
* Fonction qui retourne le temps à un message passé en paramétre
*/
function addTime(msg) {
const a = Math.abs(new Date() - dateOrigin);
const date = new Date(a);
const message = `<${date.getMinutes()}:${date.getSeconds()}:${date.getMilliseconds()}>${msg}`;
return message;
}
function sendConsole(msg) {
io.emit('consoleIn', addTime(msg));
}
/*
* Conversion chaine de caractére avec des informations hexa en chaine de caractére
* avec les informations ascii.
*/
function sendNotification(typemes, text) {
const myObj = {
msg: `${numMes} ${text}`,
type: typemes,
};
io.emit('notifications', myObj);
numMes++;
}
function hex2a(hexx) {
const hex = hexx.toString();
let str = '';
for (let i = 0; i < hex.length; i += 2) {
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
}
return str;
}
function disconnectTcp() {
socket.write('STO: ');
lastRequest = 'disconnection';
sendConsole('Déconnecté de la cible');
}
function moveForward() {
socket.write('DMB:F ');
lastRequest = 'GoingForward';
sendConsole('Avance');
}
function moveBack() {
socket.write('DMB:B ');
lastRequest = 'GoingBack';
sendConsole('recule');
}
function moveRight() {
socket.write('DMB:R ');
lastRequest = 'TurnRight';
sendConsole('pivot droite');
}
function moveLeft() {
socket.write('DMB:L ');
lastRequest = 'TurnLeft';
sendConsole('pivotGauche');
}
function stopMove() {
socket.write('DMB:S ');
lastRequest = 'Stop';
sendConsole('Stop');
}
function startWithWDDumby() {
socket.write('DMB:W ');
lastRequest = 'StartDumbyWithWD';
}
function startWithoutWDDumby() {
socket.write('DMB:u ');
lastRequest = 'StartDumbyWithoutWD';
}
function restartState() {
socket.write('DMB:r ');
lastRequest = 'idleDumby';
}
function sendPos(position) {
socket.write(`POS:${position.x},${position.y} `);
lastRequest = 'SendPosition';
sendConsole(`Envoi position : ${position.x}, ${position.y}`);
}
function setDisplayPos(data){
if(data === true){
socket.write('CAM:p ');
lastRequest='displayPos';
}else{
socket.write('CAM:s ');
lastRequest='dontDisplayPos';
}
}
function eventArena(data) {
if(data === 'ask'){
socket.write('CAM:y ');
lastRequest = 'askArena';
} else if(data ==='ok'){
socket.write('CAM:x ');
lastRequest = 'arenaConfirm';
}else if(data ==='nok'){
socket.write('CAM:z ');
lastRequest = 'arenaInfirm';
}
}
/*
* Traite une trame de donnée reçu.
*/
function traitmentMessage(val) {
let payload = val.substring(6);
const header = hex2a(val.substring(0, 6));
if (header !== 'IMG') {
payload = hex2a(payload);
sendConsole(`Reçu : ${header}:${payload}`);
}
if (header === 'LCD') {
io.emit('lostSerial', false);
sendNotification('error', 'Communication Xbee : Perdu');
}
if (header === 'ACK') {
if (lastRequest === 'IdleDumby') {
io.emit('dumberDisconected');
}
if (lastRequest === 'openSerial') {
io.emit('serialOpen', true);
sendNotification('success', 'Communication Xbee : OK');
}
if (lastRequest === 'closeSerial') {
io.emit('serialOpen', false);
sendNotification('warning', 'Communication Xbee : fermé');
}
if (lastRequest === 'cameraActive') {
io.emit('cameraState', true);
sendNotification('info', 'Camera activé');
}
if (lastRequest === 'cameraInactive') {
io.emit('cameraState', false);
sendNotification('info', 'Camera désactivé');
}
if (lastRequest === 'StartDumbyWithWD' || lastRequest === 'StartDumbyWithoutWD') {
sendConsole('Dumby is now started');
io.emit('dumbyStart', true);
}
if (lastRequest === 'idleDumby') {
sendConsole('Dumby is now stoped');
io.emit('dumbyStart', false);
}
} else if (header === 'NAK') {
sendNotification('error', `Error with last request : ${lastRequest}`);
if(lastRequest === 'askArena'){
io.emit('detectionError');
}
} else {
if(header === 'BAT'){
io.emit(header, payload.toString());
}else{
io.emit(header, payload);
}
}
}
function serial(data) {
if (data === true) {
socket.write('COM:o ');
lastRequest = 'openSerial';
} else {
socket.write('COM:C ');
lastRequest = 'closeSerial';
}
}
function specialMes(data) {
sendConsole(data);
socket.write(`MSG:${data} `);
}
function camera(state) {
if (state === true) {
lastRequest = 'cameraActive';
socket.write('CAM:A ');
} else {
lastRequest = 'cameraInactive';
socket.write('CAM:I ');
}
}
/*
* Descriptions des fonctions de call back
*/
function connectTcp() {
socket.connect(8080, ipSuperviseur, (err) => {
if (!err) {
dateOrigin = new Date();
io.emit('superViseurConnection', true);
sendNotification('success', 'Connecté au superviseur');
//sendConsole('Connecté au superviseur');
} else {
console.log('erreurConnection');
sendNotification('danger', 'Erreur lors de la connection');
//sendConsole('Erreur survenue lors de la connection au superviseur');
}
});
lastRequest = 'Connection';
}
/*
* bind des evenenements aux fonctions de callback
* Gestion des clients connecté dans la list client[]
*/
function handleConnection(client) {
sendNotification('success', 'Votre interface est connecté au serveur node');
connections.push(client);
client.on('askConnection', connectTcp);
client.on('askDisconnection', disconnectTcp);
client.on('startWithWD', startWithWDDumby);
client.on('startWitouthWD', startWithoutWDDumby);
client.on('idle', restartState);
client.on('sendPos', sendPos);
client.on('displayPos', setDisplayPos);
client.on('arena', eventArena);
client.on('MOVEFORWARD', moveForward);
client.on('MOVEBACK', moveBack);
client.on('MOVERIGHT', moveRight);
client.on('MOVELEFT', moveLeft);
client.on('MOVESTOP', stopMove);
client.on('openSerial', serial);
client.on('camera', camera);
client.on('console-out', specialMes);
client.on('disconnect', () => {
const index = connections.indexOf(client);
connections.splice(index, 1);
});
}
/** FIN DECLARATION FONCTION */
socket.on('error', (err) => {
console.log(`${err}`);
sendNotification('error', 'Erreur impossible de se connecter');
io.emit('superViseurConnection', false);
lastRequest = '';
});
io.on('connection', handleConnection);
socket.on('end', () => {
console.log('superviseur déconnecté');
sendNotification('error', ' Connection au superviseur perdu');
io.emit('superViseurConnection', false);
lastRequest = '';
});
/*
* Evenement sur une donnée tcp reçu
*/
socket.on('data', (data) => {
const trame = data.split('5452414d45'); // Trame sera le parser final ici écrit en hexa
dataInReception += trame[0];
if (trame.length > 1) {
traitmentMessage(dataInReception);
for (let i = 1; i < trame.length - 1; i++) {
traitmentMessage(trame[i]);
}
dataInReception = trame[trame.length - 1];
}
});
/* Gestions des routes
* */
app.get('/', (req, res) => {
res.sendFile('index.html');
});
http.listen(port); // Lancement du serveur web
console.log(`server is now running on port ${port}`);