Files
we_monitor/client.py
T
jensandClaude Sonnet 4.6 4492692786 Update clients, docs, and install for CarConnectivity migration
- gui_client: update _TRACE_LABELS paths to new flat snapshot structure
- README: update library name, pip command, domain list, credentials
  template, output example, and module reference
- install.sh: update credentials template and systemd service description
- client.py: update docstring
- data_model: Kelvin→Celsius conversion for battery temps (CarConnectivity
  returns Kelvin); round range_estimated_full to 1 decimal
- jay_diff: fix merge when old value is not a dict; skip empty-dict
  branches in delete

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-27 11:43:41 +02:00

74 lines
2.3 KiB
Python

#!/usr/bin/env python3
"""
CarConnectivity push-server test client.
Connects to the collect.py TCP push server and prints each incoming
snapshot to stdout as pretty-printed JSON.
Usage
-----
python client.py
python client.py --host 192.168.1.10 --port 9999
python client.py --diff # reconstruct from diff stream
"""
import argparse
import json
import socket
import sys
from utils.jay_diff import jay_merge_full
def main() -> None:
p = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
p.add_argument("--host", default="127.0.0.1", help="Server host (default: 127.0.0.1)")
p.add_argument("--port", type=int, default=9999, help="Server port (default: 9999)")
p.add_argument("--diff", action="store_true", help="Reconstruct full snapshots from diff stream")
args = p.parse_args()
print(f"Connecting to {args.host}:{args.port} …", flush=True)
try:
sock = socket.create_connection((args.host, args.port))
except OSError as e:
print(f"Connection failed: {e}", file=sys.stderr)
sys.exit(1)
print("Connected. Waiting for data (Ctrl-C to quit).\n", flush=True)
buf = ""
count = 0
state: dict = {}
try:
with sock:
while True:
chunk = sock.recv(4096).decode()
if not chunk:
print("Server closed the connection.")
break
buf += chunk
while "\n" in buf:
line, buf = buf.split("\n", 1)
line = line.strip()
if not line:
continue
try:
data = json.loads(line)
if args.diff:
state = jay_merge_full(state, data)
display = state
else:
display = data
count += 1
print(json.dumps(display, indent=2))
print(f"--- #{count} ", "-" * 50, flush=True)
except json.JSONDecodeError as e:
print(f"[invalid JSON] {e}: {line!r}", file=sys.stderr)
except KeyboardInterrupt:
print("\nStopped by user.")
if __name__ == "__main__":
main()