Browse Source

Pretty print config backup json

softuart
Xose Pérez 6 years ago
parent
commit
efa88c962f
2 changed files with 54 additions and 41 deletions
  1. +49
    -38
      code/espurna/settings.ino
  2. +5
    -3
      code/espurna/web.ino

+ 49
- 38
code/espurna/settings.ino View File

@ -85,11 +85,42 @@ String _settingsKeyName(unsigned int index) {
}
std::vector<String> _settingsKeys() {
// Get sorted list of keys
std::vector<String> keys;
//unsigned int size = settingsKeyCount();
unsigned int size = _settingsKeyCount();
for (unsigned int i=0; i<size; i++) {
//String key = settingsKeyName(i);
String key = _settingsKeyName(i);
bool inserted = false;
for (unsigned char j=0; j<keys.size(); j++) {
// Check if we have to insert it before the current element
if (keys[j].compareTo(key) > 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<String> commands;
@ -122,32 +153,10 @@ void _settingsHelp() {
}
void _settingsKeys() {
void _settingsKeysCommand() {
// Get sorted list of keys
std::vector<String> keys;
//unsigned int size = settingsKeyCount();
unsigned int size = _settingsKeyCount();
for (unsigned int i=0; i<size; i++) {
//String key = settingsKeyName(i);
String key = _settingsKeyName(i);
bool inserted = false;
for (unsigned char j=0; j<keys.size(); j++) {
// Check if we have to insert it before the current element
if (keys[j].compareTo(key) > 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<String> 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<size; i++) {
String key = _settingsKeyName(i);
String value = getSetting(key);
root[key] = value;
// Get sorted list of keys
std::vector<String> keys = _settingsKeys();
// Add the key-values to the json object
for (unsigned int i=0; i<keys.size(); i++) {
String value = getSetting(keys[i]);
root[keys[i]] = value;
}
}


+ 5
- 3
code/espurna/web.ino View File

@ -45,17 +45,19 @@ void _onGetConfig(AsyncWebServerRequest *request) {
webLog(request);
if (!_authenticate(request)) return request->requestAuthentication(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);
}


Loading…
Cancel
Save