Skip to content

Commit

Permalink
[temporary commit]
Browse files Browse the repository at this point in the history
  • Loading branch information
pnt325 committed Apr 3, 2024
1 parent c1ab99b commit 027ffea
Show file tree
Hide file tree
Showing 214 changed files with 1,154 additions and 506,046 deletions.
2 changes: 0 additions & 2 deletions examples/OneOpenAir/OneOpenAir.ino
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ CC BY-SA 4.0 Attribution-ShareAlike 4.0 International License
*/

#include "mqtt_client.h"
#include <HTTPClient.h>
#include <HardwareSerial.h>
#include <WiFiManager.h>

Expand Down
24 changes: 11 additions & 13 deletions src/AgApiClient.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#include "AgApiClient.h"
#include "AgConfigure.h"
#include "AirGradient.h"
#include <HTTPClient.h>

void AgApiClient::printLog(String log) {
debugLog.printf("[AgApiClient] %s\r\n", log.c_str());
// Serial.printf("[AgApiClient] %s\r\n", log.c_str());
}

AgApiClient::AgApiClient(Stream &debug, AgConfigure &config)
: debugLog(debug), config(config) {}
:PrintLog(debug, "ApiClient"), config(config) {
}

AgApiClient::~AgApiClient() {}

Expand All @@ -18,7 +16,7 @@ AgApiClient::~AgApiClient() {}
void AgApiClient::begin(void) {
getConfigFailed = false;
postToServerFailed = false;
printLog("Begin");
logInfo("begin");
}

