forked from rwaldron/johnny-five
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor.js
1605 lines (1355 loc) · 59.6 KB
/
sensor.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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require("./common/bootstrap");
function getShape(sensor) {
return {
id: sensor.id,
custom: sensor.custom,
mode: sensor.mode,
freq: sensor.freq,
range: sensor.range,
limit: sensor.limit,
threshold: sensor.threshold,
isScaled: sensor.isScaled,
pin: sensor.pin,
state: {
enabled: sensor.state.enabled,
booleanBarrier: sensor.state.booleanBarrier,
scale: sensor.state.scale,
value: sensor.state.value,
freq: sensor.state.freq,
}
};
}
exports["Sensor - Analog"] = {
setUp: function(done) {
this.sandbox = sinon.sandbox.create();
this.board = newBoard();
this.clock = this.sandbox.useFakeTimers();
this.analogRead = this.sandbox.spy(MockFirmata.prototype, "analogRead");
this.sensor = new Sensor({
pin: "A1",
board: this.board
});
// Complete visible property information expected for the above sensor instance,
// excluding the 'external' references for the board and io properties.
this.defShape = {
id: this.sensor.id,
custom: {},
mode: this.sensor.io.MODES.ANALOG,
freq: 25,
range: [0, 1023],
limit: null,
threshold: 1,
isScaled: false,
pin: 1,
state: {
enabled: true,
booleanBarrier: null,
scale: null,
value: 0, // Starts at null, but gets updated before first checks
freq: 25
}
};
// Methods expected to be found on the prototype for sensor instances
this.methods = [
"booleanAt",
"constructor",
"enable",
"disable",
"scale",
"scaleTo",
"fscaleTo",
"within",
];
// All properties expected to be found (directly) on any sensor instance
this.members = {
id: {
type: "string"
},
custom: {
type: "object"
},
pin: {
type: "number"
},
mode: {
type: "number"
},
freq: {
type: "number"
},
range: {
type: "object"
},
threshold: {
type: "number"
},
isScaled: {
type: "boolean"
},
raw: {
type: "object"
}, // defined property that returns var inited to null
analog: {
type: "number"
}, // defined property
constrained: {
type: "number"
}, // defined property
boolean: {
type: "boolean"
}, // defined property always true or false
scaled: {
type: "number"
}, // defined property
value: {
type: "object"
}, // defined property
state: {
type: "object"
}, // defined (for test mode) property
board: {
type: "object"
},
io: {
type: "object"
},
limit: {
type: "object"
} // null initial value
};
done();
}, // ./setUp: function(done)
tearDown: function(done) {
Board.purge();
this.sandbox.restore();
done();
}, // ./tearDown: function(done)
shape: function(test) {
var propsActual, propsExpected, methodsActual;
propsActual = Object.getOwnPropertyNames(this.sensor);
propsExpected = Object.getOwnPropertyNames(this.members);
methodsActual = Object.getOwnPropertyNames(Object.getPrototypeOf(this.sensor));
test.expect(3 + 3 * (this.methods.length + propsExpected.length));
// Verify that all of the expected prototype functions and properties exist for the instance
this.methods.forEach(function(proto) {
test.ok(methodsActual.includes(proto), "missing '" + proto + "' sensor prototype method");
}, this);
propsExpected.forEach(function(property) {
test.ok(propsActual.includes(property), "missing '" + property + "' sensor instance property");
}, this);
// Make sure that all of the existing instance properties and prototype methods are actually expected, and the correct datatype
propsActual.forEach(function(property) {
test.ok(propsExpected.includes(property), "found unexpected '" + property + "' sensor instance member");
test.ok(propsExpected.includes(property) && typeof this.sensor[property] === this.members[property].type,
"Unexpected datatype '" + typeof this.sensor[property] + "' found for '" + property + "' property");
}, this);
methodsActual.forEach(function(proto) {
test.ok(this.methods.includes(proto), "found unexpected '" + proto + "' sensor prototype method");
test.strictEqual(typeof this.sensor[proto], "function", "Unexected datatype found for '" + proto + "' method");
}, this);
// Check that the 'standard' component properties reference the expected objects
test.strictEqual(this.sensor.board, this.board, "Expected to be the mock board");
test.strictEqual(this.sensor.io, this.board.io, "Expected to be the same io as the mock board");
// See if the visible instance properties match the expected default values
test.deepEqual(getShape(this.sensor), this.defShape, "sensor instance properties should match default shape values");
test.done();
}, // ./shape: function(test)
emitter: function(test) {
test.expect(1);
test.ok(this.sensor instanceof Emitter);
test.done();
}, // ./emitter: function(test)
data: function(test) {
var tickAccum, tickDelta, spy = this.sandbox.spy();
test.expect(4);
// Make sure that no event is emitted before the end of the initial interval is reached
this.sensor.on("data", spy);
tickAccum = 0;
tickDelta = this.defShape.freq - 1;
this.clock.tick(tickDelta);
// accumulated (elapsed) time is one tick (ms) before the end of the first interval
tickAccum += tickDelta;
test.ok(!spy.called, "tick " + tickAccum + ": data event handler should not be called until tick " + this.defShape.freq);
// Make sure that an event is emitted when the initial interval ends
tickDelta = 1;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the first (freq) interval
tickAccum += tickDelta;
test.ok(spy.calledOnce, "tick " + tickAccum + ": data event handler should have been called first time at tick " + this.defShape.freq);
// Make sure no additional event is emitted before the end of the next interval
tickDelta = this.defShape.freq - 1;
this.clock.tick(tickDelta);
// elapsed time is now one tick before the end of the second interval
tickAccum += tickDelta;
test.ok(spy.calledOnce, "tick" + tickAccum + ": data event handler should not be called again until tick " + (this.defShape.freq * 2));
// Make sure the next event is emitted at the end of the second interval
tickDelta = 1;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the second interval
tickAccum += tickDelta;
test.ok(spy.calledTwice, "tick " + tickAccum + ": data event handler should be called second time at tick " + (this.defShape.freq * 2));
test.done();
}, // ./data: function(test)
filtered: function(test) {
var callback = this.analogRead.args[0][1],
dataSpy = this.sandbox.spy(),
chgSpy = this.sandbox.spy(),
tickDelta, tickAccum, spyCall, raw, filtered;
test.expect(37);
this.sensor.on("data", dataSpy);
this.sensor.on("change", chgSpy);
// Check that the default noise filtering calculates the median value, while the individual reads track the raw values
tickAccum = 0;
raw = 100;
callback(raw);
test.strictEqual(this.sensor.raw, raw, "tick " + tickAccum + ": sensor raw property expected to be the last value (" + raw + ") injected");
test.strictEqual(this.sensor.value, raw, "tick " + tickAccum + ": sensor value property expected to be the last value (" + raw + ") injected");
tickDelta = 1;
this.clock.tick(tickDelta);
tickAccum += tickDelta;
raw = 102;
callback(raw);
test.strictEqual(this.sensor.raw, raw, "tick " + tickAccum + ": sensor raw property expected to be the last value (" + raw + ") injected");
test.strictEqual(this.sensor.value, raw, "tick " + tickAccum + ": sensor value property expected to be the last value (" + raw + ") injected");
this.clock.tick(tickDelta);
tickAccum += tickDelta;
raw = 101;
callback(raw);
test.strictEqual(this.sensor.raw, raw, "tick " + tickAccum + ": sensor raw property expected to be the last value (" + raw + ") injected");
test.strictEqual(this.sensor.value, raw, "tick " + tickAccum + ": sensor value property expected to be the last value (" + raw + ") injected");
this.clock.tick(tickDelta);
tickAccum += tickDelta;
raw = 103;
callback(raw);
test.strictEqual(this.sensor.raw, raw, "tick " + tickAccum + ": sensor raw property expected to be the last value (" + raw + ") injected");
test.strictEqual(this.sensor.value, raw, "tick " + tickAccum + ": sensor value property expected to be the last value (" + raw + ") injected");
tickDelta = this.defShape.freq - tickAccum - 1;
this.clock.tick(tickDelta);
// elapsed time is now 1 tick (ms) before the end of the first (freq) event throttling interval
tickAccum += tickDelta;
raw = 104;
callback(raw);
test.strictEqual(this.sensor.raw, raw, "tick " + tickAccum + ": sensor raw property expected to be the last value (" + raw + ") injected");
test.strictEqual(this.sensor.value, raw, "tick " + tickAccum + ": sensor value property expected to be the last value (" + raw + ") injected");
test.ok(!dataSpy.called, "tick " + tickAccum + ": data event handler should not be called until tick " + this.defShape.freq);
test.ok(!chgSpy.called, "tick " + tickAccum + ": change event handler should not be called until tick " + this.defShape.freq);
// Make sure that the events are emitted, with the median of the raw read values, at the end of the interval
tickDelta = 1;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the first (freq) event throttling interval
tickAccum += tickDelta;
// Median of values sent through callback since previous data event
filtered = 102;
test.ok(dataSpy.calledOnce, "tick " + tickAccum + ": data event handler should be called at tick " + this.defShape.freq);
test.ok(chgSpy.calledOnce, "tick " + tickAccum + ": change event handler should be called at tick " + this.defShape.freq);
test.strictEqual(this.sensor.raw, raw, "tick " + tickAccum + ": sensor raw property expected to be the last value (" + raw + ") injected");
test.strictEqual(this.sensor.value, raw, "tick " + tickAccum + ": sensor value property expected to be the last value (" + raw + ") injected");
// Check the arguments and context provided for the emitted events
spyCall = dataSpy.getCall(0);
test.strictEqual(spyCall.args[0], filtered, "data event value expected to be the median (" + filtered + ") value");
test.ok(spyCall.calledOn(this.sensor), "data event 'this' parameter expected to be source sensor object");
spyCall = chgSpy.getCall(0);
test.strictEqual(spyCall.args[0], filtered, "change event value expected to be the median (" + filtered + ") value");
test.ok(spyCall.calledOn(this.sensor), "change event 'this' parameter expected to be source sensor object");
// Check for non-integer median value (when even number of data points and odd delta between middle two)
tickDelta = 1;
this.clock.tick(tickDelta);
tickAccum += tickDelta;
callback(202);
this.clock.tick(tickDelta);
tickAccum += tickDelta;
callback(206);
this.clock.tick(tickDelta);
tickAccum += tickDelta;
callback(201);
tickDelta = this.defShape.freq * 2 - tickAccum - 1;
this.clock.tick(tickDelta);
// elapsed time is now 1 tick before the end of the second interval
tickAccum += tickDelta;
raw = 203;
callback(raw);
// Check that no event is emitted before the end of the next interval
test.ok(dataSpy.calledOnce, "tick " + tickAccum + ": data event handler should not be called again until tick " + this.defShape.freq * 2);
test.ok(chgSpy.calledOnce, "tick " + tickAccum + ": change event handler should not be called again until tick " + this.defShape.freq * 2);
filtered = 202.5; // Round median of values sent through callback (last === 102) (avg(102,103))
// Check that events are emitted at the end of the second interval
tickDelta = 1;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the second event throttling interval
tickAccum += tickDelta;
test.ok(dataSpy.calledTwice, "tick " + tickAccum + ": data event handler should be called again at tick " + this.defShape.freq * 2);
test.ok(chgSpy.calledTwice, "tick " + tickAccum + ": change event handler should be called again at tick " + this.defShape.freq * 2);
test.strictEqual(this.sensor.raw, raw, "sensor raw property expected to be the last value (" + raw + ") read (injected)");
test.strictEqual(this.sensor.value, raw, "sensor value property expected to be the last value (" + raw + ") read (injected)");
// Check that both events provide the correct median value and context
spyCall = dataSpy.getCall(1);
test.strictEqual(spyCall.args[0], filtered, "data event value expected to be the median (" + filtered + ") value");
test.ok(spyCall.calledOn(this.sensor), "data event 'this' parameter expected to be source sensor object");
spyCall = chgSpy.getCall(1);
test.strictEqual(spyCall.args[0], filtered, "change event value expected to be the median (" + filtered + ") value");
test.ok(spyCall.calledOn(this.sensor), "change event 'this' parameter expected to be source sensor object");
// Check that no events are emitted before the end of the next throttling interval
tickDelta = this.defShape.freq - 1;
this.clock.tick(tickDelta);
// elapsed time is now 1 tick before the end of the third interval
tickAccum += tickDelta;
test.ok(dataSpy.calledTwice, "tick " + tickAccum + ": data event handler should not be called again until tick " + this.defShape.freq * 3);
test.ok(chgSpy.calledTwice, "tick " + tickAccum + ": change event handler should not be called again until at least tick " + this.defShape.freq * 3);
// check that only the data event is emitted when no new values are read during the interval
tickDelta = 1;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the third interval
tickAccum += tickDelta;
test.ok(dataSpy.calledThrice, "tick " + tickAccum + ": data event handler should be called again at tick " + this.defShape.freq * 3);
test.ok(chgSpy.calledTwice, "tick " + tickAccum + ": change event handler should not be called at " + this.defShape.freq * 3 + " without new value");
test.strictEqual(this.sensor.raw, raw, "sensor raw property expected to be the last value (" + raw + ") read (injected)");
test.strictEqual(this.sensor.value, raw, "sensor value property expected to be the last value (" + raw + ") read (injected)");
// Check that the data event has the same filtered value as the previous event
spyCall = dataSpy.getCall(2);
test.strictEqual(spyCall.args[0], filtered, "data event value expected to be still the median (" + filtered + ") value");
test.done();
}, // ./filtered: function(test)
change: function(test) {
var callback = this.analogRead.args[0][1],
spy = this.sandbox.spy(),
tickAccum, tickDelta, chgValue;
test.expect(8);
this.sensor.on("change", spy);
// Make sure that no change event is emitted before the end of the first (freq) throttling interval
tickAccum = 0;
tickDelta = this.defShape.freq - 1;
this.clock.tick(tickDelta);
// accumulated (elapsed) time is one tick (ms) before the end of the first interval
tickAccum += tickDelta;
chgValue = 1023;
callback(chgValue);
// Reading a data value should not (immediately) cause an event to be emitted
test.ok(!spy.called, "tick " + tickAccum + ": change event handler should not be called until tick " + this.defShape.freq);
// Make sure that a change event is emitted at the end of the throttling interval
tickDelta = 1;
this.clock.tick(tickDelta);
// elapsed (fake) time is at the end of the first (event throttling) interval
tickAccum += tickDelta;
test.ok(spy.calledOnce, "tick " + tickAccum + ": change event handler should have been called first time at tick " + this.defShape.freq);
test.strictEqual(spy.getCall(0).args[0], chgValue, "first change event value expected to be " + chgValue);
// Make sure that no change event is emitted before the end of the next throttling interval
tickDelta = this.defShape.freq - 1;
this.clock.tick(tickDelta);
// elapsed time is one tick before the end of the second throttling interval
tickAccum += tickDelta;
// duplicate of previous data value
chgValue = 1023;
callback(chgValue);
test.ok(spy.calledOnce, "tick " + tickAccum + ": change event handler should not be called again until at least tick " + this.defShape.freq * 2);
// Make sure that no change event is emitted when the reading does not change
tickDelta = 1;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the second event throttling interval
tickAccum += tickDelta;
test.ok(spy.calledOnce, "tick " + tickAccum + ": change event handler should not be called without a new data value");
// Make sure that no change event is emitted before the end of the next throttling interval
tickDelta = this.defShape.freq - 1;
this.clock.tick(tickDelta);
tickAccum += tickDelta;
// different value
chgValue = 512;
callback(chgValue);
test.ok(spy.calledOnce, "tick" + tickAccum + ": change event handler should not be called again until tick " + (this.defShape.freq * 3));
// Make sure that a different (greater than threshold) value change emits a new change event
tickDelta = 1;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the third event throttling interval
tickAccum += tickDelta;
test.ok(spy.calledTwice, "tick " + tickAccum + ": change event handler should be called second time at tick " + (this.defShape.freq * 3));
test.strictEqual(spy.getCall(1).args[0], chgValue, "second change event value expected to be " + chgValue);
test.done();
}, // ./change: function(test)
// Tests to check that the thresholds are handled correctly to control when change events get emitted
threshold: function(test) {
var callback = this.analogRead.args[0][1],
spy = this.sandbox.spy(),
tickDelta, tickAccum, spyCall, raw, filtered, newShape;
test.expect(45);
this.sensor.on("change", spy);
test.strictEqual(this.sensor.threshold, 1, "Following tests assume a (default) threshold of 1");
tickAccum = 0;
tickDelta = 1;
this.clock.tick(tickDelta);
tickAccum += tickDelta;
// An initial reference value to base threshold checks against
raw = 512;
filtered = raw; // last value = null
callback(raw);
tickDelta = this.defShape.freq - 1;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the first (freq) event throttling interval
tickAccum += tickDelta;
test.strictEqual(this.sensor.raw, raw, "tick " + tickAccum +
": sensor raw property expected to be the last value (" + raw + ") injected");
test.strictEqual(this.sensor.value, raw, "tick " + tickAccum +
": sensor value property expected to be the last value (" + raw + ") injected");
// Change event should always be triggered after first interval (at tick this.defShape.freq)
test.strictEqual(spy.callCount, 1, "tick " + tickAccum +
": change event handler should be called first time at tick " + this.defShape.freq);
// Verify the arguments and context for the call to the event handler
spyCall = spy.getCall(0);
test.strictEqual(spyCall.args[0], filtered,
"change event value expected to be the median (" + filtered + ") value");
test.ok(spyCall.calledOn(this.sensor),
"change event 'this' parameter expected to be source sensor object");
// Check that no new change event is emitted when the (filtered) value changes (up) by (just) less than the threshold
tickDelta = 1;
this.clock.tick(tickDelta);
tickAccum += tickDelta;
raw = 512;
callback(raw);
tickDelta = 1;
this.clock.tick(tickDelta);
tickAccum += tickDelta;
raw = 513;
// last filtered value = 512
filtered = 512.5;
callback(raw);
tickDelta = this.defShape.freq - 2;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the second event throttling interval
tickAccum += tickDelta;
test.strictEqual(this.sensor.raw, raw, "tick " + tickAccum +
": sensor raw property expected to be the last value (" + raw + ") injected");
test.strictEqual(this.sensor.value, raw, "tick " + tickAccum +
": sensor value property expected to be the last value (" + raw + ") injected");
test.strictEqual(spy.callCount, 1, "tick " + tickAccum +
": change event handler should not be called at tick " + this.defShape.freq * 2 + "; new median within threshold");
// Check that a change event is emitted when the new filtered value is right on the threshold boundary
tickDelta = 1;
this.clock.tick(tickDelta);
tickAccum += tickDelta;
raw = 511;
// last emitted value = 512
filtered = raw;
callback(raw);
tickDelta = this.defShape.freq - 1;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the third event throttling interval
tickAccum += tickDelta;
test.strictEqual(this.sensor.raw, raw, "tick " + tickAccum +
": sensor raw property expected to be the last value (" + raw + ") injected");
test.strictEqual(this.sensor.value, raw, "tick " + tickAccum +
": sensor value property expected to be the last value (" + raw + ") injected");
test.strictEqual(spy.callCount, 2, "tick " + tickAccum +
": change event handler should be called at tick " + this.defShape.freq * 3 + "; new median on threshold boundary");
// Verify the arguments and context for the call to the event handler
spyCall = spy.getCall(1);
test.strictEqual(spyCall.args[0], filtered, "change event value expected to be the median (" + filtered + ") value");
test.ok(spyCall.calledOn(this.sensor), "change event 'this' parameter expected to be source sensor object");
// Check that no new change event is emitted when the (filtered) value changes (down) by (just) less than the threshold
tickDelta = 1;
this.clock.tick(tickDelta);
tickAccum += tickDelta;
raw = 500;
callback(raw);
tickDelta = 1;
this.clock.tick(tickDelta);
tickAccum += tickDelta;
raw = 521;
// last emitted value = 511
filtered = 510.5;
callback(raw);
tickDelta = this.defShape.freq - 2;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the fourth event throttling interval
tickAccum += tickDelta;
test.strictEqual(this.sensor.raw, raw, "tick " + tickAccum +
": sensor raw property expected to be the last value (" + raw + ") injected");
test.strictEqual(this.sensor.value, raw, "tick " + tickAccum +
": sensor value property expected to be the last value (" + raw + ") injected");
test.strictEqual(spy.callCount, 2, "tick " + tickAccum +
": change event handler should not be called at tick " + this.defShape.freq * 4);
// Check that setting a new threshold property value changes the boundaries for when change events are emitted
newShape = Fn.cloneDeep(this.defShape);
newShape.threshold = 0.5;
// Any (normal) change should trigger a change event
this.sensor.threshold = newShape.threshold;
// Setting a new threshold value should change (only) the threshold property of the instance
test.deepEqual(newShape, getShape(this.sensor), "sensor instance properties should match new shape values");
tickDelta = 1;
this.clock.tick(tickDelta);
tickAccum += tickDelta;
raw = 511;
// last emitted value = 511
filtered = raw;
callback(raw);
// Check that no new event is emitted when the filtered value does not change
tickDelta = this.defShape.freq - 1;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the fifth event throttling interval
tickAccum += tickDelta;
test.strictEqual(this.sensor.raw, raw, "tick " + tickAccum +
": sensor raw property expected to be the last value (" + raw + ") injected");
test.strictEqual(this.sensor.value, raw, "tick " + tickAccum +
": sensor value property expected to be the last value (" + raw + ") injected");
test.strictEqual(spy.callCount, 2, "tick " + tickAccum +
": change event handler should not be called at tick " + this.defShape.freq * 5 + "; new median same as last");
tickDelta = 1;
this.clock.tick(tickDelta);
tickAccum += tickDelta;
raw = 501;
callback(raw);
tickDelta = 1;
this.clock.tick(tickDelta);
tickAccum += tickDelta;
raw = 522;
// last emitted value = 511
filtered = 511.5;
callback(raw);
// Check that a change event is emitted with a filtered value on the new upper threshold boundary
tickDelta = this.defShape.freq - 2;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the sixth event throttling interval
tickAccum += tickDelta;
test.strictEqual(this.sensor.raw, raw, "tick " + tickAccum +
": sensor raw property expected to be the last value (" + raw + ") injected");
test.strictEqual(this.sensor.value, raw, "tick " + tickAccum +
": sensor value property expected to be the last value (" + raw + ") injected");
test.strictEqual(spy.callCount, 3, "tick " + tickAccum +
": change event handler should be called at tick " + this.defShape.freq * 6 + "; on threshold boundary");
// Verify the arguments and context for the call to the event handler
spyCall = spy.getCall(2);
test.strictEqual(spyCall.args[0], filtered, "change event value expected to be the median (" + filtered + ") value");
test.ok(spyCall.calledOn(this.sensor), "change event 'this' parameter expected to be source sensor object");
// Only changes of 10 or more should trigger a change event
newShape.threshold = 10;
this.sensor.threshold = newShape.threshold;
test.deepEqual(newShape, getShape(this.sensor), "sensor instance properties should match new shape values");
tickDelta = 1;
this.clock.tick(tickDelta);
tickAccum += tickDelta;
raw = 520;
callback(raw);
tickDelta = 1;
this.clock.tick(tickDelta);
tickAccum += tickDelta;
raw = 522;
// last emitted value = 511.5
filtered = 521;
callback(raw);
// Check that upward changes of (just) less than the new threshold do not emit a change event
tickDelta = this.defShape.freq - 2;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the seventh event throttling interval
tickAccum += tickDelta;
test.strictEqual(this.sensor.raw, raw, "tick " + tickAccum +
": sensor raw property expected to be the last value (" + raw + ") injected");
test.strictEqual(this.sensor.value, raw, "tick " + tickAccum +
": sensor value property expected to be the last value (" + raw + ") injected");
test.strictEqual(spy.callCount, 3, "tick " + tickAccum +
": change event handler should not be called at tick " + this.defShape.freq * 7 + "; median change less than threshold");
tickDelta = 1;
this.clock.tick(tickDelta);
tickAccum += tickDelta;
raw = 497;
callback(raw);
tickDelta = 1;
this.clock.tick(tickDelta);
tickAccum += tickDelta;
raw = 507;
// last emitted value = 511.5
filtered = 502;
callback(raw);
// Check that downward changes of (just) less than the new threshold do not emit a change event
tickDelta = this.defShape.freq - 2;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the eighth event throttling interval
tickAccum += tickDelta;
test.strictEqual(this.sensor.raw, raw, "tick " + tickAccum +
": sensor raw property expected to be the last value (" + raw + ") injected");
test.strictEqual(this.sensor.value, raw, "tick " + tickAccum +
": sensor value property expected to be the last value (" + raw + ") injected");
test.strictEqual(spy.callCount, 3, "tick " + tickAccum +
": change event handler should not be called at tick " + this.defShape.freq * 8 + " since change does not exceed threshold");
tickDelta = 1;
this.clock.tick(tickDelta);
tickAccum += tickDelta;
raw = 520;
callback(raw);
tickDelta = 1;
this.clock.tick(tickDelta);
tickAccum += tickDelta;
raw = 527;
// last emitted value = 511.5
filtered = 523.5;
callback(raw);
// Check that changes above the (upper) threshold emit a change event
tickDelta = this.defShape.freq - 2;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the ninth event throttling interval
tickAccum += tickDelta;
test.strictEqual(this.sensor.raw, raw, "tick " + tickAccum +
": sensor raw property expected to be the last value (" + raw + ") injected");
test.strictEqual(this.sensor.value, raw, "tick " + tickAccum +
": sensor value property expected to be the last value (" + raw + ") injected");
test.strictEqual(spy.callCount, 4, "tick " + tickAccum +
": change event handler should be called at tick " + this.defShape.freq * 9 + " since change threshold exceeded");
// Verify the arguments and context for the call to the event handler
spyCall = spy.getCall(3);
test.strictEqual(spyCall.args[0], filtered, "change event value expected to be the median (" + filtered + ") value");
test.ok(spyCall.calledOn(this.sensor), "change event 'this' parameter expected to be source sensor object");
// do a new read at the end of the current interval, after the event has been emitted.
raw = 515;
callback(raw);
// last emitted value = 523.5
filtered = 515;
// Check that a downward change of less than the current threshold does not emit a change event
tickDelta = this.defShape.freq;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the tenth event throttling interval
tickAccum += tickDelta;
test.strictEqual(this.sensor.raw, raw, "tick " + tickAccum +
": sensor raw property expected to be the last value (" + raw + ") injected");
test.strictEqual(this.sensor.value, raw, "tick " + tickAccum +
": sensor value property expected to be the last value (" + raw + ") injected");
test.strictEqual(spy.callCount, 4, "tick " + tickAccum +
": change event handler should not be called at tick " + this.defShape.freq * 10 + " since change does not exceed threshold");
// Changes of 5 or more should trigger a change event
newShape.threshold = 5;
this.sensor.threshold = newShape.threshold;
test.deepEqual(newShape, getShape(this.sensor), "sensor instance properties should match new shape values");
tickDelta = this.defShape.freq;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the eleventh event throttling interval
tickAccum += tickDelta;
test.strictEqual(spy.callCount, 5, "tick " + tickAccum +
": change event handler should be called at tick " + this.defShape.freq * 11 +
"; changed threshold moves existing filtered value outside range");
// Verify the arguments and context for the call to the event handler
spyCall = spy.getCall(4);
test.strictEqual(spyCall.args[0], filtered, "change event value expected to be the median (" + filtered + ") value");
test.ok(spyCall.calledOn(this.sensor), "change event 'this' parameter expected to be source sensor object");
test.done();
}, // ./threshold: function(test)
id: function(test) {
var newShape, newId;
test.expect(3);
newShape = Fn.cloneDeep(this.defShape);
newId = "test sensor id";
newShape.id = newId;
this.sensor.id = newId;
test.deepEqual(newShape, getShape(this.sensor), "sensor instance properties should match shape with new id");
newId = "1234";
this.sensor.id = newId;
test.strictEqual(this.sensor.id, newId, "id specified as string \"1234\"");
newId = 1234;
this.sensor.id = newId;
test.strictEqual(this.sensor.id, newId, "id specified as numeric 1234");
test.done();
}, // ./id: function(test)
limit: function(test) {
var callback = this.analogRead.args[0][1],
dataSpy = this.sandbox.spy(),
limitSpy = this.sandbox.spy(),
lowerSpy = this.sandbox.spy(),
upperSpy = this.sandbox.spy(),
newShape, raw, filtered, tickDelta, tickAccum, lowerLimit, upperLimit;
test.expect(46);
this.sensor.on("data", dataSpy);
this.sensor.on("limit", limitSpy);
this.sensor.on("limit:lower", lowerSpy);
this.sensor.on("limit:upper", upperSpy);
test.strictEqual(this.sensor.limit, null, "sensor limit property should default to null value");
// Check that no limit events are emitted while no limit is configured (low value)
tickAccum = 0;
tickDelta = this.defShape.freq - 1;
this.clock.tick(tickDelta);
// elapsed time is now 1 tick before the end of the first event throttling interval
tickAccum += tickDelta;
raw = 0;
filtered = raw;
callback(raw);
test.strictEqual(dataSpy.callCount + limitSpy.callCount + lowerSpy.callCount + upperSpy.callCount, 0,
"tick " + tickAccum + ": no event handlers should not be called until tick " + this.defShape.freq);
tickDelta = 1;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the first event throttling interval
tickAccum += tickDelta;
test.strictEqual(dataSpy.callCount, 1, "tick " + tickAccum +
": data event handler should be called first time at tick " + this.defShape.freq);
test.strictEqual(dataSpy.getCall(0).args[0], filtered, "data event value expected to be the median (" + filtered + ") value");
test.strictEqual(limitSpy.callCount + lowerSpy.callCount + upperSpy.callCount, 0,
"tick " + tickAccum + ": no limit event handlers should be called while limit is null");
// Check that no limit events are emitted while no limit is configured (high value)
tickDelta = this.defShape.freq - 1;
this.clock.tick(tickDelta);
// elapsed time is now 1 tick before the end of the second event throttling interval
tickAccum += tickDelta;
raw = 1023;
filtered = raw;
callback(raw);
test.strictEqual(dataSpy.callCount + limitSpy.callCount + lowerSpy.callCount + upperSpy.callCount, 1,
"tick " + tickAccum + ": no more event handlers should not be called until tick " + this.defShape.freq * 2);
tickDelta = 1;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the second event throttling interval
tickAccum += tickDelta;
test.strictEqual(dataSpy.callCount, 2, "tick " + tickAccum +
": data event handler should be called again time at tick " + this.defShape.freq * 2);
test.strictEqual(dataSpy.getCall(1).args[0], filtered, "data event value expected to be the median (" + filtered + ") value");
test.strictEqual(limitSpy.callCount + lowerSpy.callCount + upperSpy.callCount, 0,
"tick " + tickAccum + ": no limit event handlers should be called while limit is null");
newShape = Fn.cloneDeep(this.defShape);
// test.deepEqual(this.defShape, newShape);//DBG verify that deep copy is working
// test.deepStrictEqual(this.defShape, newShape);//DBG verify that deep copy is working
// newShape.limit = "junk";
// newShape.limit = {test: "junk"};
// newShape.limit = {0: "junk"};
// newShape.limit = 123;
// Check that setting limit boundaries changes the instance shape to match
lowerLimit = 0;
upperLimit = 1023;
newShape.limit = [lowerLimit, upperLimit];
this.sensor.limit = [lowerLimit, upperLimit];
test.deepEqual(getShape(this.sensor), newShape, "sensor instance properties should match shape with new limit");
lowerLimit = 100;
upperLimit = 101;
newShape.limit = [lowerLimit, upperLimit];
this.sensor.limit = [lowerLimit, upperLimit];
test.deepEqual(getShape(this.sensor), newShape, "sensor instance properties should match shape with new limit");
lowerLimit = 450;
upperLimit = 550;
newShape.limit = [lowerLimit, upperLimit];
this.sensor.limit = [lowerLimit, upperLimit];
test.deepEqual(getShape(this.sensor), newShape, "sensor instance properties should match shape with new limit");
// Check that a value just above the lower limit does not emit any limit events
raw = 450;
callback(raw);
tickDelta = this.defShape.freq - 1;
this.clock.tick(tickDelta);
// elapsed time is now 1 tick before the end of the third event throttling interval
tickAccum += tickDelta;
raw = 451;
filtered = 450.5;
callback(raw);
tickDelta = 1;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the third event throttling interval
tickAccum += tickDelta;
test.strictEqual(dataSpy.callCount, 3, "tick " + tickAccum +
": data event handler should be called every multiple of " + this.defShape.freq + " ticks");
test.strictEqual(dataSpy.getCall(2).args[0], filtered, "data event value expected to be the median (" + filtered + ") value");
test.strictEqual(limitSpy.callCount + lowerSpy.callCount + upperSpy.callCount, 0,
"tick " + tickAccum + ": no limit event handlers should be called while value is within the limits");
// Check that a value just above the lower limit does not emit any limit events
raw = 550;
callback(raw);
tickDelta = this.defShape.freq - 1;
this.clock.tick(tickDelta);
// elapsed time is now 1 tick before the end of the fourth event throttling interval
tickAccum += tickDelta;
raw = 549;
filtered = 549.5;
callback(raw);
tickDelta = 1;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the fourth event throttling interval
tickAccum += tickDelta;
test.strictEqual(dataSpy.callCount, 4, "tick " + tickAccum +
": data event handler should be called every multiple of " + this.defShape.freq + " ticks");
test.strictEqual(dataSpy.getCall(3).args[0], filtered, "data event value expected to be the median (" + filtered + ") value");
test.strictEqual(limitSpy.callCount + lowerSpy.callCount + upperSpy.callCount, 0,
"tick " + tickAccum + ": no limit event handlers should be called while value is within the limits");
// check that a value matching the lower limit emits limit and limit:lower events
raw = 450;
filtered = raw;
callback(raw);
tickDelta = this.defShape.freq;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the fifth event throttling interval
tickAccum += tickDelta;
test.strictEqual(dataSpy.callCount, 5, "tick " + tickAccum +
": data event handler should be called every multiple of " + this.defShape.freq + " ticks");
test.strictEqual(limitSpy.callCount, 1, "tick " + tickAccum +
": limit event handler should be called at " + this.defShape.freq + " ticks; value at lower limit");
test.strictEqual(lowerSpy.callCount, 1, "tick " + tickAccum +
": limit:lower event handler should be called at " + this.defShape.freq + " ticks; value at lower limit");
test.strictEqual(upperSpy.callCount, 0, "tick " + tickAccum +
": limit:upper event handler should not be called at " + this.defShape.freq + " ticks; value at lower limit");
test.strictEqual(dataSpy.getCall(4).args[0], filtered, "data event value expected to be the median (" + filtered + ") value");
test.deepEqual(limitSpy.getCall(0).args[0], {
boundary: "lower",
value: filtered
},
"limit event value expected to be the lower boundary with the median (" + filtered + ") value");
test.strictEqual(lowerSpy.getCall(0).args[0], filtered, "limit:lower event value expected to be the median (" + filtered + ") value");
// check that a value matching the upper limit emits limit and limit:upper events
raw = 550;
filtered = raw;
callback(raw);
tickDelta = this.defShape.freq;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the sixth event throttling interval
tickAccum += tickDelta;
test.strictEqual(dataSpy.callCount, 6, "tick " + tickAccum +
": data event handler should be called every multiple of " + this.defShape.freq + " ticks");
test.strictEqual(limitSpy.callCount, 2, "tick " + tickAccum +
": limit event handler should be called at " + this.defShape.freq + " ticks; value at upper limit");
test.strictEqual(lowerSpy.callCount, 1, "tick " + tickAccum +
": limit:lower event handler should not be called at " + this.defShape.freq + " ticks; value at upper limit");
test.strictEqual(upperSpy.callCount, 1, "tick " + tickAccum +
": limit:upper event handler should be called at " + this.defShape.freq + " ticks; value at upper limit");
test.strictEqual(dataSpy.getCall(5).args[0], filtered, "data event value expected to be the median (" + filtered + ") value");
test.deepEqual(limitSpy.getCall(1).args[0], {
boundary: "upper",
value: filtered
},
"limit event value expected to be the lower boundary with the median (" + filtered + ") value");
test.strictEqual(upperSpy.getCall(0).args[0], filtered, "limit:upper event value expected to be the median (" + filtered + ") value");
// check that a very low value emits limit and limit:lower events
raw = 0;
filtered = raw;
callback(raw);
tickDelta = this.defShape.freq;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the seventh event throttling interval
tickAccum += tickDelta;
test.strictEqual(dataSpy.callCount, 7, "tick " + tickAccum +
": data event handler should be called every multiple of " + this.defShape.freq + " ticks");
test.strictEqual(limitSpy.callCount, 3, "tick " + tickAccum +
": limit event handler should be called at " + this.defShape.freq + " ticks; value at lower limit");
test.strictEqual(lowerSpy.callCount, 2, "tick " + tickAccum +
": limit:lower event handler should be called at " + this.defShape.freq + " ticks; value at lower limit");
test.strictEqual(upperSpy.callCount, 1, "tick " + tickAccum +
": limit:upper event handler should not be called at " + this.defShape.freq + " ticks; value at lower limit");
test.strictEqual(dataSpy.getCall(6).args[0], filtered, "data event value expected to be the median (" + filtered + ") value");
test.deepEqual(limitSpy.getCall(2).args[0], {
boundary: "lower",
value: filtered
},
"limit event value expected to be the lower boundary with the median (" + filtered + ") value");
test.strictEqual(lowerSpy.getCall(1).args[0], filtered, "limit:lower event value expected to be the median (" + filtered + ") value");
// check that a very high value emits limit and limit:upper events
raw = 1023;
filtered = raw;
callback(raw);
tickDelta = this.defShape.freq;
this.clock.tick(tickDelta);
// elapsed time is now at the end of the eighth event throttling interval
tickAccum += tickDelta;
test.strictEqual(dataSpy.callCount, 8, "tick " + tickAccum +
": data event handler should be called every multiple of " + this.defShape.freq + " ticks");
test.strictEqual(limitSpy.callCount, 4, "tick " + tickAccum +
": limit event handler should be called at " + this.defShape.freq + " ticks; value at upper limit");
test.strictEqual(lowerSpy.callCount, 2, "tick " + tickAccum +
": limit:lower event handler should not be called at " + this.defShape.freq + " ticks; value at upper limit");
test.strictEqual(upperSpy.callCount, 2, "tick " + tickAccum +
": limit:upper event handler should be called at " + this.defShape.freq + " ticks; value at upper limit");
test.strictEqual(dataSpy.getCall(7).args[0], filtered, "data event value expected to be the median (" + filtered + ") value");
test.deepEqual(limitSpy.getCall(3).args[0], {
boundary: "upper",
value: filtered
},
"limit event value expected to be the lower boundary with the median (" + filtered + ") value");
test.strictEqual(upperSpy.getCall(1).args[0], filtered, "limit:upper event value expected to be the median (" + filtered + ") value");
test.done();
}, // ./limit: function(test)
freq: function(test) {
var spy = this.sandbox.spy(),
newShape, newFreq, tickDelta, tickAccum;
test.expect(10);
this.sensor.on("data", spy);
test.deepEqual(this.defShape, getShape(this.sensor), "sensor instance properties should match default shape values");
// Make sure that the data event does not get emitted (first time) until the initial (default)
// interval (currently 25 ms === 25 ticks) has passed.
tickAccum = 0;
// One less tick than the expected time to emit a data event
tickDelta = this.defShape.freq - 1;
this.clock.tick(tickDelta);
tickAccum += tickDelta;
test.strictEqual(spy.callCount, 0, "tick " + tickAccum +
": data event handler should not be called first time until tick " + this.defShape.freq);
// As above, many message in this (and other) test start with the accumluated (tick) time, then have
// another time at the end. The first is the actual (fake) elapsed time. The second is the the time
// point boundary being used as a reference. Have both simplifies debugging the testing code, to get
// the specified time deltas correct.
// Make sure that a data event does get emitted at the time specified by the initial (default)
// freq interval.
tickDelta = 1;
this.clock.tick(tickDelta);
// Acumulated ticks are now up to the default freq interval
tickAccum += tickDelta;
test.strictEqual(spy.callCount, 1, "tick " + tickAccum +
": data event handler should be called first time at tick " + this.defShape.freq);
// After explicitly setting the frep property value of the existing instance, the shape (properties)
// of the instance should match the default shape, with (only) the freq property value and the
// clone in the state information, updated to match the specified value.
newShape = Fn.cloneDeep(this.defShape);
newFreq = 35;
newShape.freq = newFreq;
newShape.state.freq = newFreq;
this.sensor.freq = newFreq;