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-10 23:11:38 +00:00
|
|
|
# Generic PropertyPublisher classes for the FG1000 MFD using Emesary
|
2018-01-05 16:37:39 +00:00
|
|
|
# Publishes property values to Emesary for consumption by the MFD
|
2018-01-10 23:11:38 +00:00
|
|
|
#
|
|
|
|
# Two variants:
|
|
|
|
# - PeriodicPropertyPublisher which publishes on a periodic basis
|
2018-06-07 18:01:18 +00:00
|
|
|
# - TriggeredPropertyPublisher which publishes based on listening to properties
|
|
|
|
# but also publishes all properties on a periodic basis to ensure new clients
|
|
|
|
# receive property state.
|
2018-01-10 23:11:38 +00:00
|
|
|
#
|
2018-01-05 16:37:39 +00:00
|
|
|
|
2018-01-10 23:11:38 +00:00
|
|
|
var PropMap = {
|
|
|
|
new : func(name, property)
|
|
|
|
{
|
|
|
|
var obj = { parents : [ PropMap ] };
|
|
|
|
obj._name = name;
|
|
|
|
obj._prop = globals.props.getNode(property, 1);
|
|
|
|
return obj;
|
|
|
|
},
|
2018-01-05 16:37:39 +00:00
|
|
|
|
2018-01-10 23:11:38 +00:00
|
|
|
getName : func() { return me._name; },
|
|
|
|
getPropPath : func() { return me._prop.getPath(); },
|
2018-02-26 21:24:21 +00:00
|
|
|
getValue : func() {
|
|
|
|
var val = me._prop.getValue();
|
|
|
|
if (val == nil) val = 0;
|
|
|
|
return val;
|
|
|
|
},
|
2018-01-10 23:11:38 +00:00
|
|
|
getProp: func() { return me._prop; },
|
|
|
|
};
|
2018-01-05 16:37:39 +00:00
|
|
|
|
2018-01-10 23:11:38 +00:00
|
|
|
var PeriodicPropertyPublisher =
|
|
|
|
{
|
|
|
|
new : func (notification, frequency=0.25) {
|
2018-01-05 16:37:39 +00:00
|
|
|
var obj = {
|
2018-01-10 23:11:38 +00:00
|
|
|
parents : [ PeriodicPropertyPublisher ],
|
|
|
|
_notification : notification,
|
2018-01-05 16:37:39 +00:00
|
|
|
_frequency : frequency,
|
|
|
|
_propmaps : [],
|
|
|
|
};
|
|
|
|
|
2018-01-10 23:11:38 +00:00
|
|
|
obj._transmitter = emesary.GlobalTransmitter;
|
2018-01-05 16:37:39 +00:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
},
|
|
|
|
|
|
|
|
addPropMap : func(name, prop) {
|
2018-01-10 23:11:38 +00:00
|
|
|
append(me._propmaps, PropMap.new(name, prop));
|
2018-01-05 16:37:39 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
publish : func() {
|
2018-01-10 23:11:38 +00:00
|
|
|
var data = {};
|
|
|
|
|
|
|
|
foreach (var propmap; me._propmaps) {
|
|
|
|
var name = propmap.getName();
|
|
|
|
data[name] = propmap.getValue();
|
|
|
|
}
|
2018-01-05 16:37:39 +00:00
|
|
|
|
|
|
|
var notification = notifications.PFDEventNotification.new(
|
|
|
|
"MFD",
|
|
|
|
1,
|
2018-01-10 23:11:38 +00:00
|
|
|
me._notification,
|
2018-01-05 16:37:39 +00:00
|
|
|
data);
|
|
|
|
|
|
|
|
me._transmitter.NotifyAll(notification);
|
|
|
|
},
|
|
|
|
|
|
|
|
start : func() {
|
|
|
|
me._timer = maketimer(me._frequency, me, me.publish);
|
|
|
|
me._timer.start();
|
|
|
|
},
|
|
|
|
stop : func() {
|
|
|
|
if(me._timer != nil) me._timer.stop();
|
|
|
|
me._timer = nil;
|
|
|
|
},
|
|
|
|
};
|
2018-01-10 23:11:38 +00:00
|
|
|
|
|
|
|
var TriggeredPropertyPublisher =
|
|
|
|
{
|
2018-06-07 18:01:18 +00:00
|
|
|
new : func (notification, frequency=5) {
|
2018-01-10 23:11:38 +00:00
|
|
|
var obj = {
|
|
|
|
parents : [ TriggeredPropertyPublisher ],
|
|
|
|
_notification : notification,
|
2018-06-07 18:01:18 +00:00
|
|
|
_frequency : frequency,
|
2018-01-10 23:11:38 +00:00
|
|
|
_propmaps : {},
|
|
|
|
_listeners : [],
|
2018-06-07 18:01:18 +00:00
|
|
|
_timer: nil,
|
2018-01-10 23:11:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
obj._transmitter = emesary.GlobalTransmitter;
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
},
|
|
|
|
|
|
|
|
addPropMap : func(name, prop) {
|
|
|
|
me._propmaps[prop] = name;
|
|
|
|
},
|
|
|
|
|
|
|
|
publish : func(propNode) {
|
|
|
|
var data = {};
|
|
|
|
var name = me._propmaps[propNode.getPath()];
|
|
|
|
assert(name != nil, "Unable to find property map for " ~ name);
|
|
|
|
data[name] = propNode.getValue();
|
|
|
|
|
|
|
|
var notification = notifications.PFDEventNotification.new(
|
|
|
|
"MFD",
|
|
|
|
1,
|
|
|
|
me._notification,
|
|
|
|
data);
|
|
|
|
|
|
|
|
me._transmitter.NotifyAll(notification);
|
|
|
|
},
|
|
|
|
|
2018-06-07 18:01:18 +00:00
|
|
|
publishAll : func() {
|
|
|
|
var data = {};
|
|
|
|
|
|
|
|
foreach (var prop; keys(me._propmaps)) {
|
|
|
|
var name = me._propmaps[prop];
|
|
|
|
var value = props.globals.getNode(prop, 1).getValue();
|
|
|
|
data[name] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
var notification = notifications.PFDEventNotification.new(
|
|
|
|
"MFD",
|
|
|
|
1,
|
|
|
|
me._notification,
|
|
|
|
data);
|
|
|
|
|
|
|
|
me._transmitter.NotifyAll(notification);
|
|
|
|
},
|
|
|
|
|
|
|
|
|
2018-01-10 23:11:38 +00:00
|
|
|
start : func() {
|
|
|
|
foreach (var prop; keys(me._propmaps)) {
|
|
|
|
# Set up a listener triggering on create (to ensure all values are set at
|
|
|
|
# start of day) and only on changed values. These are the last two
|
|
|
|
# arguments to the setlistener call.
|
2018-03-25 16:55:03 +00:00
|
|
|
var listener = setlistener(prop, func(p) { me.publish(p); }, 1, 1);
|
2018-01-10 23:11:38 +00:00
|
|
|
append(me._listeners, listener);
|
|
|
|
}
|
2018-06-07 18:01:18 +00:00
|
|
|
|
|
|
|
me._timer = maketimer(me._frequency, me, me.publishAll);
|
|
|
|
me._timer.start();
|
2018-01-10 23:11:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
stop : func() {
|
2018-06-07 18:01:18 +00:00
|
|
|
foreach (var l; me._listeners) {
|
2018-01-10 23:11:38 +00:00
|
|
|
removelistener(l);
|
2018-06-07 18:01:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(me._timer != nil) me._timer.stop();
|
|
|
|
me._timer = nil;
|
2018-01-10 23:11:38 +00:00
|
|
|
},
|
|
|
|
};
|