mirror of
https://github.com/HASwitchPlate/openHASP.git
synced 2025-07-27 05:06:44 +00:00
Only save config on changes
This commit is contained in:
parent
3267bd3112
commit
d0284408d8
@ -2,6 +2,7 @@
|
|||||||
#include "ArduinoJson.h"
|
#include "ArduinoJson.h"
|
||||||
#include "ArduinoLog.h"
|
#include "ArduinoLog.h"
|
||||||
#include <FS.h> // Include the SPIFFS library
|
#include <FS.h> // Include the SPIFFS library
|
||||||
|
#include "EEPROM.h"
|
||||||
|
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
#include "SPIFFS.h"
|
#include "SPIFFS.h"
|
||||||
@ -16,6 +17,7 @@
|
|||||||
#include "hasp_ota.h"
|
#include "hasp_ota.h"
|
||||||
#include "hasp_spiffs.h"
|
#include "hasp_spiffs.h"
|
||||||
#include "hasp_telnet.h"
|
#include "hasp_telnet.h"
|
||||||
|
//#include "hasp_eeprom.h"
|
||||||
#include "hasp.h"
|
#include "hasp.h"
|
||||||
|
|
||||||
#include "hasp_conf.h"
|
#include "hasp_conf.h"
|
||||||
@ -127,6 +129,33 @@ void configGetConfig(JsonDocument & settings, bool setupdebug = false)
|
|||||||
Log.error(F("CONF: Failed to load %s"), configFile.c_str());
|
Log.error(F("CONF: Failed to load %s"), configFile.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void configBackupToEeprom()
|
||||||
|
{
|
||||||
|
String configFile((char *)0);
|
||||||
|
configFile.reserve(128);
|
||||||
|
configFile = String(FPSTR(HASP_CONFIG_FILE));
|
||||||
|
|
||||||
|
EEPROM.begin(1024);
|
||||||
|
uint8_t buffer[128];
|
||||||
|
size_t index = 0;
|
||||||
|
|
||||||
|
File file = SPIFFS.open(configFile, "r");
|
||||||
|
if(file) {
|
||||||
|
|
||||||
|
while(size_t count = file.read(buffer, sizeof(buffer)) > 0) {
|
||||||
|
for(size_t i = 0; i < count; i++) {
|
||||||
|
EEPROM.write(index, buffer[i]);
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
file.close();
|
||||||
|
EEPROM.commit();
|
||||||
|
|
||||||
|
Log.verbose(F("CONF: Written %u to EEPROM"), index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void configWriteConfig()
|
void configWriteConfig()
|
||||||
{
|
{
|
||||||
String configFile((char *)0);
|
String configFile((char *)0);
|
||||||
@ -134,35 +163,71 @@ void configWriteConfig()
|
|||||||
configFile = String(FPSTR(HASP_CONFIG_FILE));
|
configFile = String(FPSTR(HASP_CONFIG_FILE));
|
||||||
|
|
||||||
/* Read Config File */
|
/* Read Config File */
|
||||||
DynamicJsonDocument settings(1024 * 2);
|
DynamicJsonDocument settings(6 * 256);
|
||||||
Log.notice(F("CONF: Config LOADING first %s"), configFile.c_str());
|
Log.notice(F("CONF: Config LOADING first %s"), configFile.c_str());
|
||||||
configGetConfig(settings, false);
|
configGetConfig(settings, false);
|
||||||
Log.trace(F("CONF: Config LOADED first %s"), configFile.c_str());
|
Log.trace(F("CONF: Config LOADED first %s"), configFile.c_str());
|
||||||
|
|
||||||
bool changed = true;
|
bool writefile = false;
|
||||||
|
bool changed = false;
|
||||||
|
|
||||||
#if HASP_USE_WIFI
|
#if HASP_USE_WIFI
|
||||||
changed |= wifiGetConfig(settings[F("wifi")].to<JsonObject>());
|
changed = wifiGetConfig(settings[F("wifi")]);
|
||||||
|
if(changed) {
|
||||||
|
Log.verbose(F("WIFI: Settings changed"));
|
||||||
|
writefile = true;
|
||||||
|
}
|
||||||
#if HASP_USE_MQTT
|
#if HASP_USE_MQTT
|
||||||
changed |= mqttGetConfig(settings[F("mqtt")].to<JsonObject>());
|
changed = mqttGetConfig(settings[F("mqtt")]);
|
||||||
|
if(changed) {
|
||||||
|
Log.verbose(F("MQTT: Settings changed"));
|
||||||
|
writefile = true;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#if HASP_USE_TELNET
|
#if HASP_USE_TELNET
|
||||||
changed |= telnetGetConfig(settings[F("telnet")].to<JsonObject>());
|
changed = telnetGetConfig(settings[F("telnet")]);
|
||||||
|
if(changed) {
|
||||||
|
Log.verbose(F("TELNET: Settings changed"));
|
||||||
|
writefile = true;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#if HASP_USE_MDNS
|
#if HASP_USE_MDNS
|
||||||
changed |= mdnsGetConfig(settings[F("mdns")].to<JsonObject>());
|
changed = mdnsGetConfig(settings[F("mdns")]);
|
||||||
|
if(changed) {
|
||||||
|
Log.verbose(F("MDNS: Settings changed"));
|
||||||
|
writefile = true;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#if HASP_USE_HTTP
|
#if HASP_USE_HTTP
|
||||||
changed |= httpGetConfig(settings[F("http")].to<JsonObject>());
|
changed = httpGetConfig(settings[F("http")]);
|
||||||
|
if(changed) {
|
||||||
|
Log.verbose(F("HTTP: Settings changed"));
|
||||||
|
writefile = true;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
changed |= debugGetConfig(settings[F("debug")].to<JsonObject>());
|
changed = debugGetConfig(settings[F("debug")]);
|
||||||
changed |= guiGetConfig(settings[F("gui")].to<JsonObject>());
|
if(changed) {
|
||||||
changed |= haspGetConfig(settings[F("hasp")].to<JsonObject>());
|
Log.verbose(F("DEBUG: Settings changed"));
|
||||||
|
writefile = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
changed = guiGetConfig(settings[F("gui")]);
|
||||||
|
if(changed) {
|
||||||
|
Log.verbose(F("GUI: Settings changed"));
|
||||||
|
writefile = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
changed = haspGetConfig(settings[F("hasp")]);
|
||||||
|
if(changed) {
|
||||||
|
Log.verbose(F("HASP: Settings changed"));
|
||||||
|
writefile = true;
|
||||||
|
}
|
||||||
|
|
||||||
// changed |= otaGetConfig(settings[F("ota")].to<JsonObject>());
|
// changed |= otaGetConfig(settings[F("ota")].to<JsonObject>());
|
||||||
|
|
||||||
if(changed) {
|
if(writefile) {
|
||||||
File file = SPIFFS.open(configFile, "w");
|
File file = SPIFFS.open(configFile, "w");
|
||||||
if(file) {
|
if(file) {
|
||||||
Log.notice(F("CONF: Writing %s"), configFile.c_str());
|
Log.notice(F("CONF: Writing %s"), configFile.c_str());
|
||||||
@ -170,13 +235,15 @@ void configWriteConfig()
|
|||||||
file.close();
|
file.close();
|
||||||
if(size > 0) {
|
if(size > 0) {
|
||||||
Log.verbose(F("CONF: [SUCCESS] Saved %s"), configFile.c_str());
|
Log.verbose(F("CONF: [SUCCESS] Saved %s"), configFile.c_str());
|
||||||
|
configBackupToEeprom();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.error(F("CONF: Failed to write %s"), configFile.c_str());
|
Log.error(F("CONF: Failed to write %s"), configFile.c_str());
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
Log.verbose(F("CONF: Configuration was not changed"));
|
Log.notice(F("CONF: Configuration was not changed"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -141,18 +141,30 @@ void debugStop()
|
|||||||
|
|
||||||
bool debugGetConfig(const JsonObject & settings)
|
bool debugGetConfig(const JsonObject & settings)
|
||||||
{
|
{
|
||||||
|
bool changed = false;
|
||||||
|
|
||||||
|
if(debugSerialBaud != settings[FPSTR(F_CONFIG_BAUD)].as<uint16_t>()) changed = true;
|
||||||
settings[FPSTR(F_CONFIG_BAUD)] = debugSerialBaud;
|
settings[FPSTR(F_CONFIG_BAUD)] = debugSerialBaud;
|
||||||
|
|
||||||
|
if(debugTelePeriod != settings[FPSTR(F_DEBUG_TELEPERIOD)].as<uint16_t>()) changed = true;
|
||||||
settings[FPSTR(F_DEBUG_TELEPERIOD)] = debugTelePeriod;
|
settings[FPSTR(F_DEBUG_TELEPERIOD)] = debugTelePeriod;
|
||||||
|
|
||||||
#if HASP_USE_SYSLOG != 0
|
#if HASP_USE_SYSLOG != 0
|
||||||
|
if(strcmp(debugSyslogHost, settings[FPSTR(F_CONFIG_HOST)].as<String>().c_str()) != 0) changed = true;
|
||||||
settings[FPSTR(F_CONFIG_HOST)] = debugSyslogHost;
|
settings[FPSTR(F_CONFIG_HOST)] = debugSyslogHost;
|
||||||
|
|
||||||
|
if(debugSyslogPort != settings[FPSTR(F_CONFIG_PORT)].as<uint16_t>()) changed = true;
|
||||||
settings[FPSTR(F_CONFIG_PORT)] = debugSyslogPort;
|
settings[FPSTR(F_CONFIG_PORT)] = debugSyslogPort;
|
||||||
|
|
||||||
|
if(debugSyslogProtocol != settings[FPSTR(F_CONFIG_PROTOCOL)].as<uint8_t>()) changed = true;
|
||||||
settings[FPSTR(F_CONFIG_PROTOCOL)] = debugSyslogProtocol;
|
settings[FPSTR(F_CONFIG_PROTOCOL)] = debugSyslogProtocol;
|
||||||
|
|
||||||
|
if(debugSyslogFacility != settings[FPSTR(F_CONFIG_LOG)].as<uint8_t>()) changed = true;
|
||||||
settings[FPSTR(F_CONFIG_LOG)] = debugSyslogFacility;
|
settings[FPSTR(F_CONFIG_LOG)] = debugSyslogFacility;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
configOutput(settings);
|
if(changed) configOutput(settings);
|
||||||
return true;
|
return changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Set DEBUG Configuration.
|
/** Set DEBUG Configuration.
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#include <EEPROM.h>
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include "EEPROM.h"
|
||||||
|
|
||||||
#include "hasp_debug.h"
|
#include "hasp_debug.h"
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user