| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- #!/usr/bin/env python3
- """Read-only live Qt-clean Training/UART observer; never starts a host."""
- from __future__ import annotations
- import argparse
- import asyncio
- import base64
- import json
- from pathlib import Path
- import time
- import aiohttp
- async def observe(args):
- history = []
- total_uart_bytes = 0
- sample = bytearray()
- async with aiohttp.ClientSession() as client:
- async with client.ws_connect(args.url, heartbeat=30, max_msg_size=2 * 1024 * 1024) as ws:
- until = time.monotonic() + args.duration
- while time.monotonic() < until:
- try:
- frame = await ws.receive_json(timeout=min(5.0, max(.1, until - time.monotonic())))
- except asyncio.TimeoutError:
- continue
- if frame.get("event") == "bios_uart":
- item = frame.get("result", {})
- if item.get("station_id") != args.station:
- continue
- data = base64.b64decode(item.get("data_base64", ""), validate=True)
- total_uart_bytes += len(data)
- if len(sample) < 1024 * 1024:
- sample.extend(data[:1024 * 1024 - len(sample)])
- elif frame.get("event") == "state":
- stations = frame.get("result", {}).get("stations", [])
- state = next((s for s in stations if s.get("station_id") == args.station), {})
- if not state:
- continue
- channels = state.get("channels", [])
- row = {"unix_ms": int(time.time() * 1000), "boot_id": state.get("boot_id"),
- "phase": state.get("phase"), "run_state": state.get("run_state"),
- "detected": sum(bool(c.get("present")) for c in channels),
- "green": sum(c.get("state") == "pass" for c in channels),
- "red": sum(c.get("state") == "fail" for c in channels),
- "idle": sum(c.get("state") == "idle" for c in channels),
- "verdict": state.get("verdict", "unknown")}
- if not history or history[-1]["boot_id"] != row["boot_id"] or any(
- history[-1][key] != row[key] for key in ("phase", "detected", "green", "red", "idle", "verdict")):
- history.append(row)
- print(json.dumps(row, ensure_ascii=False), flush=True)
- output = {"station": args.station, "history": history, "uart_bytes": total_uart_bytes,
- "raw_training_signature": b"DDR Training -- SUCCEEDED" in sample,
- "saw_initial_all_idle": any(row["idle"] == 24 and row["green"] == 0 and row["red"] == 0 for row in history),
- "saw_detected_green": any(row["detected"] > 0 and row["green"] == row["detected"]
- and row["idle"] == 24 - row["detected"] and row["red"] == 0 for row in history),
- "saw_before_amt": any(row["detected"] > 0 and row["green"] == row["detected"]
- and row["idle"] == 24 - row["detected"] and row["red"] == 0
- and row["phase"] in ("training", "rmt", "uip") for row in history),
- "final_verdict": history[-1]["verdict"] if history else "unknown"}
- if args.output:
- Path(args.output).write_text(json.dumps(output, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
- print(json.dumps({key: value for key, value in output.items() if key != "history"}, ensure_ascii=False))
- return 0 if output["saw_detected_green"] and total_uart_bytes and output["raw_training_signature"] else 1
- if __name__ == "__main__":
- parser = argparse.ArgumentParser()
- parser.add_argument("--url", default="ws://127.0.0.1:8080/ws?view=qt-clean")
- parser.add_argument("--station", default="S1")
- parser.add_argument("--duration", type=float, default=90.0)
- parser.add_argument("--output")
- raise SystemExit(asyncio.run(observe(parser.parse_args())))
|