b2a0471b08
Add the rongta PrinterDevice driver (ESC/POS over raw TCP 9100) and the device-agnostic pieces around it: - Roles + failover: each printer declares a role (entry-dispenser/booth- receipt) and failoverRank; printer-routing.ts picks the best healthy printer and falls back outside->booth for entry tickets (never the reverse). - Live status: MonitorableDevice.readStatus()/PrinterStatus capability. The Rongta driver scrapes the device's own /prn_stat.htm (Cover/Cutter/Paper End/Near End/Off-Line) rather than hand-decoding DLE EOT, whose reply bytes on this clone don't match the canonical ESC/POS bit layout (verified on hardware) -- avoids a false-healthy. Maps to ready/degraded/offline, fail safe on an unreachable or unexpected page. - Server PrinterMonitor polls enabled printers (PRINTER_POLL_MS, default 5s), caches latest, emits "printer-status" on change. Exposed via GET /api/printers/status and an SSE stream for the booth UI. Verified against 10.0.10.6: ready when healthy, offline when unreachable (no throw), bus emits on change and suppresses unchanged reads. Wiki: new rongta-printer entity, printer-roles-failover and printer-status-monitoring concepts; BOM/index/log updated.
51 lines
1.9 KiB
TypeScript
51 lines
1.9 KiB
TypeScript
import type { FastifyInstance } from "fastify";
|
|
import { requireRole } from "../auth.js";
|
|
import { deviceEvents } from "../device-events.js";
|
|
import type { PrinterMonitor } from "../printer-monitor.js";
|
|
|
|
// Live printer-status API. The PrinterMonitor polls printers in the background;
|
|
// these endpoints expose its cache (snapshot) and a live push stream (SSE) so the
|
|
// booth UI shows paper-out / cover-open / offline in real time. Any authenticated
|
|
// operator may read status (it's operational, not a setup action).
|
|
|
|
export async function printerRoutes(
|
|
app: FastifyInstance,
|
|
monitor: PrinterMonitor,
|
|
): Promise<void> {
|
|
const guard = requireRole("admin", "operator", "cashier", "readonly");
|
|
|
|
// Current status of every monitored printer (cached — no device round-trip).
|
|
app.get("/api/printers/status", { preHandler: guard }, async () => ({
|
|
printers: monitor.snapshot(),
|
|
}));
|
|
|
|
// Live stream: emits the full snapshot on connect, then one event per change.
|
|
// Server-Sent Events — one-way, survives proxies, trivially consumed by the SPA.
|
|
app.get("/api/printers/status/stream", { preHandler: guard }, (req, reply) => {
|
|
reply.raw.writeHead(200, {
|
|
"content-type": "text/event-stream",
|
|
"cache-control": "no-cache",
|
|
connection: "keep-alive",
|
|
});
|
|
|
|
const send = (event: string, data: unknown) => {
|
|
reply.raw.write(`event: ${event}\n`);
|
|
reply.raw.write(`data: ${JSON.stringify(data)}\n\n`);
|
|
};
|
|
|
|
// Initial state so a fresh client doesn't wait for the next change.
|
|
send("snapshot", { printers: monitor.snapshot() });
|
|
|
|
const unsubscribe = deviceEvents.onPrinterStatus((e) => send("status", e));
|
|
|
|
// Heartbeat keeps intermediaries from closing an idle connection.
|
|
const heartbeat = setInterval(() => reply.raw.write(": ping\n\n"), 25000);
|
|
heartbeat.unref?.();
|
|
|
|
req.raw.on("close", () => {
|
|
clearInterval(heartbeat);
|
|
unsubscribe();
|
|
});
|
|
});
|
|
}
|