From: Ralf Jung Date: Tue, 15 Oct 2013 08:55:08 +0000 (+0200) Subject: add on_leave callback and use it; send email when the space was open for 24h (as... X-Git-Url: https://git.ralfj.de/saartuer.git/commitdiff_plain/c18f24f8dd55661841deb73acf04be9eb7011202?ds=inline;hp=0c9ef9a101c6adfe914f9d039c8ec6698ccf8eb5 add on_leave callback and use it; send email when the space was open for 24h (as that should rarely be the case) --- diff --git a/statemachine.odg b/statemachine.odg index 883e183..d32026c 100644 Binary files a/statemachine.odg and b/statemachine.odg differ diff --git a/statemachine.py b/statemachine.py index f6b45dd..0fc24bd 100644 --- a/statemachine.py +++ b/statemachine.py @@ -79,6 +79,8 @@ class StateMachine(): def handle_wakeup_event(self): if self._nerver is not None: return self._nerver.nerv() + def on_leave(self): + pass def pins(self): return self.state_machine.pins def old_pins(self): @@ -144,17 +146,13 @@ class StateMachine(): for cb in self.callbacks: if cb is not None: cb(s) - def handle_pins_event(self): - # overriding superclass as we need to do notification (TODO can this be done better? on_leave?) - if not self.pins().door_locked: - self.notify(True) - return StateMachine.StateAboutToOpen(self.state_machine) + def on_leave(self): + self.notify(not self.pins().door_locked) def handle_cmd_unlock_event(self,callback): # TODO: 202 notification also here if possible self.callbacks.append(callback) def could_not_open(self): logger.critical("StateMachine: Couldn't open door after %d tries. Going back to StateZu." % OPEN_REPEAT_NUMBER) - self.notify(False) return StateMachine.StateZu(self.state_machine) class AbstractStateWhereUnlockingIsRedundant(AbstractUnlockedState): @@ -174,7 +172,8 @@ class StateMachine(): class StateAuf(AbstractStateWhereUnlockingIsRedundant): def __init__(self,sm): - super().__init__(sm) + nervlist = [(24*60*60, lambda: logger.critical("Space is now open for 24h. Is everything all right?"))] + super().__init__(sm, nervlist) self.last_buzzed = None def handle_pins_event(self): pins = self.pins() @@ -249,6 +248,7 @@ class StateMachine(): newstate = self.current_state.handle_event(cmd,arg) # returns None or an instance of the new state self.old_pins = self.pins while newstate is not None: + self.current_state.on_leave() logger.debug("StateMachine: Doing state transition %s -> %s" % (self.current_state.__class__.__name__, newstate.__class__.__name__)) self.current_state = newstate newstate = self.current_state.handle_event(StateMachine.CMD_PINS, self.pins)