Files
refusol-datalogging/backend/app/sun.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

36 lines
1.2 KiB
Python

"""Sun elevation from date/time and position (NOAA approximation, ±0.5°)."""
import math
from datetime import datetime, timezone
def elevation(when: datetime, lat: float, lon: float) -> float:
when = when.astimezone(timezone.utc)
day_of_year = when.timetuple().tm_yday
hours = when.hour + when.minute / 60 + when.second / 3600
g = 2 * math.pi / 365 * (day_of_year - 1 + (hours - 12) / 24)
eq_time = 229.18 * (
0.000075
+ 0.001868 * math.cos(g)
- 0.032077 * math.sin(g)
- 0.014615 * math.cos(2 * g)
- 0.040849 * math.sin(2 * g)
)
decl = (
0.006918
- 0.399912 * math.cos(g)
+ 0.070257 * math.sin(g)
- 0.006758 * math.cos(2 * g)
+ 0.000907 * math.sin(2 * g)
- 0.002697 * math.cos(3 * g)
+ 0.00148 * math.sin(3 * g)
)
true_solar_minutes = hours * 60 + eq_time + 4 * lon
hour_angle = math.radians(true_solar_minutes / 4 - 180)
lat_r = math.radians(lat)
cos_zenith = math.sin(lat_r) * math.sin(decl) + math.cos(lat_r) * math.cos(decl) * math.cos(
hour_angle
)
return 90 - math.degrees(math.acos(max(-1.0, min(1.0, cos_zenith))))