Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
05241c15dThis file is pinned to the selected release. Download the original to open it in your design tool.
/**
* The single source of truth for "what state is the arcade in, and what can the user do".
*
* Three failure domains feed this (see useSystemStatus): the viewer's own LiveKit socket, the Pi
* control bridge (`pi-control` — owns the queue + arms + is the only thing that can physically park
* an arm), and the camera publisher. This module is pure (no React) so the precedence + gating logic
* is testable in isolation; the hook layers on the live signals, timers and debounce.
*
* Rule from the product owner: every non-LIVE status pauses the queue, and on any degradation the
* bridge parks the active arm + persists the queue, resuming assignment when the system is LIVE again.
*/
/** Ordered least→most severe. Precedence is "most severe wins" when several conditions hold at once. */
export type SystemStatus =
| "LIVE"
| "CAMERAS_DEGRADED"
| "ARMS_OFFLINE"
| "MAINTENANCE"
| "BRIDGE_OFFLINE"
| "RECONNECTING"
| "OFFLINE";
/** Actions a surface may offer that a status can gate. */
export type StatusAction = "join" | "leave" | "drive" | "pinCamera" | "watch";
export type StatusTone = "green" | "amber" | "red" | "magenta" | "cyan";
export type StatusMeta = {
/** precedence rank — higher wins when multiple raw conditions are true simultaneously */
severity: number;
/** short marquee word */
label: string;
/** one-line caption (default; the hook may specialize it per role/variant) */
caption: string;
tone: StatusTone;
/** actions blocked while in this state */
disabled: StatusAction[];
/** every non-LIVE status pauses the queue */
queuePaused: boolean;
/**
* ms the raw signal must persist before the UI COMMITS to this state (asymmetric debounce).
* Degrade-fast where a false "healthy" is dangerous, degrade-slow where a false alarm just annoys;
* entering LIVE is deliberately unhurried so a recovering-but-flapping system doesn't strobe.
*/
enterDelayMs: number;
};
const GATE_ALL: StatusAction[] = ["join", "leave", "drive", "pinCamera"];
const GATE_START: StatusAction[] = ["join", "drive", "pinCamera"];
export const STATUS_META: Record<SystemStatus, StatusMeta> = {
LIVE: {
severity: 0,
label: "LIVE",
caption: "arms controllable",
tone: "green",
disabled: [],
queuePaused: false,
enterDelayMs: 1000,
},
CAMERAS_DEGRADED: {
severity: 1,
label: "LOW SIGNAL",
caption: "need 3+ camera feeds to drive",
tone: "amber",
disabled: GATE_START,
queuePaused: true,
enterDelayMs: 3000,
},
ARMS_OFFLINE: {
severity: 2,
label: "ARMS RESTING",
caption: "robots offline",
tone: "amber",
disabled: GATE_START,
queuePaused: true,
enterDelayMs: 1500,
},
// Admin pause is intentional downtime — calmer copy, and it wins over ARMS_OFFLINE so the cause
// ("we paused it") shows instead of the symptom ("arms offline").
MAINTENANCE: {
severity: 3,
label: "BE RIGHT BACK",
caption: "paused for maintenance",
tone: "amber",
disabled: GATE_START,
queuePaused: true,
enterDelayMs: 500,
},
BRIDGE_OFFLINE: {
severity: 4,
label: "CONTROL OFFLINE",
caption: "reconnecting to the robot",
tone: "red",
disabled: GATE_ALL,
queuePaused: true,
enterDelayMs: 2000,
},
RECONNECTING: {
severity: 5,
label: "SYNC",
caption: "reconnecting your link",
tone: "cyan",
disabled: GATE_ALL,
queuePaused: true,
enterDelayMs: 1500,
},
OFFLINE: {
severity: 6,
label: "OFFLINE",
caption: "connection lost",
tone: "magenta",
disabled: [...GATE_ALL, "watch"],
queuePaused: true,
enterDelayMs: 0,
},
};
/** OFFLINE variants distinguished by the token route's 503 reason: "demo" = LIVEKIT_* creds missing
* (local dev), "offair" = the ARMCADE_OFFLINE kill switch (deliberate downtime — public-friendly
* copy, and the client never connects so LiveKit/Vercel cost nothing while it's set). */
export type OfflineReason = "demo" | "offair";
/** How many operational camera feeds are required before an arm is drivable. */
export const REQUIRED_CAMS = 3;
/** A bridge broadcast older than this (or a bridge that never spoke within this of connecting) is stale. */
export const BRIDGE_STALE_MS = 3000;
export type StatusInputs = {
/** RoomProvider connection status for THIS browser's socket. "dormant" = deliberately
* disconnected because the rig has been gone a while (idle-cost saver, not a fault). */
conn: "idle" | "connecting" | "connected" | "unconfigured" | "dormant";
/** which flavor of unconfigured (from the token route's 503 body); undefined defaults to "demo" */
offlineVariant?: OfflineReason;
/** is `pi-control` a remote participant right now */
bridgePresent: boolean;
/**
* ms since the last `session` broadcast. When connected but the bridge has never broadcast, the
* hook passes time-since-connect here, so a bridge that stays silent past BRIDGE_STALE_MS reads as
* offline. `null` only while not connected (peer health is then unknowable and moot).
*/
sessionAgeMs: number | null;
/** camera feeds that are present AND flowing frames (frozen/absent feeds excluded) */
operationalCams: number;
/** have we received any arm info yet (before that, arm health is unknown, not "offline") */
armsKnown: boolean;
/** at least one arm answered its liveness probe (per the owner: ≥1 online ⇒ arms are online) */
anyArmOnline: boolean;
/** bridge echoed an admin pause (robots and/or cameras) */
paused: boolean;
};
/**
* The raw (pre-debounce) status from the fused signals, most-severe-first. The ordering IS the
* precedence: a down/silent socket dominates everything (we can't observe peers without it); a dead
* bridge invalidates all arm/camera claims (no lock brain ⇒ nothing commandable); admin pause is an
* authoritative cause; then "nothing to control" (arms) over "can't see well enough" (cameras).
*/
export function deriveRawStatus(i: StatusInputs): {
status: SystemStatus;
offlineReason?: OfflineReason;
} {
if (i.conn === "unconfigured") return { status: "OFFLINE", offlineReason: i.offlineVariant ?? "demo" };
// Dormant = we hung up on an empty room to stop the LiveKit meter; the CAUSE is still the robot
// side being gone, so keep reading BRIDGE_OFFLINE (not RECONNECTING — nothing is retrying hard).
if (i.conn === "dormant") return { status: "BRIDGE_OFFLINE" };
if (i.conn !== "connected") return { status: "RECONNECTING" };
// connected — now peer health is observable
if (!i.bridgePresent) return { status: "BRIDGE_OFFLINE" };
if (i.sessionAgeMs != null && i.sessionAgeMs > BRIDGE_STALE_MS) return { status: "BRIDGE_OFFLINE" };
if (i.paused) return { status: "MAINTENANCE" };
if (i.armsKnown && !i.anyArmOnline) return { status: "ARMS_OFFLINE" };
if (i.operationalCams < REQUIRED_CAMS) return { status: "CAMERAS_DEGRADED" };
return { status: "LIVE" };
}
export function isDisabled(status: SystemStatus, action: StatusAction): boolean {
return STATUS_META[status].disabled.includes(action);
}