Files
parking_solution/firmware/relay-proxy/src/config_store.cpp
T
julian c08e232339 feat(firmware): relay-proxy PoC — NodeMCU as a Dingtian DT-R004 + cloud proxy/watchdog
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.
2026-10-22 12:08:20 +02:00

91 lines
2.8 KiB
C++

#include "config_store.h"
#include <LittleFS.h>
#include "io.h"
static const char* CFG_PATH = "/config.json";
static const char* WEB_PATH = "/web.json";
void ConfigStore::makeDefaults() {
cfg.clear();
cfg["status"] = 0;
JsonObject rc = cfg["relay_connect"].to<JsonObject>();
rc["relay_pw"] = 0;
// Factory: udp1 = binary, udp2 = string (password-less!), everything else off.
rc["udp1"]["p"] = 1; rc["udp1"]["port"] = 60000;
rc["udp2"]["p"] = 0; rc["udp2"]["port"] = 60001;
rc["rs485"]["p"] = 255; rc["can"]["p"] = 255;
rc["tcpc"]["p"] = 255; rc["tcps"]["p"] = 255;
rc["mqtt"]["p"] = 255;
// Factory: every input auto-fires its matching relay (the UHPPOTE-style gotcha
// the booth's precondition fix turns off).
JsonObject ilr = cfg["input_link_relay"].to<JsonObject>();
ilr["input_link_relay"] = 1;
JsonArray oa = ilr["on_action_on"].to<JsonArray>();
for (int i = 0; i < CHANNELS; i++) oa.add<JsonArray>().add(i);
JsonObject ilu = cfg["input_link_url"].to<JsonObject>();
ilu["cnt"] = CHANNELS;
ilu["en"] = 0;
const char* intKeys[] = {"active_level", "tls", "auth", "on_method", "off_method"};
for (const char* k : intKeys) {
JsonArray a = ilu[k].to<JsonArray>();
for (int i = 0; i < CHANNELS; i++) a.add(0);
}
{
JsonArray a = ilu["port"].to<JsonArray>();
for (int i = 0; i < CHANNELS; i++) a.add(80);
}
const char* strKeys[] = {"server", "user", "pass", "on_path", "off_path", "on_body", "off_body"};
for (const char* k : strKeys) {
JsonArray a = ilu[k].to<JsonArray>();
for (int i = 0; i < CHANNELS; i++) a.add("");
}
}
void ConfigStore::begin() {
LittleFS.begin();
bool ok = false;
File f = LittleFS.open(CFG_PATH, "r");
if (f) {
ok = deserializeJson(cfg, f) == DeserializationError::Ok;
f.close();
}
if (!ok || !cfg["relay_connect"].is<JsonObject>()) {
makeDefaults();
save();
}
webUser = "admin";
webPass = "admin";
File w = LittleFS.open(WEB_PATH, "r");
if (w) {
JsonDocument d;
if (deserializeJson(d, w) == DeserializationError::Ok) {
webUser = d["user"] | "admin";
webPass = d["pass"] | "admin";
}
w.close();
}
}
bool ConfigStore::save() {
File f = LittleFS.open(CFG_PATH, "w");
if (!f) return false;
serializeJson(cfg, f);
f.close();
return true;
}
bool ConfigStore::saveWeb() {
JsonDocument d;
d["user"] = webUser;
d["pass"] = webPass;
File f = LittleFS.open(WEB_PATH, "w");
if (!f) return false;
serializeJson(d, f);
f.close();
return true;
}
uint16_t ConfigStore::relayPw() const { return cfg["relay_connect"]["relay_pw"] | 0; }
bool ConfigStore::stringProtoEnabled() const { return (cfg["relay_connect"]["udp2"]["p"] | 255) != 255; }
bool ConfigStore::linkRelayEnabled() const { return (cfg["input_link_relay"]["input_link_relay"] | 0) == 1; }