199d0da675
New seen_macs table: permanent, insert-only, MAC-keyed record of the first time each device was ever seen -- deliberately decoupled from devices.first_seen (IP-keyed, would false-positive on every DHCP lease change). Bootstrap-safe: first call seeds the baseline from whatever's currently on the network without alerting on all 71+ existing devices at once. Verified locally: bootstrap call reports nothing new, repeat calls with the same MACs report nothing new, one genuinely new MAC gets reported exactly once. Pushes a Home Assistant persistent_notification when a new MAC appears (gated behind HOME_ASSISTANT_TOKEN + homeAssistant.url in hosts.yaml -- missing config just means no push, detection still runs). Also surfaced directly in the dashboard as a blue "new" badge for anything first seen in the last 24h, independent of HA config. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
63 lines
2.1 KiB
TypeScript
63 lines
2.1 KiB
TypeScript
import { readFile } from "node:fs/promises";
|
|
import type Database from "better-sqlite3";
|
|
import { upsertDevices, recordSeenMacs, type DiscoveredDevice } from "../db/index.js";
|
|
import { notifyNewDevices, type HomeAssistantConfig, type NewDeviceInfo } from "./homeAssistant.js";
|
|
// mac-oui-lookup is CommonJS; see routes/devices.ts for why this needs the
|
|
// default-import-and-destructure form instead of a named import.
|
|
import macOuiLookup from "mac-oui-lookup";
|
|
const { getVendor } = macOuiLookup;
|
|
|
|
interface RawEntry {
|
|
ip: string;
|
|
mac: string;
|
|
state: string;
|
|
mdnsHostname?: 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<string, string>,
|
|
homeAssistant: HomeAssistantConfig | undefined,
|
|
onNotifyError: (err: unknown) => void
|
|
): Promise<void> {
|
|
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,
|
|
mdnsHostname: entry.mdnsHostname ?? null,
|
|
}));
|
|
|
|
upsertDevices(db, devices);
|
|
|
|
const newMacs = new Set(recordSeenMacs(db, raw.map((r) => r.mac)));
|
|
if (newMacs.size > 0 && homeAssistant) {
|
|
const newDevices: NewDeviceInfo[] = raw
|
|
.filter((r) => newMacs.has(r.mac))
|
|
.map((r) => ({
|
|
ip: r.ip,
|
|
mac: r.mac,
|
|
vendor: getVendor(r.mac),
|
|
name: knownDevices.get(r.ip) ?? r.mdnsHostname ?? null,
|
|
}));
|
|
try {
|
|
await notifyNewDevices(homeAssistant, newDevices);
|
|
} catch (err) {
|
|
onNotifyError(err);
|
|
}
|
|
}
|
|
}
|