| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- #!/usr/bin/env python3
- """Subscribe to telemetry. Default: in-process MockClient (no daemon, no serial).
- Use --socket /run/hth/observe.sock to talk to a running hthd. Observe only.
- """
- from __future__ import annotations
- import argparse
- import asyncio
- import sys
- async def _run_mock(count: int) -> None:
- from hth_sdk.advanced import MockClient
- async with MockClient() as client:
- devices = await client.list_devices()
- print(f"devices={[d.device_id for d in devices]} backend=mock")
- n = 0
- async for event in client.subscribe_telemetry(devices[0].device_id):
- snap = event.snapshot
- print(
- f"seq={snap.snapshot_seq} epoch={snap.device_epoch} "
- f"lifecycle={snap.lifecycle} gap={event.data_gap} "
- f"epoch_changed={event.epoch_changed} confirm={snap.device_confirmation}"
- )
- n += 1
- if n >= count:
- return
- async def _run_socket(socket: str, count: int) -> None:
- from hth_sdk.advanced import connect_observe
- async with await connect_observe(socket) as client:
- devices = await client.list_devices()
- if not devices:
- print("no devices", file=sys.stderr)
- return
- n = 0
- async for event in client.subscribe_telemetry(devices[0].device_id):
- snap = event.snapshot
- print(
- f"seq={snap.snapshot_seq} epoch={snap.device_epoch} "
- f"lifecycle={snap.lifecycle} gap={event.data_gap} "
- f"epoch_changed={event.epoch_changed}"
- )
- n += 1
- if n >= count:
- return
- def main() -> int:
- p = argparse.ArgumentParser(description="Observe hth telemetry (mock by default).")
- p.add_argument("--socket", default="", help="observe Unix socket; empty = in-process mock")
- p.add_argument("--count", type=int, default=3)
- args = p.parse_args()
- if args.socket:
- asyncio.run(_run_socket(args.socket, args.count))
- else:
- asyncio.run(_run_mock(args.count))
- return 0
- if __name__ == "__main__":
- raise SystemExit(main())
|