diff --git a/leads_vec_rc/cli.py b/leads_vec_rc/cli.py index 7a4546de..c7c0601f 100644 --- a/leads_vec_rc/cli.py +++ b/leads_vec_rc/cli.py @@ -3,6 +3,7 @@ from json import loads, JSONDecodeError from os import mkdir from os.path import abspath, exists +from time import sleep from typing import Any from fastapi import FastAPI @@ -29,14 +30,26 @@ ), time_stamp_record, voltage_record, speed_record, None, None, None, None, None, None, None, None, None) +def retry(service: Service) -> Client: + L.warn("Retrying connection...") + return start_client(config.comm_addr, create_client(service.port(), callback), True) + + class CommCallback(Callback): + def __init__(self) -> None: + super().__init__() + self.client: Client = start_client(config.comm_addr, create_client(config.comm_port, self), True) + def on_connect(self, service: Service, connection: Connection) -> None: self.super(service=service, connection=connection) - L.debug("Connected") + L.info("Connected") def on_fail(self, service: Service, error: Exception) -> None: self.super(service=service, error=error) L.error(f"Comm client error: {repr(error)}") + sleep(10) + assert isinstance(service, Client) + self.client = retry(service) def on_receive(self, service: Service, msg: bytes) -> None: self.super(service=service, msg=msg) @@ -54,10 +67,17 @@ def on_receive(self, service: Service, msg: bytes) -> None: except JSONDecodeError as e: L.error(repr(e)) + def on_disconnect(self, service: Service, connection: ConnectionBase) -> None: + self.super(service=service, connection=connection) + L.info("Disconnected") + sleep(10) + assert isinstance(service, Client) + self.client = retry(service) + -client = start_client(config.comm_addr, create_client(config.comm_port, CommCallback()), True) +callback: CommCallback = CommCallback() -app = FastAPI(title="LEADS VeC Remote Analyst") +app: FastAPI = FastAPI(title="LEADS VeC Remote Analyst") app.add_middleware( CORSMiddleware, @@ -90,13 +110,13 @@ async def speed() -> list[float]: @app.get("/time_lap") async def time_lap() -> str: - client.send(b"time_lap") + callback.client.send(b"time_lap") return "done" @app.get("/hazard") async def hazard() -> str: - client.send(b"hazard") + callback.client.send(b"hazard") return "done"