diff --git a/code/espurna/settings.ino b/code/espurna/settings.ino index 89a2cd54..19013663 100644 --- a/code/espurna/settings.ino +++ b/code/espurna/settings.ino @@ -375,19 +375,30 @@ size_t settingsMaxSize() { bool settingsRestoreJson(JsonObject& data) { + // Check this is an ESPurna configuration file (must have "app":"ESPURNA") const char* app = data["app"]; - if (strcmp(app, APP_NAME) != 0) return false; + if (!app || strcmp(app, APP_NAME) != 0) { + DEBUG_MSG_P(PSTR("[SETTING] Wrong or missing 'app' key\n")); + return false; + } - for (unsigned int i = EEPROM_DATA_END; i < SPI_FLASH_SEC_SIZE; i++) { - EEPROMr.write(i, 0xFF); + // Clear settings + bool is_backup = data["backup"]; + if (is_backup) { + for (unsigned int i = EEPROM_DATA_END; i < SPI_FLASH_SEC_SIZE; i++) { + EEPROMr.write(i, 0xFF); + } } + // Dump settings to memory buffer for (auto element : data) { if (strcmp(element.key, "app") == 0) continue; if (strcmp(element.key, "version") == 0) continue; + if (strcmp(element.key, "backup") == 0) continue; setSetting(element.key, element.value.as()); } + // Persist to EEPROM saveSettings(); DEBUG_MSG_P(PSTR("[SETTINGS] Settings restored successfully\n")); diff --git a/code/espurna/web.ino b/code/espurna/web.ino index ed302a60..5afa9070 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -123,14 +123,17 @@ void _onGetConfig(AsyncWebServerRequest *request) { response->addHeader("X-Content-Type-Options", "nosniff"); response->addHeader("X-Frame-Options", "deny"); - response->printf("{\n \"app\": \"%s\",\n \"version\": \"%s\"", APP_NAME, APP_VERSION); + response->printf("{\n\"app\": \"%s\"", APP_NAME); + response->printf(",\n\"version\": \"%s\"", APP_VERSION); + response->printf(",\n\"backup\": \"1\""); + response->printf(",\n\"timestamp\": \"%s\"", ntpDateTime().c_str()); // Write the keys line by line (not sorted) unsigned long count = settingsKeyCount(); for (unsigned int i=0; iprintf(",\n \"%s\": \"%s\"", key.c_str(), value.c_str()); + response->printf(",\n\"%s\": \"%s\"", key.c_str(), value.c_str()); } response->printf("\n}");