+ class StateLocking(State):
+ # TODO: share code with StateUnlocking
+ def __init__(self,sm):
+ # construct a nervlist
+ nervlist = [(CLOSE_REPEAT_TIMEOUT, lambda: self.actor().act(Actor.CMD_LOCK)) for t in range(CLOSE_REPEAT_NUMBER)]
+ nervlist += [(CLOSE_REPEAT_TIMEOUT, self.could_not_close)]
+ super().__init__(sm, nervlist)
+ if self.pins().door_closed: # this should always be true, but just to be sure...
+ self.actor().act(Actor.CMD_LOCK)
+ def handle_pins_event(self):
+ 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("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):
+ logger.critical("StateMachine: Couldn't close door after %d tries. Going back to StateAboutToOpen." % CLOSE_REPEAT_NUMBER)
+ return StateMachine.StateAboutToOpen(self.state_machine)