Ingests the 2026-07-09 live session (source page + raw GetConfiguration dump): charger identified as SimpleCharge MultiSocketCharger, 60 kW DC dual-standard (gun 1 EU/CCS, gun 2 GB/T); RFID and remote start/stop verified; by-vehicle auth closed as not supported (three escalating tests incl. MacEnabled=true after power-cycle); touchscreen-shows-VIN observation with the DataTransferVendorId long-shot lead. Adds/updates: architecture plan (FastAPI + PWA, phases 1-3 done), backend entity, wifi-relays entity (Tuya local-control path + key extraction steps), authorization concept (API user/scope model, per-gun cards), overview, runbook, index, log.
5.4 KiB
type, updated, tags
| type | updated | tags | |||||
|---|---|---|---|---|---|---|---|
| entity | 2026-07-11 |
|
Backend (../backend/)
The production successor to csms-toolkit (built 2026-07-11): one FastAPI/uvicorn process on port 9000 that is both the private CSMS for the anfu-charger and the controller for the wifi-relays — phases 1–2 of the architecture-plan.
backend/ (run: venv\Scripts\python -m backend.main, from the repo root)
├── main.py app assembly + entrypoint (uvicorn :9000), serves the panel
├── csms.py OCPP 1.6J ChargePoint handlers, decide() gate, remote start/stop
├── relays.py tinytuya local control of the SmartLife relays
├── routes.py REST under /api/* (login + per-scope permission checks)
├── users.py passwords (PBKDF2), sessions (HMAC), scope matcher, user CLI
├── store.py SQLite session store: who charged, when, how many kWh + meter samples
├── config.py config.json load/save
├── journal.py events.jsonl + seen_tags.json (raw ground truth)
├── static/ the web panel PWA (index.html, manifest, service worker, icon)
├── config.json users + allowlist + relay registry — GITIGNORED (password
│ hashes, session secret, Tuya local keys); see config.example.json
└── sessions.db SQLite (gitignored, auto-created)
Surfaces (all on :9000 — the touchscreen URL never changes)
ws://<host>:9000/<ChargerID>— OCPP WebSocket (catch-all path, the JC-6620 dials in)GET /— the web panel (mobile-first PWA, vanilla JS, self-contained; login → tabs Control / History / Admin, buttons filtered by the user's scopes; polls/api/statusevery 5 s)POST /api/login|logout,GET /api/me— username/password → 30-day session (cookie + bearer)GET /api/sessions?limit&cp&connector— charging history: who / when / kWh (visibility: own sessions + guns within scope; admin sees all);GET /api/sessions/{tx}/samples— per-minute energy/power/SoC for chartsGET /api/status— mode, connected chargers, gun states, active transactions, auth events, relays (filtered to what the caller may control)GET /api/events?limit&event=— tail the journal (admin)POST /api/charger/start|stop[/{tx}]|unlock/{n}— gun-scoped;trigger/{...}|setconfig|probe— adminGET/PUT/DELETE /api/allowlist[/{idTag}]+POST /api/mode?learn=— admin; learn mode flips at runtime, no restartGET/PUT/DELETE /api/users[/{name}]— user CRUD (admin), enables a PWA admin page laterGET /api/relays[?live=true],GET /api/relays/{name},POST /api/relays/{name}/on|off|toggle— relay-scoped/docs— interactive OpenAPI docs
Design facts
- Carried over from the proven discovery code: the
decide()gate (authorization) — now with optional per-gun card restriction ("connectors": [2]) — the_WSAdapterWebSocket bridge, learn mode, the JSONL event journal.DataTransferframes are journaled too (the VIN long-shot lead in authorization). - Multi-user auth (2026-07-11, replaced the interim
api_token): username/password users with permission scopes —charger:<cp>:<gun>,relay:<name>,admin— full model in authorization. Stdlib crypto only (PBKDF2 + HMAC sessions). Zero users = open API (startup warns); bootstrap viapython -m backend.users add <name> --scopes admin. - Remote starts run under the calling user's own idTag (
app-<name>, auto-enrolled) → per-person session attribution. - Consumption tracking (
store.py, 2026-07-11): everyStartTransactionopens a row insessions.dbwith a resolved "who" (allowlist name or username);StopTransactioncloses it and computes Wh from the meter diff;MeterValuesrows land in asamplestable (energy/power/SoC, kW→W normalized). Transaction ids continue fromMAX(tx)across restarts, and stop-by-tx falls back to the store so sessions running through a backend restart stay stoppable. - Relay calls are blocking tinytuya I/O, run via
asyncio.to_threadso the OCPP socket never stalls. - Venv at repo root (
../venv/, Python 3.13); deps pinned inbackend/requirements.txt(fastapi, uvicorn, websockets, ocpp, tinytuya) — the auth system added none. - ✅ Smoke-tested 2026-07-11 (37 checks): REST surface, allowlist CRUD incl. gun-restricted cards, login/sessions/scopes, user CRUD, the full consumption pipeline (start → meter sample → stop → kWh → visibility filtering), static panel serving, and a fake charge point over the real WS route. ⚠️ Not yet run against the live charger or a real phone browser; relays unconfigured until the wifi-relays is done.
Web panel notes
Vanilla JS on purpose (no framework, no CDN — the panel must work with NetBird-only reachability). Start button enables only when the gun reports Preparing/Available and the user holds that gun's scope. Gun cards show who is charging and since when; History tab lists who/when/kWh; Admin tab (admin scope only) manages allowlist (incl. per-gun cards + "seen but not enrolled" quick-enroll), users, and the learn/enforce toggle. Trivial service worker = installable ("add to home screen") without cache-staleness risk.
Deliberately deferred
SSE live push (the panel polls), per-session charts from the samples table (data is already collected), auto-interrogation on connect (manual POST /api/charger/probe).