Adds auto test stop

This commit is contained in:
Etienne Chassaing 2025-08-27 15:07:17 +02:00
parent d56b3614c4
commit 73b4eaf861
3 changed files with 89 additions and 30 deletions

View File

@ -23,7 +23,7 @@ class CANBackend:
# Placeholder for thermal loop cleaning
pass
def send_state_command(self, state: str, pu_number : int, ploop_setpoint : float):
def send_state_command(self, state: str, pu_number : int, ploop_setpoint : float, qperm_setpoint : float):
# Placeholder for sending mode command
PUs_states[pu_number-1] = {"PU_MODE": state, "ploop_setpoint":ploop_setpoint}

79
main.py
View File

@ -61,6 +61,9 @@ latest_setpoints: Dict[str, Any] = {
active_PUs: list[int] = []
# Dictionary to hold running tasks
tasks: dict[str, asyncio.Task] = {}
# RECORDER
recording_flag = False
recording_task = None
@ -424,7 +427,7 @@ async def record_data_loop():
## AUTOMATIC TESTING
async def send_command_with_delay(
state: str, pu: int, delay_s: int = 0, ploop_setpoint: float = 0.0, qperm_setpoint:float = 1200.0
state: str, pu: int, delay_s: int = 0, ploop_setpoint: float = 2.5, qperm_setpoint:float = 1200.0
):
await asyncio.sleep(delay_s)
logging.info(f"[AUTO TEST] Sending {state} to PU{pu} after {delay_s}s")
@ -439,35 +442,73 @@ async def set_patients_with_delay(count: int, delay_s: int):
async def auto_test_pu1(ploop_setpoint: float = Query(0.0)):
pu = 1
logging.info("[AUTO TEST] Starting automatic test for 1 PU")
asyncio.create_task(run_auto_test_pu1(pu, ploop_setpoint))
# Cancel existing task if still running
if "pu1" in tasks and not tasks["pu1"].done():
tasks["pu1"].cancel()
logging.info("[AUTO TEST] PU1 Cancelled")
task = asyncio.create_task(run_auto_test_pu1(pu, ploop_setpoint))
tasks["pu1"] = task
return {"status": "started", "pu": pu}
@router.post("/test/auto/2")
async def auto_test_pu2(ploop_setpoint: float = Query(0.0)):
logging.info("[AUTO TEST] Starting automatic test for 2 PUs")
asyncio.create_task(run_auto_test_pu2(ploop_setpoint))
if "pu2" in tasks and not tasks["pu2"].done():
tasks["pu2"].cancel()
logging.info("[AUTO TEST] PU2 Cancelled")
task = asyncio.create_task(run_auto_test_pu2(ploop_setpoint))
tasks["pu2"] = task
return {"status": "started", "pu": [1, 2]}
@router.post("/test/auto/stop/{pu}")
async def stop_auto_test(pu: int):
key = f"pu{pu}"
logging.info(f"[AUTO TEST] Stopping {pu}")
print("tasks",tasks)
if key in tasks and not tasks[key].done():
tasks[key].cancel()
await send_command_with_delay("IDLE", pu, delay_s=0)
logging.info(f"[AUTO TEST] {key} STOPPED")
return {"status": "stopped", "pu": pu}
logging.info(f"[AUTO TEST] Stopping {pu} No test Runining")
return {"status": "no task running", "pu": pu}
async def run_auto_test_pu1(pu: int, ploop_setpoint: float):
await send_command_with_delay("PRE-PRODUCTION", pu, delay_s=0, ploop_setpoint=ploop_setpoint)
# await send_command_with_delay("PRODUCTION", pu, delay_s=180, ploop_setpoint=ploop_setpoint)
await set_patients_with_delay(5, delay_s=60)
await set_patients_with_delay(10, delay_s=60)
await send_command_with_delay("IDLE", pu, delay_s=60, ploop_setpoint=ploop_setpoint)
logging.info("[AUTO TEST] Finished PU1 test")
try:
await send_command_with_delay("PRE-PRODUCTION", pu, delay_s=0, ploop_setpoint=ploop_setpoint)
# await send_command_with_delay("PRODUCTION", pu, delay_s=180, ploop_setpoint=ploop_setpoint)
await set_patients_with_delay(5, delay_s=60)
await set_patients_with_delay(10, delay_s=60)
await send_command_with_delay("IDLE", pu, delay_s=60, ploop_setpoint=ploop_setpoint)
logging.info("[AUTO TEST] Finished PU1 test")
except asyncio.CancelledError:
logging.info(f"[AUTO TEST] PU 1 task cancelled")
# optional cleanup
raise
async def run_auto_test_pu2(ploop_setpoint: float):
# Step 1: Run PU1 test
# await run_auto_test_pu1(1, ploop_setpoint)
try:
# Step 1: Run PU1 test
# await run_auto_test_pu1(1, ploop_setpoint)
# Step 2: PU2 sequence
await send_command_with_delay("PRE-PRODUCTION", 2, delay_s=0, ploop_setpoint=ploop_setpoint)
# await send_command_with_delay("PRODUCTION", 2, delay_s=180, ploop_setpoint=ploop_setpoint)
await set_patients_with_delay(15, delay_s=60)
await set_patients_with_delay(0, delay_s=60)
await send_command_with_delay("IDLE", 2, delay_s=60, ploop_setpoint=ploop_setpoint)
await send_command_with_delay("IDLE", 1, delay_s=60, ploop_setpoint=ploop_setpoint)
logging.info("[AUTO TEST] Finished PU1 + PU2 test")
# Step 2: PU2 sequence
await send_command_with_delay("PRE-PRODUCTION", 2, delay_s=0, ploop_setpoint=ploop_setpoint)
# await send_command_with_delay("PRODUCTION", 2, delay_s=180, ploop_setpoint=ploop_setpoint)
await set_patients_with_delay(15, delay_s=60)
await set_patients_with_delay(0, delay_s=60)
await send_command_with_delay("IDLE", 2, delay_s=60, ploop_setpoint=ploop_setpoint)
await send_command_with_delay("IDLE", 1, delay_s=60, ploop_setpoint=ploop_setpoint)
logging.info("[AUTO TEST] Finished PU1 + PU2 test")
except asyncio.CancelledError:
logging.info(f"[AUTO TEST] PU 2 task cancelled")
# optional cleanup
raise
@router.post("/test/auto/3")
async def auto_test_pu3():

View File

@ -234,6 +234,11 @@
.button-group button:hover {
background-color: #005f6b;
}
.auto-running {
background-color: #ffcc00 !important; /* yellow */
color: black !important;
}
</style>
</head>
<body>
@ -324,6 +329,7 @@
<button onclick="runAutoTest(2)">Automatic Test PU2</button>
<button onclick="runAutoTest(3)">Automatic Test PU3</button>
</div>
</div>
<div class="right-panel">
<div class="monitor-block">
@ -501,19 +507,31 @@
}
}
function runAutoTest(puNumber) {
const endpoint = `/test/auto/${puNumber}`;
fetch(endpoint, {
method: 'POST'
})
.then(response => {
if (!response.ok) {
throw new Error(`Test PU${puNumber} failed`);
async function runAutoTest(puNumber) {
const button = document.querySelector(`button[onclick="runAutoTest(${puNumber})"]`);
if (!button.classList.contains("auto-running")) {
// START test
const res = await fetch(`/test/auto/${puNumber}`, { method: "POST" });
if (res.ok) {
button.classList.add("auto-running");
button.textContent = `Stop Auto Test PU${puNumber}`;
} else {
alert("Failed to start auto test");
}
return response.json();
});
} else {
// STOP test
const res = await fetch(`/test/auto/stop/${puNumber}`, { method: "POST" });
if (res.ok) {
button.classList.remove("auto-running");
button.textContent = `Automatic Test PU${puNumber}`;
} else {
alert("Failed to stop auto test");
}
}
}
async function fetchPUStatus() {
const response = await fetch("/api/pu_status");
const data = await response.json();