diff --git a/ground-station.py b/ground-station.py index a4b8933..1dbc0f8 100644 --- a/ground-station.py +++ b/ground-station.py @@ -13,20 +13,32 @@ BOAT_STATE_INCTRL = 2 BOAT_STATE_AVAILABLE = 1 BOAT_STATE_LOCKED = 0 BOAT_STATE_TERMINATED = -1 +BOAT_STATE_DELETE = -2 Boats = [] Clients = [] +def client_disconnect(client): + if client['state'] != 'kicked': + client['state'] = 'del' + if client['boat'] is not None: + client['boat']['state'] = BOAT_STATE_AVAILABLE + client['boat'] = None + for i, cli in enumerate(Clients): + if cli['state'] == 'del': + del Clients[i] + +def boat_disconnect(boat): + boat['state'] = BOAT_STATE_DELETE + for i, cli in enumerate(Boats): + if cli['state'] == BOAT_STATE_DELETE: + del Boats[i] + async def sendToClient(client, msg): - if client['state'] != 'terminated': - try: - await client['ws'].send(msg) - except websockets.exceptions.ConnectionClosed: - if client['state'] != 'kicked': - client['state'] = 'terminated' - if client['boat'] is not None: - client['boat']['state'] = BOAT_STATE_AVAILABLE - client['boat'] = None + try: + await client['ws'].send(msg) + except websockets.exceptions.ConnectionClosed: + client_disconnect(client) async def sendToBoat(boat, cmd): boat['log'].append({"t": datetime.timestamp(datetime.now()), "type": "tx", "msg": cmd}) @@ -229,8 +241,9 @@ async def run(ws, path): else: break except websockets.exceptions.ConnectionClosed: - print("INFO: client " + client['id'] + ": disconnected") - client['state'] = "terminated" + print("INFO: client " + client['id'] + ": execption") + print("INFO: client " + client['id'] + ": disconnected") + client_disconnect(client) break elif (len(message) == 3 and message[1] == "3440"): client = await new_boat(message[0], message[2], ws) @@ -247,11 +260,12 @@ async def run(ws, path): "msg": message }) except websockets.exceptions.ConnectionClosed: - print("INFO: boat " + client['id'] + ": disconnected") - client['state'] = "terminated" + print("INFO: boat " + client['id'] + ": execption") + print("INFO: boat " + client['id'] + ": disconnected") + boat_disconnect(client) break except websockets.exceptions.ConnectionClosed: - print("INFO: connection disconected") + print("WARN: connection disconected") async def main(): """main"""