Adds Qconso

This commit is contained in:
Etienne Chassaing 2025-07-16 17:20:52 +02:00
parent ff4050e180
commit b7ab8b797d
2 changed files with 3851 additions and 33 deletions

76
main.py
View File

@ -20,6 +20,8 @@ import datetime
from valveBackend import ValveBackend from valveBackend import ValveBackend
import csv import csv
from collections import deque from collections import deque
import numpy as np
import aiohttp
if platform.system() in ["Darwin"]: # macOS or Windows if platform.system() in ["Darwin"]: # macOS or Windows
from MockCAN import CANBackend from MockCAN import CANBackend
@ -52,41 +54,47 @@ write_buffer = deque()
flush_interval = 1.0 # flush every 1 second flush_interval = 1.0 # flush every 1 second
last_flush_time = datetime.datetime.now() last_flush_time = datetime.datetime.now()
def format_data(data): def format_data(data):
return { return {
"timestamp": datetime.datetime.now().isoformat(), "timestamp": datetime.datetime.now().isoformat(),
"Qperm": data.get("FM2", 0.0), "Qperm": np.round(data.get("FM2", 0.0), 1),
"Qdilute": data.get("FM1", 0.0), "Qdilute": np.round(data.get("FM1", 0.0), 1),
"Qdrain": data.get("FM4", 0.0), "Qdrain": np.round(data.get("FM4", 0.0), 1),
"Qrecirc": data.get("FM3", 0.0), "Qrecirc": np.round(data.get("FM3", 0.0), 1),
"Pro": np.round(data.get("PS1", 0.0), 1),
"Pro": data.get("PS1", 0.0), "Pdilute": np.round(data.get("PS3", 0.0), 1),
"Pdilute": data.get("PS3", 0.0), "Prentate": np.round(data.get("PS2", 0.0), 1),
"Prentate": data.get("PS2", 0.0), "Conductivity": np.round(data.get("Cond", 0.0), 1),
"MV02": np.round(data.get("MV02", 0.0), 1),
"Conductivity": data.get("Cond", 0.0), "MV02_sp": np.round(data.get("MV02_sp", 0.0), 1),
"MV03": np.round(data.get("MV03", 0.0), 1),
"MV02": data.get("MV02", 0.0), "MV03_sp": np.round(data.get("MV03_sp", 0.0), 1),
"MV02_sp": data.get("MV02_sp", 0.0), "MV04": np.round(data.get("MV05", 0.0), 1),
"MV04_sp": np.round(data.get("MV05_sp", 0.0), 1),
"MV03": data.get("MV03", 0.0), "MV05": np.round(data.get("MV05", 0.0), 1),
"MV03_sp": data.get("MV03_sp", 0.0), "MV05_sp": np.round(data.get("MV05_sp", 0.0), 1),
"MV04": data.get("MV05", 0.0), "MV06": np.round(data.get("MV06", 0.0), 1),
"MV04_sp": data.get("MV05_sp", 0.0), "MV06_sp": np.round(data.get("MV06_sp", 0.0), 1),
"MV05": data.get("MV05", 0.0), "MV07": np.round(data.get("MV07", 0.0), 1),
"MV05_sp": data.get("MV05_sp", 0.0), "MV07_sp": np.round(data.get("MV07_sp", 0.0), 1),
"MV06": data.get("MV06", 0.0), "MV08": np.round(data.get("MV08", 0.0), 1),
"MV06_sp": data.get("MV06_sp", 0.0), "MV08_sp": np.round(data.get("MV08_sp", 0.0), 1),
"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),
} }
## 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 # CREDENTIALS
@ -220,15 +228,16 @@ async def update_latest_data():
while True: while True:
for pu in [1, 2, 3]: for pu in [1, 2, 3]:
data = can_backend.get_latest_data(pu_number=pu) data = can_backend.get_latest_data(pu_number=pu)
logging.info(f"[MONITOR BUFFER] PU{pu}: ") # {data} # logging.info(f"[MONITOR BUFFER] PU{pu}: ") # {data}
latest_data[f"PU_{pu}"] = format_data(data) latest_data[f"PU_{pu}"] = format_data(data)
await asyncio.sleep(0.2) # Update every 100ms latest_data[f"PU_{pu}"]["Qconso"] = latest_flow
await asyncio.sleep(0.2)
@app.get("/monitor") @app.get("/monitor")
async def get_monitor_data(pu_number: Optional[int] = Query(None)): async def get_monitor_data(pu_number: Optional[int] = Query(None)):
logging.info(f"[MONITOR DATA QUERY] {pu_number}") logging.info(f"[MONITOR DATA QUERY] {pu_number}")
if pu_number: if pu_number is not None:
return latest_data.get(f"PU_{pu_number}", {}) return latest_data.get(f"PU_{pu_number}", {})
else: else:
return latest_data return latest_data
@ -237,6 +246,7 @@ async def get_monitor_data(pu_number: Optional[int] = Query(None)):
@app.on_event("startup") @app.on_event("startup")
async def startup_event(): async def startup_event():
asyncio.create_task(update_latest_data()) asyncio.create_task(update_latest_data())
asyncio.create_task(update_latest_flow())
@app.get("/can_status") @app.get("/can_status")

File diff suppressed because it is too large Load Diff