From 3788aa27468f755207135bc397081add46d23ae6 Mon Sep 17 00:00:00 2001 From: Phat Nguyen Date: Mon, 1 Apr 2024 09:15:10 +0700 Subject: [PATCH] change `locallyControlled` to `configurationControl` and update relate logic --- examples/OneOpenAir/LocalConfig.cpp | 71 ++++++++++++++++++++--------- examples/OneOpenAir/LocalConfig.h | 4 +- examples/OneOpenAir/OneOpenAir.ino | 15 ++++-- src/AirGradient.h | 8 +++- 4 files changed, 68 insertions(+), 30 deletions(-) diff --git a/examples/OneOpenAir/LocalConfig.cpp b/examples/OneOpenAir/LocalConfig.cpp index 78b6433..075c695 100644 --- a/examples/OneOpenAir/LocalConfig.cpp +++ b/examples/OneOpenAir/LocalConfig.cpp @@ -1,6 +1,9 @@ #include "LocalConfig.h" #include "EEPROM.h" +const char *CONFIGURATION_CONTROL_NAME[] = { + [Local] = "local", [Cloud] = "cloud", [Both] = "both"}; + void LocalConfig::printLog(String log) { debugLog.printf("[LocalConfig] %s\r\n", log.c_str()); } @@ -54,10 +57,10 @@ void LocalConfig::defaultConfig(void) { // Default MQTT broker is null. memset(config.mqttBroker, 0, sizeof(config.mqttBroker)); + config.configurationControl = ConfigurationControl::Both; config.inUSAQI = false; // pmStandard = ugm3 config.inF = false; config.postDataToAirGradient = true; - config.locallyControlled = true; config.displayMode = true; config.useRGBLedBar = UseLedBar::UseLedBarCO2; config.abcDays = 7; @@ -102,6 +105,37 @@ bool LocalConfig::parse(String data, bool isLocal) { /** Is configuration changed */ bool changed = false; + /** Get ConfigurationControl */ + if (JSON.typeof_(root["configurationControl"]) == "string") { + String configurationControl = root["configurationControl"]; + if (configurationControl == + String(CONFIGURATION_CONTROL_NAME[ConfigurationControl::Local])) { + config.configurationControl = (uint8_t)ConfigurationControl::Local; + changed = true; + } else if (configurationControl == + String( + CONFIGURATION_CONTROL_NAME[ConfigurationControl::Cloud])) { + config.configurationControl = (uint8_t)ConfigurationControl::Cloud; + changed = true; + } else if (configurationControl == + String(CONFIGURATION_CONTROL_NAME[ConfigurationControl::Both])) { + config.configurationControl = (uint8_t)ConfigurationControl::Both; + changed = true; + } else { + printLog("'configurationControl' value '" + configurationControl + + "' invalid"); + return false; + } + } else { + return false; + } + + if ((config.configurationControl == (byte)ConfigurationControl::Cloud)) { + printLog("Ignore, cause ConfigurationControl is " + + String(CONFIGURATION_CONTROL_NAME[config.configurationControl])); + return false; + } + if (JSON.typeof_(root["country"]) == "string") { String country = root["country"]; if (country.length() == 2) { @@ -267,18 +301,6 @@ bool LocalConfig::parse(String data, bool isLocal) { } } - /** This field only allow on local configure */ - if (isLocal) { - if (JSON.typeof_(root["locallyControlled"]) == "boolean") { - bool locallyControlled = root["locallyControlled"]; - if (locallyControlled != config.locallyControlled) { - changed = true; - config.locallyControlled = locallyControlled; - printLog("set locallyControlled: " + String(locallyControlled)); - } - } - } - /** Parse data only got from AirGradient server */ if (isLocal == false) { if (JSON.typeof_(root["model"]) == "string") { @@ -339,8 +361,9 @@ String LocalConfig::toString(void) { /** "temperatureUnit" */ root["temperatureUnit"] = String(config.temperatureUnit); - /** "locallyControlled" */ - root["locallyControlled"] = config.locallyControlled; + /** configurationControl */ + root["configurationControl"] = + String(CONFIGURATION_CONTROL_NAME[config.configurationControl]); /** "postDataToAirGradient" */ root["postDataToAirGradient"] = config.postDataToAirGradient; @@ -374,11 +397,14 @@ bool LocalConfig::isPostDataToAirGradient(void) { return config.postDataToAirGradient; } -bool LocalConfig::isLocallyControlled(void) { return config.locallyControlled; } +ConfigurationControl LocalConfig::getConfigurationControl(void) { + return (ConfigurationControl)config.configurationControl; +} /** - * @brief CO2 manual calib request, the request flag will clear after get. Must call this after parse success - * + * @brief CO2 manual calib request, the request flag will clear after get. Must + * call this after parse success + * * @return true Requested * @return false Not requested */ @@ -389,8 +415,9 @@ bool LocalConfig::isCo2CalibrationRequested(void) { } /** - * @brief LED bar test request, the request flag will clear after get. Must call this function after parse success - * + * @brief LED bar test request, the request flag will clear after get. Must call + * this function after parse success + * * @return true Requested * @return false Not requested */ @@ -411,7 +438,7 @@ void LocalConfig::reset(void) { /** * @brief Get model name, it's usage for offline mode - * - * @return String + * + * @return String */ String LocalConfig::getModel(void) { return String(config.model); } diff --git a/examples/OneOpenAir/LocalConfig.h b/examples/OneOpenAir/LocalConfig.h index 3e79911..809f01f 100644 --- a/examples/OneOpenAir/LocalConfig.h +++ b/examples/OneOpenAir/LocalConfig.h @@ -17,7 +17,7 @@ private: bool postDataToAirGradient; /** If true, monitor will not POST data to airgradient server. Make sure no error message shown on monitor */ - bool locallyControlled; /** If true, configuration from airgradient server + uint8_t configurationControl; /** If true, configuration from airgradient server will be ignored */ bool displayMode; /** true if enable display */ uint8_t useRGBLedBar; @@ -56,7 +56,7 @@ public: bool getDisplayMode(void); String getMqttBrokerUri(void); bool isPostDataToAirGradient(void); - bool isLocallyControlled(void); + ConfigurationControl getConfigurationControl(void); bool isCo2CalibrationRequested(void); bool isLedBarTestRequested(void); void reset(void); diff --git a/examples/OneOpenAir/OneOpenAir.ino b/examples/OneOpenAir/OneOpenAir.ino index d1f5f3a..816ca58 100644 --- a/examples/OneOpenAir/OneOpenAir.ino +++ b/examples/OneOpenAir/OneOpenAir.ino @@ -168,7 +168,6 @@ public: /** * @brief Initialize airgradient server, it's load the server configuration if * failed load it to default. - * */ void begin(void) { configFailed = false; @@ -184,8 +183,12 @@ public: * @return false Failure */ bool fetchServerConfiguration(String id) { - if (config.isLocallyControlled()) { + if (config.getConfigurationControl() == ConfigurationControl::Local) { Serial.println("Ignore fetch server configuration"); + + // Clear server configuration failed flag, cause it's ignore but not + // really failed + configFailed = false; return false; } @@ -985,14 +988,16 @@ static void localConfigGet() { } static void localConfigPut() { String data = webServer.arg(0); - String response = "Failure"; + String response = ""; + int statusCode = 400; // Status code for data invalid if (localConfig.parse(data, true)) { localConfigUpdate = true; + statusCode = 200; response = "Success"; } else { - Serial.println("PUT data invalid"); + response = "Set for cloud configuration. Local configuration ignored"; } - webServer.send(200, "text/plain", response); + webServer.send(statusCode, "text/plain", response); } static String getServerSyncData(bool localServer) { diff --git a/src/AirGradient.h b/src/AirGradient.h index f592149..a7eea27 100644 --- a/src/AirGradient.h +++ b/src/AirGradient.h @@ -15,7 +15,7 @@ /** * @brief RGB LED bar mode for ONE_INDOOR board - * + * */ enum UseLedBar { UseLedBarOff, /** Don't use LED bar */ @@ -23,6 +23,12 @@ enum UseLedBar { UseLedBarCO2, /** Use LED bar for CO2 */ }; +enum ConfigurationControl { + Local, /** Allow set configuration from local over HTTP server */ + Cloud, /** Allow set configuration from Airgradient webserver */ + Both /** Allow set configuration from Local and Cloud */ +}; + /** * @brief Class with define all the sensor has supported by Airgradient. Each * sensor usage must be init before use.