/**
Expand All @@ -31,7 +29,7 @@ void AgApiClient::begin(void) {
bool AgApiClient::fetchServerConfiguration(String deviceId) {
if (config.getConfigurationControl() ==
ConfigurationControl::ConfigurationControlLocal) {
printLog("Ignore fetch server configuratoin");
logWarning("Ignore fetch server configuration");

// Clear server configuration failed flag, cause it's ignore but not
// really failed
Expand Down Expand Up @@ -64,7 +62,7 @@ bool AgApiClient::fetchServerConfiguration(String deviceId) {
String respContent = client.getString();
client.end();

printLog("Server configuration: " + respContent);
logInfo("Get configuration: " + respContent);

/** Parse configuration and return result */
return config.parse(respContent, false);
Expand All @@ -80,7 +78,7 @@ bool AgApiClient::fetchServerConfiguration(String deviceId) {
*/
bool AgApiClient::postToServer(String deviceId, String data) {
if (config.isPostDataToAirGradient() == false) {
printLog("Ignore post data to server");
logWarning("Ignore post data to server");
return true;
}

Expand All @@ -90,8 +88,8 @@ bool AgApiClient::postToServer(String deviceId, String data) {

String uri =
"http://hw.airgradient.com/sensors/airgradient:" + deviceId + "/measures";
printLog("Post uri: " + uri);
printLog("Post data: " + data);
logInfo("Post uri: " + uri);
logInfo("Post data: " + data);

WiFiClient wifiClient;
HTTPClient client;
Expand All @@ -106,7 +104,7 @@ bool AgApiClient::postToServer(String deviceId, String data) {
postToServerFailed = false;
return true;
} else {
printLog("Post response failed code: " + String(retCode));
logError("Post response failed code: " + String(retCode));
}
postToServerFailed = true;
return false;
Expand Down
11 changes: 6 additions & 5 deletions src/AgApiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@
#define _AG_API_CLIENT_H_

#include "AgConfigure.h"
#include "Main/PrintLog.h"
#include <Arduino.h>

class AgApiClient {
class AgConfigure;
class AirGradient;

class AgApiClient : public PrintLog {
private:
Stream &debugLog;
AgConfigure &config;

bool getConfigFailed;;
bool getConfigFailed;
bool postToServerFailed;

void printLog(String log);

public:
AgApiClient(Stream &stream, AgConfigure &config);
~AgApiClient();
Expand Down
68 changes: 32 additions & 36 deletions src/AgConfigure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@

const char *CONFIGURATION_CONTROL_NAME[] = {
[ConfigurationControlLocal] = "local",
[ConfigurationControlCloud] = "cloud",
[ConfigurationControlCloud] = "cloud",
[ConfigurationControlBoth] = "both"};

void AgConfigure::printLog(String log) {
debugLog.printf("[AgConfigure] %s\r\n", log.c_str());
}

String AgConfigure::getLedBarModeName(LedBarMode mode) {
LedBarMode ledBarMode = mode;
if (ledBarMode == LedBarModeOff) {
Expand All @@ -32,12 +28,12 @@ void AgConfigure::saveConfig(void) {
}
EEPROM.writeBytes(0, &config, sizeof(config));
EEPROM.commit();
printLog("Save Config");
logInfo("Save Config");
}

void AgConfigure::loadConfig(void) {
if (EEPROM.readBytes(0, &config, sizeof(config)) != sizeof(config)) {
printLog("Load configure failed");
logError("Load configure failed");
defaultConfig();
} else {
uint32_t sum = 0;
Expand All @@ -48,7 +44,7 @@ void AgConfigure::loadConfig(void) {
}

if (sum != config._check) {
printLog("Configure validate invalid");
logError("Configure validate invalid");
defaultConfig();
}
}
Expand All @@ -73,9 +69,9 @@ void AgConfigure::defaultConfig(void) {
saveConfig();
}

void AgConfigure::printConfig(void) { printLog(toString()); }
void AgConfigure::printConfig(void) { logInfo(toString().c_str()); }

AgConfigure::AgConfigure(Stream &debugLog) : debugLog(debugLog) {}
AgConfigure::AgConfigure(Stream &debugLog) : PrintLog(debugLog, "Configure") {}

AgConfigure::~AgConfigure() {}

Expand All @@ -99,10 +95,10 @@ bool AgConfigure::begin(void) {
bool AgConfigure::parse(String data, bool isLocal) {
JSONVar root = JSON.parse(data);
if (JSON.typeof_(root) == "undefined") {
printLog("Configuration JSON invalid");
logError("Configuration JSON invalid");
return false;
}
printLog("Parse configure success");
logInfo("Parse configure success");

/** Is configuration changed */
bool changed = false;
Expand All @@ -129,7 +125,7 @@ bool AgConfigure::parse(String data, bool isLocal) {
(uint8_t)ConfigurationControl::ConfigurationControlBoth;
changed = true;
} else {
printLog("'configurationControl' value '" + configurationControl +
logError("'configurationControl' value '" + configurationControl +
"' invalid");
return false;
}
Expand All @@ -139,8 +135,8 @@ bool AgConfigure::parse(String data, bool isLocal) {

if ((config.configurationControl ==
(byte)ConfigurationControl::ConfigurationControlCloud)) {
printLog("Ignore, cause ConfigurationControl is " +
String(CONFIGURATION_CONTROL_NAME[config.configurationControl]));
logWarning("Ignore, cause ConfigurationControl is " +
String(CONFIGURATION_CONTROL_NAME[config.configurationControl]));
return false;
}

Expand All @@ -150,7 +146,7 @@ bool AgConfigure::parse(String data, bool isLocal) {
if (country != String(config.country)) {
changed = true;
snprintf(config.country, sizeof(config.country), country.c_str());
printLog("Set country: " + country);
logInfo("Set country: " + country);
}

// Update temperature unit if get configuration from server
Expand All @@ -168,9 +164,9 @@ bool AgConfigure::parse(String data, bool isLocal) {
}
}
} else {
printLog("Country name " + country +
" invalid. Find details here (ALPHA-2): "
"https://www.iban.com/country-codes");
logInfo("Country name " + country +
" invalid. Find details here (ALPHA-2): "
"https://www.iban.com/country-codes");
}
}

Expand All @@ -184,18 +180,18 @@ bool AgConfigure::parse(String data, bool isLocal) {
if (inUSAQI != config.inUSAQI) {
config.inUSAQI = inUSAQI;
changed = true;
printLog("Set PM standard: " + pmStandard);
logInfo("Set PM standard: " + pmStandard);
}
}

if (JSON.typeof_(root["co2CalibrationRequested"]) == "boolean") {
co2CalibrationRequested = root["co2CalibrationRequested"];
printLog("Set co2CalibrationRequested: " + String(co2CalibrationRequested));
logInfo("Set co2CalibrationRequested: " + String(co2CalibrationRequested));
}

if (JSON.typeof_(root["ledBarTestRequested"]) == "boolean") {
ledBarTestRequested = root["ledBarTestRequested"];
printLog("Set ledBarTestRequested: " + String(ledBarTestRequested));
logInfo("Set ledBarTestRequested: " + String(ledBarTestRequested));
}

if (JSON.typeof_(root["ledBarMode"]) == "string") {
Expand All @@ -209,13 +205,13 @@ bool AgConfigure::parse(String data, bool isLocal) {
ledBarMode = LedBarModeOff;
} else {
ledBarMode = config.useRGBLedBar;
printLog("ledBarMode value '" + mode + "' invalid");
logInfo("ledBarMode value '" + mode + "' invalid");
}

if (ledBarMode != config.useRGBLedBar) {
config.useRGBLedBar = ledBarMode;
changed = true;
printLog("Set ledBarMode: " + mode);
logInfo("Set ledBarMode: " + mode);
}
}

Expand All @@ -228,13 +224,13 @@ bool AgConfigure::parse(String data, bool isLocal) {
displayMode = false;
} else {
displayMode = config.displayMode;
printLog("displayMode '" + mode + "' invalid");
logInfo("displayMode '" + mode + "' invalid");
}

if (displayMode != config.displayMode) {
changed = true;
config.displayMode = displayMode;
printLog("Set displayMode: " + mode);
logInfo("Set displayMode: " + mode);
}
}

Expand All @@ -243,7 +239,7 @@ bool AgConfigure::parse(String data, bool isLocal) {
if (abcDays != config.abcDays) {
config.abcDays = abcDays;
changed = true;
printLog("Set abcDays: " + String(abcDays));
logInfo("Set abcDays: " + String(abcDays));
}
}

Expand All @@ -252,7 +248,7 @@ bool AgConfigure::parse(String data, bool isLocal) {
if (tvocLearningOffset != config.tvocLearningOffset) {
changed = true;
config.tvocLearningOffset = tvocLearningOffset;
printLog("Set tvocLearningOffset: " + String(tvocLearningOffset));
logInfo("Set tvocLearningOffset: " + String(tvocLearningOffset));
}
}

Expand All @@ -261,7 +257,7 @@ bool AgConfigure::parse(String data, bool isLocal) {
if (noxLearningOffset != config.noxLearningOffset) {
changed = true;
config.noxLearningOffset = noxLearningOffset;
printLog("Set noxLearningOffset: " + String(noxLearningOffset));
logInfo("Set noxLearningOffset: " + String(noxLearningOffset));
}
}

Expand All @@ -271,10 +267,10 @@ bool AgConfigure::parse(String data, bool isLocal) {
if (broker != String(config.mqttBroker)) {
changed = true;
snprintf(config.mqttBroker, sizeof(config.mqttBroker), broker.c_str());
printLog("Set mqttBrokerUrl: " + broker);
logInfo("Set mqttBrokerUrl: " + broker);
}
} else {
printLog("Error: mqttBroker length invalid: " + String(broker.length()));
logError("Error: mqttBroker length invalid: " + String(broker.length()));
}
}

Expand All @@ -294,9 +290,9 @@ bool AgConfigure::parse(String data, bool isLocal) {
changed = true;
config.temperatureUnit = temperatureUnit;
if (temperatureUnit == 0) {
printLog("set temperatureUnit: null");
logInfo("set temperatureUnit: null");
} else {
printLog("set temperatureUnit: " + String(temperatureUnit));
logInfo("set temperatureUnit: " + String(temperatureUnit));
}
}

Expand All @@ -305,7 +301,7 @@ bool AgConfigure::parse(String data, bool isLocal) {
if (post != config.postDataToAirGradient) {
changed = true;
config.postDataToAirGradient = post;
printLog("Set postDataToAirGradient: " + String(post));
logInfo("Set postDataToAirGradient: " + String(post));
}
}

Expand All @@ -319,7 +315,7 @@ bool AgConfigure::parse(String data, bool isLocal) {
snprintf(config.model, sizeof(config.model), model.c_str());
}
} else {
printLog("Error: modal name length invalid: " + String(model.length()));
logError("Error: modal name length invalid: " + String(model.length()));
}
}
}
Expand Down Expand Up @@ -440,7 +436,7 @@ bool AgConfigure::isLedBarTestRequested(void) {
*/
void AgConfigure::reset(void) {
defaultConfig();
printLog("Reset to default configure");
logInfo("Reset to default configure");
printConfig();
}

Expand Down
19 changes: 9 additions & 10 deletions src/AgConfigure.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#ifndef _AG_CONFIG_H_
#define _AG_CONFIG_H_

#include <AirGradient.h>
#include "App/AppDef.h"
#include "Main/PrintLog.h"
#include <Arduino.h>
#include <Arduino_JSON.h>

class AgConfigure {
class AgConfigure : public PrintLog {
private:
struct Config {
char model[20];
Expand All @@ -14,12 +15,12 @@ class AgConfigure {
bool inUSAQI; /** If PM standard "ugm3" inUSAQI = false, otherwise is true
*/
bool inF; /** Temperature unit F */
bool postDataToAirGradient; /** If true, monitor will not POST data to
airgradient server. Make sure no error
message shown on monitor */
uint8_t configurationControl; /** If true, configuration from airgradient server
will be ignored */
bool displayMode; /** true if enable display */
bool postDataToAirGradient; /** If true, monitor will not POST data to
airgradient server. Make sure no error
message shown on monitor */
uint8_t configurationControl; /** If true, configuration from airgradient
server will be ignored */
bool displayMode; /** true if enable display */
uint8_t useRGBLedBar;
uint8_t abcDays;
int tvocLearningOffset;
Expand All @@ -31,9 +32,7 @@ class AgConfigure {
struct Config config;
bool co2CalibrationRequested;
bool ledBarTestRequested;
Stream &debugLog;

void printLog(String log);
String getLedBarModeName(LedBarMode mode);
void saveConfig(void);
void loadConfig(void);
Expand Down
Loading

0 comments on commit 027ffea

Please sign in to comment.