--- type: note updated: 2026-07-11 tags: [architecture, decision, plan] --- # Architecture & Build Plan Filed 2026-07-09 from a stack discussion (Flutter? Fastify vs FastAPI? web frontend?). Extends the requirements in [[2026-07-09-findings-and-requirements]] and the roadmap in [[overview]]. > [!note] Scope expansion (2026-07-11): the backend also controls the household [[wifi-relays|Tuya/SmartLife WiFi relays]] — locally via tinytuya, same process, under `/api/relays/*`. This strengthens the FastAPI/one-process decision: charger + relays + panel behind one NetBird-reachable port. ## Stack decision **Backend: FastAPI (Python) — decided.** The deciding constraint: the backend *is* the CSMS. `RemoteStartTransaction`/`RemoteStopTransaction` can only be sent down the live OCPP WebSocket the [[jc-6620|JC-6620]] holds open, so the API server and the OCPP server must share process state. The proven code ([[csms-toolkit]]) is already Python on the `ocpp` library; Fastify would mean rewriting OCPP in Node (immature libraries) or a two-process IPC design. FastAPI hosts everything in one uvicorn process on one port: ``` FastAPI app (uvicorn, :9000, on the always-on host) ├── / OCPP 1.6J WebSocket (catch-all) ← JC-6620 dials in here ├── /api/* REST: status, charger start/stop, allowlist CRUD, sessions ├── /api/relays/* Tuya relay control (tinytuya, local protocol) ├── /api/events journal tail (SSE live push later, for the panel) └── / static PWA (the web panel) ``` (The `ocpp` ChargePoint class only needs send/recv; a FastAPI WebSocket route wraps it. Keep port 9000 so the charger touchscreen URL doesn't change — [[troubleshooting]] #4: changing it needs a power-cycle.) **Web frontend: mobile-first PWA, no build pipeline — decided.** Plain HTML/JS (Alpine.js or htmx + SSE), served by FastAPI. Features: status card, start/stop, allowlist management, session history. PWA manifest → "add to home screen". Reached only over [[netbird|NetBird]]. **Flutter mobile app: phase 2, optional.** The PWA covers ~95% of the value. Flutter earns its keep only for push notifications ("charging complete") or as a learning project. It would consume the same REST API (FastAPI's OpenAPI spec → typed Dart client codegen), so nothing built now is wasted. Phone needs the NetBird client either way. **Storage: SQLite** (single file) for sessions/meter values/seen tags; the allowlist can migrate from `station_config.json` into it once the panel manages it. JSON files were fine for discovery; history queries want a real table. → ✅ implemented 2026-07-11 as `backend/store.py` (`sessions` + `samples` tables); allowlist/users stayed in config.json — right size for a household, revisit only if it grows. ## Build phases 1. **Productionize the CSMS** — ✅ done 2026-07-11 as the [[backend]] package (`../backend/`); SQLite session store added same day (JSONL journal stays as raw ground truth). 2. **REST API** — ✅ done 2026-07-11: status, remote start/stop, allowlist CRUD, runtime learn-mode flip, **relay on/off/toggle**, multi-user login with permission scopes, **session history (who/when/kWh)**. ⚠️ Relay half awaits the [[wifi-relays|local-key extraction]]. 3. **Web panel (PWA)** — ✅ built 2026-07-11 (`backend/static/`, vanilla JS instead of Alpine — zero dependencies): login, per-gun status + start/stop gated by scopes and gun readiness, relay toggles, consumption history, admin tab. ⚠️ Untested on a real phone. 4. **Deploy** — pick the always-on host (❓ open, [[overview]]), run as a service (systemd or NSSM on Windows), static IP / DHCP reservation, firewall rule for :9000, joined to NetBird. 5. **Hardware tasks** — RFID enrollment via learn mode; the Autocharge plug-in test ([[authorization]]) to settle "by vehicle". 6. **(Optional) Flutter app** — consumes the same API; adds push notifications. Phases 1–3 are pure software and can proceed before the host decision; only phase 4 needs it.