server: GEE/Dingtian QR reader endpoint + synchronous ReadOutcome

The reader HTTP-GETs on each scan and beeps/acts on our JSON reply (host-in-the-
loop, synchronous). New route GET/POST /qa/mcardsea.php parses the SDK query,
runs the scan through the read dispatcher (permit match -> permit flow; else
transient exit), and replies the SDK verdict: status 1=valid (beep 2x) /
0=invalid (beep 1x), output, time-sync.

Refactored the read flows to return a ReadOutcome {accepted, direction, reason}
so the reply reflects the real accept/reject decision (ReadDispatcher.dispatch,
ExitFlow.handleAt, PermitFlow.run). Fire-and-forget readers ignore it.

Reader's lane is keyed off its serial (cjihao) as lane_devices.id for now;
endpoint is public (reader has no auth, on the device subnet).

Verified via inject: valid permit QR -> status:1 + open; re-scan -> permit exit;
unknown QR -> status:0; barrier-less lane -> status:0.
This commit is contained in:
2026-06-16 12:12:09 +02:00
parent f67c1ead87
commit 392d44d842
8 changed files with 173 additions and 30 deletions
+83
View File
@@ -0,0 +1,83 @@
import type { FastifyInstance } from "fastify";
import type { DeviceReadEvent } from "../device-events.js";
import type { ReadDispatcher } from "../read-dispatch.js";
// GEE/Dingtian QR reader endpoint. The reader is configured (vendor tool) with our
// host as its "server"; on each scan it sends an HTTP GET and BEEPS/acts based on
// our JSON reply — host-in-the-loop and synchronous. Protocol from the QRCode SDK
// v1.6.5; see wiki/sources/qrcode-sdk.md and wiki/entities/gee-qr-er80.md.
//
// reader → GET /qa/mcardsea.php?cardid=<QR>&mjihao=<devId>&cjihao=<devSN>&status=<2ch>&time=<utc>
// server → {"data":[{cardid,cjihao,mjihao,status,time,output}],"code":0,"message":""}
// reply status: 1 = valid (beep 2×) / 0 = invalid (beep 1×)
// reply output: 0 = Access, 1 = WG26, 2 = WG34 (line driven on a valid read)
// reply time: UTC — syncs the device clock
//
// The "server language" set on the device only selects this URL path; we accept the
// SDK default path. No auth on the device side (it can't); the reader sits on the
// device subnet (network-isolation) and the signed ledger is the real guarantee.
interface ReaderQuery {
cardid?: string;
mjihao?: string; // device id
cjihao?: string; // device serial
status?: string; // 2 chars: high valid/invalid, low 1=in/0=out
time?: string;
}
const SDK_PATH = "/qa/mcardsea.php";
export async function qrReaderRoutes(app: FastifyInstance, dispatcher: ReadDispatcher): Promise<void> {
// No auth: the reader is a machine on the isolated device subnet and offers no
// auth on its side. Public route, like the Dingtian input push.
const handler = async (req: { query: ReaderQuery }) => {
const q = req.query;
const cardid = (q.cardid ?? "").trim();
const mjihao = q.mjihao != null ? Number(q.mjihao) : 0;
// The device id we map to a lane is the configured reader's lane_devices id.
// The reader sends its own mjihao/cjihao; the admin records that as the device's
// config so we can resolve it. For now we key the read on the device serial
// (cjihao) as the lane_devices id — see wiki note; refine when assignment lands.
const deviceId = (q.cjihao ?? "").trim() || String(mjihao);
let accepted = false;
if (cardid) {
const read: DeviceReadEvent = {
driverId: "gee-qr-er80",
deviceId,
value: cardid,
kind: "qr",
at: new Date().toISOString(),
};
try {
const outcome = await dispatcher.dispatch(read);
accepted = outcome.accepted;
if (!accepted) app.log.info(`QR ${cardid} rejected: ${outcome.reason ?? "?"}`);
} catch (err) {
app.log.error(`QR dispatch failed for ${cardid}: ${(err as Error).message}`);
}
}
// Reply the SDK verdict. status 1 → beep 2× (valid) / 0 → beep 1× (invalid).
// output 0 = Access (drive the reader's access line on a valid read).
return {
data: [
{
cardid,
cjihao: q.cjihao ?? 0,
mjihao,
status: accepted ? 1 : 0,
time: String(Math.floor(Date.now() / 1000)),
output: 0,
},
],
code: 0,
message: "",
};
};
// The reader uses GET; accept POST too in case a variant differs.
app.get<{ Querystring: ReaderQuery }>(SDK_PATH, handler);
app.post<{ Querystring: ReaderQuery }>(SDK_PATH, handler);
}