Fixed issue with Bell Assignments

This commit is contained in:
2025-10-26 11:08:19 +02:00
parent d1835beff5
commit 06891e8d82
5 changed files with 85 additions and 5 deletions

View File

@@ -87,6 +87,12 @@ bool ConfigManager::begin() {
LOG_INFO("ConfigManager: Creating default bell durations file");
saveBellDurations();
}
// Load bell outputs, save defaults if not found
if (!loadBellOutputs()) {
LOG_INFO("ConfigManager: Creating default bell outputs file");
saveBellOutputs();
}
// Load clock config, save defaults if not found
if (!loadClockConfig()) {
@@ -373,6 +379,60 @@ bool ConfigManager::saveBellDurations() {
return true;
}
bool ConfigManager::loadBellOutputs() {
if (!ensureSDCard()) {
LOG_ERROR("ConfigManager: SD Card not initialized. Using default bell outputs (1:1 mapping).");
return false;
}
File file = SD.open("/settings/bellOutputs.json", FILE_READ);
if (!file) {
LOG_WARNING("ConfigManager: Bell outputs file not found on SD. Using default 1:1 mapping.");
return false;
}
StaticJsonDocument<512> doc;
DeserializationError error = deserializeJson(doc, file);
file.close();
if (error) {
LOG_ERROR("ConfigManager: Failed to parse bell outputs from SD. Using defaults.");
return false;
}
for (uint8_t i = 0; i < 16; i++) {
String key = String("b") + (i + 1);
if (doc.containsKey(key)) {
bellConfig.outputs[i] = doc[key].as<uint16_t>(); // Already 0-indexed in file
}
}
LOG_INFO("ConfigManager: Bell outputs loaded from SD");
return true;
}
bool ConfigManager::saveBellOutputs() {
if (!ensureSDCard()) return false;
StaticJsonDocument<512> doc;
for (uint8_t i = 0; i < 16; i++) {
String key = String("b") + (i + 1);
doc[key] = bellConfig.outputs[i]; // Save 0-indexed outputs
}
char buffer[512];
size_t len = serializeJson(doc, buffer, sizeof(buffer));
if (len == 0 || len >= sizeof(buffer)) {
LOG_ERROR("ConfigManager: Failed to serialize bell outputs JSON");
return false;
}
saveFileToSD("/settings", "bellOutputs.json", buffer);
LOG_INFO("ConfigManager: Bell outputs saved to SD");
return true;
}
void ConfigManager::updateBellDurations(JsonVariant doc) {
for (uint8_t i = 0; i < 16; i++) {
String key = String("b") + (i + 1);
@@ -388,11 +448,16 @@ void ConfigManager::updateBellOutputs(JsonVariant doc) {
String key = String("b") + (i + 1);
if (doc.containsKey(key)) {
bellConfig.outputs[i] = doc[key].as<uint16_t>() - 1;
LOG_VERBOSE("ConfigManager: Bell %d output set to %d", i + 1, bellConfig.outputs[i]);
}
}
LOG_INFO("ConfigManager: Updated bell outputs");
}
uint16_t ConfigManager::getBellDuration(uint8_t bellIndex) const {
if (bellIndex >= 16) return 90;
return bellConfig.durations[bellIndex];
@@ -829,6 +894,7 @@ bool ConfigManager::resetAllToDefaults() {
"/settings/deviceConfig.json",
"/settings/networkConfig.json",
"/settings/relayTimings.json",
"/settings/bellOutputs.json", // <-- ADD THIS LINE
"/settings/clockConfig.json",
"/settings/clockState.json",
"/settings/updateServers.json"