Business operations API: get_battery_soc/get_household_wattage/get_device_inventory

This commit is contained in:
kfj001 committed 2026-10-16 04:58:33 +00:00
1 parent 363c7cc6a2
commit b79adc5d54
7 files changed
+307 -112

No files matched your search

+21 -26
View File
@@ -1,9 +1,8 @@
#!/usr/bin/env python3
"""Host probe v7 — per-pack BMS reads via the library business API.
"""Host probe v7 — battery charge via the business operations API.
The app's ``queryBmsInfoBySocket`` passes ``arguments={"id": pack}``; the
id-less query answers the all-zero ``id: 0`` slot. Pack ids 1..2 select
the two batteries (``BMS_SN …B1/…B2``); ``batSoc`` is the pack charge.
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
@@ -18,48 +17,44 @@ import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(
os.path.abspath(__file__))))
from franklinwh_agate_client.battery import extract_soc # noqa: E402
from franklinwh_agate_client.device_local import ( # noqa: E402
LocalDeviceClient,
)
from franklinwh_agate_client.protocol import ( # noqa: E402
CommunicationCmd,
generate_secret_key,
)
def run(label, client, call):
try:
reply = call()
except (ConnectionError, socket.error, TimeoutError) as exc:
print("--- %s: error: %s: %s" % (label, type(exc).__name__, exc))
return None
print("--- %s (%d B reply)" % (label, len(json.dumps(reply))))
print(" reply: " + json.dumps(reply, default=str)[:1500])
result = extract_soc(reply)
print(" ==> soc: %s (source=%s)" % (result["value"], result["source"]))
return reply
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()]:
print("=== port %d" % port)
client = LocalDeviceClient(args.sn, host=args.host,
socket_port=port, timeout=args.timeout)
run("bms/id1", client, lambda: client.query_bms_info(1))
run("bms/id2", client, lambda: client.query_bms_info(2))
run("apower/update2", client, lambda: client.send_rpc(
CommunicationCmd.DEVICE_APOWER_INFO, data={"update": 2}))
run("ems", client, client.query_ems_info)
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__":
main()
raise SystemExit(main())
+23 -30
View File
@@ -1,8 +1,8 @@
#!/usr/bin/env python3
"""Host probe v8 — live household wattage via the library business API.
"""Host probe v8 — household wattage via the business operations API.
Three socket APIs: realtime detail (home-page poll) -> ``totalPower``,
per-switch load status -> ``SWxExpPower``, EMS telemetry -> watts math.
One call per port: ``LocalDeviceClient.get_household_wattage`` (realtime
detail → per-switch load → EMS math). Prints the watts.
Run from the repo root on the SoftAP-joined host:
python3 scripts/host_probe8.py --sn A03X2516013210060005 --ports 9000
@@ -20,50 +20,43 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(
from franklinwh_agate_client.device_local import ( # noqa: E402
LocalDeviceClient,
)
from franklinwh_agate_client.power import ( # noqa: E402
ems_load_watts,
extract_power,
)
from franklinwh_agate_client.protocol import ( # noqa: E402
generate_secret_key,
)
def run(label, client, call):
try:
reply = call()
except (ConnectionError, socket.error, TimeoutError) as exc:
print("--- %s: error: %s: %s" % (label, type(exc).__name__, exc))
return None
print("--- %s" % label)
print(" reply: " + json.dumps(reply, default=str)[:2000])
result = extract_power(reply)
value, source = result["value"], result["source"]
if value is None:
watts = ems_load_watts(reply)
if watts is not None:
value, source = watts, "ems(loadCurr*V)"
print(" ==> watts: %s (source=%s)" % (value, source))
return reply
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()]:
print("=== port %d" % port)
client = LocalDeviceClient(args.sn, host=args.host,
socket_port=port, timeout=args.timeout)
run("detail/1301", client, client.query_detail_info)
run("load/1411", client, client.query_load_status)
run("ems/1707", client, client.query_ems_info)
try:
result = client.get_household_wattage()
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"]
if value is not None:
print("==> watts: %gW (source=%s, cmd=%s)"
% (value, result["source"], result["cmd"]))
else:
print("==> watts: unknown")
return 0
if __name__ == "__main__":
main()
raise SystemExit(main())