better logging; properly detect whether the bell button was newly pressed
[saartuer.git] / statemachine.py
index 07d6574ff7dd601396669640f8a9dd62eed07543..01cf83f33b2d600bb383024a276c89c2d1e3f904 100644 (file)
@@ -27,10 +27,6 @@ ABOUTOPEN_NERVLIST = [(5, lambda : play_sound("flipswitch")), (5, lambda:play_so
        (10, lambda:play_sound("flipswitch")), (10, lambda:play_sound("flipswitch")), (0, lambda:logger.error("Space open but switch not flipped for 30 seconds")),\
        (10, lambda:play_sound("flipswitch")), (10, lambda:play_sound("flipswitch")), (6, lambda:play_sound("flipswitch")), (4, lambda:logger.critical("Space open but switch not flipped for 60 seconds"))]
 
        (10, lambda:play_sound("flipswitch")), (10, lambda:play_sound("flipswitch")), (0, lambda:logger.error("Space open but switch not flipped for 30 seconds")),\
        (10, lambda:play_sound("flipswitch")), (10, lambda:play_sound("flipswitch")), (6, lambda:play_sound("flipswitch")), (4, lambda:logger.critical("Space open but switch not flipped for 60 seconds"))]
 
-# StateAuf constants
-#  time that must pass between two bell_ringing events to buzz the door again (seconds)
-AUF_BELLBUZZ_REPEAT_TIME = 2
-
 # Timeout we wait after the switch was switched to "Closed", until we assume nobody will open the door and we just lock it
 # ALso the time we wait after the door was opend, till we assume something went wrong and start nerving
 LEAVE_TIMEOUT = 20
 # Timeout we wait after the switch was switched to "Closed", until we assume nobody will open the door and we just lock it
 # ALso the time we wait after the door was opend, till we assume something went wrong and start nerving
 LEAVE_TIMEOUT = 20
@@ -84,6 +80,8 @@ class StateMachine():
                                return self._nerver.nerv()
                def pins(self):
                        return self.state_machine.pins
                                return self._nerver.nerv()
                def pins(self):
                        return self.state_machine.pins
+               def old_pins(self):
+                       return self.state_machine.old_pins
                def actor(self):
                        return self.state_machine.actor
                def handle_event(self,ev,arg): # don't override
                def actor(self):
                        return self.state_machine.actor
                def handle_event(self,ev,arg): # don't override
@@ -102,7 +100,7 @@ class StateMachine():
                def handle_pins_event(self):
                        super().handle_pins_event()
                        pins = self.pins()
                def handle_pins_event(self):
                        super().handle_pins_event()
                        pins = self.pins()
-                       if pins.door_locked is None or pins.door_closed is None or pins.space_dactive is None or pins.bell_ringing is None:
+                       if pins.door_locked is None or pins.door_closed is None or pins.space_active is None or pins.bell_ringing is None:
                                return None # wait till we have all sensors non-None
                        if pins.door_locked:
                                return StateMachine.StateZu(self.state_machine)
                                return None # wait till we have all sensors non-None
                        if pins.door_locked:
                                return StateMachine.StateZu(self.state_machine)
@@ -144,7 +142,7 @@ class StateMachine():
                        # TODO: 202 notification also here if possible
                        self.callbacks.append(callback)
                def could_not_open(self):
                        # TODO: 202 notification also here if possible
                        self.callbacks.append(callback)
                def could_not_open(self):
-                       logger.critical("Couldn't open door after %d tries. Going back to StateZu." % OPEN_REPEAT_NUMBER)
+                       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)
        
                        self.notify(False)
                        return StateMachine.StateZu(self.state_machine)
        
@@ -152,7 +150,7 @@ class StateMachine():
                def handle_cmd_unlock_event(self, callback):
                        # intentionally not calling super() implementation
                        callback("299 redundant: Space seems to be already open. Still processing your request tough.")
                def handle_cmd_unlock_event(self, callback):
                        # intentionally not calling super() implementation
                        callback("299 redundant: Space seems to be already open. Still processing your request tough.")
