Changeset 321:8f6b55fb98ed for src/mlx/checks.py
- Timestamp:
- 10/07/12 18:16:07 (12 years ago)
- Branch:
- default
- hg-Phase:
- (<MercurialRepository 1 'hg:/home/ivaradi/mlx/hg' '/'>, 'public')
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/mlx/checks.py
r320 r321 270 270 - _changed(self, oldState, state): returns a boolean indicating if the 271 271 value has changed or not 272 - _getMessage(self, flight, state ): return a strings containing the273 message to log with the new value272 - _getMessage(self, flight, state, forced): return a strings containing 273 the message to log with the new value 274 274 """ 275 275 self._logInitial = logInitial … … 288 288 289 289 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) 293 297 294 298 #------------------------------------------------------------------------------- … … 340 344 341 345 newValue = self._getValue(state) 342 if self._ oldValue!=newValue:346 if self._isDifferent(self._oldValue, newValue): 343 347 if self._firstChange is None: 344 348 self._firstChange = state.timestamp … … 359 363 self._lastChangeState is not None else state.timestamp 360 364 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 361 371 #--------------------------------------------------------------------------------------- 362 372 … … 370 380 self._template = template 371 381 372 def _getMessage(self, flight, state ):382 def _getMessage(self, flight, state, forced): 373 383 """Get the message.""" 374 384 value = self._getValue(state) … … 393 403 #--------------------------------------------------------------------------------------- 394 404 405 class 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 395 443 class AltimeterLogger(StateChangeLogger, SingleValueMixin, 396 444 DelayedChangeMixin): … … 404 452 DelayedChangeMixin._getLogTimestamp(self, state) 405 453 406 def _getMessage(self, flight, state ):454 def _getMessage(self, flight, state, forced): 407 455 """Get the message to log on a change.""" 408 456 logState = self._lastChangeState if \ … … 413 461 #--------------------------------------------------------------------------------------- 414 462 415 class NAVLogger(StateChangeLogger, DelayedChangeMixin ):463 class NAVLogger(StateChangeLogger, DelayedChangeMixin, ForceableLoggerMixin): 416 464 """Logger for NAV radios. 417 465 418 466 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 419 474 def __init__(self, attrName, logName): 420 475 """Construct the NAV logger.""" 421 476 StateChangeLogger.__init__(self, logInitial = True) 422 477 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) 423 492 424 493 self._attrName = attrName 425 494 self._logName = logName 426 495 427 496 def _getValue(self, state): 428 497 """Get the value. … … 432 501 frequency = getattr(state, self._attrName) 433 502 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): 437 507 """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 441 518 442 519 #--------------------------------------------------------------------------------------- … … 458 535 #--------------------------------------------------------------------------------------- 459 536 460 class ADF1Logger(GenericStateChangeLogger): 537 class 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 558 class ADF1Logger(ADFLogger): 461 559 """Logger for the ADF1 radio setting.""" 462 560 def __init__(self): 463 561 """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 566 class ADF2Logger(ADFLogger): 470 567 """Logger for the ADF2 radio setting.""" 471 568 def __init__(self): 472 569 """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") 475 571 476 572 #--------------------------------------------------------------------------------------- … … 494 590 self._template = template 495 591 496 def _getMessage(self, flight, state ):592 def _getMessage(self, flight, state, forced): 497 593 """Get the message from the given state.""" 498 594 return self._template % ("ON" if self._getValue(state) else "OFF") … … 539 635 SingleValueMixin.__init__(self, "flapsSet") 540 636 541 def _getMessage(self, flight, state ):637 def _getMessage(self, flight, state, forced): 542 638 """Get the message to log on a change.""" 543 639 speed = state.groundSpeed if state.groundSpeed<80.0 else state.ias … … 555 651 SingleValueMixin.__init__(self, "gearControlDown") 556 652 557 def _getMessage(self, flight, state ):653 def _getMessage(self, flight, state, forced): 558 654 """Get the message to log on a change.""" 559 655 return "Gears SET to %s at %.0f %s, %.0f feet" % \
Note:
See TracChangeset
for help on using the changeset viewer.