c08e232339
A NodeMCU v3 (ESP8266) that speaks the Dingtian wire contract (binary UDP 60000 with relay_pw, string UDP 60001, config CGI incl. the setconfig gotcha, userset.cgi, Digest input push) so the booth's existing driver needs no change, plus a heartbeat/command/ watchdog path to an ephemeral cloud stand-in (poc/cloud-standin.mjs). Verified at park-lab: the booth's assign flow configured it like real hardware and a button press reached the booth (401→Digest→200) and the cloud with the booth's status. Watchdog probes /health and power-cycles the PC via a spare relay with backoff. IO behind one interface for the ESP32/ Dingtian port. wiki: relay-proxy-firmware page; esp32-flashability-test runbook (esptool lock test); dingtian-relay "can we flash our own firmware" (vendor locks the standard SKU); site-device-installation: booth ufw must allow device-net → 3000 (push targets PORT, not Caddy's 80); open-questions: cloud takeover vs signed ledger (parked). .gitignore: firmware venv/.pio/secrets.h.
81 lines
3.5 KiB
JavaScript
81 lines
3.5 KiB
JavaScript
#!/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)`));
|