import { readFile } from "node:fs/promises"; import type Database from "better-sqlite3"; import { upsertDevices, type DiscoveredDevice } from "../db/index.js"; interface RawEntry { ip: string; mac: string; state: string; } // Reads the JSON file produced by scripts/discover-devices.sh (a systemd timer // on the deploy host, not this process) and labels each entry against the // known-device list from config/hosts.yaml. See docs/device-discovery.md. export async function refreshDevices( db: Database.Database, filePath: string, knownDevices: Map ): Promise { let raw: RawEntry[]; try { raw = JSON.parse(await readFile(filePath, "utf8")); } catch (err) { // Missing file (timer hasn't run yet, or not deployed on this host) isn't // fatal — just means no device data this cycle. if ((err as NodeJS.ErrnoException).code === "ENOENT") return; throw err; } const devices: DiscoveredDevice[] = raw.map((entry) => ({ ip: entry.ip, mac: entry.mac, knownName: knownDevices.get(entry.ip) ?? null, })); upsertDevices(db, devices); }