Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions leads_vec_rc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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"


Expand Down