2018-02-09 18:55:53 +00:00
|
|
|
# Copyright 2018 Stuart Buchanan
|
|
|
|
# This file is part of FlightGear.
|
|
|
|
#
|
2018-05-28 19:15:57 +00:00
|
|
|
# FlightGear is free software: you can redistribute it and/or modify
|
2018-02-09 18:55:53 +00:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# FlightGear is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with FlightGear. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
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-02-09 15:01:33 +00:00
|
|
|
new : func (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
|
|
|
_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");
|
|
|
|
var notificationtype = me._notificationType;
|
|
|
|
var eventID = me._eventID;
|
2018-01-05 16:37:39 +00:00
|
|
|
var controller = me;
|
2018-02-04 21:40:37 +00:00
|
|
|
# Note that we don't care about the device we receive the update from.
|
2018-01-05 16:37:39 +00:00
|
|
|
me._recipient.Receive = func(notification)
|
|
|
|
{
|
2018-02-04 21:40:37 +00:00
|
|
|
if (notification.NotificationType == notificationtype and
|
|
|
|
notification.Event_Id == eventID 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();
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|