Changeset 22:bedd79d8e854


Ignore:
Timestamp:
02/15/12 20:02:59 (11 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
hg-Phase:
(<MercurialRepository 1 'hg:/home/ivaradi/mlx/hg' '/'>, 'public')
Message:

Delayed loggers now log sooner after the last change and they preserve the entire state at the last change.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/checks.py

    r21 r22  
    153153        self._logInitial = logInitial   
    154154
     155    def _getLogTimestamp(self, state):
     156        """Get the log timestamp."""
     157        return state.timestamp
     158
    155159    def check(self, flight, aircraft, logger, oldState, state):
    156160        """Check if the state has changed, and if so, log the new state."""
     
    162166       
    163167        if shouldLog:
    164             logger.message(state.timestamp, self._getMessage(state))
     168            logger.message(self._getLogTimestamp(state), self._getMessage(state))
    165169       
    166170#---------------------------------------------------------------------------------------
     
    197201    Child classes should define the following function:
    198202    - _getValue(state): get the value we are interested in."""
    199     def __init__(self, delay = 10.0):
     203    def __init__(self, minDelay = 3.0, maxDelay = 10.0):
    200204        """Construct the mixin with the given delay in seconds."""
    201         self._delay = delay
     205        self._minDelay = minDelay
     206        self._maxDelay = maxDelay
    202207        self._oldValue = None
    203208        self._firstChange = None
    204 
     209        self._lastChangeState = None
     210       
    205211    def _changed(self, oldState, state):
    206212        """Determine if the value has changed."""
     
    209215           
    210216        newValue = self._getValue(state)
    211         if newValue!=self._oldValue:
     217        if self._oldValue!=newValue:
    212218            if self._firstChange is None:
    213219                self._firstChange = state.timestamp
    214             if state.timestamp >= (self._firstChange + self._delay):
    215                 self._oldValue = newValue
     220            self._lastChangeState = state
     221            self._oldValue = newValue
     222
     223        if self._firstChange is not None:
     224            if state.timestamp >= min(self._lastChangeState.timestamp + self._minDelay,
     225                                      self._firstChange + self._maxDelay):
    216226                self._firstChange = None
    217227                return True
    218         else:
    219             self._firstChange = None
    220 
     228           
    221229        return False
     230
     231    def _getLogTimestamp(self, state):
     232        """Get the log timestamp."""
     233        return self._lastChangeState.timestamp if \
     234               self._lastChangeState is not None else state.timestamp
    222235
    223236#---------------------------------------------------------------------------------------
     
    242255    """Base for generic state change loggers that monitor a single value in the
    243256    state possibly with a delay and the logged message comes from a template"""
    244     def __init__(self, attrName, template, logInitial = True, delay = 0.0):
     257    def __init__(self, attrName, template, logInitial = True,
     258                 minDelay = 0.0, maxDelay = 0.0):
    245259        """Construct the object."""
    246260        StateChangeLogger.__init__(self, logInitial = logInitial)
    247261        SingleValueMixin.__init__(self, attrName)
    248         DelayedChangeMixin.__init__(self, delay = delay)
     262        DelayedChangeMixin.__init__(self, minDelay = minDelay, maxDelay = maxDelay)
    249263        TemplateMessageMixin.__init__(self, template)
     264        self._getLogTimestamp = lambda state: \
     265                                DelayedChangeMixin._getLogTimestamp(self, state)
    250266
    251267#---------------------------------------------------------------------------------------
     
    259275        SingleValueMixin.__init__(self, "altimeter")
    260276        DelayedChangeMixin.__init__(self)
     277        self._getLogTimestamp = lambda state: \
     278                                DelayedChangeMixin._getLogTimestamp(self, state)
    261279
    262280    def _getMessage(self, state):
    263281        """Get the message to log on a change."""
    264         return "Altimeter: %.0f hPa at %.0f feet" % (state.altimeter, state.altitude)
     282        logState = self._lastChangeState if \
     283                   self._lastChangeState is not None else state
     284        return "Altimeter: %.0f hPa at %.0f feet" % \
     285               (logState.altimeter, logState.altitude)
    265286
    266287#---------------------------------------------------------------------------------------
     
    287308        """Construct the logger."""
    288309        super(SquawkLogger, self).__init__("squawk", "Squawk code: %s",
    289                                            delay = 10.0)
     310                                           minDelay = 3.0, maxDelay = 10.0)
    290311
    291312#---------------------------------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.