Files
refusol-datalogging/README.md
T

141 lines
6.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Refusol Solar Monitor
Live dashboard for the Refusol 020K inverters. A poller on the site laptop
reads the inverters every 60 s and POSTs the values here; the dashboard
answers two questions first:
1. **Is every inverter operating, or has one failed?** (Operating / Check
needed / Fault / Offline / Sleeping at night / No data)
2. **Is any inverter producing much less than the others?** (output
comparison vs the fleet median, plus an MPPT1-vs-MPPT2 string check)
Public read-only, installable on phones as an app (PWA), served at
`solar.bonamin.net`.
## Tech Stack
- **Backend:** Python / FastAPI, SQLite (`./data/solar.db`)
- **Frontend:** React + Tailwind CSS (Vite), Recharts, vite-plugin-pwa
- **Deployment:** Docker Compose + Nginx (single port for NPM)
- **Poller:** `poller/backend_client.py` (stdlib only) used by `refusol_poller.py`
## Getting Started
```bash
git clone https://gitea.bonamin.net/bonamin/refusol-datalogging.git
cd refusol-datalogging
cp .env.example .env # set API_KEY, SITE_LATITUDE/LONGITUDE, HTTP_PORT
docker compose up -d --build
```
The stack listens on `HTTP_PORT` (default 8090). In Nginx Proxy Manager add a
proxy host `solar.bonamin.net` → `<vps>:8090` with SSL (Let's Encrypt, force
HTTPS) — the PWA install needs HTTPS.
Server deploys: `deploy-host.sh` (fetch `main`, reset, `docker compose up -d --build`).
It works from wherever the repo is cloned.
To see it with fake data locally:
```bash
python poller/fake_poller.py --url http://localhost:8090 --key change-me --backfill-days 30
```
## Project Structure
```
refusol-datalogger/
├── backend/ # FastAPI API (app/), tests/
├── frontend/ # React PWA
├── nginx/ # Single-port reverse proxy (/api → backend, / → frontend)
├── poller/ # backend_client.py for the site laptop + fake_poller.py
├── data/ # SQLite database (gitignored, bind-mounted)
├── docker-compose.yml
└── .env # from .env.example
```
## API
Interactive docs: `/api/docs` (OpenAPI at `/api/openapi.json` — usable to
generate a Flutter client later).
| Method | Path | |
|---|---|---|
| `POST` | `/api/readings` | Ingest one poll cycle (JSON array, one object per inverter). `X-API-Key` required. Empty array = heartbeat. Duplicates (same inverter + timestamp) are ignored. |
| `GET` | `/api/summary` | Every inverter's latest reading + health + issues, fleet totals, datalogger status |
| `GET` | `/api/inverters` | All known inverters (auto-registered on first reading) |
| `GET` | `/api/inverters/{id}` | One inverter |
| `PATCH` | `/api/inverters/{id}` | Name, `capacity_kwp`, `mppt_count`, `sort_order`, `notes`. `X-API-Key` required |
| `GET` | `/api/inverters/{id}/latest` | Latest reading (plus derived DC power / efficiency) |
| `GET` | `/api/inverters/{id}/history` | `from`, `to` (ISO), optional `bucket` seconds, `field` (repeatable). Downsampled to ≤600 points |
| `GET` | `/api/inverters/{id}/daily` | Energy + peak power per local day (`days`, default 365, or `from`/`to` dates) |
| `GET` | `/api/power` | AC power of all inverters over time (default 24 h; `last_run=true` = since the inverters last woke up) |
| `GET` | `/api/energy/daily` | Energy per day per inverter + total (default 30 days) |
| `GET` | `/api/meta/fields` | Label / unit / group of every parameter — render from this |
| `GET` | `/api/settings` | Dashboard display options (comparison averaging / scale / reference, power chart range / smoothing, voltage display, default language) |
| `PUT` | `/api/settings` | Replace them — applies to every viewer (no accounts). `X-API-Key` required |
### Adding parameters
The reading parameters are defined once in `backend/app/fields.py`. Unknown
keys the poller sends are stored as-is (`extra`) and shown on the inverter
page as "Other (unmapped)". To promote one, add it to `FIELDS` — the column is
created on the next start and it appears in the API, the parameter table and
the chart picker without further changes.
### Health rules
- **Offline** — no reading for `ONLINE_WINDOW_SECONDS` (5 min) — or 2.5× the
inverter's own reporting interval if that is longer (INV5 comes from the
cloud every ~5 min → ~12 min) — while the datalogger is still posting and
the sun is up. At night that's **Sleeping**;
if the datalogger itself is silent the state is **No data** (and a banner
says so).
- **Fault** — the reading carries a non-zero `error_code`. No `error_code`
(the poller doesn't send one yet) means no fault.
- **Feeding / Standby** — online and `status_code` is in
`FEEDING_STATUS_CODES` (7, 8) → Feeding; any other code → Standby. An
inverter in standby while its peers produce is caught by the
underperformance check below.
- **Check needed** — output below `UNDERPERFORM_RATIO` (75%) of the fleet
median over the last 10 min (per kWp once every inverter has its array size
set), or one MPPT input below half of the other.
Thresholds are in `.env`.
### Energy
Daily energy uses, in order: the inverter's daily counter
(`daily_energy_wh`), the growth of its lifetime counter, or — while neither
is sent (INV1–INV4 today; only INV5 has real counters) — AC power
integrated over time (trapezoid between consecutive readings, gaps over 5 min
not bridged). The estimate is flagged (`estimated: true` in the API, "≈" on
the dashboard). Lifetime energy only exists where `total_energy_wh` is sent;
the dashboard total says how many inverters it covers.
### Dashboard settings and language
The ⚙ page sets the display options above for everyone; the API key is asked
once and remembered on the device. The language (Greek / English) can be
switched per device from the header; devices that haven't picked one use the
shared default. Issue texts are translated client-side from each issue's
`code` + `params`, so new clients (Flutter) can do the same.
The current state of the register map is in `poller/README.md`.
## Data retention
Raw readings older than `RETENTION_DAYS` (730) are purged every 6 h. The
per-day energy table is kept indefinitely.
## Development
```bash
cd backend && python -m venv .venv && .venv/Scripts/pip install -r requirements-dev.txt
.venv/Scripts/python -m pytest
.venv/Scripts/python -m uvicorn app.main:app --reload # :8000
cd frontend && npm install && npm run dev # :5173, proxies /api to :8000
```