X-Git-Url: https://git.ralfj.de/saartuer.git/blobdiff_plain/b8b6e14e6923809efc61c71dd94b1e66026aad9b..d084286e81b121106a0a165e3c151762635466e0:/statemachine.py diff --git a/statemachine.py b/statemachine.py index a937f50..5ad83dd 100644 --- a/statemachine.py +++ b/statemachine.py @@ -205,19 +205,20 @@ class StateMachine(): nervlist = [(OPEN_REPEAT_TIMEOUT, lambda: self.actor().act(Actor.CMD_UNLOCK)) for t in range(OPEN_REPEAT_NUMBER)] nervlist += [(OPEN_REPEAT_TIMEOUT, self.could_not_open)] super().__init__(sm,nervlist) - self.callbacks=[callback] - # TODO: can we send "202 processing: Trying to unlock the door" here? Are the callbacks multi-use? + self.callbacks = [] self.actor().act(Actor.CMD_UNLOCK) - def notify(self, did_it_work): - s = "200 okay: door unlocked" if did_it_work else ("500 internal server error: Couldn't unlock door with %d tries à %f seconds" % (OPEN_REPEAT_NUMBER,OPEN_REPEAT_TIMEOUT)) + # enqueue the callback + self.handle_cmd_unlock_event(callback) + def notify(self, s, lastMsg): for cb in self.callbacks: - if cb is not None: - cb(s) + cb(s, lastMsg) def on_leave(self): - self.notify(not self.pins().door_locked) + s = "200 okay: door unlocked" if not self.pins().door_locked else ("500 internal server error: Couldn't unlock door with %d tries à %f seconds" % (OPEN_REPEAT_NUMBER,OPEN_REPEAT_TIMEOUT)) + self.notify(s, lastMsg=True) def handle_cmd_unlock_event(self,callback): - # TODO: 202 notification also here if possible - self.callbacks.append(callback) + if callback is not None: + callback("202 processing: Trying to unlock the door", lastMsg=False) + 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) return StateMachine.StateZu(self.state_machine)