Handle saving back to eeprom

rename the function
This commit is contained in:
samuelbles07
2024-11-02 00:53:33 +07:00
parent 16c932962a
commit ea46b812c1
2 changed files with 15 additions and 4 deletions

View File

@ -1,5 +1,4 @@
#include "AgConfigure.h" #include "AgConfigure.h"
#include "Libraries/Arduino_JSON/src/Arduino_JSON.h"
#if ESP32 #if ESP32
#include "FS.h" #include "FS.h"
#include "SPIFFS.h" #include "SPIFFS.h"
@ -54,6 +53,7 @@ JSON_PROP_DEF(co2CalibrationRequested);
JSON_PROP_DEF(ledBarTestRequested); JSON_PROP_DEF(ledBarTestRequested);
JSON_PROP_DEF(offlineMode); JSON_PROP_DEF(offlineMode);
JSON_PROP_DEF(monitorDisplayCompensatedValues); JSON_PROP_DEF(monitorDisplayCompensatedValues);
JSON_PROP_DEF(corrections);
#define jprop_model_default "" #define jprop_model_default ""
#define jprop_country_default "TH" #define jprop_country_default "TH"
@ -114,7 +114,7 @@ PMCorrectionAlgorithm Configuration::matchPmAlgorithm(String algorithm) {
return Unknown; return Unknown;
} }
bool Configuration::parsePmCorrection(JSONVar &json) { bool Configuration::updatePmCorrection(JSONVar &json) {
if (!json.hasOwnProperty("corrections")) { if (!json.hasOwnProperty("corrections")) {
// TODO: need to response message? // TODO: need to response message?
Serial.println("corrections not found"); Serial.println("corrections not found");
@ -777,7 +777,9 @@ bool Configuration::parse(String data, bool isLocal) {
} }
// Corrections // Corrections
if (parsePmCorrection(root)) { if (updatePmCorrection(root)) {
// Deep copy corrections from root to jconfig, so it will be saved later
jconfig[jprop_corrections] = JSON.parse(JSON.stringify(root["corrections"]));
changed = true; changed = true;
} }
@ -1232,6 +1234,15 @@ void Configuration::toConfig(const char *buf) {
jprop_monitorDisplayCompensatedValues_default; jprop_monitorDisplayCompensatedValues_default;
} }
// Set default first before parsing local config
pmCorrection.algorithm = PMCorrectionAlgorithm::None;
pmCorrection.intercept = 0;
pmCorrection.scalingFactor = 0;
pmCorrection.useEPA = false;
// Load correction from saved config
updatePmCorrection(jconfig);
if (changed) { if (changed) {
saveConfig(); saveConfig();
} }

View File

@ -35,7 +35,7 @@ private:
String getLedBarModeName(LedBarMode mode); String getLedBarModeName(LedBarMode mode);
PMCorrectionAlgorithm matchPmAlgorithm(String algorithm); PMCorrectionAlgorithm matchPmAlgorithm(String algorithm);
bool parsePmCorrection(JSONVar &json); bool updatePmCorrection(JSONVar &json);
void saveConfig(void); void saveConfig(void);
void loadConfig(void); void loadConfig(void);
void defaultConfig(void); void defaultConfig(void);