-                       logger.info("Received OPEN command in StateAboutToOpen. This should not be necessary.")
+                       logger.info("StateMachine: Received UNLOCK command in StateAboutToOpen. This should not be necessary.")
                        self.actor().act(Actor.CMD_UNLOCK)
        
        class StateAboutToOpen(AbstractStateWhereOpeningIsRedundant):
                        self.actor().act(Actor.CMD_UNLOCK)
        
        class StateAboutToOpen(AbstractStateWhereOpeningIsRedundant):
@@ -173,18 +171,15 @@ class StateMachine():
                def handle_pins_event(self):
                        super().handle_pins_event()
                        pins = self.pins()
                def handle_pins_event(self):
                        super().handle_pins_event()
                        pins = self.pins()
-                       if pins.bell_ringing:
-                               # TODO: use old_pins instead of a timer
-                               now = time.time()
-                               if self.last_buzzed is None or now-self.last_buzzed < AUF_BELLBUZZ_REPEAT_TIME:
-                                       logger.info("buzzing because of bell ringing in state auf")
-                                       self.actor().act(Actor.CMD_BUZZ)
-                                       self.last_buzzed = now
+                       if pins.bell_ringing and not self.old_pins().bell_ringing:
+                               # someone just pressed the bell
+                               logger.info("StateMachine: buzzing because of bell ringing in state auf")
+                               self.actor().act(Actor.CMD_BUZZ)
                        if not pins.space_active:
                        if not pins.space_active:
-                               logger.info("space switch off - starting leaving procedure")
+                               logger.info("StateMachine: space switch off - starting leaving procedure")
                                return StateMachine.StateAboutToLeave(self.state_machine)
                        if pins.door_locked:
                                return StateMachine.StateAboutToLeave(self.state_machine)
                        if pins.door_locked:
-                               logger.info("door manually locked, but space switch on - going to StateZu")
+                               logger.info("StateMachine: door manually locked, but space switch on - going to StateZu")
                                play_sound("manual_lock")
                                return StateMachine.StateZu(self.state_machine)
        
                                play_sound("manual_lock")
                                return StateMachine.StateZu(self.state_machine)
        
@@ -201,14 +196,14 @@ class StateMachine():
                        pins = self.pins()
                        if not pins.door_closed:
                                # TODO play a sound? This shouldn't happen, door was opened while we are locking
                        pins = self.pins()
                        if not pins.door_closed:
                                # TODO play a sound? This shouldn't happen, door was opened while we are locking
-                               logger.warning("door manually opened during locking")
+                               logger.warning("StateMachine: door manually opened during locking")
                                return StateMachine.StateAboutToOpen(self.state_machine)
                        if pins.door_locked:
                                return StateMachine.StateZu(self.state_machine)
                def handle_cmd_unlock_event(self,callback):
                        callback("409 conflict: The server is currently trying to lock the door. Try again later.")
                def could_not_close(self):
                                return StateMachine.StateAboutToOpen(self.state_machine)
                        if pins.door_locked:
                                return StateMachine.StateZu(self.state_machine)
                def handle_cmd_unlock_event(self,callback):
                        callback("409 conflict: The server is currently trying to lock the door. Try again later.")
                def could_not_close(self):
-                       logger.critical("Couldn't close door after %d tries. Going back to StateAboutToOpen." % CLOSE_REPEAT_NUMBER)
+                       logger.critical("StateMachine: Couldn't close door after %d tries. Going back to StateAboutToOpen." % CLOSE_REPEAT_NUMBER)
                        return StateMachine.StateAboutToOpen(self.state_machine)
        
        class StateAboutToLeave(State):
                        return StateMachine.StateAboutToOpen(self.state_machine)
        
        class StateAboutToLeave(State):
@@ -253,6 +248,6 @@ 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:
-                       logger.debug("StateMachine: new state = %s" % newstate.__class__.__name__)
+                       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)
                        self.current_state = newstate
                        newstate = self.current_state.handle_event(StateMachine.CMD_PINS, self.pins)