| 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 PropertyPublisher classes for the FG1000 MFD using Emesary |
| 18 |
# Publishes property values to Emesary for consumption by the MFD |
| 19 |
# |
| 20 |
# Two variants: |
| 21 |
# - PeriodicPropertyPublisher which publishes on a periodic basis |
| 22 |
# - TriggeredPropertyPublisher which publishes based on listening to properties |
| 23 |
# but also publishes all properties on a periodic basis to ensure new clients |
| 24 |
# receive property state. |
| 25 |
# |
| 26 |
|
| 27 |
var PropMap = { |
| 28 |
new : func(name, property, epsilon) |
| 29 |
{ |
| 30 |
var obj = { parents : [ PropMap ] }; |
| 31 |
obj._name = name; |
| 32 |
obj._prop = globals.props.getNode(property, 1); |
| 33 |
obj._epsilon = epsilon; |
| 34 |
obj._lastValue = nil; |
| 35 |
return obj; |
| 36 |
}, |
| 37 |
|
| 38 |
getName : func() { return me._name; }, |
| 39 |
getPropPath : func() { return me._prop.getPath(); }, |
| 40 |
getValue : func() { |
| 41 |
var val = me._prop.getValue(); |
| 42 |
if (val == nil) val = 0; |
| 43 |
return val; |
| 44 |
}, |
| 45 |
hasChanged : func() { |
| 46 |
var val = me._prop.getValue(); |
| 47 |
if (me._epsilon == nil) return 1; |
| 48 |
if ((me._lastValue == nil) and (val != nil)) return 1; |
| 49 |
if (! isnum(val) and val != me._lastValue) return 1; |
| 50 |
if (isnum(val) and abs(val - me._lastValue) > me._epsilon) return 1; |
| 51 |
return 0; |
| 52 |
}, |
| 53 |
updateValue : func() { me._lastValue = me.getValue(); }, |
| 54 |
getProp: func() { return me._prop; }, |
| 55 |
}; |
| 56 |
|
| 57 |
var PeriodicPropertyPublisher = |
| 58 |
{ |
| 59 |
new : func (notification, period=0.25) { |
| 60 |
var obj = { |
| 61 |
parents : [ PeriodicPropertyPublisher ], |
| 62 |
_notification : notification, |
| 63 |
_period : period, |
| 64 |
_propmaps : [], |
| 65 |
}; |
| 66 |
|
| 67 |
obj._transmitter = emesary.GlobalTransmitter; |
| 68 |
|
| 69 |
return obj; |
| 70 |
}, |
| 71 |
|
| 72 |
addPropMap : func(name, prop, epsilon=nil) { |
| 73 |
append(me._propmaps, PropMap.new(name, prop, epsilon)); |
| 74 |
}, |
| 75 |
|
| 76 |
publish : func() { |
| 77 |
var data = {}; |
| 78 |
var names = ""; |
| 79 |
|
| 80 |
foreach (var propmap; me._propmaps) { |
| 81 |
var name = propmap.getName(); |
| 82 |
if (propmap.hasChanged()) { |
| 83 |
data[name] = propmap.getValue(); |
| 84 |
propmap.updateValue(); |
| 85 |
names = sprintf("%s %s", names, name); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
if (size(data) > 0) { |
| 90 |
var notification = notifications.PFDEventNotification.new( |
| 91 |
"MFD", |
| 92 |
1, |
| 93 |
me._notification, |
| 94 |
data); |
| 95 |
|
| 96 |
me._transmitter.NotifyAll(notification); |
| 97 |
#print(sprintf("NOTIFY total of %i properties changed out of %i: %s", size(data), size(me._propmaps), names)); |
| 98 |
} |
| 99 |
}, |
| 100 |
|
| 101 |
start : func() { |
| 102 |
me._timer = maketimer(me._period, me, me.publish); |
| 103 |
me._timer.start(); |
| 104 |
}, |
| 105 |
stop : func() { |
| 106 |
if(me._timer != nil) me._timer.stop(); |
| 107 |
me._timer = nil; |
| 108 |
}, |
| 109 |
}; |
| 110 |
|
| 111 |
var TriggeredPropertyPublisher = |
| 112 |
{ |
| 113 |
new : func (notification, period=5) { |
| 114 |
var obj = { |
| 115 |
parents : [ TriggeredPropertyPublisher ], |
| 116 |
_notification : notification, |
| 117 |
_period : period, |
| 118 |
_propmaps : {}, |
| 119 |
_listeners : [], |
| 120 |
_timer: nil, |
| 121 |
}; |
| 122 |
|
| 123 |
obj._transmitter = emesary.GlobalTransmitter; |
| 124 |
|
| 125 |
return obj; |
| 126 |
}, |
| 127 |
|
| 128 |
addPropMap : func(name, prop, epsilon=nil) { |
| 129 |
me._propmaps[prop] = PropMap.new(name, prop, epsilon); |
| 130 |
}, |
| 131 |
|
| 132 |
publish : func(propNode) { |
| 133 |
var data = {}; |
| 134 |
var propmap = me._propmaps[propNode.getPath()]; |
| 135 |
assert(propmap != nil, "Unable to find property map for " ~ propNode.getPath()); |
| 136 |
if (propmap.hasChanged()) { |
| 137 |
data[propmap._name] = propNode.getValue(); |
| 138 |
propmap.updateValue(); |
| 139 |
|
| 140 |
var notification = notifications.PFDEventNotification.new( |
| 141 |
"MFD", |
| 142 |
1, |
| 143 |
me._notification, |
| 144 |
data); |
| 145 |
|
| 146 |
me._transmitter.NotifyAll(notification); |
| 147 |
} |
| 148 |
}, |
| 149 |
|
| 150 |
publishAll : func() { |
| 151 |
var data = {}; |
| 152 |
|
| 153 |
foreach (var prop; keys(me._propmaps)) { |
| 154 |
var propmap = me._propmaps[prop]; |
| 155 |
data[propmap._name] = propmap.getValue(); |
| 156 |
propmap.updateValue(); |
| 157 |
} |
| 158 |
|
| 159 |
var notification = notifications.PFDEventNotification.new( |
| 160 |
"MFD", |
| 161 |
1, |
| 162 |
me._notification, |
| 163 |
data); |
| 164 |
|
| 165 |
me._transmitter.NotifyAll(notification); |
| 166 |
}, |
| 167 |
|
| 168 |
|
| 169 |
start : func() { |
| 170 |
foreach (var prop; keys(me._propmaps)) { |
| 171 |
# Set up a listener triggering on create (to ensure all values are set at |
| 172 |
# start of day) and only on changed values. These are the last two |
| 173 |
# arguments to the setlistener call. |
| 174 |
var propmap = me._propmaps[prop]; |
| 175 |
var listener = setlistener(propmap.getPropPath(), func(p) { me.publish(p); }, 1, 1); |
| 176 |
append(me._listeners, listener); |
| 177 |
} |
| 178 |
|
| 179 |
me._timer = maketimer(me._period, me, me.publishAll); |
| 180 |
me._timer.start(); |
| 181 |
}, |
| 182 |
|
| 183 |
stop : func() { |
| 184 |
foreach (var l; me._listeners) { |
| 185 |
# In some circumstances we may not have a valid listener ID, so we |
| 186 |
# just ignore the problem. |
| 187 |
var err = []; |
| 188 |
call( func removelistener(l), nil, err); |
| 189 |
if (size(err)) print("Ignoring error : " ~ err[0]); |
| 190 |
} |
| 191 |
|
| 192 |
if(me._timer != nil) me._timer.stop(); |
| 193 |
me._timer = nil; |
| 194 |
}, |
| 195 |
}; |