From ea46b812c13403d7c6bfa47173deee718849ab27 Mon Sep 17 00:00:00 2001 From: samuelbles07 Date: Sat, 2 Nov 2024 00:53:33 +0700 Subject: [PATCH] Handle saving back to eeprom rename the function --- src/AgConfigure.cpp | 17 ++++++++++++++--- src/AgConfigure.h | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/AgConfigure.cpp b/src/AgConfigure.cpp index 5e438af..16f91b0 100644 --- a/src/AgConfigure.cpp +++ b/src/AgConfigure.cpp @@ -1,5 +1,4 @@ #include "AgConfigure.h" -#include "Libraries/Arduino_JSON/src/Arduino_JSON.h" #if ESP32 #include "FS.h" #include "SPIFFS.h" @@ -54,6 +53,7 @@ JSON_PROP_DEF(co2CalibrationRequested); JSON_PROP_DEF(ledBarTestRequested); JSON_PROP_DEF(offlineMode); JSON_PROP_DEF(monitorDisplayCompensatedValues); +JSON_PROP_DEF(corrections); #define jprop_model_default "" #define jprop_country_default "TH" @@ -114,7 +114,7 @@ PMCorrectionAlgorithm Configuration::matchPmAlgorithm(String algorithm) { return Unknown; } -bool Configuration::parsePmCorrection(JSONVar &json) { +bool Configuration::updatePmCorrection(JSONVar &json) { if (!json.hasOwnProperty("corrections")) { // TODO: need to response message? Serial.println("corrections not found"); @@ -777,7 +777,9 @@ bool Configuration::parse(String data, bool isLocal) { } // 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; } @@ -1232,6 +1234,15 @@ void Configuration::toConfig(const char *buf) { 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) { saveConfig(); } diff --git a/src/AgConfigure.h b/src/AgConfigure.h index 3a7be13..58d0ee8 100644 --- a/src/AgConfigure.h +++ b/src/AgConfigure.h @@ -35,7 +35,7 @@ private: String getLedBarModeName(LedBarMode mode); PMCorrectionAlgorithm matchPmAlgorithm(String algorithm); - bool parsePmCorrection(JSONVar &json); + bool updatePmCorrection(JSONVar &json); void saveConfig(void); void loadConfig(void); void defaultConfig(void);