control_mock.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/usr/bin/env python3
  2. """Control example. DEFAULTS TO IN-PROCESS MOCK. Does not send real hardware actions.
  3. A real Unix control socket is refused unless BOTH are passed:
  4. --i-understand-this-is-real-hardware
  5. --allow-real-endpoint unix:///run/hth/control/control.sock
  6. The example calls EnterSafeMode. It does not copy daemon safety set-points
  7. into an ActionBatch.
  8. """
  9. from __future__ import annotations
  10. import argparse
  11. import asyncio
  12. import sys
  13. from datetime import timedelta
  14. from hth_sdk.advanced import MockClient
  15. from hth_sdk.exceptions import ActionRejected, HthError
  16. async def _run_mock() -> int:
  17. async with MockClient() as client:
  18. caps = await client.get_capabilities("hood.0")
  19. print(f"api_version={caps.api_version} ack_supported={caps.ack_supported}")
  20. print(f"firmware_version_quality={caps.firmware_version_quality}")
  21. print(f"rpm_feedback_supported={caps.rpm_feedback_supported}")
  22. async with await client.acquire_lease("hood.0", "control_mock", timedelta(seconds=8)) as lease:
  23. result = await lease.enter_safe_mode("control_mock-example")
  24. print(
  25. f"enter_safe accepted={result.accepted} dispatched={result.dispatched is not None} "
  26. f"confirmation={result.confirmation}"
  27. )
  28. print("UNCONFIRMED is expected; no ACK on Prod2U UART.")
  29. print("clients call EnterSafeMode; they do not copy daemon safety set-points.")
  30. print("door close-percent 0 means FULL OPEN, not STOP.")
  31. return 0
  32. async def _run_real(observe: str, control: str) -> int:
  33. from hth_sdk.advanced import connect_control
  34. async with await connect_control(observe=observe, control=control) as client:
  35. devices = await client.list_devices()
  36. if not devices:
  37. print("no devices", file=sys.stderr)
  38. return 2
  39. device_id = devices[0].device_id
  40. await client.get_capabilities(device_id)
  41. async with await client.acquire_lease(device_id, "control_mock", timedelta(seconds=8)) as lease:
  42. result = await lease.enter_safe_mode("control_mock-example")
  43. print(f"enter_safe confirmation={result.confirmation} accepted={result.accepted}")
  44. return 0
  45. def main() -> int:
  46. p = argparse.ArgumentParser(
  47. description="Mock control demo. Refuses real hardware unless explicitly forced."
  48. )
  49. p.add_argument("--mock", action="store_true", default=True, help="in-process mock (default)")
  50. p.add_argument("--i-understand-this-is-real-hardware", action="store_true", default=False)
  51. p.add_argument("--allow-real-endpoint", default="", help="control Unix socket")
  52. p.add_argument("--observe-socket", default="/run/hth/observe/observe.sock")
  53. args = p.parse_args()
  54. if args.allow_real_endpoint:
  55. if not args.i_understand_this_is_real_hardware:
  56. print(
  57. "refusing real endpoint: pass --i-understand-this-is-real-hardware",
  58. file=sys.stderr,
  59. )
  60. return 2
  61. return asyncio.run(_run_real(args.observe_socket, args.allow_real_endpoint))
  62. try:
  63. return asyncio.run(_run_mock())
  64. except (ActionRejected, HthError) as exc:
  65. print(f"error: {exc}", file=sys.stderr)
  66. return 1
  67. if __name__ == "__main__":
  68. raise SystemExit(main())