Commit d084286e authored by Ralf's avatar Ralf
Browse files

there can be more than one message on the socket; empty the queue when...

there can be more than one message on the socket; empty the queue when shutting down a callback-thread; increase the SpaceAPI-retry-time
parent b8b6e14e
...@@ -111,6 +111,13 @@ class ThreadFunction(): ...@@ -111,6 +111,13 @@ class ThreadFunction():
self._q.put((ThreadFunction._CALL, arg)) self._q.put((ThreadFunction._CALL, arg))
def stop(self): def stop(self):
# empty the queue
try:
while True:
self._q.get_nowait()
except queue.Empty:
pass
# now wait till the job-in-progress is done
self._q.put((ThreadFunction._TERM, None)) self._q.put((ThreadFunction._TERM, None))
self._t.join() self._t.join()
......
...@@ -4,7 +4,7 @@ import urllib.request, time ...@@ -4,7 +4,7 @@ import urllib.request, time
from config import spaceApiKey from config import spaceApiKey
RETRY_TIME = 30 RETRY_TIME = 60
HEARTBEAT_TIME = 10*60 HEARTBEAT_TIME = 10*60
class SpaceApi: class SpaceApi:
......
...@@ -205,19 +205,20 @@ class StateMachine(): ...@@ -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, lambda: self.actor().act(Actor.CMD_UNLOCK)) for t in range(OPEN_REPEAT_NUMBER)]
nervlist += [(OPEN_REPEAT_TIMEOUT, self.could_not_open)] nervlist += [(OPEN_REPEAT_TIMEOUT, self.could_not_open)]
super().__init__(sm,nervlist) super().__init__(sm,nervlist)
self.callbacks=[callback] self.callbacks = []
# TODO: can we send "202 processing: Trying to unlock the door" here? Are the callbacks multi-use?
self.actor().act(Actor.CMD_UNLOCK) self.actor().act(Actor.CMD_UNLOCK)
def notify(self, did_it_work): # enqueue the callback
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)) self.handle_cmd_unlock_event(callback)
def notify(self, s, lastMsg):
for cb in self.callbacks: for cb in self.callbacks:
if cb is not None: cb(s, lastMsg)
cb(s)
def on_leave(self): 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): def handle_cmd_unlock_event(self,callback):
# TODO: 202 notification also here if possible if callback is not None:
self.callbacks.append(callback) callback("202 processing: Trying to unlock the door", lastMsg=False)
self.callbacks.append(callback)
def could_not_open(self): def could_not_open(self):
logger.critical("StateMachine: 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)
return StateMachine.StateZu(self.state_machine) return StateMachine.StateZu(self.state_machine)
......
...@@ -10,6 +10,8 @@ while True: ...@@ -10,6 +10,8 @@ while True:
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(tuerSock) s.connect(tuerSock)
s.send(command.encode()) s.send(command.encode())
data = s.recv(64) while True:
data = s.recv(256)
if not len(data): break
print(data)
s.close() s.close()
print(str(data))
...@@ -41,9 +41,10 @@ try: ...@@ -41,9 +41,10 @@ try:
the_socket.accept() the_socket.accept()
except KeyboardInterrupt: except KeyboardInterrupt:
# this is what we waited for! # this is what we waited for!
logger.info("Got SIGINT, terminating...")
pass pass
logger.info("Terminating...")
# bring 'em all down # bring 'em all down
the_waker.stop() # this one first, it "randomly" calls other threads the_waker.stop() # this one first, it "randomly" calls other threads
the_pins.stop() # as does this the_pins.stop() # as does this
......
...@@ -43,9 +43,11 @@ def sendcmd(addr, cmd): ...@@ -43,9 +43,11 @@ def sendcmd(addr, cmd):
s.connect(addr) s.connect(addr)
s.settimeout(60.0) s.settimeout(60.0)
s.send(cmd.encode()) s.send(cmd.encode())
data = s.recv(256) while True:
data = s.recv(256)
if not len(data): break
print(data.decode('utf-8'))
s.close() s.close()
print(data.decode('utf-8'))
return run return run
def exitcmd(c): def exitcmd(c):
......
...@@ -44,10 +44,11 @@ class TySocket(): ...@@ -44,10 +44,11 @@ class TySocket():
self._sock.listen(1) self._sock.listen(1)
def _answer(self, conn): def _answer(self, conn):
def answer(msg): def answer(msg, lastMsg = True):
# this is called in another thread, so it should be quick and not touch the TySocket # this is called in another thread, so it should be quick and not touch the TySocket
waynesend(conn, msg) waynesend(conn, msg)
conn.close() if lastMsg:
conn.close()
return answer return answer
def accept(self): def accept(self):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment