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:
@@ -0,0 +1,84 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import type { FastifyBaseLogger } from "fastify";
|
||||
import { deviceEvents as deviceEventsTable, type Db, type DeviceRow } from "@parking/db";
|
||||
import type { AccessControlDevice } from "@parking/devices";
|
||||
import type { Direction } from "./device-resolve.js";
|
||||
|
||||
// The ONE place a barrier relay is pulsed from business logic. Every pulse is logged at
|
||||
// info (relay, controller, who asked and for which identity) and echoed into the UNSIGNED
|
||||
// device_events stream (kind "relay") whether it succeeded or failed — so "did the server
|
||||
// open the barrier at 15:50?" is answerable from `docker logs` or the telemetry table
|
||||
// without reading the ledger. The signed ledger stays the authority on WHY (the
|
||||
// vehicle_entry/exit/anomaly is appended BEFORE the pulse); this is the device-side echo
|
||||
// (wiki/concepts/device-events.md). Failures are recorded, then re-thrown so each flow keeps
|
||||
// its own anomaly/result handling. park-2 2026-10-09: a "barrier opened by itself" claim
|
||||
// took ledger + snapshots to settle because pulses were only logged when they failed.
|
||||
|
||||
export interface PulseContext {
|
||||
/** The barrier's direction (the relay's configured event). */
|
||||
readonly direction: Direction;
|
||||
/** The session/ticket/card/ref the open is for (matches the signed event's identity). */
|
||||
readonly identity: string;
|
||||
/** Who asked: ticket | manual | booth | lpr | subscription | relay-test … (free-form). */
|
||||
readonly source: string;
|
||||
readonly operator?: string;
|
||||
}
|
||||
|
||||
export async function pulseBarrier(
|
||||
db: Db,
|
||||
logger: FastifyBaseLogger,
|
||||
access: Pick<AccessControlDevice, "pulseOpen">,
|
||||
target: { readonly controller: DeviceRow; readonly relay: number },
|
||||
ctx: PulseContext,
|
||||
): Promise<void> {
|
||||
const { controller, relay } = target;
|
||||
const who = ctx.operator ? ` by ${ctx.operator}` : "";
|
||||
logger.info(
|
||||
`relay pulse: ${ctx.direction} relay ${relay} on ${controller.driverId} ${controller.id} ← ${ctx.source} ${ctx.identity}${who}`,
|
||||
);
|
||||
const startedAt = Date.now();
|
||||
const firedAt = new Date(startedAt).toISOString();
|
||||
try {
|
||||
await access.pulseOpen(relay);
|
||||
record(db, logger, controller, relay, ctx, firedAt, { ok: true, tookMs: Date.now() - startedAt });
|
||||
} catch (err) {
|
||||
const error = (err as Error).message;
|
||||
logger.error(`relay pulse FAILED: ${ctx.direction} relay ${relay} on ${controller.id} for ${ctx.identity}: ${error}`);
|
||||
record(db, logger, controller, relay, ctx, firedAt, { ok: false, error, tookMs: Date.now() - startedAt });
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
function record(
|
||||
db: Db,
|
||||
logger: FastifyBaseLogger,
|
||||
controller: DeviceRow,
|
||||
relay: number,
|
||||
ctx: PulseContext,
|
||||
firedAt: string,
|
||||
outcome: { ok: boolean; error?: string; tookMs: number },
|
||||
): void {
|
||||
// Telemetry must never block or fail the open — swallow insert errors (logged).
|
||||
try {
|
||||
db.insert(deviceEventsTable)
|
||||
.values({
|
||||
id: randomUUID(),
|
||||
deviceId: controller.id,
|
||||
category: "access",
|
||||
kind: "relay",
|
||||
detail: {
|
||||
driverId: controller.driverId,
|
||||
relay,
|
||||
direction: ctx.direction,
|
||||
identity: ctx.identity,
|
||||
source: ctx.source,
|
||||
...(ctx.operator ? { operator: ctx.operator } : {}),
|
||||
...outcome,
|
||||
},
|
||||
occurredAt: firedAt,
|
||||
})
|
||||
.run();
|
||||
} catch (err) {
|
||||
logger.error(`relay pulse telemetry insert failed: ${(err as Error).message}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user