#!/usr/bin/env python3 """Host probe v7 — battery charge via the business operations API. One call per port: ``LocalDeviceClient.get_battery_soc`` (discovery, apower, per-pack BMS reads). Prints the percentage. Run from the repo root on the SoftAP-joined host: python3 scripts/host_probe7.py --sn A03X2516013210060005 --ports 9000 """ import argparse import json import os import socket import sys sys.path.insert(0, os.path.dirname(os.path.dirname( os.path.abspath(__file__)))) from franklinwh_agate_client.device_local import ( # noqa: E402 LocalDeviceClient, ) from franklinwh_agate_client.protocol import ( # noqa: E402 generate_secret_key, ) def main(): ap = argparse.ArgumentParser() ap.add_argument("--sn", required=True) ap.add_argument("--host", default="10.100.1.1") ap.add_argument("--ports", default="9000") ap.add_argument("--timeout", type=float, default=5.0) ap.add_argument("--raw", action="store_true", help="print the full reply envelope") args = ap.parse_args() print("secret key for %s: %s (radix 16)" % (args.sn, generate_secret_key(args.sn))) for port in [int(p) for p in args.ports.split(",") if p.strip()]: client = LocalDeviceClient(args.sn, host=args.host, socket_port=port, timeout=args.timeout) try: result = client.get_battery_soc() except (ConnectionError, socket.error, TimeoutError) as exc: print("=== port %d: error: %s: %s" % (port, type(exc).__name__, exc)) continue print("=== port %d" % port) if args.raw: print(json.dumps(result.get("reply"), indent=2, default=str)) return 0 value = result["value"] print("==> soc: %s%% (source=%s, cmd=%s)" % (value, result["source"], result["cmd"]) if value is not None else "==> soc: unknown") return 0 if __name__ == "__main__": raise SystemExit(main())