#include "config_store.h" #include #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(); 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(); ilr["input_link_relay"] = 1; JsonArray oa = ilr["on_action_on"].to(); for (int i = 0; i < CHANNELS; i++) oa.add().add(i); JsonObject ilu = cfg["input_link_url"].to(); 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(); for (int i = 0; i < CHANNELS; i++) a.add(0); } { JsonArray a = ilu["port"].to(); 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(); 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()) { 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; }