diff --git a/examples/OneOpenAir/OneOpenAir.ino b/examples/OneOpenAir/OneOpenAir.ino index 691c7e1..9c656fb 100644 --- a/examples/OneOpenAir/OneOpenAir.ino +++ b/examples/OneOpenAir/OneOpenAir.ino @@ -193,6 +193,7 @@ void setup() { /** Initialize local configure */ configuration.begin(); + configuration.setConfigurationUpdatedCallback(configUpdateHandle); /** Init I2C */ Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN); @@ -370,8 +371,11 @@ void loop() { /** factory reset handle */ factoryConfigReset(); - /** check that local configuration changed then do some action */ - configUpdateHandle(); + if (configuration.isCommandRequested()) { + // Each state machine already has an independent request command check + stateMachine.executeCo2Calibration(); + stateMachine.executeLedBarTest(); + } } static void co2Update(void) { @@ -1074,8 +1078,8 @@ static void configurationUpdateSchedule(void) { } std::string config = agClient->httpFetchConfig(); - if (agClient->isLastFetchConfigSucceed() && configuration.parse(config.c_str(), false)) { - configUpdateHandle(); + if (agClient->isLastFetchConfigSucceed()) { + configuration.parse(config.c_str(), false); } } @@ -1084,8 +1088,6 @@ static void configUpdateHandle() { return; } - stateMachine.executeCo2Calibration(); - String mqttUri = configuration.getMqttBrokerUri(); if (mqttClient.isCurrentUri(mqttUri) == false) { mqttClient.end(); @@ -1157,11 +1159,6 @@ static void configUpdateHandle() { if (configuration.isDisplayBrightnessChanged()) { oledDisplay.setBrightness(configuration.getDisplayBrightness()); } - - stateMachine.executeLedBarTest(); - } - else if(ag->isOpenAir()) { - stateMachine.executeLedBarTest(); } // Update display and led bar notification based on updated configuration diff --git a/src/AgConfigure.cpp b/src/AgConfigure.cpp index 1f9b87c..bec2455 100644 --- a/src/AgConfigure.cpp +++ b/src/AgConfigure.cpp @@ -456,6 +456,10 @@ bool Configuration::begin(void) { return true; } +void Configuration::setConfigurationUpdatedCallback(ConfigurationUpdatedCallback_t callback) { + _callback = callback; +} + /** * @brief Parse JSON configura string to local configure * @@ -951,15 +955,18 @@ bool Configuration::parse(String data, bool isLocal) { changed = true; } + if (ledBarTestRequested || co2CalibrationRequested) { + commandRequested = true; + updated = true; + } + if (changed) { updated = true; saveConfig(); printConfig(); - } else { - if (ledBarTestRequested || co2CalibrationRequested) { - updated = true; - } + _callback(); } + return true; } @@ -1159,6 +1166,12 @@ bool Configuration::isUpdated(void) { return updated; } +bool Configuration::isCommandRequested(void) { + bool oldState = this->commandRequested; + this->commandRequested = false; + return oldState; +} + String Configuration::jsonTypeInvalidMessage(String name, String type) { return "'" + name + "' type is invalid, expecting '" + type + "'"; } diff --git a/src/AgConfigure.h b/src/AgConfigure.h index 49785d4..6c1fb59 100644 --- a/src/AgConfigure.h +++ b/src/AgConfigure.h @@ -28,6 +28,7 @@ private: bool co2CalibrationRequested; bool ledBarTestRequested; bool updated; + bool commandRequested = false; String failedMessage; bool _noxLearnOffsetChanged; bool _tvocLearningOffsetChanged; @@ -70,6 +71,9 @@ public: bool hasSensorSGP = true; bool hasSensorSHT = true; + typedef void (*ConfigurationUpdatedCallback_t)(); + void setConfigurationUpdatedCallback(ConfigurationUpdatedCallback_t callback); + bool begin(void); bool parse(String data, bool isLocal); String toString(void); @@ -90,6 +94,7 @@ public: void reset(void); String getModel(void); bool isUpdated(void); + bool isCommandRequested(void); String getFailedMesage(void); void setPostToAirGradient(bool enable); bool noxLearnOffsetChanged(void); @@ -116,6 +121,8 @@ public: PMCorrection getPMCorrection(void); TempHumCorrection getTempCorrection(void); TempHumCorrection getHumCorrection(void); +private: + ConfigurationUpdatedCallback_t _callback; }; #endif /** _AG_CONFIG_H_ */