Websocket Fix, Added Clock NTP Sync, Updated MQTT IP

This commit is contained in:
2025-12-26 09:33:24 +02:00
parent b04590d270
commit 7d9bc42078
6 changed files with 193 additions and 56 deletions

View File

@@ -79,7 +79,13 @@ bool ConfigManager::begin() {
LOG_WARNING("ConfigManager - ⚠️ Creating default network config file");
saveNetworkConfig();
}
// Load time config, save defaults if not found
if (!loadTimeConfig()) {
LOG_WARNING("ConfigManager - ⚠️ Creating default time config file (GMT+2)");
saveTimeConfig();
}
// Load bell durations, save defaults if not found
if (!loadBellDurations()) {
LOG_WARNING("ConfigManager - ⚠️ Creating default bell durations file");
@@ -580,48 +586,50 @@ void ConfigManager::updateClockSilence(JsonVariant doc) {
bool ConfigManager::loadClockConfig() {
if (!ensureSDCard()) return false;
File file = SD.open("/settings/clockConfig.json", FILE_READ);
if (!file) {
LOG_WARNING("ConfigManager - ⚠️ Clock config file not found - using defaults");
return false;
}
StaticJsonDocument<512> doc;
DeserializationError error = deserializeJson(doc, file);
file.close();
if (error) {
LOG_ERROR("ConfigManager - ❌ Failed to parse clock config from SD: %s", error.c_str());
return false;
}
if (doc.containsKey("enabled")) clockConfig.enabled = doc["enabled"].as<bool>();
if (doc.containsKey("c1output")) clockConfig.c1output = doc["c1output"].as<uint8_t>();
if (doc.containsKey("c2output")) clockConfig.c2output = doc["c2output"].as<uint8_t>();
if (doc.containsKey("pulseDuration")) clockConfig.pulseDuration = doc["pulseDuration"].as<uint16_t>();
if (doc.containsKey("pauseDuration")) clockConfig.pauseDuration = doc["pauseDuration"].as<uint16_t>();
LOG_DEBUG("ConfigManager - Clock config loaded");
return true;
}
bool ConfigManager::saveClockConfig() {
if (!ensureSDCard()) return false;
StaticJsonDocument<512> doc;
doc["enabled"] = clockConfig.enabled;
doc["c1output"] = clockConfig.c1output;
doc["c2output"] = clockConfig.c2output;
doc["pulseDuration"] = clockConfig.pulseDuration;
doc["pauseDuration"] = clockConfig.pauseDuration;
char buffer[512];
size_t len = serializeJson(doc, buffer, sizeof(buffer));
if (len == 0 || len >= sizeof(buffer)) {
LOG_ERROR("ConfigManager - ❌ Failed to serialize clock config JSON");
return false;
}
saveFileToSD("/settings", "clockConfig.json", buffer);
LOG_DEBUG("ConfigManager - Clock config saved");
return true;
@@ -727,8 +735,8 @@ std::vector<String> ConfigManager::getUpdateServers() const {
void ConfigManager::updateTimeConfig(long gmtOffsetSec, int daylightOffsetSec) {
timeConfig.gmtOffsetSec = gmtOffsetSec;
timeConfig.daylightOffsetSec = daylightOffsetSec;
saveToSD();
LOG_DEBUG("ConfigManager - TimeConfig updated - GMT offset %ld sec, DST offset %d sec",
saveTimeConfig(); // Save time config specifically
LOG_DEBUG("ConfigManager - TimeConfig updated - GMT offset %ld sec, DST offset %d sec",
gmtOffsetSec, daylightOffsetSec);
}
@@ -826,12 +834,12 @@ bool ConfigManager::loadNetworkConfig() {
bool ConfigManager::saveNetworkConfig() {
if (!ensureSDCard()) return false;
StaticJsonDocument<512> doc;
// Save hostname (user can customize)
doc["hostname"] = networkConfig.hostname;
// Save static IP configuration
doc["useStaticIP"] = networkConfig.useStaticIP;
doc["ip"] = networkConfig.ip.toString();
@@ -839,20 +847,93 @@ bool ConfigManager::saveNetworkConfig() {
doc["subnet"] = networkConfig.subnet.toString();
doc["dns1"] = networkConfig.dns1.toString();
doc["dns2"] = networkConfig.dns2.toString();
char buffer[512];
size_t len = serializeJson(doc, buffer, sizeof(buffer));
if (len == 0 || len >= sizeof(buffer)) {
LOG_ERROR("ConfigManager - ❌ Failed to serialize network config JSON");
return false;
}
saveFileToSD("/settings", "networkConfig.json", buffer);
LOG_DEBUG("ConfigManager - Network config saved to SD");
return true;
}
// ════════════════════════════════════════════════════════════════════════════
// TIME CONFIGURATION PERSISTENCE
// ════════════════════════════════════════════════════════════════════════════
bool ConfigManager::loadTimeConfig() {
if (!ensureSDCard()) return false;
File file = SD.open("/settings/timeConfig.json", FILE_READ);
if (!file) {
LOG_DEBUG("ConfigManager - Time config file not found - using defaults (GMT+2)");
return false;
}
StaticJsonDocument<256> doc;
DeserializationError error = deserializeJson(doc, file);
file.close();
if (error) {
LOG_ERROR("ConfigManager - ❌ Failed to parse time config from SD: %s", error.c_str());
return false;
}
// Load NTP server if present
if (doc.containsKey("ntpServer")) {
String ntpServer = doc["ntpServer"].as<String>();
if (!ntpServer.isEmpty()) {
timeConfig.ntpServer = ntpServer;
}
}
// Load GMT offset
if (doc.containsKey("gmtOffsetSec")) {
timeConfig.gmtOffsetSec = doc["gmtOffsetSec"].as<long>();
}
// Load daylight saving offset
if (doc.containsKey("daylightOffsetSec")) {
timeConfig.daylightOffsetSec = doc["daylightOffsetSec"].as<int>();
}
LOG_DEBUG("ConfigManager - Time config loaded - NTP: %s, GMT offset: %ld, DST offset: %d",
timeConfig.ntpServer.c_str(),
timeConfig.gmtOffsetSec,
timeConfig.daylightOffsetSec);
return true;
}
bool ConfigManager::saveTimeConfig() {
if (!ensureSDCard()) return false;
StaticJsonDocument<256> doc;
// Save NTP server
doc["ntpServer"] = timeConfig.ntpServer;
// Save timezone offsets
doc["gmtOffsetSec"] = timeConfig.gmtOffsetSec;
doc["daylightOffsetSec"] = timeConfig.daylightOffsetSec;
char buffer[256];
size_t len = serializeJson(doc, buffer, sizeof(buffer));
if (len == 0 || len >= sizeof(buffer)) {
LOG_ERROR("ConfigManager - ❌ Failed to serialize time config JSON");
return false;
}
saveFileToSD("/settings", "timeConfig.json", buffer);
LOG_DEBUG("ConfigManager - Time config saved to SD");
return true;
}
// ═══════════════════════════════════════════════════════════════════════════════
// SETTINGS RESET IMPLEMENTATION
// ═══════════════════════════════════════════════════════════════════════════════
@@ -878,6 +959,7 @@ bool ConfigManager::resetAllToDefaults() {
const char* settingsFiles[] = {
"/settings/deviceConfig.json",
"/settings/networkConfig.json",
"/settings/timeConfig.json",
"/settings/relayTimings.json",
"/settings/bellOutputs.json",
"/settings/clockConfig.json",