Files
paulandClaude Opus 5.5 b983528183 Review fixes: demo fans, record averages and restarts, combined alerts, quiet sensors
- demo.py: FakeBoard grows rpms(). The top fans' stall check (b2da221) called
  it, so under --demo every poll raised AttributeError and the case fans sat
  in CONTROL ERROR at 100%.
- control.py: TopFanCurve.update hands the headers back to the BIOS on any
  failure, a bug included, instead of letting it escape into the case fans'
  loop (unexpected errors are logged with a traceback once). The worker's
  catch-all now fails the top fans safe as well as the case fans, as the
  device-error path already did. The snapshot gives None for a channel whose
  tach has never turned, and the UI says "no tach reading" (and "Channel",
  not "Port") rather than showing SYS_FAN1's unwired tach as a dead fan.
- record.py: an hour's average is over the samples that carried the metric,
  not all of them: a GPU that dropped out for half an hour no longer halves
  its recorded power. Lines carry the count as a fourth number; the reader
  weights by it, and treats three-number lines as before.
- record.py: the hour in progress is checkpointed to records-current.json
  every minute and on close and resumed at start, so a hard stop (schtasks
  /End, the documented restart) loses at most a minute instead of the hour;
  a checkpoint from an earlier hour is written out at start. A failing write
  no longer stops recording for the run: it is logged at most once an hour
  and finished hours wait in memory (up to 48) until the disk works.
- app.py: alerts that start together share one balloon (Windows shows one at
  a time, so each notify() replaced the last), shortened to fit 255
  characters; a problem that clears and comes back balloons again only after
  10 minutes.
- telemetry/service.py: health also counts a source that returns no
  temperature three reads running. GpuSensors and CpuSensors don't raise
  when their device is lost, so a lost GPU driver used to show as healthy.
- hid.py: buffers of IRPs the driver never finished cancelling are kept for
  the life of the process; freeing them with their channel could still let a
  late completion write into reused memory.
- Tests: 13 new (160 pass). README updated to match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-05 18:31:11 +13:00

135 lines
4.1 KiB
Python

"""Simulated sensors and devices for `python -m kurgan --demo`.
Lets the web UI and the AI swapping be worked on while the real Kurgan owns the hardware:
nothing here touches USB, PawnIO, the SMBus or NVAPI. Run it with its own data folder and
web port, e.g.
$env:KURGAN_DATA_DIR = "$env:TEMP\\kurgan-demo"; python -m kurgan --demo --port 18080 --no-tray
"""
from __future__ import annotations
import math
import threading
import time
from kurgan.telemetry.readings import PowerReadings, Readings
_T0 = time.monotonic()
def _wave(period: float, phase: float = 0.0) -> float:
"""0..1, slowly rising and falling."""
return 0.5 + 0.5 * math.sin((time.monotonic() - _T0) * 2 * math.pi / period + phase)
class FakeCpu:
status = "simulated"
def read(self) -> Readings:
load = 5 + 70 * _wave(150) ** 3
return Readings(temp=38 + 0.5 * load, load=load, clock=1200 + 40 * load, power=15 + 1.6 * load)
class FakeGpu:
def read(self) -> Readings:
load = 2 + 95 * _wave(90, 1.0) ** 4
return Readings(temp=34 + 0.42 * load, load=load, clock=210 + 26 * load, power=18 + 4.2 * load)
def memory(self) -> tuple[int, int]:
return int((1.4 + 18 * _wave(300, 2.0)) * 2**30), 24 * 2**30
class FakeCommander:
controlled = False
def __init__(self):
self.duty = {p: 100 for p in range(1, 7)}
def _rpm(self, port: int) -> int:
return int(525 + 9.75 * self.duty[port])
def read(self) -> list[int]:
return [self._rpm(p) for p in range(1, 7)]
def set_duty(self, ports, duty: int) -> dict[int, int]:
for p in ports:
self.duty[p] = duty
return {p: self._rpm(p) for p in ports}
def close(self) -> None:
pass
class FakeSystem:
def __init__(self):
self.fans = FakeCommander()
def read(self, cpu: Readings, gpu: Readings) -> PowerReadings:
parts = {"Motherboard & chipset": 15.0, "Memory (4 modules)": 10 + 0.1 * (cpu.load or 0),
"Case fans (6, Commander PRO)": sum(2 * (r / 1500) ** 2.5 for r in self.fans.read()),
"USB devices & RGB lighting (fixed)": 12.0}
return PowerReadings(cpu=cpu.power, gpu=gpu.power, other=sum(parts.values()), parts=parts)
class FakeBoard:
BIOS_RPM = 700 # what a channel left to the BIOS curve turns at
def __init__(self):
self.mask = 0
self.duties: dict[int, int] = {}
def manual_mask(self) -> int:
return self.mask
def rpms(self) -> list[int]:
"""All 16 tachs, like Nct6687.rpms(): 14 RPM per % of duty, a fixed speed under the BIOS."""
return [14 * self.duties.get(c, 0) if self.mask >> c & 1 else self.BIOS_RPM for c in range(16)]
def set_manual(self, channels, duty: int) -> None:
self.mask |= sum(1 << c for c in channels)
self.duties.update((c, duty) for c in channels)
def set_auto(self, channels) -> None:
self.mask &= ~sum(1 << c for c in channels)
def close(self) -> None:
pass
class FakeCpuTemperature:
def read(self, now: float) -> float:
return FakeCpu().read().temp
def close(self) -> None:
pass
class FakeLights:
"""Stands in for a lighting worker: reports which pattern it would be running."""
def __init__(self, device: str):
self.device = device
self.status = "off"
def set_temperature(self, cpu_temp, gpu_temp=None) -> None:
pass
def set_power(self, total_watts) -> None:
pass
def run(self, stop: threading.Event, *events: threading.Event) -> None:
names = ("temperature", "chase") if len(events) == 2 else ("chase",)
while not stop.is_set():
active = [n for n, e in zip(names, events) if e.is_set()]
self.status = f"{active[0].capitalize()} on the {self.device} (simulated)" if active else "off"
stop.wait(0.2)
def hardware() -> dict:
return {
"cpu": FakeCpu(), "gpu": FakeGpu(), "system": FakeSystem(), "board": FakeBoard,
"fan_sensors": (FakeGpu, FakeCpuTemperature),
"lighting": {"rgb": FakeLights("case fans"), "memory": FakeLights("RAM"), "gpu": FakeLights("GPU")},
}