refactor(backend): single SQLite store — config.json eliminated
backend.db (renamed from sessions.db) now holds everything: the sessions
and samples tables plus a settings table with the app-state sections
(users, allowlist, relays, chargers, learn_mode, session_secret) as JSON
blobs. All mutations go through the API/panel and take effect immediately;
there is no config file and no hand-edit-then-restart ambiguity.
- legacy config.json auto-imports into the db on first start (renamed to
config.json.migrated as a backup)
- new admin endpoints: PUT/DELETE /api/relays/{name} (how tinytuya wizard
results are registered now) and PUT /api/chargers/{cp_id} (panel labels)
- BACKEND_DB env override so tests run on an isolated temp database
- smoke test: 45 checks, real db verified untouched
This commit is contained in:
@@ -16,12 +16,11 @@ backend/ (run: venv\Scripts\python -m backend.main, from the repo root)
|
||||
├── 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
|
||||
├── config.py app-state sections (users/allowlist/relays/chargers), db-backed
|
||||
├── 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)
|
||||
└── backend.db THE one store (SQLite, gitignored): sessions + samples +
|
||||
settings (users, allowlist, relays, chargers, session secret)
|
||||
```
|
||||
|
||||
## Surfaces (all on :9000 — the touchscreen URL never changes)
|
||||
@@ -44,10 +43,11 @@ backend/ (run: venv\Scripts\python -m backend.main, from the repo root)
|
||||
- **Multi-charger by design**: a second charger only needs the same URL with a unique ChargerID; scopes, sessions, card restrictions, and the panel are all keyed by charger id. Gun display names come from the `"chargers"` config section (not hardcoded).
|
||||
- **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 via `python -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): every `StartTransaction` opens a row in `sessions.db` with a resolved "who" (allowlist name or username); `StopTransaction` closes it and computes Wh from the meter diff; `MeterValues` rows land in a `samples` table (energy/power/SoC, kW→W normalized). Transaction ids continue from `MAX(tx)` across restarts, and stop-by-tx falls back to the store so sessions running through a backend restart stay stoppable.
|
||||
- **Consumption tracking** (`store.py`, 2026-07-11): every `StartTransaction` opens a row in `backend.db` with a resolved "who" (allowlist name or username); `StopTransaction` closes it and computes Wh from the meter diff; `MeterValues` rows land in a `samples` table (energy/power/SoC, kW→W normalized). Transaction ids continue from `MAX(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_thread` so the OCPP socket never stalls.
|
||||
- Venv at repo root (`../venv/`, Python 3.13); deps pinned in `backend/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|key extraction]] is done.
|
||||
- ✅ Smoke-tested 2026-07-11 (45 checks, isolated temp db via the `BACKEND_DB` env override): REST surface, allowlist CRUD incl. gun-restricted cards, login/sessions/scopes, user/relay/charger 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.
|
||||
- ✅ **Live charger connected** (2026-07-11 evening): `eu092025` dials the backend (events.jsonl). ⚠️ Panel not yet exercised from a real phone; relays unconfigured until the [[wifi-relays|key extraction]] is done.
|
||||
|
||||
## Web panel notes
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ Local control needs each device's **id + local key**, extracted once via Tuya's
|
||||
1. Create a (free) account at `iot.tuya.com` → Cloud → create a project (data center matching the SmartLife account region).
|
||||
2. Link the SmartLife app account to the project (Devices → Link App Account → scan QR in SmartLife).
|
||||
3. From the repo root: `venv\Scripts\python -m tinytuya wizard` — enter the project's API id/secret; it dumps every paired device's `id`, `key`, `ip`, and protocol `version`.
|
||||
4. Copy each relay into `backend/config.json` → `"relays"` (shape in `config.example.json`). The Tuya cloud account is never needed again unless a device is re-paired (**re-pairing regenerates the local key**).
|
||||
4. Register each relay via `PUT /api/relays/<name>` (admin; interactive form at `/docs`) with its `id`, `key`, `ip`, `version`. The Tuya cloud account is never needed again unless a device is re-paired (**re-pairing regenerates the local key**).
|
||||
|
||||
Recommended: DHCP reservations for the relays and pinned `"ip"` entries in the config — `"ip": "auto"` works via UDP broadcast discovery but is slower and only finds relays on the same L2 segment as the backend host (❓ verify which subnet the relays are on vs. the future always-on host, see [[network-topology]]).
|
||||
|
||||
|
||||
Reference in New Issue
Block a user