add on_leave callback and use it; send email when the space was open for 24h (as...
authorRalf Jung <post@ralfj.de>
Tue, 15 Oct 2013 08:55:08 +0000 (10:55 +0200)
committerRalf Jung <post@ralfj.de>
Tue, 15 Oct 2013 08:58:02 +0000 (10:58 +0200)
statemachine.odg
statemachine.py

index 883e18348f8e3399d346b2f5c278b9c666b1d12a..d32026c3944167b32079e33514bdb8eda9882713 100644 (file)
Binary files a/statemachine.odg and b/statemachine.odg differ
index f6b45dd1cbbd8112a6e6db89fb4f92a8a9da0b23..0fc24bdf08e1c4d9b91c80752cf6c6cc97d5e704 100644 (file)
@@ -79,6 +79,8 @@ class StateMachine():
                def handle_wakeup_event(self):
                        if self._nerver is not None:
                                return self._nerver.nerv()
                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):
                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)
                        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)
                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):
                        return StateMachine.StateZu(self.state_machine)
        
        class AbstractStateWhereUnlockingIsRedundant(AbstractUnlockedState):
@@ -174,7 +172,8 @@ class StateMachine():
        
        class StateAuf(AbstractStateWhereUnlockingIsRedundant):
                def __init__(self,sm):
        
        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()
                        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:
                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)
                        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)