-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathServer.js
668 lines (457 loc) · 13.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
const { debug } = require("console");
const Tasks = require("./Tasks");
const
express = require("express"),
app = express(),
http = require("http").Server(app),
io = require("socket.io")(http),
cors = require("cors"),
TASKS = require("./Tasks"),
fs = require('fs'),
DeviceInfo = require("./DeviceInfo")
;
class Server{
constructor(){
// Server base configuration
this.config = {
port : 80,
debug : false,
device_id_min_size : 10,
device_id_max_size : 128,
};
}
async begin(){
try{
await this.loadConfig();
}catch(err){
console.error("Unable to load config, using default. Error: ", err);
}
// Begin
this.allowDebug = this.config.debug;
this.port = +this.config.port;
// Start HTTP listening
http.listen(this.port, () => {
console.log("Server online", this.port);
});
app.use('/api',cors());
app.use('/cdn',cors());
app.use('/socket.io',cors());
// If you want a custom front end when visited in a browser etc, you can create a /site/index.js file to drive that
if( fs.existsSync(__dirname+'/site/index.js') ){
const site = require('./site/index.js');
site(app, io);
}
// Handle http requests
app.get('/api', (req, res) => {
this.onGet(req, res);
});
// Handle WS requests
io.on("connection", socket => {
this.debug("sIO New user connected");
socket.on("disconnect", () => { this.onDisconnect(socket); });
socket.on(TASKS.TASK_ADD_DEVICE, (data, res) => { this.onDeviceConnected(socket, data, res); });
socket.on(TASKS.TASK_HOOKUP, (id, res) => { this.onAppHookup(socket, id, res); });
socket.on(TASKS.TASK_HOOKDOWN, (id, res) => { this.onAppHookdown(socket, id, res); });
socket.on(TASKS.TASK_PWM, hex => { this.onSocketPWM(socket, hex); });
socket.on(TASKS.TASK_ADD_APP, (name, res) => { this.onAppName(socket, name, res); });
socket.on(TASKS.TASK_CUSTOM_TO_DEVICE, data => { this.onCustomToDevice(socket, data); });
socket.on(TASKS.TASK_CUSTOM_TO_APP, data => { this.onCustomToApp(socket, data); });
socket.on(TASKS.TASK_GET, async data => {
this.debug("Program received", JSON.stringify(data), typeof data);
if( typeof data !== "object" )
return this.debug("Program is not acceptable, expected object, got ", typeof data);
try{
await this.handleGet(data.id, data.data, data.type);
this.debug("Program successfully accepted");
}catch( err ){
this.debug("Program error", err);
}
});
socket.on(TASKS.TASK_PWM_SPECIFIC, hex => { this.onSocketPWM(socket, hex, TASKS.TASK_PWM_SPECIFIC); });
});
}
loadConfig(){
// Get config
return new Promise( (res, rej) => {
fs.readFile(__dirname+'/config.json', 'utf8', (err, data) => {
if( err )
return rej("Unable to read config, using defaults: "+err.code);
try{
let json = JSON.parse(data);
if( typeof json !== "object" )
return rej('Config is not an object');
else{
for( let i in json ){
if( this.config.hasOwnProperty(i) ){
if( typeof this.config[i] === typeof json[i] )
this.config[i] = json[i];
else
console.log("Invalid type of config", i, "got", typeof json[i], "expected", typeof this.config[i]);
}
else
console.log("Unknown config property", i);
}
}
}catch(e){
rej(e);
}
res();
});
});
}
// makes sure device id is valid, throws a catchable exception if not, otherwise returns the ID
formatDeviceID( id ){
if( typeof id !== "string" )
throw "Invalid device ID type";
if( id.length < this.config.device_id_min_size || id.length > this.device_id_max_size )
throw "Invalid device ID size. Must be between "+this.device_id_min_size+" and "+this.device_id_max_size+" bytes.";
return id;
}
// CONNECTIONS
onDisconnect( socket ){
this.debug("Client disconnected");
// Let any connected apps know that a device was disconnected
this.sendToAppsByDeviceSocket(socket, TASKS.TASK_DEVICE_OFFLINE, [
socket._device_id,
socket.id
]);
// Let any connected devices know that an app was disconnected
this.sendToDevicesByAppSocket(socket, TASKS.TASK_APP_OFFLINE, [
socket._app_name || '',
socket.id
]);
}
// Enables a device for listening
// Data can be a string id for "anonymous" devices, or an object with metadata
async onDeviceConnected( socket, data, res ){
let id = data;
if( typeof data === "object" )
id = data.id;
else
data = {};
try{
id = this.formatDeviceID(id);
}catch(err){
res({error : "Invalid ID"});
return this.debug(err);
}
socket.leaveAll();
// Yeet any existing devices in this room, there can be only one!
try{
const sockets = await this.getSocketsInRoom(Server.deviceSelfRoom(id));
if( sockets )
sockets.map(socket => socket.disconnect());
}catch(err){
this.debug(err);
return;
}
const selfRoom = Server.deviceSelfRoom(id);
socket.join(selfRoom);
socket._device_id = id;
socket._device_info = new DeviceInfo(data);
// Tell any apps subscribed to this id that a device has come online
this.sendToAppsByDeviceSocket(socket, TASKS.TASK_DEVICE_ONLINE, [id, socket.id, socket._device_info.export()]);
this.debug("device", id, "is now listening");
try{
let apps = await Server.getAppsControllingDevice(socket);
for( let app of apps )
this.sendToSocket(socket, TASKS.TASK_ADD_APP, [app._app_name || '', app.id]);
}
catch(err){
console.error("Unable to get apps controlling device", err);
}
}
// Returns all sockets in a room. Returns a PROMISE, or false if the room is invalid
getSocketsInRoom( room ){
if( typeof room !== "string" )
return false;
return new Promise((res, rej) => {
io.in(room).clients((err, ids) => {
if( err ){
rej(err);
return;
}
const out = [];
ids.forEach(id => {
out.push(io.sockets.sockets[id]);
});
res(out);
});
});
}
// Adds one or more devices to an app connection
onAppHookup( socket, ids, res ){
if( !Array.isArray(ids) )
ids = [ids];
if( !Array.isArray(socket._devices) )
socket._devices = [];
for( let id of ids ){
try{
id = this.formatDeviceID(id);
}catch(err){
continue;
}
// Add the device if not found
let pos = socket._devices.indexOf(id);
if( pos === -1 ){
socket._devices.push(id);
// Tell any connected devices with this id that an app is now connected to it
this.sendToRoom(Server.deviceSelfRoom(id), TASKS.TASK_ADD_APP, [
socket._app_name || '',
socket.id
]);
// Join the app room for the device
const appRoom = Server.deviceAppRoom(id);
socket.join(appRoom);
// Get any devices actively connected with this id and raise connection events to the app
// There should only be one, but might as well
this.getSocketsInRoom(Server.deviceSelfRoom(id))
.then(sockets => {
for( let s of sockets ){
let dinfo = new DeviceInfo();
if( s._device_info )
dinfo = s._device_info;
this.sendToSocket(socket, TASKS.TASK_DEVICE_ONLINE, [
id, s.id, dinfo.export()
]);
}
})
.catch(err => this.debug(err));
}
}
res(socket._devices);
}
// Removes a device from an app connection
onAppHookdown( socket, ids, res ){
if( !Array.isArray(socket._devices) )
socket._devices = [];
// Delete all
if( Array.isArray(ids) && !ids.length )
ids = socket._devices;
if( !Array.isArray(ids) )
ids = [ids];
for( let id of ids ){
try{
id = this.formatDeviceID(id);
}catch(err){
continue;
}
let pos = socket._devices.indexOf(id);
if( ~pos ){
socket._devices.splice(pos, 1);
// Tell any connected devices with this id that the app has disconnected
this.sendToRoom(Server.deviceSelfRoom(id), TASKS.TASK_APP_OFFLINE, [
socket._app_name || '',
socket.id
]);
}
}
res(socket._devices);
}
onAppName( socket, name, res ){
if( typeof name !== "string" )
return;
name = name.substr(0, 128);
socket._app_name = name;
// Tell any connected devices that we changed name
this.sendToDevicesByAppSocket(socket, TASKS.TASK_ADD_APP, [
socket._app_name || '',
socket.id
]);
res(true);
}
// DATA
// Forwards a hex string to the device
onSocketPWM( socket, hex, task ){
this.debug("Hex received ", hex);
task = task || TASKS.TASK_PWM;
if(
typeof hex !== "string" || // Hex is not a string
hex.length < 4 || // Hex needs to contain at least 2 bytes
hex.length%2 || // Hex is not a multiple of 2
!hex.match(/-?[0-9a-fA-F]+/) || // Hex is not hexadecimal
!Array.isArray(socket._devices) // The socket is not an app
)return;
let index = parseInt(hex.substr(0, 2), 16);
let device = socket._devices[index];
if( !device )
return;
this.sendToRoom(
Server.deviceSelfRoom(device),
task,
hex.substr(2).toLowerCase()
);
}
// Program request via socket, works the same as GET
onSocketProgram( socket, program ){
if( !Array.isArray(program) )
return;
this.sendToRoom(Server.deviceSelfRoom(), type, data);
}
// handles a REST task
async handleGet( id, data, type ){
if( typeof id !== "string" )
throw "Request device ID invalid. Use id=deviceID in GET request.";
if( !Array.isArray(data) && typeof data !== "string" )
throw "Request data invalid. Should be a JSON array. Use data=[...] in GET request.";
if( typeof type !== "string" )
throw "Request type invalid. Use type=requestType in GET request.";
if( !Array.isArray(data) )
data = JSON.parse(data);
// ID always required
id = this.formatDeviceID(id);
if( type === Tasks.TASK_VIB ){
if( !Array.isArray(data) )
throw "Data must be an array.";
this.sendToRoom(Server.deviceSelfRoom(id), type, data);
}
else if( type === Tasks.TASK_WHOIS ){
const sockets = await this.getSocketsInRoom(Server.deviceSelfRoom(id));
let data = new DeviceInfo();
if( sockets.length && sockets[0]._device_info )
data = sockets[0]._device_info;
return data.export();
}
else
throw 'Unknown call type.';
}
// GET Request received from webserver
async onGet( req, res ){
let out = {
status : 400,
message : 'OK',
};
try{
let message = await this.handleGet(
req.query.id,
req.query.data,
req.query.type
);
if( message )
out.message = message;
out.status = 200;
}
catch(err){
out.message = {error : err};
if( typeof err === "object" && err.message )
out.message = err.message;
}
res.status(out.status);
res.json({
message : out.message,
success : out.status === 200
});
}
// Custom data to a device by id
onCustomToDevice( socket, data ){
if( !Array.isArray(data) || !Array.isArray(socket._devices) )
return;
// This app is not connected to the device
let id = data.shift();
try{
id = this.formatDeviceID(id);
}catch(err){
return this.debug(err);
}
if( socket._devices.indexOf(id) === -1 )
return;
// Ok now we can send it
this.sendToRoom(Server.deviceSelfRoom(id), TASKS.TASK_CUSTOM_TO_DEVICE, [
data.shift(),
socket._app_name || '',
socket.id
]);
}
// Sends custom data to app if the device this is sent from is connected to it
onCustomToApp( socket, data ){
// Data malformed or socket is not a device
if( !Array.isArray(data) || !socket._device_id )
return;
// Device ID is case sensitive, device is not
let id = data.shift(),
output = data.shift()
;
if( typeof id !== "string" )
return;
let app = io.sockets.sockets[id];
if( !app )
return;
// This device is not connected to the app
if( !Array.isArray(app._devices) || app._devices.indexOf(socket._device_id) === -1 )
return;
// All clear
this.sendToSocket(app, TASKS.TASK_CUSTOM_TO_APP, [
socket._device_id,
socket.id,
output
]);
}
// Send message to device
sendToRoom( id, type, data ){
io.to(id).emit(type, data);
this.debug("Emitting ", type, "to", id, "with data",data);
}
sendToSocket( socket, type, data ){
socket.emit(type, data);
this.debug("Emitting ", type, "to a direct socket with data",data);
}
// Sends message to all devices connected to an app socket
sendToDevicesByAppSocket( socket, type, data ){
if( !Array.isArray(socket._devices) )
return;
for( let device of socket._devices )
this.sendToRoom(
Server.deviceSelfRoom(device),
type,
data
);
}
sendToAppsByDeviceSocket( socket, type, data ){
if( socket._device_id )
this.sendToRoom(
Server.deviceAppRoom(socket._device_id),
type,
data
);
}
debug(){
if( !this.allowDebug )
return;
console.log.apply(this, arguments);
}
// STATIC
// Converts an ID into a device target room. This room is only joined by the device itself.
static deviceSelfRoom( id ){
return id+"_d";
}
// Converts an ID to an app target room. This room is joined by apps connecting to device id
static deviceAppRoom( id ){
return id+"_a";
}
// Converts an app name to a room for that app
static appSelfRoom( name ){
return name+"_as";
}
// Returns a promise resolving to sockets currently controlling a device id
static getAppsControllingDevice( socket ){
let devid = socket._device_id;
if( !devid )
return [];
let out = [];
let ns = io.of("/");
// make sure this socket is actually connected to that app
return new Promise((res, rej) => {
io.in(Server.deviceAppRoom(devid)).clients((err, clients) => {
if( err )
return rej(err);
for( let s of clients ){
let socket = ns.connected[s];
if( socket )
out.push(socket);
}
res(out);
});
});
}
}
module.exports = Server;