From f5a7b3c8d78ffba54d03792972deadcd0b46b204 Mon Sep 17 00:00:00 2001 From: Etienne Chassaing <60154720+cetiennec@users.noreply.github.com> Date: Thu, 17 Jul 2025 09:46:13 +0200 Subject: [PATCH] Adds Qskid --- MockCAN.py | 9 +++-- main.py | 43 ++++++++++---------- static/monitor.html | 99 ++++++++++++++++++++++++--------------------- 3 files changed, 78 insertions(+), 73 deletions(-) diff --git a/MockCAN.py b/MockCAN.py index d67874d..7572f31 100644 --- a/MockCAN.py +++ b/MockCAN.py @@ -31,10 +31,11 @@ class CANBackend: # Simulate getting the latest data with random values if pu_number ==1: return { - "FM2": 1080, - "FM3": 1080, - "FM4": 1080, - "FM5": 1080, + "FM2": 630.0, + "FM2": 1080.0, + "FM3": 1080.0, + "FM4": 1080.0, + "FM5": 1080.0, "PS1": 6.2, "PS2": 6.2, "PS3": 6.2, diff --git a/main.py b/main.py index 2dfc7f8..007b8cf 100644 --- a/main.py +++ b/main.py @@ -43,7 +43,7 @@ app.mount("/static", StaticFiles(directory="static"), name="static") ## BUFFER CREATION AND HELPER # Global object to store the latest data -latest_data: Dict[str, Any] = {"PU_1": None, "PU_2": None, "PU_3": None} +latest_data: Dict[str, Any] = {"PU_1": None, "PU_2": None, "PU_3": None, "PatientSkid" : {"QSkid":0.0}} # RECORDER recording_flag = False @@ -54,6 +54,22 @@ write_buffer = deque() flush_interval = 1.0 # flush every 1 second last_flush_time = datetime.datetime.now() +## LOGGING QSkid +async def update_latest_flow(): + async with aiohttp.ClientSession() as session: + while True: + try: + async with session.get("http://192.168.1.28:8000/instant_flow") as resp: + data = await resp.json() + latest_flow = int(data["log"]["flow"]) + logging.debug(f"Updated flow: {latest_flow}") + latest_data["PatientSkid"]["QSkid"] = latest_flow + + except Exception as e: + logging.error(f"Error fetching flow: {e}") + await asyncio.sleep(1) + + def format_data(data): return { "timestamp": datetime.datetime.now().isoformat(), @@ -81,21 +97,6 @@ def format_data(data): "MV08_sp": np.round(data.get("MV08_sp", 0.0), 1), } -## LOGGING QCONSO -latest_flow = 0 -async def update_latest_flow(): - async with aiohttp.ClientSession() as session: - while True: - try: - async with session.get("http://192.168.1.28:8000/instant_flow") as resp: - data = await resp.json() - latest_flow = int(data["log"]["flow"]) - logging.info(f"Updated flow: {latest_flow}") - - except Exception as e: - logging.error(f"Error fetching flow: {e}") - await asyncio.sleep(1) - # CREDENTIALS # Load users from JSON file at startup @@ -111,8 +112,6 @@ PASSWORD = CREDENTIALS["password"] # ======== LOGIN & SESSION HANDLING ======== - - def require_login(request: Request): user = request.session.get("user") if user != USERNAME: @@ -226,17 +225,17 @@ def get_pu_status(): async def update_latest_data(): while True: - for pu in [1, 2, 3]: + for pu in [1, 2]: # TODO: REPLACE THIS WITH CONNECTED PUs, IS GET PU STATUS SLOW? data = can_backend.get_latest_data(pu_number=pu) - # logging.info(f"[MONITOR BUFFER] PU{pu}: ") # {data} latest_data[f"PU_{pu}"] = format_data(data) - latest_data[f"PU_{pu}"]["Qconso"] = latest_flow + current_data = latest_data[f"PU_{pu}"] + logging.debug(f"[MONITOR BUFFER] PU{pu}: {current_data}") + # logging.info(f"[MONITOR BUFFER] latest_data: {latest_data}") await asyncio.sleep(0.2) @app.get("/monitor") async def get_monitor_data(pu_number: Optional[int] = Query(None)): - logging.info(f"[MONITOR DATA QUERY] {pu_number}") if pu_number is not None: return latest_data.get(f"PU_{pu_number}", {}) else: diff --git a/static/monitor.html b/static/monitor.html index 0e06589..a7b76ee 100644 --- a/static/monitor.html +++ b/static/monitor.html @@ -59,12 +59,12 @@
- - - - - - + + + + + +