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>
57 lines
1.3 KiB
Python
57 lines
1.3 KiB
Python
import asyncio
|
|
import logging
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from . import store
|
|
from .api import router
|
|
from .config import get_settings
|
|
from .db import migrate, session
|
|
|
|
log = logging.getLogger("solar")
|
|
PURGE_INTERVAL_SECONDS = 6 * 3600
|
|
|
|
|
|
def purge_once() -> None:
|
|
with session() as conn:
|
|
deleted = store.purge(conn, get_settings().retention_days)
|
|
if deleted:
|
|
log.info("Retention: purged %d readings", deleted)
|
|
|
|
|
|
async def purge_loop() -> None:
|
|
while True:
|
|
try:
|
|
await asyncio.to_thread(purge_once)
|
|
except Exception:
|
|
log.exception("Retention purge failed")
|
|
await asyncio.sleep(PURGE_INTERVAL_SECONDS)
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
if migrate():
|
|
with session() as conn:
|
|
store.rebuild_integrated_energy(conn)
|
|
task = asyncio.create_task(purge_loop())
|
|
yield
|
|
task.cancel()
|
|
|
|
|
|
app = FastAPI(
|
|
title="Refusol Solar Monitor",
|
|
lifespan=lifespan,
|
|
docs_url="/api/docs",
|
|
redoc_url=None,
|
|
openapi_url="/api/openapi.json",
|
|
)
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=get_settings().cors_origins,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
app.include_router(router)
|