live_channel_probe.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python3
  2. """Read-only live Qt-clean Training/UART observer; never starts a host."""
  3. from __future__ import annotations
  4. import argparse
  5. import asyncio
  6. import base64
  7. import json
  8. from pathlib import Path
  9. import time
  10. import aiohttp
  11. async def observe(args):
  12. history = []
  13. total_uart_bytes = 0
  14. sample = bytearray()
  15. async with aiohttp.ClientSession() as client:
  16. async with client.ws_connect(args.url, heartbeat=30, max_msg_size=2 * 1024 * 1024) as ws:
  17. until = time.monotonic() + args.duration
  18. while time.monotonic() < until:
  19. try:
  20. frame = await ws.receive_json(timeout=min(5.0, max(.1, until - time.monotonic())))
  21. except asyncio.TimeoutError:
  22. continue
  23. if frame.get("event") == "bios_uart":
  24. item = frame.get("result", {})
  25. if item.get("station_id") != args.station:
  26. continue
  27. data = base64.b64decode(item.get("data_base64", ""), validate=True)
  28. total_uart_bytes += len(data)
  29. if len(sample) < 1024 * 1024:
  30. sample.extend(data[:1024 * 1024 - len(sample)])
  31. elif frame.get("event") == "state":
  32. stations = frame.get("result", {}).get("stations", [])
  33. state = next((s for s in stations if s.get("station_id") == args.station), {})
  34. if not state:
  35. continue
  36. channels = state.get("channels", [])
  37. row = {"unix_ms": int(time.time() * 1000), "boot_id": state.get("boot_id"),
  38. "phase": state.get("phase"), "run_state": state.get("run_state"),
  39. "detected": sum(bool(c.get("present")) for c in channels),
  40. "green": sum(c.get("state") == "pass" for c in channels),
  41. "red": sum(c.get("state") == "fail" for c in channels),
  42. "idle": sum(c.get("state") == "idle" for c in channels),
  43. "verdict": state.get("verdict", "unknown")}
  44. if not history or history[-1]["boot_id"] != row["boot_id"] or any(
  45. history[-1][key] != row[key] for key in ("phase", "detected", "green", "red", "idle", "verdict")):
  46. history.append(row)
  47. print(json.dumps(row, ensure_ascii=False), flush=True)
  48. output = {"station": args.station, "history": history, "uart_bytes": total_uart_bytes,
  49. "raw_training_signature": b"DDR Training -- SUCCEEDED" in sample,
  50. "saw_initial_all_idle": any(row["idle"] == 24 and row["green"] == 0 and row["red"] == 0 for row in history),
  51. "saw_detected_green": any(row["detected"] > 0 and row["green"] == row["detected"]
  52. and row["idle"] == 24 - row["detected"] and row["red"] == 0 for row in history),
  53. "saw_before_amt": any(row["detected"] > 0 and row["green"] == row["detected"]
  54. and row["idle"] == 24 - row["detected"] and row["red"] == 0
  55. and row["phase"] in ("training", "rmt", "uip") for row in history),
  56. "final_verdict": history[-1]["verdict"] if history else "unknown"}
  57. if args.output:
  58. Path(args.output).write_text(json.dumps(output, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
  59. print(json.dumps({key: value for key, value in output.items() if key != "history"}, ensure_ascii=False))
  60. return 0 if output["saw_detected_green"] and total_uart_bytes and output["raw_training_signature"] else 1
  61. if __name__ == "__main__":
  62. parser = argparse.ArgumentParser()
  63. parser.add_argument("--url", default="ws://127.0.0.1:8080/ws?view=qt-clean")
  64. parser.add_argument("--station", default="S1")
  65. parser.add_argument("--duration", type=float, default=90.0)
  66. parser.add_argument("--output")
  67. raise SystemExit(asyncio.run(observe(parser.parse_args())))