there can be more than one message on the socket; empty the queue when shutting down...
[saartuer.git] / statemachine.py
index a937f50c035140cef91d504006a0dbbce60f7014..5ad83ddbe22608d144bab3fb9ab34a9c051f223e 100644 (file)
@@ -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)