Changeset 22:bedd79d8e854 for src
- Timestamp:
- 02/15/12 20:02:59 (13 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/checks.py
r21 r22 153 153 self._logInitial = logInitial 154 154 155 def _getLogTimestamp(self, state): 156 """Get the log timestamp.""" 157 return state.timestamp 158 155 159 def check(self, flight, aircraft, logger, oldState, state): 156 160 """Check if the state has changed, and if so, log the new state.""" … … 162 166 163 167 if shouldLog: 164 logger.message(s tate.timestamp, self._getMessage(state))168 logger.message(self._getLogTimestamp(state), self._getMessage(state)) 165 169 166 170 #--------------------------------------------------------------------------------------- … … 197 201 Child classes should define the following function: 198 202 - _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): 200 204 """Construct the mixin with the given delay in seconds.""" 201 self._delay = delay 205 self._minDelay = minDelay 206 self._maxDelay = maxDelay 202 207 self._oldValue = None 203 208 self._firstChange = None 204 209 self._lastChangeState = None 210 205 211 def _changed(self, oldState, state): 206 212 """Determine if the value has changed.""" … … 209 215 210 216 newValue = self._getValue(state) 211 if newValue!=self._oldValue:217 if self._oldValue!=newValue: 212 218 if self._firstChange is None: 213 219 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): 216 226 self._firstChange = None 217 227 return True 218 else: 219 self._firstChange = None 220 228 221 229 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 222 235 223 236 #--------------------------------------------------------------------------------------- … … 242 255 """Base for generic state change loggers that monitor a single value in the 243 256 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): 245 259 """Construct the object.""" 246 260 StateChangeLogger.__init__(self, logInitial = logInitial) 247 261 SingleValueMixin.__init__(self, attrName) 248 DelayedChangeMixin.__init__(self, delay = delay)262 DelayedChangeMixin.__init__(self, minDelay = minDelay, maxDelay = maxDelay) 249 263 TemplateMessageMixin.__init__(self, template) 264 self._getLogTimestamp = lambda state: \ 265 DelayedChangeMixin._getLogTimestamp(self, state) 250 266 251 267 #--------------------------------------------------------------------------------------- … … 259 275 SingleValueMixin.__init__(self, "altimeter") 260 276 DelayedChangeMixin.__init__(self) 277 self._getLogTimestamp = lambda state: \ 278 DelayedChangeMixin._getLogTimestamp(self, state) 261 279 262 280 def _getMessage(self, state): 263 281 """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) 265 286 266 287 #--------------------------------------------------------------------------------------- … … 287 308 """Construct the logger.""" 288 309 super(SquawkLogger, self).__init__("squawk", "Squawk code: %s", 289 delay = 10.0)310 minDelay = 3.0, maxDelay = 10.0) 290 311 291 312 #---------------------------------------------------------------------------------------
Note:
See TracChangeset
for help on using the changeset viewer.