#!/usr/bin/env node // EPHEMERAL cloud stand-in for the relay-proxy PoC. Not a product component. // Logs heartbeats/events from the device and lets you queue commands with curl. // // node poc/cloud-standin.mjs (listens on 0.0.0.0:9099) // curl http://localhost:9099/ -> last heartbeat per device // curl -X POST http://localhost:9099/fire/1 -> pulse relay 1 (500 ms) // curl -X POST "http://localhost:9099/fire/1?ms=1000" // curl -X POST http://localhost:9099/watchdog/off -> disable the PC watchdog // curl -X POST http://localhost:9099/reboot-pc -> power-cycle the PC relay import { createServer } from "node:http"; const PORT = Number(process.env.PORT ?? 9099); const last = new Map(); // dev -> last heartbeat const queue = []; // commands, oldest first (dev field optional) const stamp = () => new Date().toISOString().slice(11, 19); function body(req) { return new Promise((res) => { let s = ""; req.on("data", (c) => (s += c)); req.on("end", () => { try { res(s ? JSON.parse(s) : {}); } catch { res({ raw: s }); } }); }); } const json = (res, code, obj) => { res.writeHead(code, { "content-type": "application/json" }); res.end(JSON.stringify(obj)); }; createServer(async (req, res) => { const url = new URL(req.url, "http://x"); const p = url.pathname; if (req.method === "POST" && p === "/heartbeat") { const hb = await body(req); const prev = last.get(hb.dev); last.set(hb.dev, { ...hb, at: new Date().toISOString() }); const flip = prev && prev.booth_ok !== hb.booth_ok ? ` <-- booth ${hb.booth_ok ? "RECOVERED" : "DOWN"}` : ""; console.log(`${stamp()} hb ${hb.dev} ip=${hb.ip} booth=${hb.booth_ok ? "up" : "DOWN"}(${hb.booth_code}) fails=${hb.booth_fails} wd=${hb.watchdog} resets=${hb.pc_resets} relays=${hb.relays} inputs=${hb.inputs} pw=${hb.relay_pw_set} push=${hb.push_en} link=${hb.link_relay} udp2=${hb.string_proto} heap=${hb.heap}${flip}`); return json(res, 200, { ok: true }); } if (req.method === "POST" && p === "/event") { const ev = await body(req); console.log(`${stamp()} EVENT ${ev.dev} ${ev.type} input=${ev.input} active=${ev.active} booth_status=${ev.booth_status}`); return json(res, 200, { ok: true }); } if (req.method === "GET" && p === "/command") { const dev = url.searchParams.get("dev"); const i = queue.findIndex((c) => !c.dev || c.dev === dev); if (i < 0) return json(res, 200, {}); const [cmd] = queue.splice(i, 1); console.log(`${stamp()} -> ${dev} command ${JSON.stringify(cmd)}`); return json(res, 200, cmd); } if (req.method === "POST" && p.startsWith("/fire/")) { const ch = Number(p.split("/")[2]); const ms = Number(url.searchParams.get("ms") ?? 500); queue.push({ pulse: ch, ms }); console.log(`${stamp()} queued pulse relay ${ch} ${ms} ms`); return json(res, 200, { queued: true }); } if (req.method === "POST" && p.startsWith("/watchdog/")) { const on = p.endsWith("/on"); queue.push({ watchdog: on }); console.log(`${stamp()} queued watchdog ${on ? "on" : "off"}`); return json(res, 200, { queued: true }); } if (req.method === "POST" && p === "/reboot-pc") { queue.push({ reboot_pc: true }); console.log(`${stamp()} queued PC power-cycle`); return json(res, 200, { queued: true }); } if (req.method === "GET" && p === "/") { return json(res, 200, { devices: Object.fromEntries(last), queued: queue }); } json(res, 404, { error: "not found" }); }).listen(PORT, "0.0.0.0", () => console.log(`cloud stand-in on :${PORT} (PoC only)`));