// Dingtian relay+input hardware test. // // node apps/server/scripts/dingtian-test.mjs # status only (safe) // node apps/server/scripts/dingtian-test.mjs watch # live input/button monitor // node apps/server/scripts/dingtian-test.mjs pulse 1 # pulse relay 1 (prompts) // // Env: DINGTIAN_HOST (default 10.0.10.172), DINGTIAN_PORT (60001). // // SAFETY: `pulse` fires a relay → the barrier may move. It prompts first unless // YES=1. pulseOpen is momentary (the device self-releases). import { createInterface } from "node:readline/promises"; import { stdin, stdout } from "node:process"; import { createRequire } from "node:module"; const require = createRequire(import.meta.url); const { dingtianDriver } = require("@parking/devices"); const host = process.env.DINGTIAN_HOST ?? "10.0.10.172"; const port = process.env.DINGTIAN_PORT ? Number(process.env.DINGTIAN_PORT) : 60001; const dev = dingtianDriver.create({ host, port, channels: 4 }); const mode = process.argv[2] ?? "status"; console.log(`dingtian @ ${host}:${port}\n`); async function showStatus() { const health = await dev.healthCheck(); console.log("health:", JSON.stringify(health)); const inputs = await dev.readInputs(); console.log("inputs (active=pressed):", inputs.map((v, i) => `in${i + 1}=${v ? "ON" : "off"}`).join(" ")); for (let ch = 1; ch <= 4; ch++) { console.log(`relay ${ch}:`, await dev.getDoorStatus(ch)); } } if (mode === "status") { await showStatus(); process.exit(0); } if (mode === "watch") { console.log("── press the buttons on the inputs — Ctrl-C to stop ──\n"); const unsub = dev.onInput((e) => { console.log(`[${e.at}] input ${e.input} ${e.edge.toUpperCase()}`); }); process.on("SIGINT", () => { unsub(); console.log("\nstopped."); process.exit(0); }); // keep alive await new Promise(() => {}); } if (mode === "pulse") { const ch = Number(process.argv[3] ?? 1); if (process.env.YES !== "1") { const rl = createInterface({ input: stdin, output: stdout }); const ans = (await rl.question(`Pulse relay ${ch}? (barrier may move) [y/N] `)).trim(); rl.close(); if (ans.toLowerCase() !== "y") { console.log("aborted."); process.exit(0); } } await dev.pulseOpen(ch); console.log(`pulsed relay ${ch}.`); // show the relay state right after (likely back off — pulse is momentary) setTimeout(async () => { console.log(`relay ${ch} now:`, await dev.getDoorStatus(ch)); process.exit(0); }, 300); }