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:
+30
-4
@@ -1,27 +1,37 @@
|
||||
"""Charging-session store — who charged, when, and how many kWh.
|
||||
"""The one persistent store: SQLite (stdlib) in backend.db next to this module.
|
||||
|
||||
SQLite (stdlib) in sessions.db next to this module. Two tables:
|
||||
sessions — one row per transaction: cp, connector, idTag, resolved "who",
|
||||
start/end timestamps (charger-reported), meter readings, Wh, reason
|
||||
samples — per-minute MeterValues while charging (energy/power/SoC) for
|
||||
later charts
|
||||
settings — the app state sections (users, allowlist, relays, chargers,
|
||||
learn_mode, session_secret) as JSON blobs, one row per section.
|
||||
Always whole-section reads/writes, so blobs beat normalized
|
||||
tables here. Managed through config.py — see there.
|
||||
|
||||
The events.jsonl journal (journal.py) stays the raw ground truth; this is the
|
||||
queryable view. Transaction ids come from MAX(tx)+1 here, so they survive
|
||||
backend restarts (the in-memory counter used to reset to 0).
|
||||
backend restarts.
|
||||
|
||||
sqlite3 connections are cheap at household scale — one per call, no pooling.
|
||||
BACKEND_DB env var overrides the path (used by tests to stay off the real db).
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import sqlite3
|
||||
from contextlib import closing
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
HERE = Path(__file__).resolve().parent
|
||||
DB_PATH = HERE / "sessions.db"
|
||||
DB_PATH = Path(os.environ.get("BACKEND_DB", HERE / "backend.db"))
|
||||
|
||||
_SCHEMA = """
|
||||
CREATE TABLE IF NOT EXISTS settings (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
tx INTEGER PRIMARY KEY,
|
||||
cp TEXT NOT NULL,
|
||||
@@ -56,6 +66,22 @@ def _db():
|
||||
return closing(con)
|
||||
|
||||
|
||||
def load_settings() -> dict:
|
||||
with _db() as con, con:
|
||||
return {
|
||||
row["key"]: json.loads(row["value"])
|
||||
for row in con.execute("SELECT key, value FROM settings")
|
||||
}
|
||||
|
||||
|
||||
def save_settings(sections: dict) -> None:
|
||||
with _db() as con, con:
|
||||
con.executemany(
|
||||
"INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)",
|
||||
[(key, json.dumps(value, ensure_ascii=False)) for key, value in sections.items()],
|
||||
)
|
||||
|
||||
|
||||
def max_tx() -> int:
|
||||
with _db() as con, con:
|
||||
row = con.execute("SELECT MAX(tx) AS m FROM sessions").fetchone()
|
||||
|
||||
Reference in New Issue
Block a user