66 lines
2.3 KiB
Python
66 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Host probe v7 — per-pack BMS reads via the library business 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.
|
|
|
|
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.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)
|
|
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)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|