change locallyControlled to configurationControl and update relate logic

This commit is contained in:
Phat Nguyen
2024-04-01 09:15:10 +07:00
parent 260e904326
commit 3788aa2746
4 changed files with 68 additions and 30 deletions

View File

@ -1,6 +1,9 @@
#include "LocalConfig.h" #include "LocalConfig.h"
#include "EEPROM.h" #include "EEPROM.h"
const char *CONFIGURATION_CONTROL_NAME[] = {
[Local] = "local", [Cloud] = "cloud", [Both] = "both"};
void LocalConfig::printLog(String log) { void LocalConfig::printLog(String log) {
debugLog.printf("[LocalConfig] %s\r\n", log.c_str()); debugLog.printf("[LocalConfig] %s\r\n", log.c_str());
} }
@ -54,10 +57,10 @@ void LocalConfig::defaultConfig(void) {
// Default MQTT broker is null. // Default MQTT broker is null.
memset(config.mqttBroker, 0, sizeof(config.mqttBroker)); memset(config.mqttBroker, 0, sizeof(config.mqttBroker));
config.configurationControl = ConfigurationControl::Both;
config.inUSAQI = false; // pmStandard = ugm3 config.inUSAQI = false; // pmStandard = ugm3
config.inF = false; config.inF = false;
config.postDataToAirGradient = true; config.postDataToAirGradient = true;
config.locallyControlled = true;
config.displayMode = true; config.displayMode = true;
config.useRGBLedBar = UseLedBar::UseLedBarCO2; config.useRGBLedBar = UseLedBar::UseLedBarCO2;
config.abcDays = 7; config.abcDays = 7;
@ -102,6 +105,37 @@ bool LocalConfig::parse(String data, bool isLocal) {
/** Is configuration changed */ /** Is configuration changed */
bool changed = false; 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") { if (JSON.typeof_(root["country"]) == "string") {
String country = root["country"]; String country = root["country"];
if (country.length() == 2) { 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 */ /** Parse data only got from AirGradient server */
if (isLocal == false) { if (isLocal == false) {
if (JSON.typeof_(root["model"]) == "string") { if (JSON.typeof_(root["model"]) == "string") {
@ -339,8 +361,9 @@ String LocalConfig::toString(void) {
/** "temperatureUnit" */ /** "temperatureUnit" */
root["temperatureUnit"] = String(config.temperatureUnit); root["temperatureUnit"] = String(config.temperatureUnit);
/** "locallyControlled" */ /** configurationControl */
root["locallyControlled"] = config.locallyControlled; root["configurationControl"] =
String(CONFIGURATION_CONTROL_NAME[config.configurationControl]);
/** "postDataToAirGradient" */ /** "postDataToAirGradient" */
root["postDataToAirGradient"] = config.postDataToAirGradient; root["postDataToAirGradient"] = config.postDataToAirGradient;
@ -374,11 +397,14 @@ bool LocalConfig::isPostDataToAirGradient(void) {
return config.postDataToAirGradient; 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 true Requested
* @return false Not 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 true Requested
* @return false Not requested * @return false Not requested
*/ */
@ -411,7 +438,7 @@ void LocalConfig::reset(void) {
/** /**
* @brief Get model name, it's usage for offline mode * @brief Get model name, it's usage for offline mode
* *
* @return String * @return String
*/ */
String LocalConfig::getModel(void) { return String(config.model); } String LocalConfig::getModel(void) { return String(config.model); }

View File

@ -17,7 +17,7 @@ private:
bool postDataToAirGradient; /** If true, monitor will not POST data to bool postDataToAirGradient; /** If true, monitor will not POST data to
airgradient server. Make sure no error airgradient server. Make sure no error
message shown on monitor */ message shown on monitor */
bool locallyControlled; /** If true, configuration from airgradient server uint8_t configurationControl; /** If true, configuration from airgradient server
will be ignored */ will be ignored */
bool displayMode; /** true if enable display */ bool displayMode; /** true if enable display */
uint8_t useRGBLedBar; uint8_t useRGBLedBar;
@ -56,7 +56,7 @@ public:
bool getDisplayMode(void); bool getDisplayMode(void);
String getMqttBrokerUri(void); String getMqttBrokerUri(void);
bool isPostDataToAirGradient(void); bool isPostDataToAirGradient(void);
bool isLocallyControlled(void); ConfigurationControl getConfigurationControl(void);
bool isCo2CalibrationRequested(void); bool isCo2CalibrationRequested(void);
bool isLedBarTestRequested(void); bool isLedBarTestRequested(void);
void reset(void); void reset(void);

View File

@ -168,7 +168,6 @@ public:
/** /**
* @brief Initialize airgradient server, it's load the server configuration if * @brief Initialize airgradient server, it's load the server configuration if
* failed load it to default. * failed load it to default.
*
*/ */
void begin(void) { void begin(void) {
configFailed = false; configFailed = false;
@ -184,8 +183,12 @@ public:
* @return false Failure * @return false Failure
*/ */
bool fetchServerConfiguration(String id) { bool fetchServerConfiguration(String id) {
if (config.isLocallyControlled()) { if (config.getConfigurationControl() == ConfigurationControl::Local) {
Serial.println("Ignore fetch server configuration"); Serial.println("Ignore fetch server configuration");
// Clear server configuration failed flag, cause it's ignore but not
// really failed
configFailed = false;
return false; return false;
} }
@ -985,14 +988,16 @@ static void localConfigGet() {
} }
static void localConfigPut() { static void localConfigPut() {
String data = webServer.arg(0); String data = webServer.arg(0);
String response = "Failure"; String response = "";
int statusCode = 400; // Status code for data invalid
if (localConfig.parse(data, true)) { if (localConfig.parse(data, true)) {
localConfigUpdate = true; localConfigUpdate = true;
statusCode = 200;
response = "Success"; response = "Success";
} else { } 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) { static String getServerSyncData(bool localServer) {

View File

@ -15,7 +15,7 @@
/** /**
* @brief RGB LED bar mode for ONE_INDOOR board * @brief RGB LED bar mode for ONE_INDOOR board
* *
*/ */
enum UseLedBar { enum UseLedBar {
UseLedBarOff, /** Don't use LED bar */ UseLedBarOff, /** Don't use LED bar */
@ -23,6 +23,12 @@ enum UseLedBar {
UseLedBarCO2, /** Use LED bar for CO2 */ 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 * @brief Class with define all the sensor has supported by Airgradient. Each
* sensor usage must be init before use. * sensor usage must be init before use.