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
+82
View File
@@ -0,0 +1,82 @@
"""Single source of truth for the per-reading parameters.
Every field listed here gets its own column in `readings` (missing columns are
added on startup), is accepted by POST /api/readings, and is described by
GET /api/meta/fields so the dashboard and future apps can render it without a
code change. Anything the poller sends that is NOT listed here is still kept,
in the reading's `extra` JSON — promote it to this list once its meaning and
scaling are confirmed.
"""
from dataclasses import asdict, dataclass
@dataclass(frozen=True)
class Field:
key: str
sql_type: str # REAL | INTEGER | TEXT
label: str
unit: str | None
group: str # ac | dc | energy | device
# How values combine when history is downsampled into buckets.
agg: str = "avg" # avg | max | last
decimals: int = 1
FIELDS: list[Field] = [
Field("ac_power_total_w", "INTEGER", "AC power", "W", "ac", decimals=0),
Field("ac_voltage_l1_v", "REAL", "AC voltage L1", "V", "ac"),
Field("ac_voltage_l2_v", "REAL", "AC voltage L2", "V", "ac"),
Field("ac_voltage_l3_v", "REAL", "AC voltage L3", "V", "ac"),
Field("ac_current_l1_a", "REAL", "AC current L1", "A", "ac", decimals=2),
Field("ac_current_l2_a", "REAL", "AC current L2", "A", "ac", decimals=2),
Field("ac_current_l3_a", "REAL", "AC current L3", "A", "ac", decimals=2),
Field("grid_frequency_hz", "REAL", "Grid frequency", "Hz", "ac", decimals=2),
Field("dc_voltage_mppt1_v", "REAL", "DC voltage MPPT1", "V", "dc"),
Field("dc_voltage_mppt2_v", "REAL", "DC voltage MPPT2", "V", "dc"),
Field("dc_current_mppt1_a", "REAL", "DC current MPPT1", "A", "dc"),
Field("dc_current_mppt2_a", "REAL", "DC current MPPT2", "A", "dc"),
Field("temperature_c", "INTEGER", "Temperature", "°C", "device", decimals=0),
Field("status_code", "INTEGER", "Status code", None, "device", agg="last", decimals=0),
Field("error_code", "INTEGER", "Error code", None, "device", agg="last", decimals=0),
Field("status_text", "TEXT", "Status", None, "device", agg="last"),
Field("daily_energy_wh", "INTEGER", "Energy today", "Wh", "energy", agg="max", decimals=0),
Field("total_energy_wh", "INTEGER", "Lifetime energy", "Wh", "energy", agg="max", decimals=0),
]
FIELD_KEYS = [f.key for f in FIELDS]
FIELDS_BY_KEY = {f.key: f for f in FIELDS}
NUMERIC_KEYS = [f.key for f in FIELDS if f.sql_type != "TEXT"]
# Values derived from the stored ones, shown alongside them.
DERIVED: list[Field] = [
Field("dc_power_mppt1_w", "REAL", "DC power MPPT1", "W", "dc", decimals=0),
Field("dc_power_mppt2_w", "REAL", "DC power MPPT2", "W", "dc", decimals=0),
Field("dc_power_total_w", "REAL", "DC power total", "W", "dc", decimals=0),
Field("efficiency_pct", "REAL", "Conversion efficiency", "%", "dc", decimals=1),
]
def describe() -> list[dict]:
return [asdict(f) | {"derived": False} for f in FIELDS] + [
asdict(f) | {"derived": True} for f in DERIVED
]
def derive(r: dict) -> dict:
"""Add DC power and efficiency to a reading dict (in place) and return it."""
def mul(v, a):
return None if v is None or a is None else v * a
p1 = mul(r.get("dc_voltage_mppt1_v"), r.get("dc_current_mppt1_a"))
p2 = mul(r.get("dc_voltage_mppt2_v"), r.get("dc_current_mppt2_a"))
r["dc_power_mppt1_w"] = round(p1) if p1 is not None else None
r["dc_power_mppt2_w"] = round(p2) if p2 is not None else None
dc = None if p1 is None and p2 is None else (p1 or 0) + (p2 or 0)
r["dc_power_total_w"] = round(dc) if dc is not None else None
ac = r.get("ac_power_total_w")
r["efficiency_pct"] = (
round(ac / dc * 100, 1) if ac is not None and dc and dc > 200 else None
)
return r