feat(server): log every relay pulse and echo it to device_events (kind relay)
One pulse path, pulseBarrier (relay-pulse.ts), for all six callers: entry button/operator mint, reader and booth exits, subscription opens, manual (forgiven) exits and the setup relay test. Each pulse writes an info line naming relay, controller, direction, identity and operator, and inserts a device_events row of kind "relay" for success and failure alike; failures are still re-thrown so each flow keeps its own anomaly handling. Prompted by the 2026-10-09 park-2 "barrier opened by itself" claim: the ledger showed the why, but nothing recorded the moment a relay was actually driven. The signed ledger stays the authority; this is unsigned telemetry. Also git-ignores tmp/ (site snapshots pulled for incident review).
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { randomBytes, randomUUID } from "node:crypto";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { eq, devices, setupState, type Db } from "@parking/db";
|
||||
import { pulseBarrier } from "../relay-pulse.js";
|
||||
import {
|
||||
hasPreconditions,
|
||||
hasPushConfig,
|
||||
@@ -507,7 +508,7 @@ export async function setupRoutes(
|
||||
return reply.code(400).send({ error: `device ${id} is not a controller` });
|
||||
}
|
||||
const cfg = (row.config ?? {}) as Record<string, unknown>;
|
||||
const relays = Array.isArray(cfg.relays) ? (cfg.relays as { relay?: number }[]) : [];
|
||||
const relays = Array.isArray(cfg.relays) ? (cfg.relays as { relay?: number; direction?: string }[]) : [];
|
||||
if (!relays.some((r) => r.relay === relay)) {
|
||||
return reply.code(400).send({ error: `controller ${id} has no relay ${relay}` });
|
||||
}
|
||||
@@ -545,8 +546,14 @@ export async function setupRoutes(
|
||||
});
|
||||
|
||||
const startedAt = Date.now();
|
||||
const relayDir = relays.find((r) => r.relay === relay)?.direction;
|
||||
try {
|
||||
await device.pulseOpen(relay);
|
||||
await pulseBarrier(db, req.log, device, { controller: row, relay }, {
|
||||
direction: relayDir === "entry" || relayDir === "exit" ? relayDir : "both",
|
||||
identity: `relay-test:${id}:${relay}`,
|
||||
source: "relay-test",
|
||||
operator,
|
||||
});
|
||||
} catch (err) {
|
||||
// The failure we're testing for (relay unreachable, wrong password). The open is
|
||||
// already signed; report the pulse failure, don't 500.
|
||||
|
||||
Reference in New Issue
Block a user