Newer
Older
/**
* This module representing a Context Widget.
*
* @module Widget
* @fileOverview
*/
define([ 'easejs', 'MathUuid', 'callback', 'callbackList', 'attribute',
'attributeList', 'conditionList',
function(easejs, MathUuid, Callback, CallbackList, Attribute,
AttributeList, ConditionList,
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
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(_discoverer, _attributeTypes) {
this.discoverer = _discoverer;
this.register();
this.attributeTypes = new AttributeList();
this.constantAttributeTypes = new AttributeList();
this.attributes = new AttributeList();
this.constantAttributes = new AttributeList();
this.subscribers = new SubscriberList();
this.callbacks = new CallbackList();
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
},
/**
* 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
* @memberof Widget#
* @returns {AttributeTypeList}
*/
'public getAttributeTypes' : function() {
return this.attributeTypes;
},
/**
* Returns the available ConstantAttributeTypes
* (attributes that do not change).
*
* @public
* @alias getWidgetConstantAttributeTypes
* @memberof Widget#
* @returns {AttributeList}
*/
'public getWidgetConstantAttributeTypes' : function() {
return this.constantAttributeTypes;
},
/**
* Returns the last acquired attribute values.
* @param {AttributeList} _attributeList
* @returns {AttributeList}
'public getAttributeValues' : function(_attributeList) {
if (Class.isA(AttributeList, _attributeList)) {
return this.attributes.getSubset(_attributeList);
} else {
return this.attributes;
}
/**
* Returns the last acquired attribute value with the given attribute type.
*
* @param {AttributeType} _attributeType The attribute type to return the last value for.
* @returns {*}
*/
'public getAttributeValue': function(_attributeType) {
return this.getAttributeValues().getItemForAttributeType(_attributeType).getValue();
},
/**
* Returns the old Attributes.
*
* @private
* @alias getOldAttributes
* @memberof Widget#
* @returns {AttributeList}
*/
'public getOldAttributes' : function() {
return this.oldAttributes;
},
/**
* Returns the ConstantAttributes.
*
* @public
* @alias getConstantAttributes
* @returns {AttributeList}
'public getConstantAttributes' : function() {
* Returns a list of callbacks that can be
* subscribed to.
* @memberof Widget#
* @returns {CallbackList}
*/
'public getCallbackList' : function() {
/**
* Returns the specified callbacks that can be
* subscribed to.
*
* @public
* @alias getCallbacks
* @memberof Widget#
* @returns {Array}
*/
'public getCallbacks' : function() {
return this.callbacks.getItems();
},
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
'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.reduce(function(o, v, i) {
o[i] = v;
return o;
}, {});
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
} 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 {Attribute} _attribute AttributeValue
if (Class.isA(Attribute, _attribute)) {
var type = new Attribute().withName(_attribute.getName())
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
.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);
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
},
'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.removeSubscriberWithId(_subscriberId);
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
},
/**
* 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) {
return !!this.attributeTypes.containsTypeOf(_attribute);
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
},
/**
* 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(_attributeTypes) {
this.initAttributes();
this.initConstantAttributes();
this.initCallbacks();
this.didFinishInitialization(_attributeTypes);
/**
* Method will be invoked after the initialization of the widget finished.
* Can be overridden by inheriting classes to take action after initialization.
*
* @public
* @virtual
* @alias didFinishInitialization
* @memberof Widget#
* @param _attributeTypes
*/
'public virtual didFinishInitialization' : function(_attributeTypes) {
/**
* Notifies other components and sends the attributes.
*
* @virtual
* @public
* @alias initCallbacks
* @memberof Widget#
*/
'virtual public notify' : function() {
var callbacks = this.getCallbacks();
for (var i in callbacks) {
this.sendToSubscriber(callbacks[i]);
}
},
/**
* Queries the associated sensor and updates the attributes with new values.
* Must be overridden by the subclasses. Overriding subclasses can call
* this.__super(_function) to invoke the provided callback function.
*
* @virtual
* @public
* @alias queryGenerator
* @memberof Widget#
* @param {?function} _function For alternative actions, because an asynchronous function can be used.
*/
'virtual protected queryGenerator' : function(_function) {
if (_function && typeof(_function) == 'function') {
_function();
}
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
},
/**
* 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) {
} else if (Class.isA(AttributeList, _data)) {
list = _data.getItems();
}
for ( var i in list) {
var x = list[i];
if (Class.isA(Attribute, x) && this.isAttribute(x)) {
this.addAttribute(x);
}
}
},
/**
* Returns all available AttributeValues, Attributes and
* ConstantAtrributes.
*
* @public
* @alias queryWidget
* @memberof Widget#
* @returns {AttributeList}
var response = new AttributeList();
response.putAll(this.getAttributeValues());
response.putAll(this.getConstantAttributes());
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 {?AttributeList}
*/
'virtual public updateAndQueryWidget' : function(_function) {
if(_function && typeof(_function) === 'function'){
this.queryGenerator(_function);
} else {
this.queryGenerator();
var response = new AttributeList();
response.putAll(this.getAttributeValues());
response.putAll(this.getConstantAttributes());
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().contains(_callback)) {
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
if(this.dataValid(subscriber.getConditions())){
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.getAttributes().getSubset(conditionAttributeTypeList);
var oldValue = this.getOldAttributes.getSubset(conditionAttributeTypeList);
return condition.compare(newValue, oldValue);
}
}
return false;
},
/**
* Returns the description of this component.
* @virtual
* @public
* @memberof Widget#
* @returns {WidgetDescription}
*/
'virtual public getDescription' : function() {
var description = new WidgetDescription().withId(this.id).withName(this.name);
description.addOutAttributeTypes(this.attributeTypes);
description.addOutAttributeTypes(this.constantAttributeTypes);
var widgetCallbacks = this.callbacks.getItems();
for(var i in widgetCallbacks) {
description.addCallbackName(widgetCallbacks[i].getName());
}
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
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;
});