removed grafana and victoria
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
from .collector import main
|
||||
from .app import main
|
||||
|
||||
main()
|
||||
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import html
|
||||
import logging
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
from urllib.parse import parse_qs, urlencode, urlparse
|
||||
|
||||
from .collector import Collector, HueClient, Settings
|
||||
from .rrd_store import RRDStore
|
||||
|
||||
LOG = logging.getLogger("hue-collector")
|
||||
|
||||
|
||||
class State:
|
||||
def __init__(self):
|
||||
self.snapshot = {"groups": [], "sensors": []}
|
||||
self.last_success: float | None = None
|
||||
self.error: str | None = None
|
||||
self.lock = threading.Lock()
|
||||
|
||||
|
||||
def collection_loop(collector: Collector, store: RRDStore, state: State, interval: int) -> None:
|
||||
while True:
|
||||
started = time.monotonic()
|
||||
try:
|
||||
snapshot = collector.collect()
|
||||
store.update(snapshot)
|
||||
with state.lock:
|
||||
state.snapshot, state.last_success, state.error = snapshot, time.time(), None
|
||||
except Exception as exc:
|
||||
LOG.exception("Hue collection failed")
|
||||
with state.lock:
|
||||
state.error = str(exc)
|
||||
time.sleep(max(1, interval - (time.monotonic() - started)))
|
||||
|
||||
|
||||
def page(state: State, period: str) -> bytes:
|
||||
with state.lock:
|
||||
snapshot = state.snapshot
|
||||
last_success = state.last_success
|
||||
error = state.error
|
||||
status = f"Last update: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(last_success))}" if last_success else "Waiting for first collection"
|
||||
if error:
|
||||
status += f" — Error: {html.escape(error)}"
|
||||
controls = " ".join(
|
||||
f'<a class="{("active" if value == period else "")}" href="/?range={value}">{value}</a>'
|
||||
for value in ("6h", "24h", "7d", "30d")
|
||||
)
|
||||
group_cards = []
|
||||
for item in sorted(snapshot["groups"], key=lambda value: (value["type"], value["name"].lower())):
|
||||
query = urlencode({"kind": "group", "id": item["id"], "range": period})
|
||||
group_cards.append(
|
||||
f'<section><h2>{html.escape(item["type"].title())}: {html.escape(item["name"])}</h2>'
|
||||
f'<img loading="lazy" src="/graph?{query}" alt="Light history"></section>'
|
||||
)
|
||||
sensor_cards = []
|
||||
for item in sorted(snapshot["sensors"], key=lambda value: (value["type"], value["name"].lower())):
|
||||
query = urlencode({"kind": "sensor", "id": item["id"], "range": period})
|
||||
value = "unavailable" if item["value"] is None else f'{item["value"]:.1f} {item["unit"]}'
|
||||
location = item["room"] or item["zones"] or "No room"
|
||||
sensor_cards.append(
|
||||
f'<section><h2>{html.escape(item["name"])}</h2><p>{html.escape(location)} · {html.escape(value)}</p>'
|
||||
f'<img loading="lazy" src="/graph?{query}" alt="Sensor history"></section>'
|
||||
)
|
||||
document = f'''<!doctype html><html lang="en"><head><meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1"><meta http-equiv="refresh" content="60">
|
||||
<title>Hue history</title><style>
|
||||
:root{{color-scheme:dark;font-family:system-ui,sans-serif}}body{{margin:auto;max-width:1200px;padding:1rem;background:#0b1220;color:#e5e7eb}}
|
||||
header{{display:flex;gap:1rem;align-items:center;justify-content:space-between;flex-wrap:wrap}}nav a{{padding:.45rem .7rem;color:#93c5fd;text-decoration:none}}
|
||||
nav a.active{{background:#1d4ed8;color:white;border-radius:.35rem}}.status,p{{color:#9ca3af}}section{{background:#111827;border:1px solid #374151;border-radius:.6rem;padding:.75rem;margin:1rem 0}}
|
||||
h1,h2{{margin:.25rem 0 .5rem}}h2{{font-size:1.1rem}}p{{margin:.25rem 0}}img{{display:block;width:100%;height:auto}}
|
||||
</style></head><body><header><div><h1>Philips Hue</h1><div class="status">{status}</div></div><nav>{controls}</nav></header>
|
||||
<h1>Rooms and zones</h1>{''.join(group_cards) or '<p>No groups collected yet.</p>'}
|
||||
<h1>Sensors</h1>{''.join(sensor_cards) or '<p>No sensors collected yet.</p>'}</body></html>'''
|
||||
return document.encode()
|
||||
|
||||
|
||||
def handler_for(state: State, store: RRDStore):
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
parsed = urlparse(self.path)
|
||||
query = parse_qs(parsed.query)
|
||||
if parsed.path == "/":
|
||||
body, status, content_type = page(state, query.get("range", ["24h"])[0]), 200, "text/html; charset=utf-8"
|
||||
elif parsed.path == "/healthz":
|
||||
with state.lock:
|
||||
healthy = state.last_success is not None
|
||||
body, status, content_type = (b"ok\n", 200, "text/plain") if healthy else (b"not ready\n", 503, "text/plain")
|
||||
elif parsed.path == "/graph":
|
||||
try:
|
||||
kind, item_id = query["kind"][0], query["id"][0]
|
||||
if kind not in {"group", "sensor"}:
|
||||
raise ValueError("unknown graph kind")
|
||||
with state.lock:
|
||||
collection = state.snapshot["groups" if kind == "group" else "sensors"]
|
||||
item = next(value for value in collection if value["id"] == item_id)
|
||||
body = store.graph(kind, item, query.get("range", ["24h"])[0])
|
||||
status, content_type = 200, "image/png"
|
||||
except (KeyError, ValueError, StopIteration, FileNotFoundError) as exc:
|
||||
body, status, content_type = f"graph not found: {exc}\n".encode(), 404, "text/plain"
|
||||
except Exception as exc:
|
||||
LOG.exception("Graph rendering failed")
|
||||
body, status, content_type = f"graph error: {exc}\n".encode(), 500, "text/plain"
|
||||
else:
|
||||
body, status, content_type = b"not found\n", 404, "text/plain"
|
||||
self.send_response(status)
|
||||
self.send_header("Content-Type", content_type)
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def log_message(self, fmt, *args):
|
||||
LOG.debug(fmt, *args)
|
||||
|
||||
return Handler
|
||||
|
||||
|
||||
def main() -> None:
|
||||
logging.basicConfig(level=os.environ.get("LOG_LEVEL", "INFO"), format="%(asctime)s %(levelname)s %(message)s")
|
||||
settings = Settings.from_env()
|
||||
state = State()
|
||||
store = RRDStore(settings.data_dir, settings.interval)
|
||||
collector = Collector(HueClient(settings))
|
||||
threading.Thread(target=collection_loop, args=(collector, store, state, settings.interval), daemon=True).start()
|
||||
LOG.info("Serving Hue dashboard on http://%s:%s", settings.listen_host, settings.listen_port)
|
||||
ThreadingHTTPServer((settings.listen_host, settings.listen_port), handler_for(state, store)).serve_forever()
|
||||
+35
-158
@@ -1,38 +1,22 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import ssl
|
||||
import threading
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
from typing import Any
|
||||
|
||||
import requests
|
||||
import urllib3
|
||||
|
||||
LOG = logging.getLogger("hue-collector")
|
||||
|
||||
|
||||
def _label(value: Any) -> str:
|
||||
return str(value).replace("\\", "\\\\").replace("\n", "\\n").replace('"', '\\"')
|
||||
|
||||
|
||||
def _sample(name: str, value: float | int, **labels: str) -> str:
|
||||
rendered = ",".join(f'{key}="{_label(val)}"' for key, val in sorted(labels.items()))
|
||||
return f"{name}{{{rendered}}} {value}" if rendered else f"{name} {value}"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Settings:
|
||||
host: str
|
||||
application_key: str
|
||||
verify_tls: bool = False
|
||||
interval: float = 30
|
||||
interval: int = 30
|
||||
listen_host: str = "0.0.0.0"
|
||||
listen_port: int = 8000
|
||||
data_dir: str = "/data"
|
||||
|
||||
@classmethod
|
||||
def from_env(cls) -> "Settings":
|
||||
@@ -44,9 +28,10 @@ class Settings:
|
||||
host=host.removeprefix("https://").removeprefix("http://").rstrip("/"),
|
||||
application_key=key,
|
||||
verify_tls=os.environ.get("HUE_VERIFY_TLS", "false").lower() in {"1", "true", "yes"},
|
||||
interval=float(os.environ.get("COLLECT_INTERVAL_SECONDS", "30")),
|
||||
interval=int(os.environ.get("COLLECT_INTERVAL_SECONDS", "30")),
|
||||
listen_host=os.environ.get("LISTEN_HOST", "0.0.0.0"),
|
||||
listen_port=int(os.environ.get("LISTEN_PORT", "8000")),
|
||||
data_dir=os.environ.get("DATA_DIR", "/data"),
|
||||
)
|
||||
|
||||
|
||||
@@ -74,7 +59,7 @@ class Collector:
|
||||
def __init__(self, client: HueClient):
|
||||
self.client = client
|
||||
|
||||
def collect(self) -> str:
|
||||
def collect(self) -> dict[str, list[dict[str, Any]]]:
|
||||
resources = {kind: self.client.get(kind) for kind in self.RESOURCE_TYPES}
|
||||
devices = {item["id"]: item for item in resources["device"]}
|
||||
connected = {
|
||||
@@ -84,57 +69,42 @@ class Collector:
|
||||
light_by_device = self._by_owner(resources["light"])
|
||||
room_for_device, zones_for_device = self._locations(resources["room"], resources["zone"])
|
||||
|
||||
lines = [
|
||||
"# HELP hue_collection_success Whether the most recent Hue collection succeeded.",
|
||||
"# TYPE hue_collection_success gauge",
|
||||
"hue_collection_success 1",
|
||||
"# HELP hue_group_lights_available Number of connected lights in a room or zone.",
|
||||
"# TYPE hue_group_lights_available gauge",
|
||||
"# HELP hue_group_lights_shining Number of connected lights that are on.",
|
||||
"# TYPE hue_group_lights_shining gauge",
|
||||
"# HELP hue_group_lights_off Number of connected lights that are off.",
|
||||
"# TYPE hue_group_lights_off gauge",
|
||||
"# HELP hue_group_lights_unavailable Number of configured lights that are disconnected.",
|
||||
"# TYPE hue_group_lights_unavailable gauge",
|
||||
"# HELP hue_group_lights_shining_percent Percentage of connected lights that are on.",
|
||||
"# TYPE hue_group_lights_shining_percent gauge",
|
||||
"# HELP hue_group_lights_off_percent Percentage of connected lights that are off.",
|
||||
"# TYPE hue_group_lights_off_percent gauge",
|
||||
"# HELP hue_group_lights_unavailable_percent Percentage of configured lights that are disconnected.",
|
||||
"# TYPE hue_group_lights_unavailable_percent gauge",
|
||||
"# HELP hue_group_average_brightness_percent Average brightness of connected lights that are on.",
|
||||
"# TYPE hue_group_average_brightness_percent gauge",
|
||||
]
|
||||
groups: list[dict[str, Any]] = []
|
||||
for kind in ("room", "zone"):
|
||||
for group in resources[kind]:
|
||||
all_lights = [
|
||||
light
|
||||
for child in group.get("children", [])
|
||||
for light in light_by_device.get(child.get("rid"), [])
|
||||
]
|
||||
lights = [
|
||||
light
|
||||
for child in group.get("children", [])
|
||||
all_lights = [light for child in group.get("children", []) for light in light_by_device.get(child.get("rid"), [])]
|
||||
available = [
|
||||
light for child in group.get("children", [])
|
||||
for light in light_by_device.get(child.get("rid"), [])
|
||||
if connected.get(child.get("rid"), True)
|
||||
]
|
||||
shining = [light for light in lights if light.get("on", {}).get("on", False)]
|
||||
off = len(lights) - len(shining)
|
||||
unavailable = len(all_lights) - len(lights)
|
||||
labels = {"group_type": kind, "group": group.get("metadata", {}).get("name", group["id"]), "group_id": group["id"]}
|
||||
lines.append(_sample("hue_group_lights_available", len(lights), **labels))
|
||||
lines.append(_sample("hue_group_lights_shining", len(shining), **labels))
|
||||
lines.append(_sample("hue_group_lights_off", off, **labels))
|
||||
lines.append(_sample("hue_group_lights_unavailable", unavailable, **labels))
|
||||
lines.append(_sample("hue_group_lights_shining_percent", 100 * len(shining) / len(lights) if lights else 0, **labels))
|
||||
lines.append(_sample("hue_group_lights_off_percent", 100 * off / len(lights) if lights else 0, **labels))
|
||||
lines.append(_sample("hue_group_lights_unavailable_percent", 100 * unavailable / len(all_lights) if all_lights else 0, **labels))
|
||||
shining = [light for light in available if light.get("on", {}).get("on", False)]
|
||||
brightness = [light.get("dimming", {}).get("brightness") for light in shining]
|
||||
brightness = [value for value in brightness if value is not None]
|
||||
lines.append(_sample("hue_group_average_brightness_percent", sum(brightness) / len(brightness) if brightness else 0, **labels))
|
||||
groups.append({
|
||||
"id": group["id"], "type": kind,
|
||||
"name": group.get("metadata", {}).get("name", group["id"]),
|
||||
"available": len(available), "on": len(shining),
|
||||
"off": len(available) - len(shining),
|
||||
"unavailable": len(all_lights) - len(available),
|
||||
"brightness": sum(brightness) / len(brightness) if brightness else 0.0,
|
||||
})
|
||||
|
||||
lines.extend(self._sensor_metrics(resources, devices, connected, room_for_device, zones_for_device))
|
||||
return "\n".join(lines) + "\n"
|
||||
sensors: list[dict[str, Any]] = []
|
||||
for resource_type, field, unit in (("temperature", "temperature", "°C"), ("light_level", "light_level", "lux")):
|
||||
for sensor in resources[resource_type]:
|
||||
device_id = sensor.get("owner", {}).get("rid", "")
|
||||
device = devices.get(device_id, {})
|
||||
value = sensor.get(resource_type, {}).get(field)
|
||||
valid = not (resource_type == "light_level" and not sensor.get("light_level", {}).get("light_level_valid", True))
|
||||
available = connected.get(device_id, True)
|
||||
sensors.append({
|
||||
"id": sensor["id"], "type": resource_type,
|
||||
"name": sensor.get("metadata", {}).get("name") or device.get("metadata", {}).get("name", sensor["id"]),
|
||||
"room": room_for_device.get(device_id, ""), "zones": zones_for_device.get(device_id, ""),
|
||||
"available": available, "value": value if available and valid else None, "unit": unit,
|
||||
})
|
||||
return {"groups": groups, "sensors": sensors}
|
||||
|
||||
@staticmethod
|
||||
def _by_owner(items: list[dict[str, Any]]) -> dict[str, list[dict[str, Any]]]:
|
||||
@@ -148,102 +118,9 @@ class Collector:
|
||||
room_for_device: dict[str, str] = {}
|
||||
zones_for_device: dict[str, list[str]] = {}
|
||||
for room in rooms:
|
||||
name = room.get("metadata", {}).get("name", room["id"])
|
||||
for child in room.get("children", []):
|
||||
room_for_device[child.get("rid", "")] = name
|
||||
room_for_device[child.get("rid", "")] = room.get("metadata", {}).get("name", room["id"])
|
||||
for zone in zones:
|
||||
name = zone.get("metadata", {}).get("name", zone["id"])
|
||||
for child in zone.get("children", []):
|
||||
zones_for_device.setdefault(child.get("rid", ""), []).append(name)
|
||||
zones_for_device.setdefault(child.get("rid", ""), []).append(zone.get("metadata", {}).get("name", zone["id"]))
|
||||
return room_for_device, {key: ", ".join(sorted(value)) for key, value in zones_for_device.items()}
|
||||
|
||||
def _sensor_metrics(self, resources, devices, connected, room_for_device, zones_for_device) -> list[str]:
|
||||
lines = [
|
||||
"# HELP hue_sensor_available Whether the sensor's Hue device is connected.",
|
||||
"# TYPE hue_sensor_available gauge",
|
||||
"# HELP hue_temperature_celsius Temperature measured by a Hue sensor.",
|
||||
"# TYPE hue_temperature_celsius gauge",
|
||||
"# HELP hue_light_level_lux Illuminance measured by a Hue sensor.",
|
||||
"# TYPE hue_light_level_lux gauge",
|
||||
]
|
||||
for resource_type, metric, field in (
|
||||
("temperature", "hue_temperature_celsius", "temperature"),
|
||||
("light_level", "hue_light_level_lux", "light_level"),
|
||||
):
|
||||
for sensor in resources[resource_type]:
|
||||
device_id = sensor.get("owner", {}).get("rid", "")
|
||||
device = devices.get(device_id, {})
|
||||
labels = {
|
||||
"sensor": sensor.get("metadata", {}).get("name") or device.get("metadata", {}).get("name", sensor["id"]),
|
||||
"sensor_id": sensor["id"],
|
||||
"sensor_type": resource_type,
|
||||
"room": room_for_device.get(device_id, ""),
|
||||
"zones": zones_for_device.get(device_id, ""),
|
||||
}
|
||||
available = connected.get(device_id, True)
|
||||
lines.append(_sample("hue_sensor_available", int(available), **labels))
|
||||
value = sensor.get(resource_type, {}).get(field)
|
||||
if resource_type == "light_level" and not sensor.get("light_level", {}).get("light_level_valid", True):
|
||||
value = None
|
||||
if available and value is not None:
|
||||
lines.append(_sample(metric, value, **labels))
|
||||
return lines
|
||||
|
||||
|
||||
class State:
|
||||
def __init__(self):
|
||||
self.metrics = "hue_collection_success 0\n"
|
||||
self.healthy = False
|
||||
self.lock = threading.Lock()
|
||||
|
||||
|
||||
def run_collection_loop(collector: Collector, state: State, interval: float) -> None:
|
||||
while True:
|
||||
try:
|
||||
metrics = collector.collect()
|
||||
with state.lock:
|
||||
state.metrics, state.healthy = metrics, True
|
||||
except Exception:
|
||||
LOG.exception("Hue collection failed")
|
||||
with state.lock:
|
||||
state.metrics = "hue_collection_success 0\n"
|
||||
time.sleep(interval)
|
||||
|
||||
|
||||
def handler_for(state: State):
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
if self.path == "/metrics":
|
||||
with state.lock:
|
||||
body, status = state.metrics.encode(), 200
|
||||
content_type = "text/plain; version=0.0.4; charset=utf-8"
|
||||
elif self.path == "/healthz":
|
||||
with state.lock:
|
||||
healthy = state.healthy
|
||||
body, status, content_type = (b"ok\n", 200, "text/plain") if healthy else (b"not ready\n", 503, "text/plain")
|
||||
else:
|
||||
body, status, content_type = b"not found\n", 404, "text/plain"
|
||||
self.send_response(status)
|
||||
self.send_header("Content-Type", content_type)
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def log_message(self, fmt, *args):
|
||||
LOG.debug(fmt, *args)
|
||||
|
||||
return Handler
|
||||
|
||||
|
||||
def main() -> None:
|
||||
logging.basicConfig(level=os.environ.get("LOG_LEVEL", "INFO"), format="%(asctime)s %(levelname)s %(message)s")
|
||||
settings = Settings.from_env()
|
||||
state = State()
|
||||
collector = Collector(HueClient(settings))
|
||||
threading.Thread(target=run_collection_loop, args=(collector, state, settings.interval), daemon=True).start()
|
||||
LOG.info("Serving metrics on %s:%s", settings.listen_host, settings.listen_port)
|
||||
ThreadingHTTPServer((settings.listen_host, settings.listen_port), handler_for(state)).serve_forever()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import rrdtool
|
||||
|
||||
SAFE_ID = re.compile(r"^[A-Za-z0-9-]+$")
|
||||
|
||||
|
||||
class RRDStore:
|
||||
def __init__(self, data_dir: str, step: int):
|
||||
self.root = Path(data_dir)
|
||||
self.root.mkdir(parents=True, exist_ok=True)
|
||||
self.step = step
|
||||
self.heartbeat = max(step * 3, 120)
|
||||
|
||||
def update(self, snapshot: dict[str, list[dict[str, Any]]]) -> None:
|
||||
for group in snapshot["groups"]:
|
||||
path = self.path("group", group["id"])
|
||||
self._ensure_group(path)
|
||||
rrdtool.update(str(path), "N:%s:%s:%s:%s:%s" % (
|
||||
group["available"], group["on"], group["off"], group["unavailable"], group["brightness"]
|
||||
))
|
||||
for sensor in snapshot["sensors"]:
|
||||
path = self.path("sensor", sensor["id"])
|
||||
self._ensure_sensor(path)
|
||||
value = "U" if sensor["value"] is None else sensor["value"]
|
||||
rrdtool.update(str(path), f"N:{value}:{int(sensor['available'])}")
|
||||
|
||||
def path(self, kind: str, item_id: str) -> Path:
|
||||
if kind not in {"group", "sensor"} or not SAFE_ID.fullmatch(item_id):
|
||||
raise ValueError("Invalid RRD identifier")
|
||||
return self.root / f"{kind}-{item_id}.rrd"
|
||||
|
||||
def _archives(self) -> list[str]:
|
||||
five_minutes = max(1, round(300 / self.step))
|
||||
one_hour = max(1, round(3600 / self.step))
|
||||
return [
|
||||
f"RRA:AVERAGE:0.5:1:{max(1, round(7 * 86400 / self.step))}",
|
||||
f"RRA:AVERAGE:0.5:{five_minutes}:{90 * 288}",
|
||||
f"RRA:AVERAGE:0.5:{one_hour}:{2 * 365 * 24}",
|
||||
]
|
||||
|
||||
def _ensure_group(self, path: Path) -> None:
|
||||
if not path.exists():
|
||||
rrdtool.create(
|
||||
str(path), "--step", str(self.step),
|
||||
f"DS:available:GAUGE:{self.heartbeat}:0:U", f"DS:on:GAUGE:{self.heartbeat}:0:U",
|
||||
f"DS:off:GAUGE:{self.heartbeat}:0:U", f"DS:unavailable:GAUGE:{self.heartbeat}:0:U",
|
||||
f"DS:brightness:GAUGE:{self.heartbeat}:0:100", *self._archives(),
|
||||
)
|
||||
|
||||
def _ensure_sensor(self, path: Path) -> None:
|
||||
if not path.exists():
|
||||
rrdtool.create(
|
||||
str(path), "--step", str(self.step), f"DS:value:GAUGE:{self.heartbeat}:U:U",
|
||||
f"DS:available:GAUGE:{self.heartbeat}:0:1", *self._archives(),
|
||||
)
|
||||
|
||||
def graph(self, kind: str, item: dict[str, Any], period: str) -> bytes:
|
||||
start = {"6h": "-6h", "24h": "-24h", "7d": "-7d", "30d": "-30d"}.get(period, "-24h")
|
||||
path = self.path(kind, item["id"])
|
||||
if not path.exists():
|
||||
raise FileNotFoundError(path)
|
||||
handle, output = tempfile.mkstemp(suffix=".png")
|
||||
os.close(handle)
|
||||
try:
|
||||
args = [
|
||||
output, "--start", start, "--end", "now", "--width", "900", "--height", "260",
|
||||
"--font", "DEFAULT:0:DejaVu Sans", "--color", "BACK#111827", "--color", "CANVAS#111827",
|
||||
"--color", "FONT#d1d5db", "--color", "GRID#374151", "--color", "MGRID#4b5563",
|
||||
"--border", "0", "--slope-mode",
|
||||
]
|
||||
escaped = str(path).replace(":", "\\:")
|
||||
if kind == "group":
|
||||
args += [
|
||||
"--vertical-label", "% of lights / intensity", "--lower-limit", "0", "--upper-limit", "100", "--rigid",
|
||||
f"DEF:u={escaped}:unavailable:AVERAGE", f"DEF:o={escaped}:off:AVERAGE",
|
||||
f"DEF:n={escaped}:on:AVERAGE", f"DEF:b={escaped}:brightness:AVERAGE",
|
||||
"CDEF:t=u,o,+,n,+", "CDEF:up=t,0,EQ,0,u,t,/,100,*,IF",
|
||||
"CDEF:op=t,0,EQ,0,o,t,/,100,*,IF", "CDEF:np=t,0,EQ,0,n,t,/,100,*,IF",
|
||||
"AREA:up#8b0000:Unavailable", "AREA:op#4b5563:Off:STACK", "AREA:np#facc15:On:STACK",
|
||||
"LINE2:b#22c55e:Average intensity",
|
||||
]
|
||||
else:
|
||||
label = "Temperature (°C)" if item["type"] == "temperature" else "Illuminance (lux)"
|
||||
args += ["--vertical-label", label, f"DEF:v={escaped}:value:AVERAGE", "LINE2:v#38bdf8:Value"]
|
||||
rrdtool.graph(*args)
|
||||
return Path(output).read_bytes()
|
||||
finally:
|
||||
Path(output).unlink(missing_ok=True)
|
||||
Reference in New Issue
Block a user