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 "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,10 +397,13 @@ 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,7 +415,8 @@ 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

View File

@ -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);

View File

@ -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) {

View File

@ -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.