Files
refusol-datalogging/backend/app/schemas.py
T
bonaminandClaude Opus 5.5 d4b584e364 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>
2026-10-21 07:42:48 +03:00

125 lines
3.4 KiB
Python

from datetime import datetime
from typing import Any, Literal
from pydantic import BaseModel, ConfigDict, Field
InverterId = Field(min_length=1, max_length=64, pattern=r"^[A-Za-z0-9_.\-]+$")
class ReadingIn(BaseModel):
"""One inverter's values from one poll cycle.
Every parameter is optional — send what the poller could read. Unknown
keys are accepted and stored in `extra` (as are keys of an explicit
`extra` object), so new parameters can be logged before the backend
knows about them.
"""
model_config = ConfigDict(extra="allow")
inverter_id: str = InverterId
timestamp: datetime | None = Field(
default=None, description="UTC. Defaults to the time the backend receives it."
)
ac_voltage_l1_v: float | None = None
ac_voltage_l2_v: float | None = None
ac_voltage_l3_v: float | None = None
dc_voltage_mppt1_v: float | None = None
dc_voltage_mppt2_v: float | None = None
dc_current_mppt1_a: float | None = None
dc_current_mppt2_a: float | None = None
ac_current_l1_a: float | None = None
ac_current_l2_a: float | None = None
ac_current_l3_a: float | None = None
ac_power_total_w: int | None = None
grid_frequency_hz: float | None = None
temperature_c: int | None = None
daily_energy_wh: int | None = None
total_energy_wh: int | None = None
status_code: int | None = Field(default=None, description="Raw inverter state, if known")
error_code: int | None = Field(default=None, description="Non-zero = inverter reports a fault")
status_text: str | None = Field(default=None, max_length=200)
extra: dict[str, Any] | None = None
class IngestResult(BaseModel):
received: int
stored: int
duplicates: int
class InverterUpdate(BaseModel):
name: str | None = Field(default=None, max_length=100)
capacity_kwp: float | None = Field(default=None, ge=0, le=1000)
mppt_count: int | None = Field(default=None, ge=1, le=4)
sort_order: int | None = None
notes: str | None = Field(default=None, max_length=2000)
class Inverter(BaseModel):
id: str
name: str
capacity_kwp: float | None
mppt_count: int
sort_order: int
notes: str | None
first_seen: datetime | None
last_seen: datetime | None
Severity = Literal["info", "warning", "critical"]
State = Literal["ok", "warning", "fault", "offline", "sleeping", "unknown"]
class Issue(BaseModel):
code: str
severity: Severity
message: str
class Health(BaseModel):
online: bool = Field(description="A reading arrived within ONLINE_WINDOW_SECONDS")
state: State
issues: list[Issue]
avg_power_w: float | None = Field(description="Mean AC power over the comparison window")
relative_output: float | None = Field(
description="Output vs fleet median (1.0 = median), per kWp when capacities are set"
)
class InverterStatus(BaseModel):
inverter: Inverter
health: Health
latest: dict[str, Any] | None
class Totals(BaseModel):
ac_power_w: int
daily_energy_wh: int
total_energy_wh: int
inverter_count: int
online_count: int
problem_count: int
class Datalogger(BaseModel):
online: bool
last_ingest_at: datetime | None
class Sun(BaseModel):
elevation: float | None
daylight: bool | None
class Summary(BaseModel):
generated_at: datetime
datalogger: Datalogger
sun: Sun
totals: Totals
fleet_median_w_per_kwp: float | None
inverters: list[InverterStatus]