Newer
Older
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
/**
* This module representing a Context Widget.
*
* @module Widget
* @fileOverview
*/
define([ 'easejs', 'MathUuid', 'callback', 'callbackList', 'attributeType',
'attributeValue', 'attributeTypeList', 'attributeValueList', 'conditionList',
'subscriber', 'subscriberList', 'widgetDescription'],
function(easejs, MathUuid, Callback, CallbackList, AttributeType,
AttributeValue, AttributeTypeList, AttributeValueList, ConditionList,
Subscriber, SubscriberList, WidgetDescription) {
var AbstractClass = easejs.AbstractClass;
var Class = easejs.Class;
var Widget = AbstractClass('Widget',{
/**
* @alias name
* @public
* @type {string}
* @memberof Widget#
* @desc Name of the Widget.
*/
'public name' : 'Widget',
/**
* @alias id
* @public
* @type {string}
* @memberof Widget#
* @desc ID of the Widget. Will be generated.
*/
'public id' : '',
/**
* @alias attributeTypes
* @protected
* @type {AttributeTypeList}
* @memberof Widget#
* @desc Types of all available attributes.
*/
'protected attributeTypes' : [],
/**
* @alias constantAttributeTypes
* @protected
* @type {AttributeTypeList}
* @memberof Widget#
* @desc Types of all available ConstantAttributes.
*/
'protected constantAttributeTypes' : [],
/**
* @alias attributes
* @protected
* @type {AttributeValueList}
* @memberof Widget#
* @desc All available Attributes and their values.
*/
'protected attributes' : [],
/**
* @alias oldAttributes
* @protected
* @type {AttributeValueList}
* @memberof Widget#
* @desc This temporary variable is used for storing the old attribute values.
* So these can be used to check conditions.
*/
'protected oldAttributes' : [],
/**
* @alias constantAttributes
* @protected
* @type {AttributeValueList}
* @memberof Widget#
* @desc All available constant Attributes and their values.
*/
'protected constantAttributes' : [],
/**
* @alias callbacks
* @protected
* @type {CallbackList}
* @memberof Widget#
* @desc List of Callbacks.
*/
'protected callbacks' : [],
/**
* @alias subscribers
* @protected
* @type {SubscriberList}
* @memberof Widget#
* @desc List of Subscriber.
*/
'protected subscribers' : [],
/**
* @alias discoverer
* @protected
* @type {Discoverer}
* @memberof Widget#
* @desc Associated discoverer.
*/
'protected discoverer' : '',
/**
* Constructor: Generates the ID and initializes the
* Widget with attributes, callbacks and subscriber
* that are specified in the provided functions.
*
* @abstract
* @class Widget
* @classdesc The Widget handles the access to sensors.
* @requires easejs
* @requires MathUuid
* @requires Callback
* @requires CallbackList
* @requires AttributeType
* @requires AttributeValue
* @requires AttributeTypeList
* @requires AttributeValueList
* @requires ConditionList
* @requires Subscriber
* @requires SubscriberList
* @requires WidgetDescription
* @requires Discoverer
* @constructs Widget
*/
'virtual public __construct' : function() {
this.id = Math.uuid();
this.attributeTypes = new AttributeTypeList();
this.constantAttributeTypes = new AttributeTypeList();
this.attributes = new AttributeValueList();
this.constantAttributes = new AttributeValueList();
this.subscribers = new SubscriberList();
this.callbacks = new CallbackList();
this.init();
},
/**
* Returns the name of the widget.
*
* @public
* @alias getName
* @memberof Widget#
* @returns {string}
*/
'public getName' : function() {
return this.name;
},
/**
* Returns the id of the widget.
*
* @public
* @alias getId
* @memberof Widget#
* @returns {string}
*/
'public getId' : function() {
return this.id;
},
/**
* Returns the type of this class, in this case
* "Widget".
*
* @virtual
* @public
* @alias getType
* @memberof Widget#
* @returns {string}
*/
'virtual public getType' : function() {
return 'Widget';
},
/**
* Returns the available AttributeTypes.
*
* @public
* @alias getWidgetAttributeTypes
* @memberof Widget#
* @returns {AttributeTypeList}
*/
'public getWidgetAttributeTypes' : function() {
return this.attributeTypes;
},
/**
* Returns the available ConstantAttributeTypes
* (attributes that do not change).
*
* @public
* @alias getWidgetConstantAttributeTypes
* @memberof Widget#
* @returns {AttributeTypeList}
*/
'public getWidgetConstantAttributeTypes' : function() {
return this.constantAttributeTypes;
},
/**
* Returns the last acquired Attributes.
*
* @public
* @alias queryAttributes
* @memberof Widget#
* @returns {AttributeValueList}
*/
'public queryAttributes' : function() {
return this.attributes;
},
/**
* Returns the old Attributes.
*
* @private
* @alias getOldAttributes
* @memberof Widget#
* @returns {AttributeValueList}
*/
'public getOldAttributes' : function() {
return this.oldAttributes;
},
/**
* Returns the ConstantAttributes.
*
* @public
* @alias queryConstantAttributes
* @memberof Widget#
* @returns {AttributeValueList}
*/
'public queryConstantAttributes' : function() {
return this.constantAttributes;
},
/**
* Returns the specified Callbacks that can be
* subscribed.
*
* @public
* @alias queryCallbacks
* @memberof Widget#
* @returns {CallbackList}
*/
'public queryCallbacks' : function() {
return this.callbacks;
},
'public queryServices' : function() {
return this.services;
},
/**
* Returns the Subscriber.
*
* @public
* @alias getSubscriber
* @memberof Widget#
* @returns {SubscriberList}
*/
'public getSubscriber' : function() {
return this.subscribers;
},
/**
* Sets the name of the Widget.
*
* @protected
* @alias setName
* @memberof Widget#
* @param {string}
* _name Name of the Widget.
*/
'protected setName' : function(_name) {
if (typeof _name === 'string') {
this.name = _name;
}
},
/**
* Sets the id of the Widget.
*
* @protected
* @alias setId
* @memberof Widget#
* @param {string}
* _id Id of the Widget.
*/
'protected setId' : function(_id) {
if (typeof _id === 'string') {
this.id = _id;
}
},
/**
* Sets the AttributeValueList and also the associated
* AttributeTypes.
*
* @protected
* @alias setAttributes
* @memberof Widget#
* @param {(AttributeValueList|Array)}
* _attributes List or Array of
* AttributeValues
*/
'protected setAttributes' : function(_attributes) {
var list = new Array();
if (_attributes instanceof Array) {
list = _attributes;
} else if (Class.isA(AttributeValueList,_attributes)) {
list = _attributes.getItems();
}
this.oldAttributes = this.attributes;
for ( var i in list) {
var attribute = list[i];
if (Class.isA(AttributeValue, attribute)) {
attribute.setTimestamp(this.getCurrentTime());
this.attributes.put(attribute);
var type = new AttributeType().withName(attribute.getName())
.withType(attribute.getType())
.withParameters(attribute.getParameters());
this.attributeTypes.put(type);
}
}
},
/**
* Adds a new AttributeValue. If the given value is
* not included in the list, the associated type will
* be also added. Otherwise, only the value will be
* updated.
*
* @public
* @alias addAttribute
* @memberof Widget#
* @param {AttributeValue}
* _attribute AttributeValue
*/
'public addAttribute' : function(_attribute) {
if (Class.isA(AttributeValue, _attribute)) {
if (!this.attributes.contains(_attribute)) {
var type = new AttributeType().withName(_attribute.getName())
.withType(_attribute.getType())
.withParameters(_attribute.getParameters());
this.attributeTypes.put(type);
}
this.oldAttributes = this.attributes;
_attribute.setTimestamp(this.getCurrentTime());
this.attributes.put(_attribute);
}
},
/**
* Sets the ConstantAttributeValueList and also the
* associated AttributeTypes.
*
* @protected
* @alias setConstantAttributes
* @memberof Widget#
* @param {(AttributeValueList|Array)}
* _constantAttributes List or Array of
* AttributeValues
*/
'protected setConstantAttributes' : function(_constantAttributes) {
var list = new Array();
if (_constantAttributes instanceof Array) {
list = _constantAttributes;
} else if (Class.isA(AttributeValueList,_constantAttributes)) {
list = _constantAttributes.getItems();
}
for ( var i in list) {
var constantAttribute = list[i];
if (Class.isA(AttributeValue, constantAttribute)) {
constantAttribute.setTimestamp(this.getCurrentTime());
this.constantAttributes.put(constantAttribute);
var type = new AttributeType().withName(constantAttribute.getName())
.withType(constantAttribute.getType())
.withParameters(constantAttribute.getParameters());
this.constantAttributeTypes.put(type);
}
}
},
/**
* Adds a new constantAttributeValue. If the given value is
* not included in the list, the associated type will
* be also added. Otherwise, only the value will be
* updated.
*
* @protected
* @alias addConstantAttribute
* @memberof Widget#
* @param {AttributeValue}
* _constantAttribute AttributeValue
*/
'protected addConstantAttribute' : function(_constantAttribute) {
if (Class.isA(AttributeValue, _constantAttribute)) {
if (!this.constantAttributes
.contains(_constantAttribute)) {
var type = new AttributeType().withName(_constantAttribute.getName())
.withType(_constantAttribute.getType())
.withParameters(_constantAttribute.getParameters());
this.constantAttributeTypes.put(type);
}
_attribute.setTimestamp(this.getCurrentTime());
this.constantAttributes.put(_constantAttribute);
}
},
/**
* Sets Callbacks.
*
* @protected
* @alias setCallbacks
* @memberof Widget#
* @param {(CallbackList|Array)} _callbacks List or Array of Callbacks.
*/
'protected setCallbacks' : function(_callbacks) {
var list = new Array();
if (_callbacks instanceof Array) {
list = _subscriber;
} else if (Class.isA(CallbackList, _callbacks)) {
list = _callbacks.getItems();
}
for ( var i in list) {
var callback = list[i];
if (Class.isA(Callback, callback)) {
this.callbacks.put(callback);
}
}
},
/**
* Adds a new Callback.
*
* @protected
* @alias addCallback
* @memberof Widget#
* @param {Callback} _callback List or Array of AttributeValues.
*/
'protected addCallback' : function(_callback) {
if (Class.isA(Callback, _callback)) {
this.callbacks.put(_callback);
};
},
'protected setServices' : function(_services) {
this.services = _services;
},
/**
* Sets SubscriberList.
*
* @protected
* @alias setSubscriber
* @memberof Widget#
* @param {(SubscriberList|Array)} _subscriber List or Array of Subscriber.
*/
'protected setSubscriber' : function(_subscriber) {
var list = new Array();
if (_subscriber instanceof Array) {
list = _subscriber;
} else if (Class.isA(SubscriberList, _subscriber)) {
list = _subscriber.getItems();
}
for ( var i in list) {
var singleSubscriber = list[i];
if (Class.isA(Subscriber, singleSubscriber)) {
this.subscribers.put(singleSubscriber);
}
}
},
/**
* Adds a new Subscriber.
*
* @public
* @alias addSubscriber
* @memberof Widget#
* @param {Subscriber} _subscriber Subscriber
*/
'public addSubscriber' : function(_subscriber) {
if (Class.isA(Subscriber, _subscriber)) {
this.subscribers.put(_subscriber);
}
},
/**
* Removes the specified Subscriber.
*
* @public
* @alias removeSubscriber
* @memberof Widget#
* @param {Subscriber} _subscriber Subscriber
*/
'public removeSubscriber' : function(_subscriberId) {
this.subscribers.removeItem(_subscriberId);
},
/**
* Returns the current time.
*
* @private
* @alias getCurrentTime
* @memberof Widget#
* @returns {Date}
*/
'private getCurrentTime' : function() {
return new Date();
},
/**
* Verifies whether the specified attributes is a
* provided Attribute.
*
* @protected
* @alias isAttribute
* @memberof Widget#
* @param {AttributeValue}
* _attribute
* @returns {boolean}
*/
'protected isAttribute' : function(_attribute) {
var type = new AttributeType().withName(_attribute.getName())
.withType(_attribute.getType())
.withParameters(_attribute.getParameters());
if (this.attributeTypes.contains(type)) {
return true;
} else {
return false;
}
},
/**
* Initializes the provided Attributes.
*
* @function
* @abstract
* @protected
* @alias initAttributes
* @memberof Widget#
*/
'abstract protected initAttributes' : [],
/**
* Initializes the provided ConstantAttributes.
*
* @function
* @abstract
* @protected
* @alias initConstantAttributes
* @memberof Widget#
*/
'abstract protected initConstantAttributes' : [],
/**
* Initializes the provided Callbacks.
*
* @function
* @abstract
* @protected
* @alias initCallbacks
* @memberof Widget#
*/
'abstract protected initCallbacks' : [],
/**
* Function for initializing. Calls all initFunctions
* and will be called by the constructor.
*
* @protected
* @alias init
* @memberof Widget#
*/
'protected init' : function() {
this.initAttributes();
this.initConstantAttributes();
this.initCallbacks();
},
/**
* Notifies other components and sends the attributes.
* Must be overridden by the subclasses
*
* @virtual
* @public
* @alias initCallbacks
* @memberof Widget#
*/
'virtual public notify' : function() {
var callbacks = this.queryCallbacks().getItems();
for (var i in callbacks) {
this.sendToSubscriber(callbacks[i]);
}
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
},
/**
* Queries the associated sensor and updates the attributes with new values.
* Must be overridden by the subclasses.
*
* @virtual
* @public
* @alias queryGenerator
* @memberof Widget#
* @param {?function} _function For alternative actions, because an asynchronous function can be used.
*/
'virtual protected queryGenerator' : function(_function) {
},
/**
* Updates the attributes by calling queryGenerator.
*
* @public
* @alias updateWidgetInformation
* @memberof Widget#
* @param {?function} _function For alternative actions, because an asynchronous function can be used.
*
*/
'public updateWidgetInformation' : function(_function) {
this.queryGenerator(_function);
},
/**
* Updates the Attributes by external components.
*
* @virtual
* @public
* @alias putData
* @memberof Widget#
* @param {(AttributeValueList|Array)} _data Data that should be entered.
*
*/
'virtual public putData' : function(_data) {
var list = new Array();
if (_data instanceof Array) {
list = _data;
} else if (Class.isA(AttributeValueList, _data)) {
list = _data.getItems();
}
for ( var i in list) {
var x = list[i];
if (Class.isA(AttributeValue, x) && this.isAttribute(x)) {
this.addAttribute(x);
}
}
},
/**
* Returns all available AttributeValues, Attributes and
* ConstantAtrributes.
*
* @public
* @alias queryWidget
* @memberof Widget#
* @returns {AttributeValueList}
*/
'public queryWidget' : function() {
var response = new AttributeValueList();
response.putAll(this.queryAttributes());
response.putAll(this.queryConstantAttributes());
return response;
},
/**
* Updates and returns all available AttributeValues,
* Attributes and ConstantAtrributes.
*
* @public
* @alias updateAndQueryWidget
* @memberof Widget#
* @param {?function} _function For alternative actions, because an asynchronous function can be used.
* @returns {?AttributeValueList}
*/
'virtual public updateAndQueryWidget' : function(_function) {
if(_function && typeof(_function) === 'function'){
this.queryGenerator(_function);
} else {
this.queryGenerator();
var response = new AttributeValueList();
response.putAll(this.queryAttributes());
response.putAll(this.queryConstantAttributes());
return response;
}
},
/**
* Sends all Attributes, specified in the given callback,
* to components which are subscribed to this Callback.
* @protected
* @alias sendToSubscriber
* @memberof Widget#
* @param {string} _callbackName Name of the searched Callback.
*/
'protected sendToSubscriber' : function(_callback) {
if (_callback && Class.isA(Callback, _callback)) {
var subscriberList = this.subscribers.getItems();
for ( var i in subscriberList) {
var subscriber = subscriberList[i];
if (subscriber.getSubscriptionCallbacks().containsKey( _callback.getName())) {
console.log(subscriber.getSubscriberId());
if(this.dataValid(subscriber.getConditions())){
console.log('condition' + true);
var subscriberInstance = this.discoverer.getComponent(subscriber.getSubscriberId());
var callSubset = _callback.getAttributeTypes();
var subscriberSubset = subscriber.getAttributesSubset();
var data = this.attributes.getSubset(callSubset);
if (subscriberSubset && subscriberSubset.size() > 0) {
data = data.getSubset(subscriberSubset);
}
}
if (data) {
subscriberInstance.putData(data);
}
}
}
}
},
/**
* Verifies if the attributes match to the specified conditions in case any exists.
*
* @private
* @alias dataValid
* @memberof Widget#
* @param {string} _conditions List of Conditions that will be verified.
* @returns {boolean}
*/
'private dataValid' : function(_conditions) {
if (Class.isA(ConditionList, _conditions)) {
return true;
}
if (!_conditions.isEmpty()) {
var items = _condition.getItems();
for ( var i in items) {
var condition = items[i];
var conditionAttributeType = condition.getAttributeType();
var conditionAttributeTypeList = new AttributeTypeList()
.withItems(new Array(conditionAttributeType));
var newValue = this.queryAttributes().getSubset(conditionAttributeTypeList);
var oldValue = this.getOldAttributes.getSubset(conditionAttributeTypeList);
return condition.compare(newValue, oldValue);
}
}
return false;
},
/**
* Returns the description of this component.
* @virtual
* @public
* @alias getWidgetDescription
* @memberof Widget#
* @returns {WidgetDescription}
*/
'virtual public getWidgetDescription' : function() {
var description = new WidgetDescription().withId(this.id).withName(this.name);
description.setOutAttributeTypes(this.attributeTypes);
description.setOutAttributeTypes(this.constantAttributeTypes);
return description;
},
/**
* Runs the context acquisition constantly in an interval.
* Can be called by init.
*
* @virtual
* @protected
* @alias intervalRunning
* @memberof Widget#
* @param {integer} _interval Interval in ms
*/
'virtual protected intervalRunning' : function(_interval) {
var self = this;
if (_interval === parseInt(_interval)) {
setInterval(function() {self.queryGenerator();}, _interval);
}
},
/**
* Sets the associated Discoverer and registers to that.
* @public
* @alias setDiscoverer
* @memberof Widget#
* @param {Discoverer} _discoverer Discoverer
*/
'public setDiscoverer' : function(_discoverer) {
if (!this.discoverer) {
this.discoverer = _discoverer;
this.register();
}
},
/**
* Registers the component to the associated Discoverer.
*
* @public
* @alias register
* @memberof Widget#
*/
'protected register' : function() {
if (this.discoverer) {
this.discoverer.registerNewComponent(this);
}
// /**
// * Unregisters the component to the associated discoverer
// * and deletes the reference.
// *
// * @public
// * @alias register
// * @memberof Widget#
// */
// 'protected unregister' : function() {
// if (this.discoverer) {
// this.discoverer.unregisterComponent(this.getId());
// this.discoverer = null;
// }
// },
});
return Widget;
});