| 1 |
# Copyright 2018 Stuart Buchanan |
| 2 |
# This file is part of FlightGear. |
| 3 |
# |
| 4 |
# FlightGear is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 2 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# FlightGear is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with FlightGear. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
# |
| 17 |
# Generic class to update properties from Emesary for the MFD |
| 18 |
# |
| 19 |
# In the simplest cases where the Emesary EventParameter for the specified |
| 20 |
# Event ID is a hash whose values can be mapped directly to property values, |
| 21 |
# it can be used directly. For more complex cases, the handleNotificationEvent |
| 22 |
# method should be over-ridden, and should return |
| 23 |
# emesary.Transmitter.ReceiptStatus_OK or equivalent ReceiptStatus value. |
| 24 |
|
| 25 |
var PropertyUpdater = |
| 26 |
{ |
| 27 |
PropMap : { |
| 28 |
new : func(name, property) |
| 29 |
{ |
| 30 |
var obj = { parents : [ PropertyUpdater.PropMap ] }; |
| 31 |
obj._name = name; |
| 32 |
obj._prop = globals.props.getNode(property, 1); |
| 33 |
return obj; |
| 34 |
}, |
| 35 |
|
| 36 |
getName : func() { return me._name; }, |
| 37 |
getValue : func() { return me._prop.getValue(); }, |
| 38 |
setValue : func(val) { me._prop.setValue(val); }, |
| 39 |
}, |
| 40 |
|
| 41 |
new : func (notificationType, eventID) { |
| 42 |
var obj = { |
| 43 |
parents : [ PropertyUpdater ], |
| 44 |
_notificationType : notificationType, |
| 45 |
_eventID : eventID, |
| 46 |
_recipient : nil, |
| 47 |
_transmitter : nil, |
| 48 |
_registered : 0, |
| 49 |
_propmaps : {}, |
| 50 |
}; |
| 51 |
|
| 52 |
obj._transmitter = emesary.GlobalTransmitter; |
| 53 |
|
| 54 |
return obj; |
| 55 |
}, |
| 56 |
|
| 57 |
addPropMap : func(name, prop) { |
| 58 |
me._propmaps[name] = PropertyUpdater.PropMap.new(name, prop); |
| 59 |
}, |
| 60 |
|
| 61 |
handleNotificationEvent : func(eventParameters) { |
| 62 |
|
| 63 |
var retval = emesary.Transmitter.ReceiptStatus_NotProcessed; |
| 64 |
foreach(var name; keys(eventParameters)) { |
| 65 |
var value = eventParameters[name]; |
| 66 |
if (me._propmaps[name] != nil) { |
| 67 |
if (me._propmaps[name].getValue() != value) { |
| 68 |
# Only update on a true change. Otherwise if there is a Publisher |
| 69 |
# on this property, we risk creating a never ending loop between |
| 70 |
# the Publisher and this Updater |
| 71 |
me._propmaps[name].setValue(value); |
| 72 |
} |
| 73 |
retval = emesary.Transmitter.ReceiptStatus_OK; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
return retval; |
| 78 |
}, |
| 79 |
|
| 80 |
RegisterWithEmesary : func(){ |
| 81 |
|
| 82 |
if (me._recipient == nil){ |
| 83 |
me._recipient = emesary.Recipient.new("PropertyUpdater"); |
| 84 |
var notificationtype = me._notificationType; |
| 85 |
var eventID = me._eventID; |
| 86 |
var controller = me; |
| 87 |
# Note that we don't care about the device we receive the update from. |
| 88 |
me._recipient.Receive = func(notification) |
| 89 |
{ |
| 90 |
if (notification.NotificationType == notificationtype and |
| 91 |
notification.Event_Id == eventID and |
| 92 |
notification.EventParameter != nil) |
| 93 |
{ |
| 94 |
return controller.handleNotificationEvent(notification.EventParameter); |
| 95 |
} |
| 96 |
return emesary.Transmitter.ReceiptStatus_NotProcessed; |
| 97 |
}; |
| 98 |
} |
| 99 |
me._transmitter.Register(me._recipient); |
| 100 |
me._registered = 1; |
| 101 |
}, |
| 102 |
DeRegisterWithEmesary : func(transmitter = nil){ |
| 103 |
if (me._registered == 1) me._transmitter.DeRegister(me._recipient); |
| 104 |
me._registered = 0; |
| 105 |
}, |
| 106 |
|
| 107 |
start : func() { |
| 108 |
me.RegisterWithEmesary(); |
| 109 |
}, |
| 110 |
stop : func() { |
| 111 |
me.DeRegisterWithEmesary(); |
| 112 |
}, |
| 113 |
|
| 114 |
}; |