Files
refusol-datalogging/backend/app/schemas.py
T
bonaminandClaude Opus 5.5 d01cef991f feat: match the verified on-site poller payload
The on-site agent found that the DC (P052/P054/P05A/P05C) and energy (P063)
registers are static/frozen and ac_power_total_w is now computed V*I, so
the backend and dashboard have to cope with those fields being absent.

- Feeding/Standby from status_code (FEEDING_STATUS_CODES, default 7 and 8:
  INV1's OEM board uses 7). Online but not feeding = Standby; a standby
  inverter while peers produce is still caught by the underperformance
  check. Missing error_code stays "no fault".
- Daily energy falls back to AC power integrated between readings, flagged
  estimated (≈ in the UI); out-of-order (spooled) readings recompute the
  day. Existing databases get the column and a rebuild on startup.
- Totals are null instead of 0 when there's no energy data; lifetime shows
  "not available yet". Parameters the poller doesn't send are hidden on the
  detail page, DC lines only drawn when DC data exists.
- Timestamps more than 5 min in the future are stored at receive time and
  counted in clamped_future — the example payload carried local Athens time
  labelled +00:00.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 09:43:55 +03:00

140 lines
4.1 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
clamped_future: int = Field(
description="Readings stamped in the future (clock wrong, or local time sent as UTC); "
"stored at the time they were received instead"
)
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", "standby", "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")
feeding: bool | None = Field(
description="status_code is one of FEEDING_STATUS_CODES; null when no status_code is reported"
)
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 EnergyToday(BaseModel):
wh: int | None
estimated: bool = Field(description="Integrated from AC power, not the inverter's own counter")
class InverterStatus(BaseModel):
inverter: Inverter
health: Health
energy_today: EnergyToday
latest: dict[str, Any] | None
class Totals(BaseModel):
ac_power_w: int
daily_energy_wh: int | None
daily_energy_estimated: bool
total_energy_wh: int | None = Field(description="Null until the inverters report a lifetime counter")
inverter_count: int
online_count: int
feeding_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]