From efa88c962f16b6ebce586e0f722125f438f9a461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Fri, 9 Feb 2018 09:55:53 +0100 Subject: [PATCH] Pretty print config backup json --- code/espurna/settings.ino | 87 ++++++++++++++++++++++----------------- code/espurna/web.ino | 8 ++-- 2 files changed, 54 insertions(+), 41 deletions(-) diff --git a/code/espurna/settings.ino b/code/espurna/settings.ino index 33cf4623..d7c26a34 100644 --- a/code/espurna/settings.ino +++ b/code/espurna/settings.ino @@ -85,11 +85,42 @@ String _settingsKeyName(unsigned int index) { } +std::vector _settingsKeys() { + + // Get sorted list of keys + std::vector keys; + + //unsigned int size = settingsKeyCount(); + unsigned int size = _settingsKeyCount(); + for (unsigned int i=0; i 0) { + keys.insert(keys.begin() + j, key); + inserted = true; + break; + } + + } + + // If we could not insert it, just push it at the end + if (!inserted) keys.push_back(key); + + } + + return keys; +} + // ----------------------------------------------------------------------------- // Commands // ----------------------------------------------------------------------------- -void _settingsHelp() { +void _settingsHelpCommand() { // Get sorted list of commands std::vector commands; @@ -122,32 +153,10 @@ void _settingsHelp() { } -void _settingsKeys() { +void _settingsKeysCommand() { // Get sorted list of keys - std::vector keys; - //unsigned int size = settingsKeyCount(); - unsigned int size = _settingsKeyCount(); - for (unsigned int i=0; i 0) { - keys.insert(keys.begin() + j, key); - inserted = true; - break; - } - - } - - // If we could not insert it, just push it at the end - if (!inserted) keys.push_back(key); - - } + std::vector keys = _settingsKeys(); // Write key-values DEBUG_MSG_P(PSTR("Current settings:\n")); @@ -162,14 +171,14 @@ void _settingsKeys() { } -void _settingsFactoryReset() { +void _settingsFactoryResetCommand() { for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) { EEPROM.write(i, 0xFF); } EEPROM.commit(); } -void _settingsDump(bool ascii) { +void _settingsDumpCommand(bool ascii) { for (unsigned int i = 0; i < SPI_FLASH_SEC_SIZE; i++) { if (i % 16 == 0) DEBUG_MSG_P(PSTR("\n[%04X] "), i); byte c = EEPROM.read(i); @@ -191,14 +200,14 @@ void _settingsInitCommands() { #endif settingsRegisterCommand(F("COMMANDS"), [](Embedis* e) { - _settingsHelp(); + _settingsHelpCommand(); DEBUG_MSG_P(PSTR("+OK\n")); }); settingsRegisterCommand(F("EEPROM.DUMP"), [](Embedis* e) { bool ascii = false; if (e->argc == 2) ascii = String(e->argv[1]).toInt() == 1; - _settingsDump(ascii); + _settingsDumpCommand(ascii); DEBUG_MSG_P(PSTR("\n+OK\n")); }); @@ -210,7 +219,7 @@ void _settingsInitCommands() { }); settingsRegisterCommand(F("FACTORY.RESET"), [](Embedis* e) { - _settingsFactoryReset(); + _settingsFactoryResetCommand(); DEBUG_MSG_P(PSTR("+OK\n")); }); @@ -238,7 +247,7 @@ void _settingsInitCommands() { }); settingsRegisterCommand(F("HELP"), [](Embedis* e) { - _settingsHelp(); + _settingsHelpCommand(); DEBUG_MSG_P(PSTR("+OK\n")); }); @@ -252,7 +261,7 @@ void _settingsInitCommands() { }); settingsRegisterCommand(F("KEYS"), [](Embedis* e) { - _settingsKeys(); + _settingsKeysCommand(); DEBUG_MSG_P(PSTR("+OK\n")); }); @@ -323,7 +332,7 @@ void saveSettings() { } void resetSettings() { - _settingsFactoryReset(); + _settingsFactoryResetCommand(); } // ----------------------------------------------------------------------------- @@ -367,11 +376,13 @@ bool settingsRestoreJson(JsonObject& data) { bool settingsGetJson(JsonObject& root) { - unsigned int size = _settingsKeyCount(); - for (unsigned int i=0; i keys = _settingsKeys(); + + // Add the key-values to the json object + for (unsigned int i=0; irequestAuthentication(getSetting("hostname").c_str()); - AsyncJsonResponse * response = new AsyncJsonResponse(); - JsonObject& root = response->getRoot(); + AsyncResponseStream *response = request->beginResponseStream("text/json"); + DynamicJsonBuffer jsonBuffer; + JsonObject &root = jsonBuffer.createObject(); root["app"] = APP_NAME; root["version"] = APP_VERSION; settingsGetJson(root); + root.prettyPrintTo(*response); char buffer[100]; snprintf_P(buffer, sizeof(buffer), PSTR("attachment; filename=\"%s-backup.json\""), (char *) getSetting("hostname").c_str()); response->addHeader("Content-Disposition", buffer); - response->setLength(); + request->send(response); }