diff --git a/classCAN.py b/classCAN.py index f6fbb21..a04a0ed 100644 --- a/classCAN.py +++ b/classCAN.py @@ -12,7 +12,11 @@ class CANBackend: self.lock = threading.Lock() self.polling_thread = None self.polling_active = False - self.latest_data = {} + self.latest_data = { + 1: {}, # PU1 + 2: {}, # PU2 + 3: {} # PU3 + } self.eds_path = os.path.join(os.path.dirname(__file__), "eds_file", "processBoard_0.eds") def connect(self): @@ -58,40 +62,43 @@ class CANBackend: while self.polling_active: with self.lock: try: - # Poll only PU1 (node ID 0x02) for live monitor values - node = self.nodes.get(2) - if node: - fm1 = node.sdo[0x2004][1].raw - fm2 = node.sdo[0x2004][2].raw - fm3 = node.sdo[0x2004][3].raw - fm4 = node.sdo[0x2004][4].raw + for pu_number, node in self.nodes.items(): + try: + fm1 = node.sdo[0x2004][1].raw + fm2 = node.sdo[0x2004][2].raw + fm3 = node.sdo[0x2004][3].raw + fm4 = node.sdo[0x2004][4].raw - ps1 = node.sdo[0x2005][1].raw - ps2 = node.sdo[0x2005][2].raw - ps3 = node.sdo[0x2005][3].raw - ps4 = node.sdo[0x2005][4].raw + ps1 = node.sdo[0x2005][1].raw + ps2 = node.sdo[0x2005][2].raw + ps3 = node.sdo[0x2005][3].raw + ps4 = node.sdo[0x2005][4].raw - self.latest_data["FM1"] = fm1 / 100.0 - self.latest_data["FM2"] = fm2 / 100.0 - self.latest_data["FM3"] = fm3 / 100.0 - self.latest_data["FM4"] = fm4 / 100.0 + self.latest_data[pu_number] = { + "FM1": fm1 / 100.0, + "FM2": fm2 / 100.0, + "FM3": fm3 / 100.0, + "FM4": fm4 / 100.0, + "PS1": ps1 / 1000.0, + "PS2": ps2 / 1000.0, + "PS3": ps3 / 1000.0, + "PS4": ps4 / 1000.0 + } + + print(f"[PU{pu_number}] FM1: {fm1}, PS1: {ps1}") + except Exception as inner_e: + print(f"[SDO READ ERROR] PU{pu_number}: {inner_e}") + + except Exception as outer_e: + print(f"[SDO POLL ERROR] {outer_e}") - self.latest_data["PS1"] = ps1 / 1000.0 - self.latest_data["PS2"] = ps2 / 1000.0 - self.latest_data["PS3"] = ps3 / 1000.0 - self.latest_data["PS4"] = ps4 / 1000.0 - - print(f"[SDO POLL] FM1: {fm1}, PS1: {ps1}") - print(f"[SDO POLL] FM2: {fm2}, PS2: {ps2}") - print(f"[SDO POLL] FM3: {fm3}, PS3: {ps3}") - print(f"[SDO POLL] FM4: {fm4}, PS4: {ps4}") - except Exception as e: - print(f"[SDO POLL ERROR] {e}") time.sleep(1.0) - def get_latest_data(self): + + def get_latest_data(self, pu_number: int): with self.lock: - return self.latest_data.copy() + return self.latest_data.get(pu_number, {}).copy() + def read_current_state(self, pu_number: int): try: diff --git a/main.py b/main.py index 27b754a..c9efe81 100644 --- a/main.py +++ b/main.py @@ -145,12 +145,13 @@ def get_pu_status(): return JSONResponse(content=states) +from typing import Optional +from fastapi import Query + @app.get("/monitor") -def get_monitor_data(): - data = can_backend.get_latest_data() - print(f"[MONITOR] Latest SDO: {data}") - return { - "PU_1": { +def get_monitor_data(pu_number: Optional[int] = Query(None)): + def format_data(data): + return { "Qperm": data.get("FM1", 0.0), "Qdilute": data.get("FM2", 0.0), "Qdrain": data.get("FM3", 0.0), @@ -177,70 +178,22 @@ def get_monitor_data(): "MV07": data.get("MV07", 0.0), "MV07_sp": data.get("MV07_sp", 0.0), - "MV08": data.get("MV08", 0.0), - "MV08_sp": data.get("MV08_sp", 0.0) - }, - "PU_2": { - "Qperm": data.get("FM1", 0.0), - "Qdilute": data.get("FM2", 0.0), - "Qdrain": data.get("FM3", 0.0), - "Qrecirc": data.get("FM4", 0.0), - - "Pro": data.get("PS1", 0.0), - "Pdilute": data.get("PS2", 0.0), - "Prentate": data.get("PS3", 0.0), - - "Conductivity": data.get("Cond", 0.0), - - "MV02": data.get("MV02", 0.0), - "MV02_sp": data.get("MV02_sp", 0.0), - - "MV03": data.get("MV03", 0.0), - "MV03_sp": data.get("MV03_sp", 0.0), - - "MV05": data.get("MV05", 0.0), - "MV05_sp": data.get("MV05_sp", 0.0), - - "MV06": data.get("MV06", 0.0), - "MV06_sp": data.get("MV06_sp", 0.0), - - "MV07": data.get("MV07", 0.0), - "MV07_sp": data.get("MV07_sp", 0.0), - - "MV08": data.get("MV08", 0.0), - "MV08_sp": data.get("MV08_sp", 0.0) - }, - "PU_3": { - "Qperm": data.get("FM1", 0.0), - "Qdilute": data.get("FM2", 0.0), - "Qdrain": data.get("FM3", 0.0), - "Qrecirc": data.get("FM4", 0.0), - - "Pro": data.get("PS1", 0.0), - "Pdilute": data.get("PS2", 0.0), - "Prentate": data.get("PS3", 0.0), - - "Conductivity": data.get("Cond", 0.0), - - "MV02": data.get("MV02", 0.0), - "MV02_sp": data.get("MV02_sp", 0.0), - - "MV03": data.get("MV03", 0.0), - "MV03_sp": data.get("MV03_sp", 0.0), - - "MV05": data.get("MV05", 0.0), - "MV05_sp": data.get("MV05_sp", 0.0), - - "MV06": data.get("MV06", 0.0), - "MV06_sp": data.get("MV06_sp", 0.0), - - "MV07": data.get("MV07", 0.0), - "MV07_sp": data.get("MV07_sp", 0.0), - "MV08": data.get("MV08", 0.0), "MV08_sp": data.get("MV08_sp", 0.0) } - } + + if pu_number is not None: + data = can_backend.get_latest_data(pu_number) + print(f"[MONITOR] PU{pu_number}: {data}") + return format_data(data) + else: + all_data = {} + for pu in [1, 2, 3]: + data = can_backend.get_latest_data(pu) + print(f"[MONITOR] PU{pu}: {data}") + all_data[f"PU_{pu}"] = format_data(data) + return all_data + @app.get("/can_status") def can_status(): diff --git a/static/monitor.html b/static/monitor.html index faa7c0c..a3b9ad9 100644 --- a/static/monitor.html +++ b/static/monitor.html @@ -28,10 +28,23 @@ h1 { text-align: center; } + #puSelector { + display: block; + margin: 10px auto 20px auto; + font-size: 16px; + padding: 5px 10px; + }