The fake poller now matches the 2026-10-22 data contract: INV1/INV3 send no currents/DC/energy, INV2/INV4 add currents and MPPT1 voltage, INV5 reports via the cloud every 5 min with real daily/lifetime counters and keeps sending 0 W / Standby at night while the others go silent. Status codes are the normalized 8/1. Lets the dashboard be tried against the real mix of missing fields and cadences. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
115 lines
4.5 KiB
Python
115 lines
4.5 KiB
Python
"""Simulated inverters for trying the dashboard without the real site.
|
||
|
||
python fake_poller.py --url http://localhost:8090 --key change-me --backfill-days 30
|
||
|
||
Follows the poller data contract of 2026-10-22: five units, three sources.
|
||
INV1/INV3 (OEM board) send AC voltages, power, frequency, temperature and
|
||
status; INV2/INV4 (SunSpec) add phase currents and MPPT1 voltage; INV5
|
||
(SolarmanPV cloud) adds the real daily/lifetime energy counters, reports
|
||
only every 5 min and keeps reporting 0 W / Standby at night while the
|
||
others go silent. --backfill-days first loads that many days of history
|
||
(one reading per 5 minutes), then it posts live readings every --interval
|
||
seconds. INV4 runs at half output (as if a string fuse had blown) so the
|
||
warnings show up.
|
||
"""
|
||
|
||
import argparse
|
||
import math
|
||
import random
|
||
import time
|
||
from datetime import datetime, timedelta, timezone
|
||
|
||
from backend_client import BackendClient
|
||
|
||
INVERTERS = ["INV1", "INV2", "INV3", "INV4", "INV5"]
|
||
PEAK_W = 18_000
|
||
CLOUD_EVERY_S = 300 # INV5's cadence
|
||
INV5_LIFETIME_START_WH = 41_250_000
|
||
|
||
# INV5's daily counter, accumulated by the simulator
|
||
_inv5 = {"day": None, "daily_wh": 0.0, "total_wh": float(INV5_LIFETIME_START_WH), "last": None}
|
||
|
||
|
||
def sun_factor(t: datetime) -> float:
|
||
# Rough local solar day 06:30–19:30 UTC+3, sine shaped
|
||
hour = (t.hour + 3 + t.minute / 60) % 24
|
||
x = (hour - 6.5) / 13
|
||
return max(0.0, math.sin(math.pi * x)) if 0 < x < 1 else 0.0
|
||
|
||
|
||
def reading(inv: str, t: datetime) -> dict | None:
|
||
f = sun_factor(t) * random.uniform(0.93, 1.0)
|
||
if inv == "INV5":
|
||
if int(t.timestamp()) % CLOUD_EVERY_S >= 60:
|
||
return None # cloud value only refreshes every 5 min
|
||
elif f <= 0.01:
|
||
return None # local units power down at night and don't answer
|
||
feeding = f > 0.03
|
||
w = int(PEAK_W * f * (0.5 if inv == "INV4" else 1.0)) if feeding else 0
|
||
volts = [round(random.uniform(236, 244), 1) for _ in range(3)]
|
||
r = {
|
||
"inverter_id": inv,
|
||
"timestamp": t.isoformat(timespec="seconds"),
|
||
"ac_voltage_l1_v": volts[0],
|
||
"ac_voltage_l2_v": volts[1],
|
||
"ac_voltage_l3_v": volts[2],
|
||
"ac_power_total_w": w,
|
||
"grid_frequency_hz": round(random.uniform(49.95, 50.05), 2),
|
||
"temperature_c": int(30 + 25 * f),
|
||
"status_code": 8 if feeding else 1,
|
||
"status_text": ("MPP" if feeding else "Standby") if inv != "INV5" else ("Generating" if feeding else "Standby"),
|
||
}
|
||
if inv in ("INV1", "INV3"):
|
||
r["raw_status_oem"] = 7 if feeding else 3
|
||
return r
|
||
r.update({f"ac_current_l{i + 1}_a": round(w / 3 / v, 2) for i, v in enumerate(volts)})
|
||
r["dc_voltage_mppt1_v"] = round(random.uniform(560, 640), 1) if feeding else 0.0
|
||
if inv in ("INV2", "INV4"):
|
||
r["raw_status_sunspec"] = 4 if feeding else 2
|
||
return r
|
||
day = (t + timedelta(hours=3)).date()
|
||
if _inv5["day"] != day:
|
||
_inv5.update(day=day, daily_wh=0.0)
|
||
if _inv5["last"] is not None:
|
||
wh = w * (t - _inv5["last"]).total_seconds() / 3600
|
||
_inv5["daily_wh"] += wh
|
||
_inv5["total_wh"] += wh
|
||
_inv5["last"] = t
|
||
r.update(daily_energy_wh=int(_inv5["daily_wh"]), total_energy_wh=int(_inv5["total_wh"]), raw_status_cloud=1 if feeding else 0)
|
||
return r
|
||
|
||
|
||
def main() -> None:
|
||
ap = argparse.ArgumentParser()
|
||
ap.add_argument("--url", default="http://localhost:8090")
|
||
ap.add_argument("--key", default="change-me")
|
||
ap.add_argument("--backfill-days", type=int, default=0)
|
||
ap.add_argument("--interval", type=int, default=60)
|
||
args = ap.parse_args()
|
||
client = BackendClient(args.url, args.key, spool_path="fake_unsent.jsonl")
|
||
|
||
if args.backfill_days:
|
||
now = int(time.time())
|
||
# On the 5-min grid, so INV5's cloud readings land in the backfill too
|
||
t = datetime.fromtimestamp(now - now % CLOUD_EVERY_S - args.backfill_days * 86400, timezone.utc)
|
||
batch: list[dict] = []
|
||
while t < datetime.now(timezone.utc):
|
||
batch += [r for inv in INVERTERS if (r := reading(inv, t))]
|
||
if len(batch) >= 1000:
|
||
client.send(batch)
|
||
batch = []
|
||
t += timedelta(minutes=5)
|
||
client.send(batch)
|
||
print(f"Backfilled {args.backfill_days} days")
|
||
|
||
while True:
|
||
now = datetime.now(timezone.utc)
|
||
batch = [r for inv in INVERTERS if (r := reading(inv, now))]
|
||
ok = client.send(batch)
|
||
print(f"{now:%H:%M:%S} sent {len(batch)} readings, ok={ok}")
|
||
time.sleep(args.interval)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|