70 lines
2.2 KiB
Python
70 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Host probe v8 — live household wattage via the library business API.
|
|
|
|
Three socket APIs: realtime detail (home-page poll) -> ``totalPower``,
|
|
per-switch load status -> ``SWxExpPower``, EMS telemetry -> watts math.
|
|
|
|
Run from the repo root on the SoftAP-joined host:
|
|
python3 scripts/host_probe8.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.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)
|
|
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)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|