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():
self._q.put((ThreadFunction._CALL, arg))
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._t.join()
......
......@@ -4,7 +4,7 @@ import urllib.request, time
from config import spaceApiKey
RETRY_TIME = 30
RETRY_TIME = 60
HEARTBEAT_TIME = 10*60
class SpaceApi:
......
......@@ -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)
......
......@@ -10,6 +10,8 @@ while True:
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(tuerSock)
s.send(command.encode())
data = s.recv(64)
while True:
data = s.recv(256)
if not len(data): break
print(data)
s.close()
print(str(data))
......@@ -41,9 +41,10 @@ try:
the_socket.accept()
except KeyboardInterrupt:
# this is what we waited for!
logger.info("Got SIGINT, terminating...")
pass
logger.info("Terminating...")
# bring 'em all down
the_waker.stop() # this one first, it "randomly" calls other threads
the_pins.stop() # as does this
......
......@@ -43,9 +43,11 @@ def sendcmd(addr, cmd):
s.connect(addr)
s.settimeout(60.0)
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()
print(data.decode('utf-8'))
return run
def exitcmd(c):
......
......@@ -44,10 +44,11 @@ class TySocket():
self._sock.listen(1)
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
waynesend(conn, msg)
conn.close()
if lastMsg:
conn.close()
return answer
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