Refactor collect.py into focused modules
- data_model.py : _str, _phys, load_store, save_store, auto_out_path
- we_connect.py : domain extractors, ALL_DOMAINS, connect,
select_vehicle, collect_snapshot
- network.py : TCP push server (start_push_server, broadcast)
- log_config.py : logging setup
- collect.py : run() loop and CLI, wired to the above modules
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import json
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _str(v) -> str:
|
||||
return str(v.value) if hasattr(v, "value") else str(v)
|
||||
|
||||
|
||||
def _phys(value, unit: str) -> dict:
|
||||
return {"value": value, "unit": unit}
|
||||
|
||||
|
||||
def auto_out_path(logs_dir: Path, vin: str) -> Path:
|
||||
stamp = datetime.now(timezone.utc).strftime("%Y_%m_%d_%H_%M_%S")
|
||||
return logs_dir / f"{stamp}_{vin}.json"
|
||||
|
||||
|
||||
def load_store(path: Path, vin: str, interval: int) -> dict:
|
||||
if path.exists():
|
||||
try:
|
||||
data = json.loads(path.read_text())
|
||||
if isinstance(data, dict) and "records" in data:
|
||||
return data
|
||||
except json.JSONDecodeError:
|
||||
log.warning("Output file is corrupt — starting fresh")
|
||||
return {
|
||||
"meta": {
|
||||
"vin": vin,
|
||||
"created_at": datetime.now(timezone.utc).isoformat(),
|
||||
"interval_seconds": interval,
|
||||
},
|
||||
"records": [],
|
||||
}
|
||||
|
||||
|
||||
def save_store(path: Path, store: dict, max_records: int | None) -> None:
|
||||
if max_records and len(store["records"]) > max_records:
|
||||
store["records"] = store["records"][-max_records:]
|
||||
path.write_text(json.dumps(store, indent=2, default=str))
|
||||
Reference in New Issue
Block a user