Debian Codegraph @

Nasal/FailureMgr/public.nas

raw
1 # Failure Manager public interface
2 #
3 # Copyright (C) 2014 Anton Gomez Alvedro
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation; either version 2 of the
8 # License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19
20 var proproot = "sim/failure-manager/";
21
22
23 ##
24 # Nasal modules can subscribe to FailureMgr events.
25 # Each event has an independent dispatcher so modules can subscribe only to
26 # the events they are interested in. This also simplifies processing at client
27 # side by being able to subscibe different callbacks to different events.
28 #
29 # Example:
30 #
31 # var handle = FailureMgr.events["trigger-fired"].subscribe(my_callback);
32
33 var events = {};
34
35 # Event: trigger-fired
36 # Format: { mode_id: <failure mode id>, trigger: <trigger that fired> }
37 events["trigger-fired"] = globals.events.EventDispatcher.new();
38
39 ##
40 # Encodes a pair "category" and "failure_mode" into a "mode_id".
41 #
42 # These just have the simple form "category/mode", and are used to refer to
43 # failure modes throughout the FailureMgr API and to create a path within the
44 # sim/failure-manager property tree for the failure mode.
45 #
46 # examples of categories:
47 # structural, instrumentation, controls, sensors, etc...
48 #
49 # examples of failure modes:
50 # altimeter, pitot, left-tire, landing-light, etc...
51
52 var get_id = func(category, failure_mode) {
53 return sprintf("%s/%s", string.normpath(category), failure_mode);
54 }
55
56 ##
57 # Returns a vector containing: [category, failure_mode]
58
59 var split_id = func(mode_id) {
60 return [string.normpath(io.dirname(mode_id)), io.basename(mode_id)];
61 }
62
63 ##
64 # Subscribe a new failure mode to the system.
65 #
66 # id: Unique identifier for this failure mode.
67 # eg: "engine/carburetor-ice"
68 #
69 # description: Short text description, suitable for printing to the user.
70 # eg: "Ice in the carburetor"
71 #
72 # actuator: Object implementing the FailureActuator interface.
73 # Used by the failure manager to apply a certain level of
74 # failure to the failure mode.
75
76 var add_failure_mode = func(id, description, actuator) {
77 _failmgr.add_failure_mode(
78 FailureMode.new(id, description, actuator));
79 }
80
81 ##
82 # Returns a vector with all failure modes in the system.
83 # Each vector entry is a hash with the following keys:
84 # { id, description }
85
86 var get_failure_modes = func() {
87 _failmgr.get_failure_modes();
88 }
89
90 ##
91 # Remove a failure mode from the system.
92 # id: FailureMode id string, e.g. "systems/pitot"
93
94 var remove_failure_mode = func(id) {
95 _failmgr.remove_failure_mode(id);
96 }
97
98 ##
99 # Removes all failure modes from the failure manager.
100
101 var remove_all = func {
102 _failmgr.remove_all();
103 }
104
105 ##
106 # Attaches a trigger to the given failure mode. Discards the current trigger
107 # if any.
108 #
109 # mode_id: FailureMode id string, e.g. "systems/pitot"
110 # trigger: Trigger object or nil. Nil will just detach the current trigger
111
112 var set_trigger = func(mode_id, trigger) {
113 _failmgr.set_trigger(mode_id, trigger);
114 }
115
116 ##
117 # Returns the trigger object attached to the given failure mode.
118 # mode_id: FailureMode id string, e.g. "systems/pitot"
119
120 var get_trigger = func(mode_id) {
121 _failmgr.get_trigger(mode_id);
122 }
123
124 ##
125 # Applies a certain level of failure to this failure mode.
126 #
127 # mode_id: Failure mode id string.
128 # level: Floating point number in the range [0, 1]
129 # Zero represents no failure and one means total failure.
130
131 var set_failure_level = func(mode_id, level) {
132 setprop(proproot ~ mode_id ~ "/failure-level", level);
133 }
134
135 ##
136 # Returns the current failure level for the given failure mode.
137 # mode_id: Failure mode id string.
138
139 var get_failure_level = func(mode_id) {
140 getprop(proproot ~ mode_id ~ "/failure-level");
141 }
142
143 ##
144 # Restores all failure modes to level = 0
145
146 var repair_all = func {
147 _failmgr.repair_all();
148 }
149
150 ##
151 # Returns a vector of timestamped failure manager events, such as the
152 # messages shown in the console when there are changes to the failure conditions.
153 #
154 # Each entry in the vector has the following format:
155 # { time: <time stamp>, message: <event description> }
156
157 var get_log_buffer = func {
158 _failmgr.logbuf.get_buffer();
159 }
160
161 ##
162 # Allows applications to disable the failure manager and restore it later on.
163 # While disabled, no failure modes will be activated from the failure manager.
164
165 var enable = func setprop(proproot ~ "enabled", 1);
166 var disable = func setprop(proproot ~ "enabled", 0);
167
168 ##
169 # Encapsulates a condition that when met, will make the failure manager to
170 # apply a certain level of failure to the failure mode it is bound to.
171 #
172 # Two types of triggers are supported: pollable and asynchronous.
173 #
174 # Pollable triggers require periodic check for trigger conditions. For example,
175 # an altitude trigger will need to poll current altitude until the fire
176 # condition is reached.
177 #
178 # Asynchronous trigger do not require periodic updates. They can detect
179 # the firing condition by themselves by using timers or listeners.
180 # Async triggers must call the inherited method on_fire() to let the Failure
181 # Manager know about the fired condition.
182 #
183 # See Aircraft/Generic/Systems/failures.nas for concrete examples of triggers.
184
185 var Trigger = {
186
187 type: nil,
188 # 1 for pollable triggers, 0 for async triggers.
189 requires_polling: 0,
190 enabled: 0,
191
192 new: func {
193 return {
194 parents: [Trigger],
195 params: {},
196 armed: 0,
197 fired: 0,
198
199 ##
200 # Async triggers shall call the on_fire() callback when their fire
201 # conditions are met to notify the failure manager.
202 on_fire: func 0,
203
204 _path: nil
205 };
206 },
207
208 ##
209 # Forces a check of the firing conditions. Returns 1 if the trigger fired,
210 # 0 otherwise.
211
212 update: func 0,
213
214 ##
215 # Returns a printable string describing the trigger condition.
216
217 to_str: func "undefined trigger",
218
219 ##
220 # Modify a trigger parameter. Parameters will take effect after the next
221 # call to reset()
222
223 set_param: func(param, value) {
224 assert(me._path != nil, "Trigger.set_param: unbound trigger");
225
226 contains(me.params, param) or
227 die("Trigger.set_param: undefined param: " ~ param);
228
229 setprop(sprintf("%s/%s",me._path, param), value);
230 },
231
232 ##
233 # Load trigger parameters and reset internal state. Once armed, the trigger
234 # will fire as soon as the right conditions are met. It can be called after
235 # the trigger fires to rearm it again.
236 #
237 # The "armed" condition survives enable/disable calls.
238
239 arm: func {
240 assert(me._path != nil, "Trigger.arm: unbound trigger");
241 setprop(me._path ~ "/armed", 1);
242 },
243
244 _arm: func {
245 foreach (var p; keys(me.params))
246 me.params[p] = getprop(sprintf("%s/%s", me._path, p));
247
248 me.fired = 0;
249 me.armed = 1;
250 },
251
252 ##
253 # Deactivate the trigger. The trigger will not fire until rearmed again.
254
255 disarm: func {
256 assert(me._path != nil, "Trigger.disarm: unbound trigger");
257 setprop(me._path ~ "/armed", 0);
258 },
259
260 _disarm: func {
261 me.armed = 0;
262 },
263
264 ##
265 # Enables/disables the trigger. While a trigger is disabled, any timer
266 # or listener that could potentially own shall be disabled.
267 #
268 # The FailureMgr calls these methods when the entire system is
269 # enabled/disabled. By keeping enabled/disabled state separated from
270 # armed/disarmed allows the FailureMgr to keep its configuration while
271 # disabled, i.e. those triggers that where armed when the system was
272 # disabled will resume when the system is enabled again.
273 #
274 # The FailureMgr disables itself during a teleport.
275
276 enable: func { me.enabled = 1 },
277 disable: func { me.enabled = 0 },
278
279 ##
280 # Creates an interface for the trigger in the property tree.
281 # Every parameter in the params hash will be exposed, in addition to
282 # a path/reset property for resetting the trigger from the prop tree.
283
284 bind: func(path) {
285 assert(me._path == nil, "Trigger.bind: trigger already bound");
286
287 me._path = path;
288 props.globals.getNode(path) != nil or props.globals.initNode(path);
289 props.globals.getNode(path).setValues(me.params);
290
291 var prop = path ~ "/armed";
292 props.globals.initNode(prop, 0, "BOOL");
293 setlistener(prop,
294 func(p) { p.getValue() ? me._arm() : me._disarm() }, 0, 1);
295 },
296
297 ##
298 # Removes this trigger's interface from the property tree.
299
300 unbind: func {
301 props.globals.getNode(me._path ~ "/armed").remove();
302 foreach (var p; keys(me.params))
303 props.globals.getNode(me._path ~ "/" ~ p).remove();
304
305 me._path = nil;
306 }
307 };
308
309 ##
310 # FailureActuators encapsulate the actions required for activating the actual
311 # failure simulation.
312 #
313 # Traditionally this action was just manipulating a "serviceable" property
314 # somewhere, but the FailureActuator gives you more flexibility, allowing you
315 # to touch several properties at once or call other Nasal scripts, for example.
316 #
317 # See Aircraft/Generic/Systems/failure.nas and
318 # Aircraft/Generic/Systems/compat_failures.nas for some examples of actuators.
319
320 var FailureActuator = {
321
322 ##
323 # Called from the failure manager to activate a certain level of failure.
324 # level: Target level of failure [0 to 1].
325
326 set_failure_level: func(level) 0,
327
328 ##
329 # Returns the level of failure that is currently being simulated.
330
331 get_failure_level: func 0,
332 };