-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
1,029 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
#include "AgApiClient.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) {} | ||
|
||
AgApiClient::~AgApiClient() {} | ||
|
||
/** | ||
* @brief Initialize the API client | ||
* | ||
*/ | ||
void AgApiClient::begin(void) { | ||
getConfigFailed = false; | ||
postToServerFailed = false; | ||
printLog("Begin"); | ||
} | ||
|
||
/** | ||
* @brief Get configuration from AirGradient cloud | ||
* | ||
* @param deviceId Device ID | ||
* @return true Success | ||
* @return false Failure | ||
*/ | ||
bool AgApiClient::fetchServerConfiguration(String deviceId) { | ||
if (config.getConfigurationControl() == | ||
ConfigurationControl::ConfigurationControlLocal) { | ||
printLog("Ignore fetch server configuratoin"); | ||
|
||
// Clear server configuration failed flag, cause it's ignore but not | ||
// really failed | ||
getConfigFailed = false; | ||
return false; | ||
} | ||
|
||
String uri = "http://hw.airgradient.com/sensors/airgradient:" + deviceId + | ||
"/one/config"; | ||
|
||
/** Init http client */ | ||
HTTPClient client; | ||
if (client.begin(uri) == false) { | ||
getConfigFailed = true; | ||
return false; | ||
} | ||
|
||
/** Get data */ | ||
int retCode = client.GET(); | ||
if (retCode != 200) { | ||
client.end(); | ||
getConfigFailed = true; | ||
return false; | ||
} | ||
|
||
/** clear failed */ | ||
getConfigFailed = false; | ||
|
||
/** Get response string */ | ||
String respContent = client.getString(); | ||
client.end(); | ||
|
||
printLog("Server configuration: " + respContent); | ||
|
||
/** Parse configuration and return result */ | ||
return config.parse(respContent, false); | ||
} | ||
|
||
/** | ||
* @brief Post data to AirGradient cloud | ||
* | ||
* @param deviceId Device Id | ||
* @param data String JSON | ||
* @return true Success | ||
* @return false Failure | ||
*/ | ||
bool AgApiClient::postToServer(String deviceId, String data) { | ||
if (config.isPostDataToAirGradient() == false) { | ||
printLog("Ignore post data to server"); | ||
return true; | ||
} | ||
|
||
if (WiFi.isConnected() == false) { | ||
return false; | ||
} | ||
|
||
String uri = | ||
"http://hw.airgradient.com/sensors/airgradient:" + deviceId + "/measures"; | ||
printLog("Post uri: " + uri); | ||
printLog("Post data: " + data); | ||
|
||
WiFiClient wifiClient; | ||
HTTPClient client; | ||
if (client.begin(wifiClient, uri.c_str()) == false) { | ||
return false; | ||
} | ||
client.addHeader("content-type", "application/json"); | ||
int retCode = client.POST(data); | ||
client.end(); | ||
|
||
if ((retCode == 200) || (retCode == 429)) { | ||
postToServerFailed = false; | ||
return true; | ||
} else { | ||
printLog("Post response failed code: " + String(retCode)); | ||
} | ||
postToServerFailed = true; | ||
return false; | ||
} | ||
|
||
/** | ||
* @brief Get failed status when get configuration from AirGradient cloud | ||
* | ||
* @return true Success | ||
* @return false Failure | ||
*/ | ||
bool AgApiClient::isFetchConfigureFailed(void) { return getConfigFailed; } | ||
|
||
/** | ||
* @brief Get failed status when post data to AirGradient cloud | ||
* | ||
* @return true Success | ||
* @return false Failure | ||
*/ | ||
bool AgApiClient::isPostToServerFailed(void) { return postToServerFailed; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* @file AgApiClient.h | ||
* @brief HTTP client connect post data to Aigradient cloud. | ||
* @version 0.1 | ||
* @date 2024-Apr-02 | ||
* | ||
* @copyright Copyright (c) 2024 | ||
* | ||
*/ | ||
|
||
#ifndef _AG_API_CLIENT_H_ | ||
#define _AG_API_CLIENT_H_ | ||
|
||
#include "AgConfigure.h" | ||
#include <Arduino.h> | ||
|
||
class AgApiClient { | ||
private: | ||
Stream &debugLog; | ||
AgConfigure &config; | ||
|
||
bool getConfigFailed;; | ||
bool postToServerFailed; | ||
|
||
void printLog(String log); | ||
|
||
public: | ||
AgApiClient(Stream &stream, AgConfigure &config); | ||
~AgApiClient(); | ||
|
||
void begin(void); | ||
bool fetchServerConfiguration(String deviceId); | ||
bool postToServer(String deviceId, String data); | ||
bool isFetchConfigureFailed(void); | ||
bool isPostToServerFailed(void); | ||
}; | ||
|
||
#endif /** _AG_API_CLIENT_H_ */ |
Oops, something went wrong.