-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
408 lines (360 loc) · 14.6 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
var BufferDigester = require('common-utils').BufferDigester;
var WebSocketServer = require('ws').Server;
var WebSocket = require('ws');
var fs = require('fs');
var path = require('path');
var argv = require('minimist')(process.argv.slice(2));
// initialize connections parameters
var controllerID = argv['uc'];
var uiID = argv['ui'];
var bridgeID = argv['br'];
var port = argv['_'][0];
// initialize leg side
var side = argv['s'];
// Initialize trigger websocker client
var wstr = null;
if (argv['trsrv'] != undefined) {
var options = {};
if (argv['trcltid'] != undefined) {
options = {headers: {id: argv['trcltid']}};
}
wstr = new WebSocket('ws://' + argv['trsrv'] + '/', options);
}
// start websocket server
var wss = new WebSocketServer({port: port});
// Initialize connection with websocket server
//var ws = null;
// initialize server connections list
var clients = {};
// initialize buffer
var buffer = [];
// initialize output stream
var wstream = null;
// initialize CONTROLLER connection start time
var timeStart = null;
// initialiaze function to send data to UI
var sendData = function(data) {};
// initialize variables for file saving
var fsFolderMain = null;
var fsFileName = null;
var fsFileSaving = false;
// for debugging
var cont = 0;
var handleSamplesMsg = true;
// Handle client connection to trigger server
if (wstr != null) {
wstr.on('open', function() {
console.log('connection with trigger server open');
});
wstr.on('close', function() {
console.log('connection with trigger server closed');
});
wstr.on('message', function(message) {
var msg = JSON.parse(message);
fsFileSaving = (msg.value == 'start');
if (fsFileSaving) {
if (fsFileName != '') {
wstream = createFileWriteStreamHelper(fsFolderMain, fsFileName);
console.log("output file stream created");
}
if (uiID in clients) {
clients[uiID].send(JSON.stringify({data: 'file-saving', port: 'cmd-data-saving-in'}));
}
} else {
if (wstream != null) {
wstream.end();
wstream = null;
}
if (uiID in clients) {
clients[uiID].send(JSON.stringify({data: 'no-file-saving', port: 'cmd-data-saving-in'}));
}
}
});
}
// Handle server
wss.on('connection', function connection(ws) {
/* NOTE:
Pinging the client is the trick to get a connection closed if the client
does not repond anymore (for instance when controller is manually reset).
But closing automaticcally the connection from the server side can prevent
from creating a new one from the same client. This is an unexplained
behaviour.
The solution is not to ping at all. The controller, when reset, will begin
a new connection with the server, and the connection will overwrite the one
in clients[clientID]. This is seems to work in a stable way.
*/
/*
ws.pingssent = 0;
var interval = setInterval(function() {
if (ws.pingssent >= 1) {
ws.close();
} else {
ws.ping();
ws.pingssent++;
}
}, 5000);
ws.on('pong', function() {
ws.pingssent = 0;
console.log("received: pong");
})
*/
// Get client info ad add it to clients list
console.log(JSON.stringify(ws.upgradeReq.headers));
var clientID = ws.upgradeReq.connection.remoteAddress;
var ID = ws.upgradeReq.headers['id'];
if (ID != undefined) {
clientID += ":" + ID;
}
clients[clientID] = ws;
// notify connection open
console.log('connection open with ' + clientID);
// set time start
if (clientID == controllerID) {
timeStart = Date.now();
}
// set function to send data to UI
if (clientID == controllerID || clientID == bridgeID) {
sendData = function(data) {
if (uiID in clients) {
clients[uiID].send(JSON.stringify({port: 'uc-samples', data: data}), function() { });
console.log('message sent to UI');
} else {
console.log('UI not connected, impossible to send the signal');
}
};
bd.setDigestCallback(sendData);
}
// set close callback
ws.on('close', function() {
delete clients[clientID];
console.log('connection closed with ' + clientID);
if (clientID == uiID && wstream != null) {
wstream.end();
console.log('output file stream closed');
}
})
// check if unwanted clients are connected
if (!(clientID == uiID || clientID == controllerID || clientID == bridgeID)) {
console.log('client not UI, CONTROLLER or BRIDGE. It will be disconnected.');
ws.close();
return;
}
// set message received callback
ws.on('message', function(message) {
// notify message arrived
console.log('message arrived from ' + clientID);
if (clientID == controllerID || clientID == bridgeID) {
// split in type and payload
var parts = message.split('|');
var type = parts[0], payload = parts[1];
// manage data samples from CONTROLLER
if (type == 'SAMPLES') {
console.log(cont);
cont++;
if (handleSamplesMsg) {
// split package in samples
var samples = payload.split(';');
samples.pop(); // last one is garbage
// from each sample, take first (common X) and second (Y signal 1) measure
for (var i = 0; i < samples.length; i++) {
// extract time and measure for each signal
var measures = samples[i].split(',');
measures.pop(); // last one is garbage
console.log(JSON.stringify(measures));
// parse single measures
var TS = parseInt(measures[0]);
var MT = parseFloat(measures[1]);
var MP = parseFloat(measures[2]);
var SPC = parseFloat(measures[3]);
var SVAN = parseFloat(measures[4]);
var AAN = parseFloat(measures[5]);
var VAC = parseFloat(measures[6]);
var FVAN = parseFloat(measures[7]);
var MTP = parseFloat(measures[8]);
var AANT = parseFloat(measures[9]);
var MDET = parseFloat(measures[10]);
var GP = parseInt(measures[11]);
// create nex data point
var newSample = {
SIDE: side,
TS: TS / 1000.,
MT: MT,
MP: MP,
SPC: SPC,
SVAN: SVAN,
AAN: AAN,
FVAN: FVAN,
VAC: VAC,
MTP: MTP,
AANT: AANT,
MDET: MDET,
GP: GP,
};
var metadata = {
SIDE: "Side",
TS: "Time",
MT: "Motor torque",
MP: "Motor position",
SPC: "Spring compression",
SVAN: "Shank angle with vertical",
AAN: "Ankle angle",
FVAN: "Foot angle with vertical",
VAC: "IMU vertical acceleration",
MTP: "Motor target position",
AANT: "Ankle angle target",
MDET: "Motor detach",
GP: "Gait phase",
}
// write measure to text file
if (fsFileSaving && wstream != null) {
wstream.write(JSON.stringify(newSample) + '\n');
}
newSample.metadata = metadata;
// add new data point to buffer
buffer.push(newSample);
// estimate delay
var delay = (Date.now() - timeStart) - TS;
console.log('estimated delay for current sample (ms): ' + delay);
}
}
} else {
if (uiID in clients) {
clients[uiID].send(JSON.stringify({port: 'cmd-uC-in', data: {param: type, msg: payload}}));
}
}
} else if (clientID == uiID) {
console.log(message);
// parse message
var msg = JSON.parse(message);
// manage commands from UI
if (msg.uri == 'send-to-uC') {
// forward command from UI to CONTROLLER
clients[controllerID].send(msg.payload);
} else if (msg.uri == 'edit-Ns-Ts') {
// adjust buffer digester settings
var bds = msg.payload;
bd.setN(bds.Ns);
bd.setTs(bds.Ts);
// send new parameters back to UI
var bdsNew = {Ns: bd.getN(), Ts: bd.getTs()};
clients[uiID].send(JSON.stringify({port: 'cmd-tune-bd-in', data: bdsNew}));
console.log("buffer digester parameters set");
} else if (msg.uri == 'edit-folder-main') {
fsFolderMain = msg.payload;
console.log("folder for file saving set");
} else if (msg.uri == 'edit-file-name') {
fsFileName = msg.payload;
console.log("file name for file saving set");
} else if (msg.uri == 'edit-file-saving') {
fsFileSaving = msg.payload;
if (fsFileSaving) {
if (fsFileName != '') {
wstream = createFileWriteStreamHelper(fsFolderMain, fsFileName);
console.log("output file stream created");
}
if (wstr != null) {
wstr.send(JSON.stringify({type: 'trigger', value: 'start', fileName: fsFileName}));
}
} else {
if (wstream != null) {
wstream.end();
wstream = null;
}
if (wstr != null) {
wstr.send(JSON.stringify({type: 'trigger', value: 'stop', fileName: fsFileName}));
}
}
/*} else if (msg.uri == 'query-uC-th') {
clients[controllerID].send('ALGOPAR|TH0|R');*/
} else if (msg.uri == 'edit-uC-th') {
clients[controllerID].send('ALGOPAR|TH0|W|' + msg.payload);
} else if (msg.uri == 'edit-uC-ALGMOD') {
clients[controllerID].send('ALGOPAR|ALGMOD|W|' + msg.payload);
} else if (msg.uri == 'edit-uC-FVANTHS') {
clients[controllerID].send('ALGOPAR|FVANTHS|W|' + msg.payload);
} else if (msg.uri == 'edit-uC-VACTHS') {
clients[controllerID].send('ALGOPAR|VACTHS|W|' + msg.payload);
} else if (msg.uri == 'edit-uC-FVANTTO') {
clients[controllerID].send('ALGOPAR|FVANTTO|W|' + msg.payload);
} else if (msg.uri == 'edit-uC-VACTTO') {
clients[controllerID].send('ALGOPAR|VACTTO|W|' + msg.payload);
} else if (msg.uri == 'edit-uC-HOMOD') {
clients[controllerID].send('ALGOPAR|HOMOD|W|' + msg.payload);
} else if (msg.uri == 'edit-uC-FVANTHO') {
clients[controllerID].send('ALGOPAR|FVANTHO|W|' + msg.payload);
} else if (msg.uri == 'edit-uC-SVANTHO') {
clients[controllerID].send('ALGOPAR|SVANTHO|W|' + msg.payload);
} else if (msg.uri == 'edit-uC-TWNOHO') {
clients[controllerID].send('ALGOPAR|TWNOHO|W|' + msg.payload);
} else if (msg.uri == 'edit-uC-MAD') {
clients[controllerID].send('ALGOPAR|MAD|W|' + msg.payload);
} else if (msg.uri == 'edit-uC-MAP') {
clients[controllerID].send('ALGOPAR|MAP|W|' + msg.payload);
} else if (msg.uri == 'edit-uC-MASW') {
clients[controllerID].send('ALGOPAR|MASW|W|' + msg.payload);
} else if (msg.uri == 'edit-uC-E2R') {
clients[controllerID].send('ALGOPAR|E2R|W|' + msg.payload);
} else if (msg.uri == 'edit-uC-E3R') {
clients[controllerID].send('ALGOPAR|E3R|W|' + msg.payload);
} else if (msg.uri == 'edit-uC-E1SW') {
clients[controllerID].send('ALGOPAR|E1SW|W|' + msg.payload);
} else if (msg.uri == 'edit-uC-AANE') {
clients[controllerID].send('ALGOPAR|AANE|W|' + msg.payload);
} else if (msg.uri == 'edit-uC-ST') {
clients[controllerID].send('ALGOPAR|ST|W|' + msg.payload);
} else if (msg.uri == 'edit-uC-MECHBLOCK') {
clients[controllerID].send('SETT|MECHBLOCK|W|' + msg.payload);
} else if (msg.uri == 'edit-uC-SERVOBEH') {
clients[controllerID].send('SETT|SERVOBEH|W|' + msg.payload);
}
}
});
});
function createFileWriteStreamHelper(folderPath, fileName) {
return createFileWriteStream(path.join(folderPath, fileName + '.txt'));
};
function createFileWriteStream(filePath) {
var wstream = fs.createWriteStream(filePath);
wstream.on('finish', function(){
console.log('file has been written');
});
console.log('output file stream open');
return wstream;
}
// Following values tuned to send data at N/Ts FPS (supported by UI)
// OK (FPS: 24 Hz)
var Ts = 250; // ms
var N = 6;
/*
// OK (FPS: 35 Hz)
var Ts = 200; // ms
var N = 7;
*/
/*
// OK (FPS: 33 Hz)
var Ts = 300; // ms
var N = 10;
*/
// --- without charts
/*
// OK (100+ Hz)
var Ts = 100; // ms
var N = 10;
*/
/* In the following digest loop, the data buffer must be emptied.
The buffer contains Nc frames (depending on Fc). Only N can be sent.
*/
/*
var cont = 0;
var updateFrameNumber = function(p) {
cont++;
p.frameNumber = cont;
};*/
var bd = new BufferDigester(
buffer, sendData, {
N: N,
Ts: Ts,
//preDigestPckgEditCallback: updateFrameNumber,
selectPckgMethod: 'equi-sample',
});
bd.startDigestLoop();