Files
charging_station/backend/config.py
T
julian 70c2fe1210 feat(backend): link cards to users; charger-qualified gun restrictions; config-driven gun labels
- Allowlist entries take "user": <username> — sessions started with that
  card attribute to the person, and the owner sees their card's history
  even without gun-wide permissions (ownership looked up live)
- "guns": ["eu092025:2"] (fnmatch) replaces the charger-blind
  "connectors": [2] restriction — safe with multiple chargers
- New "chargers" config section provides per-charger/per-gun display
  names; the panel no longer hardcodes the Anfu gun labels
- Panel admin tab: owner dropdown + guns field on card entries
- Smoke test: 42 checks, incl. retroactive history visibility after
  linking a card and rejection of links to unknown users
2026-07-11 20:03:36 +02:00

50 lines
1.6 KiB
Python

"""Backend configuration — one JSON file, config.json, next to this module.
Holds the charger allowlist (learn_mode + authorized idTags, same shape the
discovery phase used), the user accounts with their permission scopes, and the
Tuya relay registry. config.json is gitignored because it contains secrets
(password hashes, the session-signing secret, Tuya local keys);
config.example.json documents the shape.
"""
import json
import secrets
from pathlib import Path
HERE = Path(__file__).resolve().parent
CONFIG_PATH = HERE / "config.json"
DEFAULTS = {
"learn_mode": True,
"users": {}, # name -> {"password": pbkdf2 hash, "scopes": [...], "id_tag": ...}
"authorized": {}, # idTag -> {"name", "kind": "card|app|car",
# optional "user": owner, "guns": ["eu092025:2"]}
"relays": {}, # name -> {"id", "key", "ip", "version", "switch", "label"}
"chargers": {}, # cp_id -> {"label", "guns": {"1": "display name", ...}} (UI only)
}
def load() -> dict:
if CONFIG_PATH.exists():
cfg = json.loads(CONFIG_PATH.read_text(encoding="utf-8"))
else:
cfg = {}
for key, default in DEFAULTS.items():
cfg.setdefault(key, default if not isinstance(default, dict) else dict(default))
return cfg
CONFIG = load()
if not CONFIG.get("session_secret"):
CONFIG["session_secret"] = secrets.token_hex(32)
CONFIG_PATH.write_text(
json.dumps(CONFIG, indent=2, ensure_ascii=False) + "\n", encoding="utf-8"
)
def save() -> None:
CONFIG_PATH.write_text(
json.dumps(CONFIG, indent=2, ensure_ascii=False) + "\n", encoding="utf-8"
)