# Failure Manager public interface # # Copyright (C) 2014 Anton Gomez Alvedro # # This program is free software; you can redistribute it and/or # modify 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. # # This program 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 this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. var proproot = "sim/failure-manager/"; ## # Nasal modules can subscribe to FailureMgr events. # Each event has an independent dispatcher so modules can subscribe only to # the events they are interested in. This also simplifies processing at client # side by being able to subscibe different callbacks to different events. # # Example: # # var handle = FailureMgr.events["trigger-fired"].subscribe(my_callback); var events = {}; # Event: trigger-fired # Format: { mode_id: , trigger: } events["trigger-fired"] = globals.events.EventDispatcher.new(); ## # Encodes a pair "category" and "failure_mode" into a "mode_id". # # These just have the simple form "category/mode", and are used to refer to # failure modes throughout the FailureMgr API and to create a path within the # sim/failure-manager property tree for the failure mode. # # examples of categories: # structural, instrumentation, controls, sensors, etc... # # examples of failure modes: # altimeter, pitot, left-tire, landing-light, etc... var get_id = func(category, failure_mode) { return sprintf("%s/%s", string.normpath(category), failure_mode); } ## # Returns a vector containing: [category, failure_mode] var split_id = func(mode_id) { return [string.normpath(io.dirname(mode_id)), io.basename(mode_id)]; } ## # Subscribe a new failure mode to the system. # # id: Unique identifier for this failure mode. # eg: "engine/carburetor-ice" # # description: Short text description, suitable for printing to the user. # eg: "Ice in the carburetor" # # actuator: Object implementing the FailureActuator interface. # Used by the failure manager to apply a certain level of # failure to the failure mode. var add_failure_mode = func(id, description, actuator) { _failmgr.add_failure_mode( FailureMode.new(id, description, actuator)); } ## # Returns a vector with all failure modes in the system. # Each vector entry is a hash with the following keys: # { id, description } var get_failure_modes = func() { _failmgr.get_failure_modes(); } ## # Remove a failure mode from the system. # id: FailureMode id string, e.g. "systems/pitot" var remove_failure_mode = func(id) { _failmgr.remove_failure_mode(id); } ## # Removes all failure modes from the failure manager. var remove_all = func { _failmgr.remove_all(); } ## # Attaches a trigger to the given failure mode. Discards the current trigger # if any. # # mode_id: FailureMode id string, e.g. "systems/pitot" # trigger: Trigger object or nil. Nil will just detach the current trigger var set_trigger = func(mode_id, trigger) { _failmgr.set_trigger(mode_id, trigger); } ## # Returns the trigger object attached to the given failure mode. # mode_id: FailureMode id string, e.g. "systems/pitot" var get_trigger = func(mode_id) { _failmgr.get_trigger(mode_id); } ## # Applies a certain level of failure to this failure mode. # # mode_id: Failure mode id string. # level: Floating point number in the range [0, 1] # Zero represents no failure and one means total failure. var set_failure_level = func(mode_id, level) { setprop(proproot ~ mode_id ~ "/failure-level", level); } ## # Returns the current failure level for the given failure mode. # mode_id: Failure mode id string. var get_failure_level = func(mode_id) { getprop(proproot ~ mode_id ~ "/failure-level"); } ## # Restores all failure modes to level = 0 var repair_all = func { _failmgr.repair_all(); } ## # Returns a vector of timestamped failure manager events, such as the # messages shown in the console when there are changes to the failure conditions. # # Each entry in the vector has the following format: # { time: