Ignore:
Timestamp:
10/07/12 18:16:07 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Added the forced logging of radio frequencies when entering certain stages of the flight

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/checks.py

    r320 r321  
    270270        - _changed(self, oldState, state): returns a boolean indicating if the
    271271        value has changed or not
    272         - _getMessage(self, flight, state): return a strings containing the
    273         message to log with the new value
     272        - _getMessage(self, flight, state, forced): return a strings containing
     273        the message to log with the new value
    274274        """
    275275        self._logInitial = logInitial   
     
    288288       
    289289        if shouldLog:
    290             message = self._getMessage(flight, state)
    291             if message is not None:
    292                 logger.message(self._getLogTimestamp(state), message)
     290            self.logState(flight, logger, state)
     291
     292    def logState(self, flight, logger, state, forced = False):
     293        """Log the state."""
     294        message = self._getMessage(flight, state, forced)
     295        if message is not None:
     296            logger.message(self._getLogTimestamp(state), message)
    293297       
    294298#-------------------------------------------------------------------------------
     
    340344           
    341345        newValue = self._getValue(state)
    342         if self._oldValue!=newValue:
     346        if self._isDifferent(self._oldValue, newValue):
    343347            if self._firstChange is None:
    344348                self._firstChange = state.timestamp
     
    359363               self._lastChangeState is not None else state.timestamp
    360364
     365    def _isDifferent(self, oldValue, newValue):
     366        """Determine if the given values are different.
     367
     368        This default implementation checks for simple equality."""
     369        return oldValue!=newValue
     370
    361371#---------------------------------------------------------------------------------------
    362372
     
    370380        self._template = template
    371381
    372     def _getMessage(self, flight, state):
     382    def _getMessage(self, flight, state, forced):
    373383        """Get the message."""
    374384        value = self._getValue(state)       
     
    393403#---------------------------------------------------------------------------------------
    394404
     405class ForceableLoggerMixin(object):
     406    """A mixin for loggers that can be forced to log a certain state.
     407
     408    The last logged state is always maintained, and when checking for a change,
     409    that state is compared to the current one (which may actually be the same,
     410    if a forced logging was performed for that state).
     411
     412    Children should implement the following functions:
     413    - _hasChanged(oldState, state): the real check for a change
     414    - _logState(flight, logger, state, forced): the real logging function
     415    """
     416    def __init__(self):
     417        """Construct the mixin."""
     418        self._lastLoggedState = None
     419
     420    def forceLog(self, flight, logger, state):
     421        """Force logging the given state."""
     422        self.logState(flight, logger, state, forced = True)
     423
     424    def logState(self, flight, logger, state, forced = False):
     425        """Log the state.
     426
     427        It calls _logState to perform the real logging, and saves the given
     428        state as the last logged one."""
     429        self._logState(flight, logger, state, forced)
     430        self._lastLoggedState = state
     431   
     432    def _changed(self, oldState, state):
     433        """Check if the state has changed.
     434
     435        This function calls _hasChanged for the real check, and replaces
     436        oldState with the stored last logged state, if any."""
     437        if self._lastLoggedState is not None:
     438            oldState = self._lastLoggedState
     439        return self._hasChanged(oldState, state)
     440
     441#---------------------------------------------------------------------------------------
     442
    395443class AltimeterLogger(StateChangeLogger, SingleValueMixin,
    396444                      DelayedChangeMixin):
     
    404452                                DelayedChangeMixin._getLogTimestamp(self, state)
    405453
    406     def _getMessage(self, flight, state):
     454    def _getMessage(self, flight, state, forced):
    407455        """Get the message to log on a change."""
    408456        logState = self._lastChangeState if \
     
    413461#---------------------------------------------------------------------------------------
    414462
    415 class NAVLogger(StateChangeLogger, DelayedChangeMixin):
     463class NAVLogger(StateChangeLogger, DelayedChangeMixin, ForceableLoggerMixin):
    416464    """Logger for NAV radios.
    417465
    418466    It also logs the OBS frequency set."""
     467    @staticmethod
     468    def getMessage(logName, frequency, obs):
     469        """Get the message for the given NAV radio setting."""
     470        message = u"%s frequency: %s MHz" % (logName, frequency)
     471        if obs is not None: message += u" [%d\u00b0]" % (obs,)
     472        return message
     473   
    419474    def __init__(self, attrName, logName):
    420475        """Construct the NAV logger."""
    421476        StateChangeLogger.__init__(self, logInitial = True)
    422477        DelayedChangeMixin.__init__(self)
     478        ForceableLoggerMixin.__init__(self)
     479
     480        self.logState = lambda flight, logger, state, forced = False: \
     481            ForceableLoggerMixin.logState(self, flight, logger, state,
     482                                          forced = forced)
     483        self._getLogTimestamp = \
     484            lambda state: DelayedChangeMixin._getLogTimestamp(self, state)
     485        self._changed = lambda oldState, state: \
     486            ForceableLoggerMixin._changed(self, oldState, state)
     487        self._hasChanged = lambda oldState, state: \
     488            DelayedChangeMixin._changed(self, oldState, state)
     489        self._logState = lambda flight, logger, state, forced: \
     490             StateChangeLogger.logState(self, flight, logger, state,
     491                                        forced = forced)
    423492
    424493        self._attrName = attrName
    425494        self._logName = logName
    426        
     495
    427496    def _getValue(self, state):
    428497        """Get the value.
     
    432501        frequency = getattr(state, self._attrName)
    433502        obs = getattr(state, self._attrName + "_obs")
    434         return None if frequency is None or obs is None else (frequency, obs)
    435 
    436     def _getMessage(self, flight, state):
     503        manual = getattr(state, self._attrName + "_manual")
     504        return (frequency, obs, manual)
     505
     506    def _getMessage(self, flight, state, forced):
    437507        """Get the message."""
    438         value = self._getValue(state)
    439         return None if value is None else \
    440                (u"%s frequency: %s MHz [%d\u00b0]" % (self._logName, value[0], value[1]))
     508        (frequency, obs, manual) = self._getValue(state)
     509        return None if frequency is None or obs is None or \
     510               (not manual and not forced) else \
     511               self.getMessage(self._logName, frequency, obs)
     512
     513    def _isDifferent(self, oldValue, newValue):
     514        """Determine if the valie has changed between the given states."""
     515        (oldFrequency, oldOBS, _oldManual) = oldValue
     516        (newFrequency, newOBS, _newManual) = newValue
     517        return oldFrequency!=newFrequency or oldOBS!=newOBS
    441518
    442519#---------------------------------------------------------------------------------------
     
    458535#---------------------------------------------------------------------------------------
    459536
    460 class ADF1Logger(GenericStateChangeLogger):
     537class ADFLogger(GenericStateChangeLogger, ForceableLoggerMixin):
     538    """Base class for the ADF loggers."""
     539    def __init__(self, attr, logName):
     540        """Construct the ADF logger."""
     541        GenericStateChangeLogger.__init__(self, attr,
     542                                          "%s frequency: %%s kHz" % (logName,),
     543                                          minDelay = 3.0, maxDelay = 10.0)
     544        ForceableLoggerMixin.__init__(self)
     545
     546        self.logState = lambda flight, logger, state, forced = False: \
     547            ForceableLoggerMixin.logState(self, flight, logger, state,
     548                                          forced = forced)
     549        self._changed = lambda oldState, state: \
     550            ForceableLoggerMixin._changed(self, oldState, state)
     551        self._hasChanged = lambda oldState, state: \
     552            DelayedChangeMixin._changed(self, oldState, state)
     553        self._logState = lambda flight, logger, state, forced: \
     554             StateChangeLogger.logState(self, flight, logger, state, forced)
     555
     556#---------------------------------------------------------------------------------------
     557
     558class ADF1Logger(ADFLogger):
    461559    """Logger for the ADF1 radio setting."""
    462560    def __init__(self):
    463561        """Construct the logger."""
    464         super(ADF1Logger, self).__init__("adf1", "ADF1 frequency: %s kHz",
    465                                          minDelay = 3.0, maxDelay = 10.0)
    466 
    467 #---------------------------------------------------------------------------------------
    468 
    469 class ADF2Logger(GenericStateChangeLogger):
     562        super(ADF1Logger, self).__init__("adf1", "ADF1")
     563
     564#---------------------------------------------------------------------------------------
     565
     566class ADF2Logger(ADFLogger):
    470567    """Logger for the ADF2 radio setting."""
    471568    def __init__(self):
    472569        """Construct the logger."""
    473         super(ADF2Logger, self).__init__("adf2", "ADF2 frequency: %s kHz",
    474                                          minDelay = 3.0, maxDelay = 10.0)
     570        super(ADF2Logger, self).__init__("adf2", "ADF2")
    475571
    476572#---------------------------------------------------------------------------------------
     
    494590        self._template = template
    495591
    496     def _getMessage(self, flight, state):
     592    def _getMessage(self, flight, state, forced):
    497593        """Get the message from the given state."""
    498594        return self._template % ("ON" if self._getValue(state) else "OFF")
     
    539635        SingleValueMixin.__init__(self, "flapsSet")
    540636
    541     def _getMessage(self, flight, state):
     637    def _getMessage(self, flight, state, forced):
    542638        """Get the message to log on a change."""
    543639        speed = state.groundSpeed if state.groundSpeed<80.0 else state.ias
     
    555651        SingleValueMixin.__init__(self, "gearControlDown")
    556652
    557     def _getMessage(self, flight, state):
     653    def _getMessage(self, flight, state, forced):
    558654        """Get the message to log on a change."""
    559655        return "Gears SET to %s at %.0f %s, %.0f feet" % \
Note: See TracChangeset for help on using the changeset viewer.