config format fix and handling fix

multiple alarms supported
This commit is contained in:
Mikkeli Matlock
2026-02-15 22:46:59 +09:00
parent 89c975bf17
commit 22c32f3538
3 changed files with 103 additions and 74 deletions

View File

@@ -47,45 +47,51 @@ def _resolve_path(relative: str) -> Path:
return p
def _prepare_alarm(entry: dict) -> dict:
"""Pre-resolve paths and load resources for a single alarm entry."""
audio_path = find_wav(_resolve_path(entry.get("alarm_audio", "assets/alarm/alarm_test.wav")))
alarm_img_path = _resolve_path(entry.get("alarm_image", "assets/img/on_alarm.png"))
pcm, sr, ch, bits = read_wav(audio_path)
img = load_status_image(alarm_img_path)
return {
"config": entry,
"pcm": pcm, "sr": sr, "ch": ch, "bits": bits,
"img": img,
"last_fired": None,
}
async def handler(ws):
"""Handle a single WebSocket connection."""
remote = ws.remote_address
log.info("Client connected: %s:%d", remote[0], remote[1])
config = load_config(_config_path)
# Resolve audio and image paths from config (or defaults)
if config:
audio_path = find_wav(_resolve_path(config.get("alarm_audio", "assets/alarm/alarm_test.wav")))
alarm_img_path = _resolve_path(config.get("alarm_image", "assets/img/on_alarm.png"))
else:
audio_path = None
alarm_img_path = IMG_DIR / "on_alarm.png"
configs = load_config(_config_path)
img_idle = load_status_image(IMG_DIR / "idle.png")
img_alarm = load_status_image(alarm_img_path)
try:
await send_status_image(ws, img_idle)
if not config:
if not configs:
log.info("No alarms configured — idling forever")
await asyncio.Future()
return
pcm, sr, ch, bits = read_wav(audio_path)
last_fired_minute = None
alarms = [_prepare_alarm(entry) for entry in configs]
while True:
if should_fire(config):
current_minute = datetime.now().strftime("%Y%m%d%H%M")
for alarm in alarms:
if should_fire(alarm["config"]):
current_minute = datetime.now().strftime("%Y%m%d%H%M")
if current_minute != last_fired_minute:
last_fired_minute = current_minute
log.info("Alarm firing at %s", current_minute)
await send_status_image(ws, img_alarm)
await stream_alarm(ws, pcm, sr, ch, bits)
await send_status_image(ws, img_idle)
if current_minute != alarm["last_fired"]:
alarm["last_fired"] = current_minute
log.info("Alarm firing: %s at %s",
alarm["config"]["alarm_time"], current_minute)
await send_status_image(ws, alarm["img"])
await stream_alarm(ws, alarm["pcm"], alarm["sr"],
alarm["ch"], alarm["bits"])
await send_status_image(ws, img_idle)
await asyncio.sleep(TICK_INTERVAL)