Newer
Older
/**
* This module representing a Context Widget.
*
* @module Widget
* @fileOverview
*/
define([ 'easejs', 'MathUuid', 'callback', 'callbackList', 'attribute',
'attributeList', 'conditionList', 'subscriber', 'subscriberList'],
function(easejs, MathUuid, Callback, CallbackList, Attribute,
AttributeList, ConditionList, Subscriber, SubscriberList) {
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 attributes
* @protected
* @type {AttributeList}
* @memberof Widget#
* @desc All available Attributes and their values.
*/
'protected outAttributes' : [],
* @type {AttributeList}
* @memberof Widget#
* @desc This temporary variable is used for storing the old attribute values.
* So these can be used to check conditions.
*/
'protected oldOutAttributes' : [],
* @type {AttributeList}
* @memberof Widget#
* @desc All available constant Attributes and their values.
*/
'protected constantOutAttributes' : [],
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
/**
* @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 Attribute
* @requires AttributeList
* @requires ConditionList
* @requires Subscriber
* @requires SubscriberList
* @requires Discoverer
* @constructs Widget
*/
'virtual public __construct' : function(_discoverer, _attributeTypes) {
this.discoverer = _discoverer;
this.register();
this.outAttributes = new AttributeList();
this.constantOutAttributes = new AttributeList();
this.subscribers = new SubscriberList();
this.callbacks = new CallbackList();
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
},
/**
* 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 getAttributes
* @returns {AttributeList}
'public getOutAttributes' : function(_attributeList) {
if (Class.isA(AttributeList, _attributeList)) {
return this.outAttributes.getSubset(_attributeList);
} else {
return this.outAttributes;
}
},
/**
* Returns the available ConstantAttributeTypes
* (attributes that do not change).
*
* @public
* @alias getWidgetConstantAttributeTypes
* @memberof Widget#
* @returns {AttributeList}
'public getConstantOutAttributes' : function(_attributeList) {
if (Class.isA(AttributeList, _attributeList)) {
return this.constantOutAttributes.getSubset(_attributeList);
} else {
return this.constantOutAttributes;
}
/**
* 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 getValueForAttributeWithTypeOf': function(_attributeType) {
return this.getOutAttributes().getAttributeWithTypeOf(_attributeType).getValue();
},
/**
* Returns the old Attributes.
*
* @private
* @alias getOldAttributes
* @memberof Widget#
* @returns {AttributeList}
return this.oldOutAttributes;
* 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();
},
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
'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 {(AttributeList|Array)} _attributes List or Array of AttributeValues
'protected setOutAttributes' : function(_attributes) {
var list = [];
list = _attributes.reduce(function(o, v, i) {
o[i] = v;
return o;
}, {});
} else if (Class.isA(AttributeValueList,_attributes)) {
list = _attributes.getItems();
}
this.oldOutAttributes = this.outAttributes;
for ( var i in list) {
var attribute = list[i];
if (Class.isA(AttributeValue, attribute)) {
attribute.setTimestamp(this.getCurrentTime());
this.outAttributes.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 addOutAttribute
* @param {Attribute} _attribute AttributeValue
'public addOutAttribute' : function(_attribute, _multipleInstances) {
_multipleInstances = typeof _multipleInstances == "undefined" ? false : _multipleInstances;
if (Class.isA(Attribute, _attribute)) {
if (!this.outAttributes.containsTypeOf(_attribute)) {
this.oldOutAttributes = this.outAttributes;
_attribute.setTimestamp(this.getCurrentTime());
this.outAttributes.put(_attribute, _multipleInstances);
}
}
},
/**
* Sets the ConstantAttributeValueList and also the
* associated AttributeTypes.
*
* @protected
* @alias setConstantOutAttributes
* @param {(AttributeList|Array)} _constantAttributes List or Array of AttributeValues
'protected setConstantOutAttributes' : function(_constantAttributes) {
var list = [];
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 addConstantOutAttribute
* @param {AttributeValue} _constantAttribute AttributeValue
'protected addConstantOutAttribute' : function(_constantAttribute) {
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
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);
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
},
'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} _subscriberId Subscriber
*/
'public removeSubscriber' : function(_subscriberId) {
this.subscribers.removeSubscriberWithId(_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 isOutAttribute
* @param {Attribute} _attribute
'protected isOutAttribute' : function(_attribute) {
return !!this.outAttributes.containsTypeOf(_attribute);
},
/**
* Initializes the provided Attributes.
*
* @function
* @abstract
* @protected
* @alias initAttributes
* @memberof Widget#
*/
'abstract protected initOutAttributes' : [],
/**
* Initializes the provided ConstantAttributes.
*
* @function
* @abstract
* @protected
* @alias initConstantAttributes
* @memberof Widget#
*/
'abstract protected initConstantOutAttributes' : [],
/**
* 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.initOutAttributes();
this.initConstantOutAttributes();
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();
}
},
/**
* 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 {(AttributeList|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.isOutAttribute(x)) {
this.addOutAttribute(x);
}
}
},
/**
* Returns all available AttributeValues, Attributes and
* ConstantAtrributes.
*
* @public
* @alias queryWidget
* @memberof Widget#
* @returns {AttributeList}
var response = new AttributeList();
response.putAll(this.getOutAttributes());
response.putAll(this.getConstantOutAttributes());
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();
return this.queryWidget();
}
},
/**
* Sends all Attributes, specified in the given callback,
* to components which are subscribed to this Callback.
* @protected
* @alias sendToSubscriber
* @memberof Widget#
* @param {string} _callback 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) {
if (subscriber.getSubscriptionCallbacks().contains(_callback)) {
if(this.dataValid(subscriber.getConditions())){
var subscriberInstance = this.discoverer.getComponent(subscriber.getSubscriberId());
var callSubset = _callback.getAttributeTypes();
var subscriberSubset = subscriber.getAttributesSubset();
var data = this.outAttributes.getSubset(callSubset);
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
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);
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
var oldValue = this.getOldAttributes.getSubset(conditionAttributeTypeList);
return condition.compare(newValue, oldValue);
}
}
return false;
},
/**
* 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);
}
/**
* Returns true if the widget can satisfy the requested attribute type.
*
* @public
* @alias doesSatisfyAttributeType
* @memberof Widget#
* @param {AttributeType} _attribute
* @returns {boolean}
*/
'virtual public doesSatisfyAttributeType': function(_attribute) {
return this.outAttributes.containsTypeOf(_attribute);
}