// Driver registry — the catalog of selectable device drivers. // // This is what makes the system admin-configurable: each category (access / // reader / camera / printer) has multiple drivers, and the first-run setup UI // reads this catalog to let the admin pick one per lane and fill in its config. // Adding support for a new device = registering one more driver here; no // business-logic changes. See wiki/concepts/device-registry.md. import type { AccessControlDevice, CameraDevice, Device, DeviceCategory, PrinterDevice, ReaderDevice, } from "./interfaces.js"; /** A single configurable connection field shown in the setup wizard. */ export interface ConfigField { readonly key: string; readonly label: string; readonly type: "string" | "number" | "boolean" | "host" | "port" | "secret" | "select"; readonly required: boolean; readonly default?: string | number | boolean; /** For type "select". */ readonly options?: readonly { value: string; label: string }[]; readonly help?: string; } /** Opaque per-instance config the admin fills in (host, port, credentials…). */ export type DeviceConfig = Record; /** * A driver: metadata describing a supported device model/family, the config * fields the admin must supply, and a factory that builds a live adapter. */ export interface DeviceDriver { readonly id: string; // stable, e.g. "dingtian", "hikvision" readonly category: DeviceCategory; readonly label: string; // human name for the picker, e.g. "Dingtian relay controller" readonly description: string; /** Transports/notes surfaced in the UI, e.g. ["tcp-ip"], ["wiegand"]. */ readonly transports: readonly string[]; readonly configFields: readonly ConfigField[]; /** Build a live adapter instance from validated config. */ create(config: DeviceConfig): T; } export type AccessDriver = DeviceDriver; export type ReaderDriver = DeviceDriver; export type CameraDriver = DeviceDriver; export type PrinterDriver = DeviceDriver; /** A device found on the LAN by a driver's discovery scan. */ export interface DiscoveredDevice { /** Identifier to pre-fill (e.g. a serial number). */ readonly id: string; readonly label: string; /** Config values to auto-fill into the setup form (host, serial, …). */ readonly config: DeviceConfig; /** Extra info to show the admin (firmware, MAC, netmask, …). */ readonly info?: Record; } /** * Optional capability: a driver that can find devices on the LAN (e.g. UDP * broadcast discovery). No bundled driver implements this yet — the Dingtian * board uses a fixed IP; cameras (ONVIF) or other UDP-discoverable devices may * add it later. See wiki/concepts/device-discovery.md. */ export interface DiscoverableDriver { discover(): Promise; } /** Type guard: does this driver support discovery? */ export function isDiscoverable( driver: DeviceDriver, ): driver is DeviceDriver & DiscoverableDriver { return typeof (driver as Partial).discover === "function"; } class DeviceRegistry { readonly #drivers = new Map(); register(driver: DeviceDriver): void { if (this.#drivers.has(driver.id)) { throw new Error(`duplicate driver id: ${driver.id}`); } this.#drivers.set(driver.id, driver); } /** All drivers, optionally filtered by category (used by the setup catalog). */ list(category?: DeviceCategory): DeviceDriver[] { const all = [...this.#drivers.values()]; return category ? all.filter((d) => d.category === category) : all; } get(id: string): DeviceDriver | undefined { return this.#drivers.get(id); } /** Validate config against a driver's declared fields and build the adapter. */ create(id: string, config: DeviceConfig): Device { const driver = this.#drivers.get(id); if (!driver) throw new Error(`unknown driver: ${id}`); for (const field of driver.configFields) { if (field.required && config[field.key] === undefined) { throw new Error(`driver ${id}: missing required config "${field.key}"`); } } return driver.create(config); } /** Catalog payload for the setup UI — drivers grouped by category, no secrets. */ catalog() { const byCategory: Record = { access: [], reader: [], camera: [], printer: [], }; for (const d of this.#drivers.values()) { byCategory[d.category].push({ id: d.id, label: d.label, description: d.description, transports: d.transports, configFields: d.configFields, }); } return byCategory; } } export interface CatalogEntry { readonly id: string; readonly label: string; readonly description: string; readonly transports: readonly string[]; readonly configFields: readonly ConfigField[]; } /** Singleton registry. Drivers self-register on import (see ./drivers). */ export const registry = new DeviceRegistry();