example_json_bridge.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python3
  2. """Executable JSONL bridge contract example. SIMULATION ONLY, no hardware I/O.
  3. Replace operation bodies with your documented thermal-board / supply protocol.
  4. The explicit --simulate flag prevents accidental use as a real device driver.
  5. """
  6. import argparse
  7. import json
  8. import sys
  9. parser = argparse.ArgumentParser(description=__doc__)
  10. parser.add_argument("--simulate", action="store_true", required=True)
  11. parser.parse_args()
  12. supply = "off"
  13. target = 80.0
  14. air = 25.0
  15. print("SIMULATION JSONL bridge; no physical I/O", file=sys.stderr, flush=True)
  16. for line in sys.stdin:
  17. request = {}
  18. try:
  19. request = json.loads(line)
  20. op, args = request["operation"], request["arguments"]
  21. if op == "supply_set":
  22. if type(args["on"]) is not bool:
  23. raise ValueError("on must be boolean")
  24. supply = "on" if args["on"] else "off"
  25. result = {"accepted": True}
  26. elif op == "supply_read":
  27. result = {"state": supply, "simulated": True}
  28. elif op == "set_target":
  29. target = float(args["target_c"])
  30. result = {"confirmed_target_c": target, "simulated": True}
  31. elif op == "read_telemetry":
  32. air += (target - air) * 0.7
  33. result = {"air_c": round(air, 2) if supply == "on" else None,
  34. "dimms": [], "quality": "valid" if supply == "on" else "unavailable",
  35. "dimm_quality": "unavailable_in_phase", "board_state": supply,
  36. "confirmed_target_c": target, "simulated": True}
  37. else:
  38. raise ValueError("unsupported operation")
  39. reply = {"id": request["id"], "ok": True, "result": result}
  40. except (ValueError, KeyError, TypeError):
  41. reply = {"id": request.get("id"), "ok": False, "error": "invalid or unsupported request"}
  42. print(json.dumps(reply, allow_nan=False), flush=True)