2018-01-05 16:37:39 +00:00
|
|
|
# Generic class to update properties from Emesary for the MFD
|
|
|
|
#
|
|
|
|
# In the simplest cases where the Emesary EventParameter for the specified
|
|
|
|
# Event ID is a hash whose values can be mapped directly to property values,
|
|
|
|
# it can be used directly. For more complex cases, the handleNotificationEvent
|
|
|
|
# method should be over-ridden, and should return
|
|
|
|
# emesary.Transmitter.ReceiptStatus_OK or equivalent ReceiptStatus value.
|
|
|
|
|
|
|
|
var PropertyUpdater =
|
|
|
|
{
|
|
|
|
PropMap : {
|
|
|
|
new : func(name, property)
|
|
|
|
{
|
|
|
|
var obj = { parents : [ PropertyUpdater.PropMap ] };
|
|
|
|
obj._name = name;
|
|
|
|
obj._prop = globals.props.getNode(property, 1);
|
|
|
|
return obj;
|
|
|
|
},
|
|
|
|
|
2018-01-13 18:53:06 +00:00
|
|
|
getName : func() { return me._name; },
|
|
|
|
getValue : func() { return me._prop.getValue(); },
|
|
|
|
setValue : func(val) { me._prop.setValue(val); },
|
2018-01-05 16:37:39 +00:00
|
|
|
},
|
|
|
|
|
2018-01-13 18:53:06 +00:00
|
|
|
new : func (device, notificationType, eventID) {
|
2018-01-05 16:37:39 +00:00
|
|
|
var obj = {
|
2018-01-13 18:53:06 +00:00
|
|
|
parents : [ PropertyUpdater ],
|
2018-01-05 16:37:39 +00:00
|
|
|
_device : device,
|
|
|
|
_notificationType : notificationType,
|
2018-01-13 18:53:06 +00:00
|
|
|
_eventID : eventID,
|
|
|
|
_recipient : nil,
|
|
|
|
_transmitter : nil,
|
|
|
|
_registered : 0,
|
2018-01-05 16:37:39 +00:00
|
|
|
_propmaps : {},
|
|
|
|
};
|
|
|
|
|
2018-01-13 18:53:06 +00:00
|
|
|
obj._transmitter = emesary.GlobalTransmitter;
|
2018-01-05 16:37:39 +00:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
},
|
|
|
|
|
|
|
|
addPropMap : func(name, prop) {
|
2018-01-13 18:53:06 +00:00
|
|
|
me._propmaps[name] = PropertyUpdater.PropMap.new(name, prop);
|
2018-01-05 16:37:39 +00:00
|
|
|
},
|
|
|
|
|
2018-01-13 18:53:06 +00:00
|
|
|
handleNotificationEvent : func(eventParameters) {
|
2018-01-05 16:37:39 +00:00
|
|
|
|
|
|
|
var retval = emesary.Transmitter.ReceiptStatus_NotProcessed;
|
|
|
|
foreach(var name; keys(eventParameters)) {
|
2018-01-13 18:53:06 +00:00
|
|
|
var value = eventParameters[name];
|
2018-01-05 16:37:39 +00:00
|
|
|
if (me._propmaps[name] != nil) {
|
2018-01-13 18:53:06 +00:00
|
|
|
if (me._propmaps[name].getValue() != value) {
|
|
|
|
# Only update on a true change. Otherwise if there is a Publisher
|
|
|
|
# on this property, we risk creating a never ending loop between
|
|
|
|
# the Publisher and this Updater
|
|
|
|
me._propmaps[name].setValue(value);
|
|
|
|
}
|
2018-01-05 16:37:39 +00:00
|
|
|
retval = emesary.Transmitter.ReceiptStatus_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
},
|
|
|
|
|
|
|
|
RegisterWithEmesary : func(){
|
|
|
|
|
|
|
|
if (me._recipient == nil){
|
2018-01-13 18:53:06 +00:00
|
|
|
me._recipient = emesary.Recipient.new("PropertyUpdater");
|
2018-01-05 16:37:39 +00:00
|
|
|
var pfd_obj = me._device;
|
2018-01-13 18:53:06 +00:00
|
|
|
var notificationtype = me._notificationType;
|
|
|
|
var eventID = me._eventID;
|
2018-01-05 16:37:39 +00:00
|
|
|
var controller = me;
|
|
|
|
me._recipient.Receive = func(notification)
|
|
|
|
{
|
|
|
|
if (notification.Device_Id == pfd_obj.device_id
|
2018-01-13 18:53:06 +00:00
|
|
|
and notification.NotificationType == notificationtype) {
|
|
|
|
if (notification.Event_Id == eventID
|
2018-01-05 16:37:39 +00:00
|
|
|
and notification.EventParameter != nil)
|
|
|
|
{
|
2018-01-13 18:53:06 +00:00
|
|
|
return controller.handleNotificationEvent(notification.EventParameter);
|
2018-01-05 16:37:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return emesary.Transmitter.ReceiptStatus_NotProcessed;
|
|
|
|
};
|
|
|
|
}
|
2018-01-13 18:53:06 +00:00
|
|
|
me._transmitter.Register(me._recipient);
|
|
|
|
me._registered = 1;
|
2018-01-05 16:37:39 +00:00
|
|
|
},
|
|
|
|
DeRegisterWithEmesary : func(transmitter = nil){
|
2018-01-13 18:53:06 +00:00
|
|
|
if (me._registered == 1) me._transmitter.DeRegister(me._recipient);
|
|
|
|
me._registered = 0;
|
2018-01-05 16:37:39 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
start : func() {
|
|
|
|
me.RegisterWithEmesary();
|
|
|
|
},
|
|
|
|
stop : func() {
|
|
|
|
me.DeRegisterWithEmesary();
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|