109 lines
5.0 KiB
Python
109 lines
5.0 KiB
Python
"""Lighting service: one worker per device, one mode per zone, saved in settings.json.
|
|
|
|
Zones and their modes (see kurgan.settings.LIGHTING_MODES):
|
|
case_fans Lighting Node CORE QL120 LEDs: off (hardware effect), temperature, chase
|
|
memory Dominator DDR5: off (held dark while running), temperature, chase
|
|
gpu ASUS RTX 4090 LED row: off (its own effect), chase
|
|
|
|
The three chases share one power-paced clock, so they stay in step.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import threading
|
|
|
|
from kurgan.lighting.case_fans import SIDE_CHASE_BOTTOM_STEP, ChaseTiming, RGBLighting
|
|
from kurgan.lighting.gpu import GpuLighting
|
|
from kurgan.lighting.memory import MemoryLighting
|
|
from kurgan.settings import LIGHTING_MODES, Settings
|
|
from kurgan.theme import Theme
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
ZONE_LABELS = {"case_fans": "Case fan LEDs", "memory": "RAM", "gpu": "GPU"}
|
|
MODE_LABELS = {"off": "Off", "temperature": "Temperature", "chase": "Red side chase"}
|
|
|
|
|
|
class LightingService:
|
|
def __init__(self, settings: Settings, theme: Theme, chase: tuple[float, float, float, float],
|
|
overrides: dict[str, str] | None = None, *, rgb=None, memory=None, gpu=None):
|
|
"""chase: (idle s/LED, full-power s/LED, idle watts, full watts).
|
|
overrides: modes for this run only (CLI flags), not saved."""
|
|
self._settings = settings
|
|
self._lock = threading.Lock()
|
|
self._events = {zone: {mode: threading.Event() for mode in modes if mode != "off"}
|
|
for zone, modes in LIGHTING_MODES.items()}
|
|
self._overridden = set(overrides or ())
|
|
modes = settings.section("lighting")
|
|
modes.update(overrides or {})
|
|
self._modes: dict[str, str] = {}
|
|
for zone, mode in modes.items():
|
|
self._apply(zone, mode)
|
|
step, fast, idle_w, full_w = chase
|
|
self.timing = ChaseTiming(step, fast, (idle_w, full_w))
|
|
self.timing.reset(SIDE_CHASE_BOTTOM_STEP)
|
|
self.rgb = rgb or RGBLighting(theme, step, fast, (idle_w, full_w), chase_timing=self.timing)
|
|
self.memory = memory or MemoryLighting(theme, chase_timing=self.timing)
|
|
self.gpu = gpu or GpuLighting(self.timing)
|
|
self._threads: list[threading.Thread] = []
|
|
self._power: float | None = None
|
|
|
|
def _apply(self, zone: str, mode: str) -> None:
|
|
events = self._events[zone]
|
|
# Clear the old pattern before setting the new one, as the tray always did.
|
|
for name, event in events.items():
|
|
if name != mode:
|
|
event.clear()
|
|
if mode != "off":
|
|
events[mode].set()
|
|
self._modes[zone] = mode
|
|
|
|
def start(self, stop: threading.Event) -> None:
|
|
e = self._events
|
|
for name, target, args in (
|
|
("case-fan-lighting", self.rgb.run, (stop, e["case_fans"]["temperature"], e["case_fans"]["chase"])),
|
|
("memory-lighting", self.memory.run, (stop, e["memory"]["temperature"], e["memory"]["chase"])),
|
|
("gpu-lighting", self.gpu.run, (stop, e["gpu"]["chase"]))):
|
|
thread = threading.Thread(target=target, args=args, name=name, daemon=True)
|
|
thread.start()
|
|
self._threads.append(thread)
|
|
|
|
def join(self) -> None:
|
|
for thread in self._threads:
|
|
thread.join()
|
|
|
|
def on_telemetry(self, cpu_temp: float | None, gpu_temp: float | None, total_watts: float | None) -> None:
|
|
self._power = total_watts
|
|
self.rgb.set_temperature(cpu_temp, gpu_temp)
|
|
self.rgb.set_power(total_watts)
|
|
self.memory.set_temperature(cpu_temp, gpu_temp)
|
|
self.memory.set_power(total_watts)
|
|
|
|
def mode(self, zone: str) -> str:
|
|
return self._modes[zone]
|
|
|
|
def set_mode(self, zone: str, mode: str) -> None:
|
|
"""Save first, then switch, so a failed save leaves the lighting as it was."""
|
|
if zone not in LIGHTING_MODES or mode not in LIGHTING_MODES[zone]:
|
|
raise ValueError(f"Unknown lighting {zone!r} mode {mode!r}")
|
|
with self._lock:
|
|
self._settings.update("lighting", **{zone: mode})
|
|
self._overridden.discard(zone)
|
|
self._apply(zone, mode)
|
|
log.info("%s lighting: %s", ZONE_LABELS[zone], mode)
|
|
|
|
def statuses(self) -> dict[str, str]:
|
|
return {"case_fans": self.rgb.status, "memory": self.memory.status, "gpu": self.gpu.status}
|
|
|
|
def snapshot(self) -> dict:
|
|
statuses = self.statuses()
|
|
# The eased rate once a chase is running; the target rate for the current power until then.
|
|
rate = self.timing.rate or self.timing.rate_for(self._power)
|
|
chasing = "chase" in self._modes.values()
|
|
return {"chase_ms_per_led": round(1000 / rate) if chasing else None,
|
|
"zones": [{"key": zone, "label": ZONE_LABELS[zone], "mode": self._modes[zone],
|
|
"modes": [{"key": m, "label": MODE_LABELS[m]} for m in modes],
|
|
"status": statuses[zone], "this_run_only": zone in self._overridden}
|
|
for zone, modes in LIGHTING_MODES.items()]}
|