-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
608 lines (540 loc) · 18.1 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
var device_count = 0;
var devices = null;
var program_count = 0;
var programs = null;
var frameSent = 0;
// CONSTANT definitions
var MAX_DEVICES = 10;
var MAX_PROGRAMS = 4;
var DEVICE_TYPES = Object.freeze({
TEMPERATURE: "Temperature",
ILLUMINATIOn: "Illumination",
CO2: "Co2",
SWITCH: "Switch",
CONTACT: "Contact",
KEYCARD_SWITCH: "KeyCardSwitch",
OCCUPANCY: "Occupancy",
ON_OFF_ACTUATOR: "OnOffActuator",
SMART_PLUG: "SmartPlug",
COLORLIGHT: "ColorLight",
DOMICUBE: "DomiCube"
});
var PICTOGRAM_IDS = Object.freeze({
// common
CONNECTION: 'connection',
DISCONNECTION: 'disconnection',
READ: 'read',
WRITE: 'write',
USER: 'user',
// temperature
TEMPERATURE_TYPE: 'temperature_type',
// switch
SWITCH_TYPE: 'switch_type',
SWITCH_STATE_1: 'switch_state_1',
SWITCH_STATE_3: 'switch_state_3',
SWITCH_STATE_5: 'switch_state_5',
SWITCH_STATE_7: 'switch_state_7',
// contact
CONTACT_TYPE: 'contact_type',
CONTACT_STATE_ON: 'contact_state_on',
CONTACT_STATE_OFF: 'contact_state_off',
// keycard
KEYCARDSWITCH_TYPE: 'keycardswitch_type',
KEYCARDSWITCH_STATE_IN: 'keycardswitch_state_in',
KEYCARDSWITCH_STATE_OUT: 'keycardswitch_state_out',
// colorlight
COLORLIGHT_TYPE: 'colorlight_type',
COLORLIGHT_STATE_ON: 'colorlight_state_on',
COLORLIGHT_STATE_OFF: 'colorlight_state_off'
});
var DEVICE_STATUS = Object.freeze({
DISCONNECTED: 0,
PROBLEM: 1,
CONNECTED: 2
});
var EVENT_TYPES = Object.freeze({
APPEAR: "appear",
CONNECTION: "connection",
UPDATE: "update",
DISCONNECTION: "disconnection",
DISAPPEAR: "disappear"
});
var CAUSALITY_TYPE = Object.freeze({
TECHNICAL: "technical",
ENVIRONMENTAL: "environmental",
USER: "user",
PROGRAM: "program"
});
var PROGRAM_STATE = Object.freeze({
INVALID: "invalid",
DISABLED: "disabled",
ENABLED: "enabled"
});
var MIN_WAIT = 1000;
var REPLAY_SPEED = 1;
// helpers
function not(thing) {
return !thing;
}
// requirements
var fs = require('fs');
var _ = require('lodash');
var docopt = require('docopt').docopt;
var colors = require('colors');
colors.setTheme({
timestamp: 'yellow',
info: 'blue',
error: 'red'
});
// parse arguments
var doc = '' +
'Usage:\n' +
' server.js [options]\n\n' +
'Options:\n' +
' --data=<filename> read data from a file\n' +
' --socket=<socket> select socket implementation [default: ws]\n' +
' --speed=<speed> change output speed [default: 1]\n' +
' --port=<port> set socket port [default: 3000]\n' +
' --stream=<stream> enable or disable live streaming [default: true]\n' +
' --dump=<filename> dump output to filename\n' +
' --help show this help and exit\n' +
' --version show version and exit\n';
var options = docopt(doc, {version: '0.3'});
REPLAY_SPEED = Math.max(options['--speed'], 1);
// hand check data
var hand_check_data = "hello world!"
process.stdout.write('\nStart server\n');
// define socket implementation
var io = null;
if (options['--socket'] == 'socket.io') {
process.stdout.write('\nUsing Socket.io implementation for WebSocket.\n'.underline);
io = require('socket.io').listen(options['--port']).sockets;
_.extend(io, {
broadcast: function (data) {
this.send(data);
}
});
} else {
process.stdout.write('\nUsing WS implementation for WebSocket.\n'.underline);
var WebSocketServer = require('ws').Server;
io = new WebSocketServer({host: "localhost", port: options['--port']});
_.extend(io, {
broadcast: function (data) {
for (var i in this.clients) {
this.clients[i].send(data);
}
}
});
}
// define writer if required
var writer = null;
if (options['--dump']) {
writer = fs.createWriteStream(options['--dump']);
writer.write('[');
}
/**
* Generate a decoration.
*
* @param order Order in which this decoration should be processed
* @param type Type of decoration (i.e. pictogram id)
* @param causality
* @param description
* @returns {*} A decoration
*/
decoration_factory = function (order, type, causality, description) {
return {
order: order,
type: type,
causality: causality || null,
description: description || "No description"
}
};
/**
* Generate device frame data.
*
* @param id
* @param type
* @param last
* @returns {*}
*/
device_factory = function (time, id, type, last) {
var minutes = new Date(time).getMinutes();
var decorationCount = 0;
// is this an initialization ?
var initialization = _.isUndefined(last);
// don't do any change 80% of the time
if (not(initialization) && Math.random() > 0.1) {
return last;
}
// define `type` if not defined
type = type || _.sample([
DEVICE_TYPES.TEMPERATURE,
DEVICE_TYPES.SWITCH,
DEVICE_TYPES.CONTACT,
DEVICE_TYPES.COLORLIGHT,
DEVICE_TYPES.KEYCARD_SWITCH
]);
// define `last` if not defined
last = last || {
id: id,
name: "" + type + "" + id,
type: type,
event: {
type: EVENT_TYPES.APPEAR
},
decorations: []
};
// prepare next event
var next = {
id: id,
name: "" + type + "" + id,
type: type,
event: last.event || {},
decorations: []
};
if (last.event.type === EVENT_TYPES.APPEAR && not(initialization)) {
// mark it as CONNECTION if previous event was APPEAR
next.event.type = EVENT_TYPES.CONNECTION;
} else if (last.event.type === EVENT_TYPES.CONNECTION && not(initialization)) {
// mark it as UPDATE if previous event was CONNECTION
next.event.type = EVENT_TYPES.UPDATE;
} else if (Math.random() > 0.98) {
// else make it appear/disappear 2% of the time
if (last.event.type === EVENT_TYPES.UPDATE) {
next.event.type = _.sample([EVENT_TYPES.DISAPPEAR, EVENT_TYPES.APPEAR]);
} else {
next.event.type = (last.event.type === EVENT_TYPES.APPEAR ? EVENT_TYPES.DISAPPEAR : EVENT_TYPES.APPEAR);
}
}
// return in case the device appears or disappears
if (next.event.type === EVENT_TYPES.DISAPPEAR) {
if (last.event.type === EVENT_TYPES.DISAPPEAR) {
// if last was already gone then return last
return last;
} else {
// else it is a new state, so return it
return next;
}
} else if (next.event.type === EVENT_TYPES.APPEAR) {
if (last.event.type === EVENT_TYPES.APPEAR) {
// if last was already in that state then return last
return last;
} else {
// else it is a new state, so return it
return next;
}
}
// update status 5% of the time or if last event's state is empty
if (Math.random() > 0.95 || _.isEmpty(last.event.state)) {
// here we don't handle the status "PROBLEM" but only CONNECTED or DISCONNECTED
if (next.event.type === EVENT_TYPES.CONNECTION) {
// if this is a connection then setup the status as connected
// we end up in this case when last was an APPEAR event.
next.event.state = {
status: DEVICE_STATUS.CONNECTED
};
} else if (last.event.state && last.event.state.status) {
next.event.state.status = (last.event.state.status === DEVICE_STATUS.DISCONNECTED ? DEVICE_STATUS.CONNECTED : DEVICE_STATUS.DISCONNECTED);
} else {
next.event.state.status = _.sample([DEVICE_STATUS.DISCONNECTED, DEVICE_STATUS.CONNECTED]);
}
// add decoration
next.decorations.push(
decoration_factory(
decorationCount++,
next.event.state.status === DEVICE_STATUS.DISCONNECTED ? PICTOGRAM_IDS.DISCONNECTION : PICTOGRAM_IDS.CONNECTION,
null,
null
)
);
// set event type
next.event.type = next.event.state.status === DEVICE_STATUS.DISCONNECTED ? EVENT_TYPES.DISCONNECTION : EVENT_TYPES.CONNECTION;
} else if (last.event.type === EVENT_TYPES.DISCONNECTION) {
// return last in case last was a disconnection since it has not change
return last;
}
// perform the update
switch (type) {
case DEVICE_TYPES.TEMPERATURE:
// update state
if (next.event.type === EVENT_TYPES.UPDATE) {
var last_temperature = last.event.state && last.event.state.value || 20;
next.event.state.value = Math.random() > 0.8 ? last_temperature : last_temperature + Math.random() - 0.5;
} else {
next.event.state.value = 0;
}
// set event pictogram
next.event.picto = PICTOGRAM_IDS.TEMPERATURE_TYPE;
break;
case DEVICE_TYPES.SWITCH:
// update state
if (next.event.type === EVENT_TYPES.UPDATE) {
next.event.state.switchNumber = _.sample([1, 3, 5, 7]);
// set event pictogram
switch (next.event.state.switchNumber) {
case 1:
next.event.picto = PICTOGRAM_IDS.SWITCH_STATE_1;
break;
case 3:
next.event.picto = PICTOGRAM_IDS.SWITCH_STATE_3;
break;
case 5:
next.event.picto = PICTOGRAM_IDS.SWITCH_STATE_5;
break;
case 7:
next.event.picto = PICTOGRAM_IDS.SWITCH_STATE_7;
break;
default:
next.event.picto = PICTOGRAM_IDS.SWITCH_TYPE;
break;
}
} else {
next.event.picto = PICTOGRAM_IDS.SWITCH_TYPE;
}
break;
case DEVICE_TYPES.CONTACT:
// update state
if (next.event.type === EVENT_TYPES.UPDATE) {
var state = next.event.state.value = _.sample(['true', 'false']);
// update event pictogram
next.event.picto = state === 'true' ? PICTOGRAM_IDS.CONTACT_STATE_ON : PICTOGRAM_IDS.CONTACT_STATE_OFF;
} else {
// update event pictogram
next.event.picto = PICTOGRAM_IDS.CONTACT_TYPE;
}
break;
case DEVICE_TYPES.KEYCARD_SWITCH:
// update state
if (next.event.type === EVENT_TYPES.UPDATE) {
var state = next.event.state.state = _.sample(['true', 'false']);
// update event pictogram
next.event.picto = state === 'true' ? PICTOGRAM_IDS.KEYCARDSWITCH_STATE_IN : PICTOGRAM_IDS.KEYCARDSWITCH_STATE_OUT;
} else {
// update event pictogram
next.event.picto = PICTOGRAM_IDS.KEYCARD_SWITCH;
}
break;
case DEVICE_TYPES.COLORLIGHT:
// update state
if (next.event.type === EVENT_TYPES.UPDATE) {
var state = next.event.state.state = _.sample(['true', 'false']);
// update event pictogram
next.event.picto = state === 'true' ? PICTOGRAM_IDS.COLORLIGHT_STATE_ON : PICTOGRAM_IDS.COLORLIGHT_STATE_OFF;
} else {
// update event pictogram
next.event.picto = PICTOGRAM_IDS.COLORLIGHT;
}
break;
}
// @todo we need a deep comparison here in case we generated the same exact state :/
// in this case we must return `last` instead of `next`.
return next;
};
/**
* Generate program frame data.
*
* @param id
* @param last
* @returns {*}
*/
program_factory = function (time, id, last) {
var minutes = new Date(time).getMinutes();
if (last !== undefined && Math.random() < 0.8) {
return last;
}
// define *type* and *last* if not defined
var setup = (last === undefined);
// define `last` if not defined
last = last || {
id: id,
name: 'Program' + id,
event: {
type: EVENT_TYPES.UPDATE,
state: {
name: PROGRAM_STATE.DISABLED
}
},
decorations: []
};
// prepare next event
var next = {
id: id,
name: 'Program' + id,
event: last.event || {},
decorations: []
};
if (last.event.type === EVENT_TYPES.APPEAR && not(initialization)) {
// mark it as UPDATE if previous event was APPEAR
next.event.type = EVENT_TYPES.UPDATE;
} else if (Math.random() > 0.98) {
// else make it appear/disappear 2% of the time
if (last.event.type === EVENT_TYPES.UPDATE) {
next.event.type = _.sample([EVENT_TYPES.DISAPPEAR, EVENT_TYPES.APPEAR]);
} else {
next.event.type = (last.event.type === EVENT_TYPES.APPEAR ? EVENT_TYPES.DISAPPEAR : EVENT_TYPES.APPEAR);
}
}
// return in case the device appears or disappears
if (next.event.type === EVENT_TYPES.DISAPPEAR) {
if (last.event.type === EVENT_TYPES.DISAPPEAR) {
// if last was already gone then return last
return last;
} else {
// else it is a new state, so return it
return next;
}
} else if (next.event.type === EVENT_TYPES.APPEAR) {
if (last.event.type === EVENT_TYPES.APPEAR) {
// if last was already in that state then return last
return last;
} else {
// else it is a new state, so return it
return next;
}
}
// update state
if (Math.random() > 0.95) {
next.event.state.name = PROGRAM_STATE.INVALID;
} else {
var change = Math.random();
if (change > 0.9) {
next.event.state.name = PROGRAM_STATE.DISABLED;
} else {
next.event.state.name = PROGRAM_STATE.ENABLED;
}
}
// @todo we need a deep comparison here
// return last if no change in state
return next;
};
/**
* Stream data
*
* @param data
*/
stream = function (data) {
if (data.length == 0) return;
// get the frame to stream
var frame = data.shift();
// update console
process.stdout.write(
('[' + frame.timestamp + '] ').timestamp +
(' ' + _.size(frame._devices) + ' device update(s), ').info +
(' ' + _.size(frame.programs) + ' program update(s)').info + '\n'
);
var trace = JSON.stringify(frame);
// stream it
io.broadcast(trace);
// dump it
if (options['--dump']) {
writer.write((frameSent < 1 ? '' : ',') + trace);
}
frameSent++;
if (data.length > 0) {
delay = (data[0].timestamp - frame.timestamp) / REPLAY_SPEED;
setTimeout(
function (data) {
stream(data)
},
delay, data
);
}
};
if (options['--data']) {
fs.readFile(options['--data'], {encoding: 'utf-8'}, function (err, data) {
if (!err) {
hand_check_data = data;
} else {
process.stderr.write(('\nUnable to parse file ' + options['--data']).error);
process.exit();
}
});
} else {
var clock = MIN_WAIT / REPLAY_SPEED;
var nextUpdate = new Date().getTime();
// stream data
var generate = function () {
// time start now
var time = new Date().getTime();
var devices_updates = {};
var programs_updates = {};
if (devices == null) {
devices = {};
for (var i = 0; i < MAX_DEVICES; i++) {
device_count++;
devices[i] = device_factory(time, i);
}
devices_updates = devices;
} else if (nextUpdate < time) {
devices = _.map(devices, function (d) {
var next = device_factory(time, d.id, d.type, d);
if (next != d) {
devices_updates[d.id] = d;
}
return next;
});
}
if (programs == null) {
programs = {};
for (i = 0; i < MAX_PROGRAMS; i++) {
program_count++;
programs[i] = program_factory(time, i);
}
programs_updates = programs;
} else if (nextUpdate < time) {
programs = _.map(programs, function (p) {
var next = program_factory(time, p.id, p);
if (next != p) {
programs_updates[p.id] = p;
}
return next;
});
}
// update console
if (_.isEmpty(devices_updates) && _.isEmpty(programs_updates)) {
process.stdout.write(
('[' + time + ']').timestamp +
' no data sent' + '\n'
);
} else {
process.stdout.write(
('[' + time + '] ').timestamp +
(' ' + _.size(devices_updates) + ' device update(s), ').info +
(' ' + _.size(programs_updates) + ' program update(s)').info + '\n'
);
}
var trace = JSON.stringify({
timestamp: time,
devices: devices_updates,
programs: programs_updates
});
// send it
io.broadcast(trace);
// dump it
if (options['--dump']) {
writer.write((frameSent < 1 ? '' : ',') + trace);
}
frameSent++;
// loop
if (nextUpdate < time && Math.random() > 0.8) {
nextUpdate = time + clock * 10 * (1 + Math.random() * 9);
} else if (nextUpdate < time) {
nextUpdate = time + clock;
}
setTimeout(generate, clock);
};
generate();
}
// say hi on connection
io.on('connection', function (socket) {
process.stdout.write("\nNew client connection.\n");
socket.send(hand_check_data);
});
// quit gracefully
process.on('SIGINT', function () {
process.stdout.write("\nGracefully shutting down (Ctrl-C)");
if (options['--dump']) {
writer.write(']');
}
process.exit();
});