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>
This commit is contained in:
@@ -26,7 +26,9 @@ def test_rejects_missing_key(client):
|
||||
def test_ingest_is_idempotent_and_keeps_unknown_fields(client):
|
||||
now = int(time.time())
|
||||
batch = [reading("inv1", now, 10000, p_new_param=42)]
|
||||
assert client.post("/api/readings", json=batch).json() == {"received": 1, "stored": 1, "duplicates": 0}
|
||||
assert client.post("/api/readings", json=batch).json() == {
|
||||
"received": 1, "stored": 1, "duplicates": 0, "clamped_future": 0
|
||||
}
|
||||
assert client.post("/api/readings", json=batch).json()["duplicates"] == 1
|
||||
|
||||
latest = client.get("/api/inverters/inv1/latest").json()
|
||||
@@ -100,3 +102,90 @@ def test_empty_batch_marks_datalogger_alive(client):
|
||||
def test_fields_meta(client):
|
||||
keys = [f["key"] for f in client.get("/api/meta/fields").json()]
|
||||
assert "ac_power_total_w" in keys and "efficiency_pct" in keys
|
||||
|
||||
|
||||
def onsite_payload(inv, ts, power=6882, status=8):
|
||||
"""Exactly what the real on-site poller sends (2026-10-21)."""
|
||||
return {
|
||||
"timestamp": datetime.fromtimestamp(ts, timezone.utc).isoformat(),
|
||||
"inverter_id": inv,
|
||||
"ac_voltage_l1_v": 232.6, "ac_voltage_l2_v": 233.35, "ac_voltage_l3_v": 232.62,
|
||||
"ac_current_l1_a": 11.75, "ac_current_l2_a": 9.16, "ac_current_l3_a": 8.95,
|
||||
"ac_power_total_w": power, "grid_frequency_hz": 50.0, "temperature_c": 36,
|
||||
"status_code": status, "status_text": "feeding" if status in (7, 8) else "standby/not feeding",
|
||||
"p06d_raw_latched": 21450, "p052_static_v": 312.5, "p05a_static_a": 45.0,
|
||||
"p063_ind0_raw": 1872,
|
||||
}
|
||||
|
||||
|
||||
def test_onsite_payload_accepted_and_ok(client):
|
||||
now = int(time.time())
|
||||
batch = [onsite_payload("INV1", now, 6900, status=7), onsite_payload("INV2", now), onsite_payload("INV4", now, 6800)]
|
||||
r = client.post("/api/readings", json=batch)
|
||||
assert r.status_code == 200
|
||||
assert r.json() == {"received": 3, "stored": 3, "duplicates": 0, "clamped_future": 0}
|
||||
|
||||
s = client.get("/api/summary").json()
|
||||
for st in s["inverters"]:
|
||||
assert st["health"]["state"] == "ok", st
|
||||
assert st["health"]["feeding"] is True
|
||||
latest = s["inverters"][1]["latest"]
|
||||
assert latest["extra"]["p06d_raw_latched"] == 21450
|
||||
assert latest["dc_power_total_w"] is None and latest["efficiency_pct"] is None
|
||||
# No energy counters sent: lifetime unknown, today not yet integrated
|
||||
assert s["totals"]["total_energy_wh"] is None
|
||||
assert s["totals"]["feeding_count"] == 3
|
||||
|
||||
|
||||
def test_not_feeding_is_standby_and_flagged_when_peers_produce(client):
|
||||
now = int(time.time())
|
||||
client.post("/api/readings", json=[
|
||||
onsite_payload("INV1", now, 6900), onsite_payload("INV2", now, 7000),
|
||||
onsite_payload("INV4", now, 0, status=3),
|
||||
])
|
||||
by_id = {x["inverter"]["id"]: x["health"] for x in client.get("/api/summary").json()["inverters"]}
|
||||
assert by_id["INV4"]["feeding"] is False
|
||||
assert by_id["INV4"]["state"] == "warning" # 0 W while the others make ~7 kW
|
||||
|
||||
|
||||
def test_all_idle_is_plain_standby(client):
|
||||
# Everyone awake but not feeding (e.g. dusk): standby, no alarm
|
||||
now = int(time.time())
|
||||
client.post("/api/readings", json=[onsite_payload(i, now, 0, status=3) for i in ("INV1", "INV2", "INV4")])
|
||||
states = {x["health"]["state"] for x in client.get("/api/summary").json()["inverters"]}
|
||||
assert states == {"standby"}
|
||||
|
||||
|
||||
def test_heartbeat(client):
|
||||
r = client.post("/api/readings", json=[])
|
||||
assert r.status_code == 200 and r.json()["received"] == 0
|
||||
|
||||
|
||||
def test_future_timestamp_is_clamped(client):
|
||||
now = int(time.time())
|
||||
r = client.post("/api/readings", json=[onsite_payload("INV2", now + 3 * 3600)]).json()
|
||||
assert r["clamped_future"] == 1
|
||||
latest = client.get("/api/inverters/INV2/latest").json()
|
||||
assert abs(latest["age_seconds"]) < 5
|
||||
|
||||
|
||||
def test_energy_integrated_from_power_when_no_counters(client):
|
||||
now = int(time.time())
|
||||
# 6 kW for 30 minutes at 60 s steps -> 3 kWh
|
||||
client.post("/api/readings", json=[onsite_payload("INV1", now - 1800 + i * 60, 6000) for i in range(31)])
|
||||
s = client.get("/api/summary").json()
|
||||
e = s["inverters"][0]["energy_today"]
|
||||
if e["wh"] is not None: # skip when the window straddles local midnight
|
||||
assert e["estimated"] is True
|
||||
assert 2900 <= e["wh"] <= 3000
|
||||
assert s["totals"]["daily_energy_estimated"] is True
|
||||
|
||||
|
||||
def test_energy_survives_out_of_order_readings(client):
|
||||
now = int(time.time())
|
||||
# Latest reading arrives first, then the earlier ones (spool re-send)
|
||||
client.post("/api/readings", json=[onsite_payload("INV1", now, 6000)])
|
||||
client.post("/api/readings", json=[onsite_payload("INV1", now - 1800 + i * 60, 6000) for i in range(30)])
|
||||
e = client.get("/api/summary").json()["inverters"][0]["energy_today"]
|
||||
if e["wh"] is not None:
|
||||
assert 2900 <= e["wh"] <= 3000
|
||||
|
||||
Reference in New Issue
Block a user