feat: solar inverter monitor — FastAPI + SQLite backend, React PWA dashboard

Monitoring for the Refusol 020K inverters. The on-site poller POSTs each
60 s cycle to /api/readings; the dashboard answers first whether every
inverter is operating and whether one is producing far less than its peers
(string fuse / breaker problems).

- Backend: FastAPI + SQLite. Parameters defined once in fields.py (columns
  auto-added on start); unknown poller keys are kept in `extra` since the
  parameter map is still being researched. Idempotent ingest, daily energy
  rollup in site-local days, history downsampling, 2-year retention.
- Health: Offline vs Sleeping (sun elevation) vs No data (datalogger
  silent, detected via empty-batch heartbeats), Fault from error_code,
  underperformance vs fleet median (per kWp when sizes set), MPPT imbalance.
- Frontend: mobile-first React + Tailwind, installable PWA, comparison bars,
  24 h power / 30 day energy charts, detail page with all parameters.
- Docker: backend + frontend + nginx on one port for NPM, as in
  bellsystems-cp; deploy-host.sh pulls main and rebuilds.
- poller/backend_client.py: stdlib client with offline spool for
  refusol_poller.py; fake_poller.py simulates the site.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-21 07:42:48 +03:00
co-authored by Claude Opus 5.5
commit d4b584e364
59 changed files with 10849 additions and 0 deletions
+94
View File
@@ -0,0 +1,94 @@
import os
import sqlite3
from collections.abc import Iterator
from contextlib import contextmanager
from .config import get_settings
from .fields import FIELDS
SCHEMA = """
CREATE TABLE IF NOT EXISTS inverters (
id TEXT PRIMARY KEY,
name TEXT,
capacity_kwp REAL,
mppt_count INTEGER NOT NULL DEFAULT 2,
sort_order INTEGER NOT NULL DEFAULT 0,
notes TEXT,
first_seen INTEGER,
last_seen INTEGER
);
-- One row per inverter per poll cycle. ts = unix seconds, UTC.
-- The per-parameter columns are added from fields.py by migrate().
CREATE TABLE IF NOT EXISTS readings (
inverter_id TEXT NOT NULL,
ts INTEGER NOT NULL,
extra TEXT,
PRIMARY KEY (inverter_id, ts)
) WITHOUT ROWID;
CREATE INDEX IF NOT EXISTS readings_ts ON readings (ts);
-- Daily rollup, kept forever (readings are purged after the retention
-- period). day is the local date at the site (SITE_TIMEZONE).
CREATE TABLE IF NOT EXISTS daily_energy (
inverter_id TEXT NOT NULL,
day TEXT NOT NULL,
daily_energy_wh INTEGER,
total_min_wh INTEGER,
total_max_wh INTEGER,
peak_power_w INTEGER,
PRIMARY KEY (inverter_id, day)
) WITHOUT ROWID;
CREATE TABLE IF NOT EXISTS meta (
key TEXT PRIMARY KEY,
value TEXT
);
"""
def connect() -> sqlite3.Connection:
path = get_settings().db_path
os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
conn = sqlite3.connect(path, timeout=30)
conn.row_factory = sqlite3.Row
conn.execute("PRAGMA journal_mode=WAL")
conn.execute("PRAGMA synchronous=NORMAL")
return conn
@contextmanager
def session() -> Iterator[sqlite3.Connection]:
conn = connect()
try:
with conn:
yield conn
finally:
conn.close()
def get_db() -> Iterator[sqlite3.Connection]:
with session() as conn:
yield conn
def migrate() -> None:
with session() as conn:
conn.executescript(SCHEMA)
existing = {r["name"] for r in conn.execute("PRAGMA table_info(readings)")}
for f in FIELDS:
if f.key not in existing:
conn.execute(f"ALTER TABLE readings ADD COLUMN {f.key} {f.sql_type}")
def get_meta(conn: sqlite3.Connection, key: str) -> str | None:
row = conn.execute("SELECT value FROM meta WHERE key = ?", (key,)).fetchone()
return row["value"] if row else None
def set_meta(conn: sqlite3.Connection, key: str, value: str) -> None:
conn.execute(
"INSERT INTO meta (key, value) VALUES (?, ?) "
"ON CONFLICT(key) DO UPDATE SET value = excluded.value",
(key, value),
)