| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- #!/usr/bin/env python3
- """Control example. DEFAULTS TO IN-PROCESS MOCK. Does not send real hardware actions.
- A real Unix control socket is refused unless BOTH are passed:
- --i-understand-this-is-real-hardware
- --allow-real-endpoint unix:///run/hth/control/control.sock
- The example calls EnterSafeMode. It does not copy daemon safety set-points
- into an ActionBatch.
- """
- from __future__ import annotations
- import argparse
- import asyncio
- import sys
- from datetime import timedelta
- from hth_sdk.advanced import MockClient
- from hth_sdk.exceptions import ActionRejected, HthError
- async def _run_mock() -> int:
- async with MockClient() as client:
- caps = await client.get_capabilities("hood.0")
- print(f"api_version={caps.api_version} ack_supported={caps.ack_supported}")
- print(f"firmware_version_quality={caps.firmware_version_quality}")
- print(f"rpm_feedback_supported={caps.rpm_feedback_supported}")
- async with await client.acquire_lease("hood.0", "control_mock", timedelta(seconds=8)) as lease:
- result = await lease.enter_safe_mode("control_mock-example")
- print(
- f"enter_safe accepted={result.accepted} dispatched={result.dispatched is not None} "
- f"confirmation={result.confirmation}"
- )
- print("UNCONFIRMED is expected; no ACK on Prod2U UART.")
- print("clients call EnterSafeMode; they do not copy daemon safety set-points.")
- print("door close-percent 0 means FULL OPEN, not STOP.")
- return 0
- async def _run_real(observe: str, control: str) -> int:
- from hth_sdk.advanced import connect_control
- async with await connect_control(observe=observe, control=control) as client:
- devices = await client.list_devices()
- if not devices:
- print("no devices", file=sys.stderr)
- return 2
- device_id = devices[0].device_id
- await client.get_capabilities(device_id)
- async with await client.acquire_lease(device_id, "control_mock", timedelta(seconds=8)) as lease:
- result = await lease.enter_safe_mode("control_mock-example")
- print(f"enter_safe confirmation={result.confirmation} accepted={result.accepted}")
- return 0
- def main() -> int:
- p = argparse.ArgumentParser(
- description="Mock control demo. Refuses real hardware unless explicitly forced."
- )
- p.add_argument("--mock", action="store_true", default=True, help="in-process mock (default)")
- p.add_argument("--i-understand-this-is-real-hardware", action="store_true", default=False)
- p.add_argument("--allow-real-endpoint", default="", help="control Unix socket")
- p.add_argument("--observe-socket", default="/run/hth/observe/observe.sock")
- args = p.parse_args()
- if args.allow_real_endpoint:
- if not args.i_understand_this_is_real_hardware:
- print(
- "refusing real endpoint: pass --i-understand-this-is-real-hardware",
- file=sys.stderr,
- )
- return 2
- return asyncio.run(_run_real(args.observe_socket, args.allow_real_endpoint))
- try:
- return asyncio.run(_run_mock())
- except (ActionRejected, HthError) as exc:
- print(f"error: {exc}", file=sys.stderr)
- return 1
- if __name__ == "__main__":
- raise SystemExit(main())
|