feat(booth): blink the Entry/Exit lights on radar presence (mirror relay 3)
Build desktop / desktop (push) Successful in 4m14s
Build & push images / images (push) Successful in 2m42s
CI / check (push) Successful in 37s

The on-screen Hyrje/Dalje barrier lights were 2-state (green=free / red=busy)
off the camera lane-status only — they couldn't show the radar-only "detected,
not yet confirmed" state that makes the physical button lamp (relay 3) blink.
Now they mirror the lamp's 3-state rule per lane:
  radar present + camera not busy → BLINK green↔red (~1 Hz)
  camera busy                     → SOLID red
  otherwise                       → SOLID green

End-to-end:
- LanePresence (lane-presence.ts): subscribes to deviceEvents.onInput, resolves
  each presence edge to its lane via the new direction-agnostic presenceLaneOf()
  (device-resolve.ts) — entry AND exit, unlike the entry-gated relayForPresence
  the one-car-one-ticket gate uses — and emits a lane-presence {entry,exit} bus
  event on change. Wired in server.ts (start + onClose).
- WS forwards it (hello snapshot + push) into live-store.radar.
- BarrierLight (BoothScreen.tsx) is now 3-state; blinks via the .lane-blink
  keyframe (index.css), which holds solid-red under prefers-reduced-motion.

Same input + same rule as the lamp, so the screen and the post never disagree.

A new test (lane-presence.test.ts) caught a real bug: the first cut reused
relayForPresence, so the EXIT lane never resolved (it's entry-gated) and never
blinked — presenceLaneOf fixes it. Covers entry/exit independence, de-dupe
across several radars on one lane, and ignoring non-presence inputs.

Full workspace build/lint/test green (185 server tests). Updated the
button-light-indicator wiki page ("On-screen twin").

Claude-Session: https://claude.ai/code/session_01Xcm6ikLgGoCxxHrxtjkk5V
This commit is contained in:
2026-06-28 11:48:12 +02:00
parent 4418594af0
commit 38481f105f
12 changed files with 378 additions and 24 deletions
+61
View File
@@ -0,0 +1,61 @@
import type { Db } from "@parking/db";
import type { FastifyBaseLogger } from "fastify";
import { deviceEvents, type DeviceInputEvent, type LanePresenceEvent } from "./device-events.js";
import { presenceLaneOf } from "./device-resolve.js";
// Per-lane RADAR presence for the booth's barrier lights. A vehicle-presence INPUT
// (loop/radar) shorted at an entry/exit barrier means "something is in the lane vicinity"
// BEFORE the camera confirms a vehicle. This is the SAME signal that makes the physical
// button lamp (relay 3) blink — see button-light.ts (#onInput) — so the on-screen light
// and the lamp stay in lockstep: both react to a presence edge resolved the SAME way
// (relayForPresence, on an entry/both relay). ADVISORY ONLY: it gates nothing.
//
// A radar serving an entry (or "both") barrier marks the ENTRY lane present; an exit radar
// marks EXIT. The lane is resolved via `presenceLaneOf` (direction-agnostic — unlike the
// entry-gated `relayForPresence` the one-car-one-ticket gate uses), so both lanes blink.
export class LanePresence {
readonly #db: Db;
readonly #logger: FastifyBaseLogger;
/** Active presence terminals per lane, keyed `${deviceId}:${input}` (several radars may
* serve one lane). A lane is "present" while its set is non-empty. */
readonly #entry = new Set<string>();
readonly #exit = new Set<string>();
#unsub: (() => void) | null = null;
constructor(db: Db, logger: FastifyBaseLogger) {
this.#db = db;
this.#logger = logger;
}
/** Subscribe to presence input edges. */
start(): void {
this.#unsub = deviceEvents.onInput((e) => this.#onInput(e));
}
/** Current snapshot (for the WS hello). */
snapshot(): LanePresenceEvent {
return { entry: this.#entry.size > 0, exit: this.#exit.size > 0 };
}
#onInput(e: DeviceInputEvent): void {
const lane = presenceLaneOf(this.#db, e.deviceId, e.input);
if (!lane) return; // not a presence terminal on a barrier relay
const key = `${e.deviceId}:${e.input}`;
const set = lane === "entry" ? this.#entry : this.#exit;
const before = set.size > 0;
if (e.edge === "on") set.add(key);
else set.delete(key);
const after = set.size > 0;
if (before !== after) {
this.#logger.info(`lane-presence: ${lane} -> ${after ? "present" : "clear"}`);
deviceEvents.emitLanePresence(this.snapshot());
}
}
/** Unsubscribe on shutdown. */
stop(): void {
this.#unsub?.();
this.#unsub = null;
}
}