/
catbus.js
1653 lines (1189 loc) · 46 KB
/
catbus.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
/**
* catbus.js (v2.4-proto) -- adding timing functions, not ready yet
*
* Copyright (c) 2015 Scott Southworth, Landon Barnickle, Nick Lorenson & Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at:
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*
* @authors Scott Southworth @darkmarmot, Landon Barnickle @landonbar, Nick Lorenson @enlore
*
*/
;(function(){
"use strict";
var catbus = {};
var externalContext = this;
catbus.$ = {};
var plugins = typeof seele !== 'undefined' && seele;
if(plugins)
plugins.register('catbus', catbus, true);
else
externalContext.Catbus = catbus;
function createQueueFrame() {
return {defer:[], batch:[], batchAndDefer:[]};
}
function createFunctor(val) {
if(val === undefined) return undefined;
return (typeof val === 'function') ? val : function() { return val; };
}
function toNameArray(arg, delimiter){
if(typeof arg === 'string'){
return stringToTrimmedArray(arg, delimiter);
}
return arg;
}
function firstWord(str){
if(!str) return '';
var i = str.indexOf(' ');
if(i === -1)
return str;
return str.substring(0, i);
}
function afterWord(str){
if(!str) return '';
var i = str.indexOf(' ');
if(i === -1)
return '';
return str.substring(i + 1);
}
function stringToTrimmedArray(str, delimiter){
delimiter = delimiter || ',';
var arr = str.split(delimiter);
var result = [];
for(var i = 0; i < arr.length; i++){
var chunk = arr[i];
var trimmed_chunk = chunk.trim();
if(trimmed_chunk)
result.push(trimmed_chunk);
}
return result;
}
catbus.uid = 0;
catbus._trees = {}; // trees by name
catbus._directions = null;
catbus._deepLinkers = {};
catbus._currentDeepLinker = 'raw';
catbus._locations = {};
catbus._hosts = {}; // hosts by name
catbus._primed = false;
catbus._queueFrame = createQueueFrame();
catbus.dropHost = function(name){
var hosts = catbus._hosts;
var host = hosts[name];
if(!host) return false;
for(var id in host._sensorMap){
var sensor = host._sensorMap[id];
sensor.drop();
}
delete hosts[name];
};
catbus.location = catbus.demandData = function(nameOrNames){
var zone = catbus.tree();
return zone.demandLocation(nameOrNames);
};
catbus.sensor = catbus.createSensor = function(){
var zone = catbus.demandTree();
return zone.createSensor();
};
catbus.watch = function(nameOrNames) {
return catbus.sensor().watch(nameOrNames);
};
catbus.matchTopics = function(sensorTopic, locationTopic){
return sensorTopic === "*" || sensorTopic === locationTopic;
};
catbus.envelope = function(msg, topic, tag, owner){
return {
prior: (owner) ? owner._last : null,
msg: msg,
topic: topic,
tag: tag,
owner: owner,
id: ++catbus.uid,
sent: Date.now()
}
};
catbus.queue = function(sensor) {
var arr;
var q = this._queueFrame;
if(sensor._defer) {
if (sensor._batch) {
arr = q.batchAndDefer;
} else {
arr = q.defer;
}
} else {
arr = q.batch;
}
arr.push(sensor);
if (this._primed) return;
this._primed = true;
if(typeof window !== 'undefined' && window.requestAnimationFrame) requestAnimationFrame(this.flush.bind(this));
else process.nextTick(this.flush.bind(this));
};
catbus._processTellArray = function(arr){
while(arr.length){
var sensor = arr.shift();
sensor.send();
}
};
catbus.flush = function(){
this._primed = false;
// todo add max loop depth to avoid infinite message doom
while(this.hasQueuedMessages()) {
var q = this._queueFrame;
this._queueFrame = createQueueFrame();
this._processTellArray(q.defer);
this._processTellArray(q.batch);
this._processTellArray(q.batchAndDefer);
}
};
catbus.hasQueuedMessages = function(){
var q = this._queueFrame;
return q.defer.length || q.batch.length || q.batchAndDefer.length;
};
catbus.tree = catbus.demandTree = function(name){
name = name || 'DEFAULT';
var trees = catbus._trees;
var tree = trees[name] || (trees[name] = catbus._createTree(name));
return tree;
};
catbus.defineDeepLinker = function(name, toLink, toDirections){
catbus._deepLinkers[name] = {toLink: toLink, toDirections: toDirections};
};
catbus.defineDeepLinker('raw',
function toLink(directions){
return encodeURIComponent(JSON.stringify(directions));
},
function toDirections(link){
return JSON.parse(decodeURIComponent(link));
}
);
catbus.setDeepLinker = function(name){
catbus._currentDeepLinker = name || 'raw';
};
catbus._createTree = function(name){
var tree = new Zone(name);
var directions = tree.demandData('__DIRECTIONS__');
var link = tree.demandData('__DEEP_LINK__');
directions.createSensor().on('update').batch().run(function(msg){
//console.log('directions:',(msg));
var deepLinkerName = catbus._currentDeepLinker;
var link = (catbus._deepLinkers[deepLinkerName]).toLink(msg);
//var dir = (catbus._deepLinkers[deepLinkerName]).toDirections(link);
//console.log('raw link', link);
//console.log('trans dir', dir);
window.history.replaceState(null,null,window.location.origin + window.location.pathname + '?' + deepLinkerName + '=' + link);
});
return tree;
};
catbus.lookForDirections = function(searchStr){
if(searchStr.indexOf('?lzs=') === 0) {
var linkData = searchStr.substr(5);
return {linkType: 'lzs', linkData: linkData};
}
return null;
};
catbus.resolveDirections = function(searchStr){
var encoding = catbus.lookForDirections(searchStr);
if(!encoding) return;
var directions = (catbus._deepLinkers[encoding.linkType]).toDirections(encoding.linkData);
return directions;
};
var Zone = function(name, isRoute) {
this._id = ++catbus.uid;
this._tree = null;
this._name = name || this._id;
this._parent = null;
this._children = {}; // by name
this._locations = {}; // by name
this._valves = null;
this._sensors = {}; // by id
this._routeKey = '';
this._isRoute = isRoute || false;
this._dropped = false;
};
Zone.prototype.drop = function(){
var i, key;
if(this._dropped) return;
var child_keys = Object.keys(this._children);
for(i = 0; i < child_keys.length; i++){
key = child_keys[i];
var child = this._children[key];
child.assignParent(null);
}
var sensor_keys = Object.keys(this._sensors);
for(i = 0; i < sensor_keys.length; i++){
key = sensor_keys[i];
var sensor = this._sensors[key];
sensor.drop();
}
var location_keys = Object.keys(this._locations);
for(i = 0; i < location_keys.length; i++){
key = location_keys[i];
var data = this._locations[key];
data.drop(true);
}
this._locations = null;
this._sensors = null;
this._children = null;
this._valves = null;
this._parent = null;
this._dropped = true;
};
Zone.prototype.snapshot = function(){
var result = {id: this._id, name: this._name, children: [], data: [], sensors: [], valves: [], parent: this._parent && this._parent._name};
var p;
for(p in this._children) { result.children.push(p); };
for(p in this._locations) { result.data.push(p); };
for(p in this._sensors) { result.data.push(p); };
if(this._valves)
for(p in this._valves) { result.children.push(p); };
return result;
};
Zone.prototype.demandChild = function(name, isRoute){
return this._children[name] || this._createChild(name, isRoute);
};
Zone.prototype._createChild = function(name, isRoute){
var child = new Zone(name, isRoute);
child.assignParent(this);
return child;
};
Zone.prototype.insertParent = function(newParent){
var oldParent = this._parent;
newParent.assignParent(oldParent);
this.assignParent(newParent);
return this;
};
Zone.prototype.assignParent = function(newParent){
// todo test name collision, initial block colons from user names
var oldParent = this._parent;
if(oldParent)
delete oldParent._children[this._name];
this._parent = newParent;
if(newParent) {
this._tree = newParent._tree || newParent;
newParent._children[this._name] = this;
}
this._determineRouteKey();
return this;
};
Zone.prototype._determineRouteKey = function(){
var baseKey = this._parent && this._parent._routeKey || '';
if(this._isRoute && this._name){
this._routeKey = baseKey ? baseKey + '.' + this._name : this._name;
} else {
this._routeKey = baseKey;
}
};
Zone.prototype.sensor = Zone.prototype.createSensor = function(){
var sensor = new Sensor();
sensor.zone(this);
return sensor;
};
Zone.prototype.demandLocation = Zone.prototype.demandData = function (nameOrNames){
var names = toNameArray(nameOrNames);
if(names.length === 1)
return this._demandLocation(names[0]);
// if an array of names, return a multi-location
var multiLoc = this._demandLocation();
var locations = multiLoc._multi = [];
for(var i = 0; i < names.length; i++){
var name = names[i];
locations.push(this._demandLocation(name));
}
return multiLoc;
};
Zone.prototype._demandLocation = function(name){
var locations = this._locations;
var location = locations[name];
if(!location) {
location = new Location(name);
locations[location._name] = location;
location._zone = this;
}
return location;
};
Zone.prototype.findData = function(nameOrArray, where, optional){
var arr = toNameArray(nameOrArray);
var data;
var data_list = [];
var result = null;
if(!arr)
return null;
for(var i = 0; i < arr.length; i++){
var name = arr[i];
if(typeof name === 'object')
data = name;
else
data = this._findData(name, where, optional);
if(data)
data_list.push(data);
}
if(data_list.length > 1){
result = this._demandLocation();
result._multi = data_list;
} else if (data_list.length === 1) {
result = data_list[0];
}
return result;
};
Zone.prototype._findData = function(name, where, optional) {
where = where || 'first';
var container_name = null;
var result = null;
if (where === 'local')
result = this._locations[name];
else if (where === 'first' || !where)
result = this._findFirst(name);
else if (where === 'outer')
result = this._findOuter(name);
else if (where === 'last')
result = this._findLast(name);
else if (where === 'parent')
result = this._findFromParent(name);
if(firstWord(where) === 'in'){
container_name = afterWord(where);
result = this._findFirstIn(name, container_name);
}
if(result || optional)
return result;
throw new Error('Data find error: ' + where + ' ' + name);
};
Zone.prototype.valves = function(valves){
var hash = null;
if(valves && valves.length > 0){
hash = {};
for(var i = 0; i < valves.length; i++){
var name = valves[i];
hash[name] = true;
}
}
this._valves = hash;
return this;
};
Zone.prototype._findFirstIn = function(name, containerName) {
var zone = this;
var checkValve = false;
do {
if(checkValve && zone._valves && !zone._valves[name])
return null; // not white-listed by a valve
checkValve = true; // not checked at the local level
if(zone._name === containerName) {
var result = zone._locations[name];
if (result)
return result;
}
} while (zone = zone._parent);
return null;
};
Zone.prototype._findFirst = function(name, fromParent) {
var zone = this;
var checkValve = fromParent || false;
do {
if(checkValve && zone._valves && !zone._valves[name])
return null; // not white-listed by a valve
checkValve = true; // not checked at the local level
var result = zone._locations[name];
if (result)
return result;
} while (zone = zone._parent);
return null;
};
Zone.prototype._findFromParent = function(name) {
var parent = this._parent;
if(!parent) return null;
return parent._findFirst(name, true);
};
Zone.prototype._findOuter = function(name) {
var zone = this;
var found = false;
var checkValve = false;
do {
if(checkValve && zone._valves && !zone._valves[name])
return null; // not white-listed by a valve on the cog
checkValve = true; // not checked at the local level (valves are on the bottom of cogs)
var result = zone._locations[name];
if (result) {
if(found)
return result;
found = true;
}
} while (zone = zone._parent);
return null;
};
Zone.prototype._findLast = function(name) {
var zone = this;
var result = null;
var checkValve = false;
do {
if(checkValve && zone._valves && !zone._valves[name])
return null; // not white-listed by a valve
checkValve = true; // not checked at the local level
result = zone._locations[name] || result;
} while (zone = zone._parent);
return result;
};
var Cluster = function(topic, location) {
this._location = location;
this._topic = topic;
this._sensors = [];
this._lastEnvelope = null;
this._dropped = false;
};
Cluster.prototype._drop = function(){
if(this._dropped) return;
for(var i = 0; i < this._sensors.length; i++){
var sensor = this._sensors[i];
sensor.drop();
}
this._location = null;
this._lastEnvelope = null;
this._sensors = null;
this._dropped = true;
};
Cluster.prototype._add = function(sensor){
this._sensors.push(sensor);
};
Cluster.prototype._remove = function(sensor){
var i = this._sensors.indexOf(sensor);
if(i == -1) return;
this._sensors.splice(i,1);
};
Cluster.prototype._tell = function(msg, topic, tag){
if(this._dropped) return;
this._lastEnvelope = catbus.envelope(msg, topic, tag, this); // message stored enveloped before sending and transforming
var sensors = [].concat(this._sensors);
for(var i = 0; i < sensors.length; i++){
var sensor = sensors[i];
sensor.tell(msg, topic, tag);
}
};
var Host = function(name){
this._name = name;
this._sensorMap = {};
};
var Sensor = function() {
this._zone = null;
this._multi = null; // list of sensors to process through sensor api
this._callback = null;
this._context = null;
this._max = null;
this._host = null;
this._bus = catbus;
this._defer = false;
this._batch = false;
this._group = false;
this._batchedByTag = {};
this._batchedAsList = [];
this._keep = null; // last or first or all or null (not batching)
this._pipe = false;
this._change = null;
this._needs = []; // array of tags needed before firing
this._retain = false; // will retain prior tag messages
this._last = null;
this._name = null;
this._cmd = null;
this._postcard = null; // wrapped msg about to be sent...
this._active = true;
this._id = ++catbus.uid;
this._appear = undefined;
this._extract = null;
this._lastAppearingMsg = undefined;
this._dropped = false;
this._locked = false;
this._optional = false; // optional data to watch
this._mergeLoc = null; // an autogenerated Location to hold merged data
this._cluster = null;
this._delay = null;
this._cancel = null;
this._timeoutId = null;
};
// todo add location and sensor reset methods, use with object pooling
Sensor.prototype.throwError = function(msg){
throw {error:"Catbus: Sensor", msg: msg, topic: this._getTopic(), tag: this._getTag() };
};
var bus_config = {
};
var location_config = {
on: {name: 'on', alias: ['topic','sensor'], type: 'string' , setter: '_setTopic', getter: '_getTopic'},
tag: {name: 'tag', type: 'string' , prop: '_tag'}
};
var sensor_config = {
keep: {name: 'keep', options: ['last', 'first', 'all'], prop: '_keep', default_set: 'last'},
retain: {name: 'retain', type: 'boolean', prop: '_retain', default_set: true},
need: {name: 'need', transform: '_toStringArray', valid: '_isStringArray', prop: '_needs'}, // todo, also accept [locs] to tags
gather: {name: 'gather', transform: '_toStringArray', valid: '_isStringArray', prop: '_gather'}, // todo, also accept [locs] to tags
host: {name: 'host', transform: '_toString', type: 'string', setter: '_setHost', prop: '_host'},
zone: {name: 'zone', valid: '_isZone', setter: '_setZone', prop: '_zone'},
defer: {name: 'defer', type: 'boolean' , prop: '_defer', default_set: true},
batch: {name: 'batch', type: 'boolean' , prop: '_batch', default_set: true, setter: '_setBatch'},
change: {name: 'change', type: 'function', prop: '_change', default_set: function(msg){ return msg;}},
optional: {name: 'optional', type: 'boolean' , prop: '_optional', default_set: true},
group: {name: 'group', type: 'function', prop: '_group', functor: true, default_set: function(msg, topic, name){ return name;}},
pipe: {name: 'pipe', valid: '_isLocation', prop: '_pipe'},
emit: {name: 'emit', prop: '_emit', functor: true},
name: {name: 'name', type: 'string' , prop: '_name'},
cmd: {name: 'cmd', type: 'string', prop: '_cmd'},
active: {name: 'active', type: 'boolean' , prop: '_active', default_set: true},
sleep: {name: 'sleep', no_arg: true , prop: '_active', default_set: false},
wake: {name: 'wake', no_arg: true , prop: '_active', default_set: true},
on: {name: 'on', alias: ['topic','sense'], type: 'string' , setter: '_setTopic', getter: '_getTopic'},
//watch: {name: 'watch', alias: ['location','at'], transform: '_toLocation', valid: '_isLocation', setter: '_watch', getter: '_getLocation'},
exit: {name: 'exit', alias: ['transform'], type: 'function', functor: true, prop: '_transformMethod'},
enter: {name: 'enter', alias: ['conform','adapt'], type: 'function', functor: true, prop:'_appear'},
extract: {name: 'extract', transform: '_toString', type: 'string', prop:'_extract'},
run: {name: 'run', type: 'function' , prop: '_callback'},
filter: {name: 'filter', type: 'function' , prop: '_filter'},
cancel: {name: 'cancel', type: 'function' , prop: '_cancel'},
as: {name: 'as', type: 'object' , prop: '_context'},
delay: {name: 'delay', transform: '_toInt', type: 'number' , prop: '_delay'},
max: {name: 'max', transform: '_toInt', type: 'number' , prop: '_max'},
once: {name: 'once', no_arg: true, prop: '_max', default_set: 1},
tag: {name: 'tag', getter: '_getTag', prop: '_tag', functor: true}
};
// build chaining setters from config
var config_name;
var config;
for(config_name in sensor_config){
config = sensor_config[config_name];
var alias = config.alias;
if(alias){
for(var i = 0; i < alias.length; i++){
var alias_name = alias[i];
sensor_config[alias_name] = config;
}
}
}
for(config_name in sensor_config){
config = sensor_config[config_name];
if(config.no_write)
continue;
(function(name, props){
Sensor.prototype[name] = function(value){
if(this._multi){
if(arguments.length === 0)
if(props.hasOwnProperty('default_set'))
return this._setMultiAttr(name, props.default_set);
else
return this._setMultiAttr(name);
else
return this._setMultiAttr(name, value);
}
if(arguments.length === 0)
if(props.hasOwnProperty('default_set'))
return this._setAttr(name, props.default_set);
else
return this._setAttr(name);
else
return this._setAttr(name, value);
};
})(config_name, config);
}
Sensor.prototype._toInt = function(num){
return Math.floor(num);
};
Sensor.prototype._getTopic = function(){
return this._cluster && this._cluster._topic;
};
Sensor.prototype._getTag = function(){
if(this._tag)
return this._tag;
var loc = this._getLocation();
return loc && loc.tag();
};
Sensor.prototype._getLocation = function(){
return this._cluster && this._cluster._location;
};
Sensor.prototype._toLocation = function(nameOrLocation){
return (typeof nameOrLocation === 'string') ? this._zone._demandLocation(nameOrLocation) : nameOrLocation;
};
Sensor.prototype._isLocation = function(location){
return location instanceof Location;
};
Sensor.prototype.toArray = function(){
if(this._multi)
return this._multi;
else
return [this];
};
Sensor.prototype.watch = function(namesOrLocations, where, optional){
var locations = this._zone.findData(namesOrLocations, where, optional);
var sensor;
var loc_list = locations.toArray();
var loc;
var new_sensors = [];
for(var i = 0; i < loc_list.length; i++){
loc = loc_list[i];
sensor = loc.createSensor();
new_sensors.push(sensor);
}
if(this._cluster && this._cluster._location)
new_sensors.push(this);
if(new_sensors.length === 0)
return this;
else if(new_sensors.length === 1)
return new_sensors[0];
this._multi = new_sensors;
return this;
};
Sensor.prototype._isZone = function(zone){ // zone can be set to null only if a sensor is already dropped
return (this._dropped && !zone) || zone instanceof Zone;
};
Sensor.prototype._toStringArray = function(stringOrStringArray){
var arr;
if(typeof stringOrStringArray === 'string')
arr = stringOrStringArray.split(/,|\./);
else
arr = stringOrStringArray;
for(var i = 0; i > arr.length; i++){
arr[i] = (arr[i]).trim();
}
return arr;
};
Sensor.prototype._toString = function(value){
if(!value) return null;
return value + '';
};
Sensor.prototype._isStringArray = function(value){
if(!(value instanceof Array))
return false;
for(var i = 0; i < value.length; i++){
var s = value[i];
if(typeof s !== 'string')
return false;
}
return true;
};
Sensor.prototype.attr = function(nameOrConfig, value){
if(this._multi){
return this._multiAttr.apply(this, arguments);
}
if(arguments.length === 1){
if(typeof nameOrConfig === 'string')
return this._getAttr(nameOrConfig);
else
return this._setHashAttr(nameOrConfig);
} else {
return this._setAttr(nameOrConfig, value);
}
};
Sensor.prototype._setMultiAttr = function(nameOrConfig, value){
var i;
var c = this._multi.length;
var s;
if(arguments.length === 1 && typeof nameOrConfig === 'object') {
for (i = 0; i < c; i++) {
s = this._multi[i];
s._setHashAttr(nameOrConfig);
}
return this;
} else {
for (i = 0; i < c; i++) {
s = this._multi[i];
s._setAttr.apply(s, arguments);
}
return this;
}
};
Sensor.prototype._multiAttr = function(nameOrConfig, value){
var i;
var result;
var c = this._multi.length;
var s;
if(arguments.length === 1) {
if (typeof nameOrConfig === 'string') {
result = [];
for (i = 0; i < c; i++) {
s = this._multi[i];
result.push(s._getAttr(nameOrConfig));
}
return result;
} else {
for (i = 0; i < c; i++) {
s = this._multi[i];
s._setHashAttr(nameOrConfig);
}
return this;
}
} else {
for (i = 0; i < c; i++) {
s = this._multi[i];
s._setAttr(nameOrConfig, value);
}
return this;
}
};
Sensor.prototype._getAttr = function(name){
var c = sensor_config[name];
if(!c)
this.throwError('Sensor getter attribute [' + name + '] not found');
return (c.getter) ? (this[c.getter]).call(this) : this[c.prop];
};
Sensor.prototype._setAttr = function(name, value){
var c = sensor_config[name];
if(!c)
this.throwError('Sensor attribute [' + name + '] does not exist');
if(c.method && c.no_arg && arguments.length > 1)
this.throwError('Sensor method [' + name + '] takes no arguments');