Open the space when the switch is toggled while we are closed
[saartuer.git] / statemachine.py
index 850eb0e598f61f2239b685b9e79a0f008f6ceef0..ca58e1d08b747ed003448f17a764142883c05430 100644 (file)
@@ -30,7 +30,7 @@ ABOUTOPEN_NERVLIST = [(5, lambda : play_sound("flipswitch")), (5, lambda:play_so
 
 # 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
 
 # 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 = 4
+LEAVE_TIMEOUT = 20
 
 # play_sound constants
 SOUNDS_DIRECTORY = "/opt/tuer/sounds/"
 
 # play_sound constants
 SOUNDS_DIRECTORY = "/opt/tuer/sounds/"
@@ -99,21 +99,37 @@ class StateMachine():
                        else:
                                raise Exception("Unknown command number: %d" % ev)
        
                        else:
                                raise Exception("Unknown command number: %d" % ev)
        
-       class AbstractLockedState(State):
+       class AbstractNonStartState(State):
+               def handle_pins_event(self):
+                       if self.pins().door_locked != (not self.pins().space_active):
+                               self.actor().act(Actor.CMD_RED_ON)
+                       else:
+                               self.actor().act(Actor.CMD_RED_OFF)
+                       return super().handle_pins_event()
+       
+       class AbstractLockedState(AbstractNonStartState):
                '''A state with invariant "The space is locked", switching to StateAboutToOpen when the space becomes unlocked'''
                '''A state with invariant "The space is locked", switching to StateAboutToOpen when the space becomes unlocked'''
+               def __init__(self, sm, nervlist = None):
+                       super().__init__(sm, nervlist)
+                       self.actor().act(Actor.CMD_GREEN_OFF)
                def handle_pins_event(self):
                        if not self.pins().door_locked:
                                logger.info("Door unlocked, space is about to open")
                                return StateMachine.StateAboutToOpen(self.state_machine)
                def handle_pins_event(self):
                        if not self.pins().door_locked:
                                logger.info("Door unlocked, space is about to open")
                                return StateMachine.StateAboutToOpen(self.state_machine)
+                       if not self.old_pins().space_active and self.pins().space_active:
+                               logger.info("Space toggled to active while it was closed - unlocking the door")
+                               return StateMachine.StateUnlocking(self.state_machine)
                        return super().handle_pins_event()
        
                        return super().handle_pins_event()
        
-       class AbstractUnlockedState(State):
+       class AbstractUnlockedState(AbstractNonStartState):
                '''A state with invariant "The space is unlocked", switching to StateZu when the space becomes locked'''
                '''A state with invariant "The space is unlocked", switching to StateZu when the space becomes locked'''
+               def __init__(self, sm, nervlist = None):
+                       super().__init__(sm, nervlist)
+                       self.actor().act(Actor.CMD_GREEN_ON)
                def handle_pins_event(self):
                        if self.pins().door_locked:
                                logger.info("Door locked, closing space")
                                if self.pins().space_active:
                def handle_pins_event(self):
                        if self.pins().door_locked:
                                logger.info("Door locked, closing space")
                                if self.pins().space_active:
-                                       # FIXME the same state can be reached by first locking the door, and then activating the space. What to do then?
                                        logger.warning("StateMachine: door manually locked, but space switch is still on - going to StateZu")
                                        play_sound("manual_lock")
                                return StateMachine.StateZu(self.state_machine)
                                        logger.warning("StateMachine: door manually locked, but space switch is still on - going to StateZu")
                                        play_sound("manual_lock")
                                return StateMachine.StateZu(self.state_machine)