This repository has been archived by the owner on May 3, 2023. It is now read-only.
/
main.js
680 lines (587 loc) · 18.7 KB
/
main.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
669
670
671
672
673
674
675
676
677
678
679
680
/*globals XDomainRequest */
'use strict';
// datasets[]
// columns[]
// id
// units
// data[]
// liveValue
// requestedValuesTimeStamp
// receivedValuesTimeStamp
require('es6-promise').polyfill();
var _ = require('lodash');
var Machina = require('machina');
var EventEmitter2 = require('eventemitter2').EventEmitter2;
var events = new EventEmitter2({
wildcard: true
});
var SensorConnectorState = Machina.Fsm.extend({
urlQueryParams: '',
urlPrefix: '',
datasets: null,
_TIME_LIMIT_IN_MS: 5000,
_LAUNCH_TIME_LIMIT_IN_MS: 20000,
_POLLING_DELAY: 500,
_COLLECTING_DELAY: 100,
_timer: null,
_hostCycler: null,
_rawQueryParams: {},
_statusIntervalId: 0,
_lastStatusTimeStamp: 0,
_datasetsById: {},
_columnsById: {},
_sessionChangedEmitted: false,
_currentSessionID: undefined,
_launchFrame: null,
initialize: function() {
var fsm = this;
this._timer = {
timerId: 0,
start: function(timeLimit) {
this._timerId = setTimeout(function() { fsm.handle('timeout'); }, timeLimit);
},
reset: function() {
this.stop();
this.start();
},
stop: function() {
clearTimeout(this._timerId);
}
};
this._hostCycler = {
get moreHosts() {
return fsm._currentHostIdx + 1 < fsm._availableHosts.length;
},
nextHost: function() {
fsm._currentHostIdx++;
fsm.urlPrefix = fsm._availableHosts[fsm._currentHostIdx];
},
reset: function() {
fsm._currentHostIdx = 0;
fsm.urlPrefix = fsm._availableHosts[fsm._currentHostIdx];
}
};
this._initializeSession();
},
initialState: 'disconnected',
states: {
disconnected: {
_onEnter: function() {
this._currentSessionID = undefined;
if (this._launchFrame !== null) {
document.body.removeChild(this._launchFrame);
this._launchFrame = null;
}
}
},
connecting: {
_onEnter: function() {
this._hostCycler.reset();
this._requestStatus();
this._timer.start(this._TIME_LIMIT_IN_MS);
},
statusReceived: function() {
this._timer.stop();
this.transition('polling');
},
statusErrored: function() {
if (this._hostCycler.moreHosts) {
this._hostCycler.nextHost();
this._requestStatus();
} else {
this._hostCycler.reset();
this._timer.stop();
this.transition('launching');
}
},
timeout: function() {
this.transition('launching');
}
},
launching: {
_onEnter: function() {
this._injectCcscFrame();
this._timer.start(this._LAUNCH_TIME_LIMIT_IN_MS);
this._requestStatus();
},
statusReceived: function() {
this._timer.stop();
this.transition('polling');
},
statusErrored: function() {
// keep polling until the launch timeout time limit passes, but poll all of the available hosts
if (this._hostCycler.moreHosts) {
this._hostCycler.nextHost();
this._requestStatus();
} else {
this._hostCycler.reset();
setTimeout(function(){
this._requestStatus();
}.bind(this), this._POLLING_DELAY);
}
},
timeout: function() {
this.transition('launchTimedOut');
}
},
polling: {
_onEnter: function() {
this._requestStatus();
},
_onExit: function() {
this._timer.stop();
},
statusReceived: function(response) {
this._timer.stop();
this._processStatus(response);
if (response.collection.isCollecting) {
this.transition('collecting');
return;
}
if (! response.collection.canControl) {
this.transition('controlDisabled');
return;
}
if (!this._hasAttachedInterface(response)) {
this.transition('interfaceMissing');
return;
}
// Schedule the next poll request
setTimeout(function() { this._requestStatus(); }.bind(this), this._POLLING_DELAY);
this._timer.start(this._TIME_LIMIT_IN_MS);
},
statusErrored: function() {
// TODO
this._timer.stop();
this.transition('errored');
},
timeout: function() {
// TODO
this.transition('errored');
}
},
collecting: {
_onEnter: function() {
events.emit('collectionStarted');
this._requestStatus();
this._timer.start(this._TIME_LIMIT_IN_MS);
},
_onExit: function() {
events.emit('collectionStopped');
},
statusReceived: function(response) {
this._timer.stop();
this._processStatus(response);
if (! response.collection.isCollecting) {
this.transition('polling');
return;
}
if (! response.collection.canControl) {
// Somehow we lost control while collecting. This _shouldn't_ ever happen...
this.transition('controlDisabled');
return;
}
if (!this._hasAttachedInterface(response)) {
this.transition('interfaceMissing');
return;
}
// Schedule the next poll request
setTimeout(function() { this._requestStatus(); }.bind(this), this._COLLECTING_DELAY);
this._timer.start(this._TIME_LIMIT_IN_MS);
},
columnData: function(colId, values, timeStamp){
var column = this._columnsById[colId];
if (timeStamp > column.receivedValuesTimeStamp) {
column.data.length = 0;
[].push.apply(column.data, values);
column.receivedValuesTimeStamp = timeStamp;
events.emit('data', colId);
}
},
statusErrored: function() {
this._timer.stop();
this.transition('errored');
},
timeout: function() {
this.transition('errored');
}
},
controlDisabled: {
_onEnter: function() {
events.emit('controlDisabled');
this._requestStatus();
this._timer.start(this._TIME_LIMIT_IN_MS);
},
_onExit: function() {
events.emit('controlEnabled');
},
statusReceived: function(response) {
this._timer.stop();
this._processStatus(response);
if (response.collection.canControl) {
this.transition('polling');
return;
}
if (!this._hasAttachedInterface(response)) {
this.transition('interfaceMissing');
return;
}
// Schedule the next poll request
setTimeout(function() { this._requestStatus(); }.bind(this), this._POLLING_DELAY);
this._timer.start(this._TIME_LIMIT_IN_MS);
},
statusErrored: function() {
this._timer.stop();
this.transition('errored');
},
timeout: function() {
this.transition('errored');
}
},
interfaceMissing: {
_onEnter: function() {
events.emit('interfaceRemoved');
this._requestStatus();
this._timer.start(this._TIME_LIMIT_IN_MS);
},
_onExit: function() {
events.emit('interfaceConnected');
},
statusReceived: function(response) {
this._timer.stop();
this._processStatus(response);
if (this._hasAttachedInterface(response)) {
if (response.collection.canControl) {
this.transition('polling');
} else {
this.transition('controlDisabled');
}
return;
}
// Schedule the next poll request
setTimeout(function() { this._requestStatus(); }.bind(this), this._POLLING_DELAY);
this._timer.start(this._TIME_LIMIT_IN_MS);
},
statusErrored: function() {
this._timer.stop();
this.transition('errored');
},
timeout: function() {
this.transition('errored');
}
},
errored: {
_onEnter: function() {
events.emit('statusErrored');
}
},
launchTimedOut: {
_onEnter: function() {
events.emit('launchTimedOut');
}
},
unsupported: {
_onEnter: function() {
events.emit('statusErrored'); // FIXME
}
}
},
setHosts: function(hosts) {
this._availableHosts = _.isArray(hosts) ? hosts : [hosts];
this._hostCycler.reset();
},
getParam: function(param) {
return this._rawQueryParams[param];
},
setParam: function(param, value) {
this._setRawQueryParams(param, value);
this._updateQueryParams();
},
_setRawQueryParams: function(k, v) {
if (v == null) {
delete this._rawQueryParams[k];
} else {
this._rawQueryParams[k] = v;
}
},
_updateQueryParams: function() {
var v;
Object.keys(this._rawQueryParams).forEach(function(k,i) {
v = this._rawQueryParams[k];
if (i === 0) {
this.urlQueryParams = '?'+k+'='+v;
} else {
this.urlQueryParams += '&'+k+'='+v;
}
}.bind(this));
},
_requestStatus: function() {
var xhr = this._createCORSRequest('GET', '/status'),
fsm = this;
// TODO set xhr timeout
if (!xhr) {
this.transition('unsupported');
return;
}
xhr.onerror = function() {
fsm.handle('statusErrored');
};
xhr.onload = function() {
var response = this.response || JSON.parse(this.responseText); // jshint ignore:line
if (typeof(response) === "string") { response = JSON.parse(response); }
fsm.handle('statusReceived', response);
};
xhr.send();
},
_hasAttachedInterface: function(response) {
const currentInterface = response && response.currentInterface;
return (currentInterface != null) && (currentInterface !== "None Found");
},
_initializeSession: function() {
this.datasets = [];
this._datasetsById = Object.create(null);
this._columnsById = Object.create(null);
this._sessionChangedEmitted = false;
},
// Return false to abort further processing.
_processStatus: function(response) {
if (response.requestTimeStamp < this._lastStatusTimeStamp) {
// stale out-of-order response; drop it like we never got it.
return false;
}
this._lastStatusTimeStamp = response.requestTimeStamp;
if ( ! this._currentSessionID ) {
this._currentSessionID = response.sessionID;
this._initializeSession();
} else if (this._currentSessionID !== response.sessionID) {
// Session ID changed on us unexpectedly. Client should probably stop polling, start polling.
if ( ! this._sessionChangedEmitted) {
events.emit('sessionChanged');
this._sessionChangedEmitted = true;
}
this._currentSessionID = response.sessionID;
}
else {
// reset flag after we've returned to the same session
this._sessionChangedEmitted = false;
}
this._processDatasets(response.sets);
this._processColumns(response.columns);
// TODO liveValue
this.inControl = response.collection.inControl;
events.emit('statusReceived');
return true;
},
// Handle 'datasets' and 'columns' in the response
_processDatasets: function(sets) {
Object.keys(sets).forEach(function(setId) {
if ( ! this._datasetsById[setId] ) {
// mind, no datasetAdded is emitted until the second collection because the first
// dataset always exists
events.emit('datasetAdded', setId);
this._datasetsById[setId] = {
columns: [],
id: setId
};
this.datasets.unshift(this._datasetsById[setId]);
}
// Set the columns array length so that it's the correct size if a column was removed
this._datasetsById[setId].columns.length = sets[setId].colIDs.length;
}.bind(this));
// make sure the highest-numbered dataset is always datasets[0]
this.datasets.sort(function(d1, d2) { return d2.setId-d1.setId; });
},
_processColumns: function(cols) {
// looks familiar
var eventsToEmit = [];
Object.keys(cols).forEach(function(colId) {
var columnFromResponse = cols[colId];
var dataset = this._datasetsById[columnFromResponse.setID];
var column = this._columnsById[colId];
if ( ! column ) {
eventsToEmit.push(['columnAdded',colId]);
// Remember, the column information can change
// HOWEVER, assume a column is never removed from one dataset and added to another
column = this._columnsById[colId] = {
id: null,
name: null,
units: null,
receivedValuesTimeStamp: 0,
requestedValuesTimeStamp: 0,
liveValueTimeStamp: 0,
liveValue: null,
data: []
};
} else if (column !== dataset.columns[columnFromResponse.position]) {
eventsToEmit.push(['columnMoved',colId]);
}
dataset.columns[columnFromResponse.position] = column;
if (column.units !== null && column.units !== columnFromResponse.units) {
eventsToEmit.push(['columnTypeChanged',colId]);
}
column.units = columnFromResponse.units;
column.name = columnFromResponse.name;
column.id = colId;
column.liveValue = parseFloat(columnFromResponse.liveValue || 0);
column.liveValueTimeStamp = columnFromResponse.liveValueTimeStamp;
if (column.requestedValuesTimeStamp < columnFromResponse.valuesTimeStamp) {
this._requestData(colId, columnFromResponse.valuesTimeStamp);
column.requestedValuesTimeStamp = columnFromResponse.valuesTimeStamp;
}
}.bind(this));
// Find columns that were removed.
Object.keys(this._columnsById).forEach(function(colId) {
if ( ! cols[colId] ) {
eventsToEmit.push(['columnRemoved', colId]);
delete this._columnsById[colId];
}
}.bind(this));
eventsToEmit.forEach(function(arr) {
events.emit(arr[0], arr[1]);
});
},
// Request data if status indicates there's more data
_requestData: function(colId, timeStamp) {
var xhr = this._createCORSRequest('GET', '/columns/' + colId),
fsm = this;
// look, we wouldn't have got here if we didn't support CORS
xhr.send();
xhr.onload = function() {
var response = this.response || JSON.parse(this.responseText);
if (typeof(response) === "string") { response = JSON.parse(response); }
var values = response.values;
fsm.handle('columnData', colId, values, timeStamp);
};
},
// see http://www.html5rocks.com/en/tutorials/cors/
_createCORSRequest: function(method, relativeUrl) {
// ignore requests before we've connected
if (!this.urlPrefix) return null;
var url = this.urlPrefix + relativeUrl + this.urlQueryParams;
var xhr = new XMLHttpRequest();
if ('withCredentials' in xhr) {
xhr.open(method, url, true);
xhr.responseType = 'json';
xhr.setRequestHeader('Accept', 'application/json');
} else if (typeof XDomainRequest !== 'undefined') {
// IE8/9's XMLHttpRequest object doesn't support CORS; instead, you have to use an
// 'XDomainRequest' object
xhr = new XDomainRequest();
// we can't set custom headers in IE9
// see http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx
xhr.open(method, url);
} else {
return null;
}
return xhr;
},
promisifyRequest: function(url) {
var fsm = this;
return new Promise(function(resolve, reject) {
var xhr = fsm._createCORSRequest('GET', url);
if ( ! xhr ) {
reject(new Error("Must connect to SensorConnector first."));
}
xhr.send();
// Simply emitting errors isn't quite right because there's no way for the consumer
// to tie the error to the particular start request
xhr.onerror = function() {
reject(this);
};
xhr.onload = resolve;
});
},
_injectCcscFrame: function() {
if (this._launchFrame !== null) {
document.body.removeChild(this._launchFrame);
}
var obj = document.createElement('div');
obj.id = 'sensor-connector-launch-frame-parent';
obj.style.visibility = 'hidden';
obj.innerHTML = '<iframe id="sensor-connector-launch-frame" src="ccsc://sensorconnector.concord.org/"></iframe>';
document.body.appendChild(obj);
this._launchFrame = document.getElementById('sensor-connector-launch-frame-parent');
}
});
/**
* @constructor
*/
var SensorConnectorInterface = function(){
return {
stateMachine: new SensorConnectorState(),
startPolling: function(addresses, clientId, clientName) {
this.stateMachine.setHosts(addresses);
this.stateMachine.setParam('client', clientId);
this.stateMachine.setParam('clientName', clientName);
this.stateMachine.transition('connecting');
},
stopPolling: function() {
this.stateMachine.transition('disconnected');
},
requestStart: function() { return this.stateMachine.promisifyRequest('/control/start'); },
requestStop: function() { return this.stateMachine.promisifyRequest('/control/stop'); },
requestExit: function() {
return this.stateMachine.promisifyRequest('/exit');
},
// Returns true if the SensorConnector is already running,
// false if launch was actually required/attempted (in which
// case client may need to delay further communication for a bit).
requestLaunch: function() {
if (this.isConnected) {
// already running/connected
return true;
}
else {
// attempt to launch
this.stateMachine.transition('launching');
return false;
}
},
on: function() {
events.on.apply(events, arguments);
},
off: function() {
if (arguments.length)
events.off.apply(events, arguments);
else
events.removeAllListeners();
},
get clientId() {
return this.stateMachine.getParam('client');
},
set clientId(id) {
this.stateMachine.setParam('client', id);
},
get clientName() {
return this.stateMachine.getParam('clientName');
},
set clientName(name) {
this.stateMachine.setParam('clientName', name);
},
get hasAttachedInterface() {
return this.stateMachine.state !== 'interfaceMissing';
},
get datasets() {
return this.stateMachine.datasets;
},
get currentActionArgs() {
return this.stateMachine.currentActionArgs;
},
get isConnected() {
return ['polling','collecting','controlDisabled','interfaceMissing'].indexOf(this.stateMachine.state) !== -1;
},
get isCollecting() {
return this.stateMachine.state === 'collecting';
},
get inControl() {
return this.stateMachine.state !== 'controlDisabled';
},
get canControl() {
return this.stateMachine.state !== 'controlDisabled';
},
get launchTimedOut() {
return this.stateMachine.state === 'launchTimedOut';
}
};
}
module.exports = SensorConnectorInterface;