Le full refresh apparaissait trop souvent: getbbox() renvoyait un seul rectangle englobant tous les pixels modifiés, donc météo (haut) + heure de MaJ (ailleurs) qui changeaient au même cycle produisaient un rectangle quasi plein écran -> ratio > partial_max_ratio -> full forcé. - frame.py: détection des bandes horizontales modifiées disjointes (_changed_regions), refresh partiel serré par zone. Full basé sur le temps écoulé (last_full_at + time.monotonic) au lieu d'un compteur de cycles. État pngs/regions en liste, get_png(client, region). - config.py: full_refresh_interval_minutes (MONITORINK_FULL_INTERVAL_MIN, défaut 120). Suppression de partial_max_ratio. - app.py: /frame.meta renvoie un bloc multi-ligne "MODE SEQ N" + N régions "i x y w h"; /frame.png?region=i. - monitorinkloop.sh: display_meta parse le bloc et fait N fbink partiels.
82 lines
2.9 KiB
Python
82 lines
2.9 KiB
Python
"""Serveur Monitorink : expose le dashboard en PNG pour la Kobo.
|
|
|
|
Endpoints :
|
|
GET /image.png -> dashboard 1264x1680 niveaux de gris (avec cache TTL)
|
|
GET /debug.html -> HTML brut (itération design, pas de screenshot)
|
|
GET /health -> sonde de vie
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
|
|
from fastapi import FastAPI, Response
|
|
from fastapi.responses import HTMLResponse, PlainTextResponse
|
|
|
|
import frame
|
|
import render
|
|
from config import config
|
|
from integrations import kobo
|
|
|
|
app = FastAPI(title="Monitorink", docs_url=None, redoc_url=None)
|
|
|
|
_cache: dict[str, object] = {"png": None, "ts": 0.0}
|
|
|
|
|
|
@app.get("/health")
|
|
async def health() -> dict:
|
|
return {"status": "ok"}
|
|
|
|
|
|
@app.get("/image.png")
|
|
async def image(fresh: int = 0, bat: int | None = None, chg: int = 0) -> Response:
|
|
# La Kobo pousse sa batterie ici (bat=0-100, chg=1 si en charge) à chaque fetch.
|
|
kobo.record(bat, bool(chg))
|
|
now = time.time()
|
|
cached = _cache["png"]
|
|
age = now - float(_cache["ts"])
|
|
if cached and not fresh and age < config.cache_ttl_seconds:
|
|
png = cached # type: ignore[assignment]
|
|
else:
|
|
png = await render.render_png()
|
|
_cache["png"] = png
|
|
_cache["ts"] = now
|
|
return Response(
|
|
content=png, # type: ignore[arg-type]
|
|
media_type="image/png",
|
|
headers={"Cache-Control": "no-store"},
|
|
)
|
|
|
|
|
|
@app.get("/frame.meta", response_class=PlainTextResponse)
|
|
async def frame_meta(
|
|
client: str = "kobo", bat: int | None = None, chg: int = 0, reset: int = 0
|
|
) -> Response:
|
|
# Refresh partiel : rend l'image, calcule les zones modifiées vs le dernier frame de ce client,
|
|
# et renvoie un bloc texte trivial à parser en shell busybox :
|
|
# MODE SEQ NREGIONS
|
|
# i x y w h (NREGIONS lignes, i = index pour /frame.png?region=i)
|
|
# MODE ∈ {full, partial, noop}. Les PNG correspondants sont récupérés via /frame.png.
|
|
# reset=1 (1er cycle après un (re)démarrage Kobo) -> oublie l'état et force un full refresh.
|
|
kobo.record(bat, bool(chg))
|
|
info = await frame.compute_frame(client, reset=bool(reset))
|
|
regions = info["regions"]
|
|
lines = [f"{info['mode']} {info['seq']} {len(regions)}"]
|
|
lines += [f"{i} {x} {y} {w} {h}" for i, (x, y, w, h) in enumerate(regions)]
|
|
return PlainTextResponse("\n".join(lines), headers={"Cache-Control": "no-store"})
|
|
|
|
|
|
@app.get("/frame.png")
|
|
async def frame_png(client: str = "kobo", region: int = 0) -> Response:
|
|
# PNG de la région `region` décidée lors du dernier /frame.meta (un crop par zone modifiée en
|
|
# partial, image pleine en full).
|
|
png = frame.get_png(client, region)
|
|
if png is None:
|
|
return Response(status_code=503)
|
|
return Response(content=png, media_type="image/png", headers={"Cache-Control": "no-store"})
|
|
|
|
|
|
@app.get("/debug.html", response_class=HTMLResponse)
|
|
async def debug_html() -> str:
|
|
context = await render.build_context()
|
|
return render.render_html(context)
|