forked from tbnobody/OpenDTU
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement HTTP(s) + JSON type Power Meter support
- Loading branch information
Bernhard Kaszt
committed
Apr 1, 2023
1 parent
b3c17c8
commit cf40e78
Showing
16 changed files
with
637 additions
and
81 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
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
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,19 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
#pragma once | ||
|
||
#include <stdint.h> | ||
|
||
class HttpPowerMeterClass { | ||
public: | ||
void init(); | ||
bool updateValues(); | ||
float getPower(int8_t phase); | ||
bool httpRequest(const char* url, const char* httpHeader, const char* httpValue, uint32_t timeout, | ||
char* response, uint32_t responseSize, char* error, uint32_t errorSize); | ||
float getFloatValueByJsonPath(const char* jsonString, const char* jsonPath, float &value); | ||
|
||
private: | ||
float power[POWERMETER_MAX_PHASES]; | ||
}; | ||
|
||
extern HttpPowerMeterClass HttpPowerMeter; |
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
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
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
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
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,127 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
#include "Configuration.h" | ||
#include "HttpPowerMeter.h" | ||
#include "MessageOutput.h" | ||
#include <WiFiClientSecure.h> | ||
#include <HTTPClient.h> | ||
#include <FirebaseJson.h> | ||
|
||
void HttpPowerMeterClass::init() | ||
{ | ||
} | ||
|
||
float HttpPowerMeterClass::getPower(int8_t phase) | ||
{ | ||
return power[phase - 1]; | ||
} | ||
|
||
bool HttpPowerMeterClass::updateValues() | ||
{ | ||
const CONFIG_T& config = Configuration.get(); | ||
|
||
char response[2000], | ||
errorMessage[256]; | ||
bool success = true; | ||
|
||
for (uint8_t i = 0; i < POWERMETER_MAX_PHASES; i++) { | ||
POWERMETER_HTTP_PHASE_CONFIG_T phaseConfig = config.Powermeter_Http_Phase[i]; | ||
|
||
if (!phaseConfig.Enabled || !success) { | ||
power[i] = 0.0; | ||
continue; | ||
} | ||
|
||
if (i == 0 || config.PowerMeter_HttpIndividualRequests) { | ||
if (!httpRequest(phaseConfig.Url, phaseConfig.HeaderKey, phaseConfig.HeaderValue, phaseConfig.Timeout, | ||
response, sizeof(response), errorMessage, sizeof(errorMessage))) { | ||
MessageOutput.printf("[HttpPowerMeter] Getting the power of phase %d failed. Error: %s\n", | ||
i + 1, errorMessage); | ||
success = false; | ||
} | ||
} | ||
|
||
if (!getFloatValueByJsonPath(response, phaseConfig.JsonPath, power[i])) { | ||
MessageOutput.printf("[HttpPowerMeter] Couldn't find a value with Json query \"%s\"\n", phaseConfig.JsonPath); | ||
success = false; | ||
} | ||
} | ||
|
||
return success; | ||
} | ||
|
||
bool HttpPowerMeterClass::httpRequest(const char* url, const char* httpHeader, const char* httpValue, uint32_t timeout, | ||
char* response, uint32_t responseSize, char* error, uint32_t errorSize) | ||
{ | ||
WiFiClient* wifiClient = NULL; | ||
HTTPClient httpClient; | ||
|
||
response[0] = '\0'; | ||
error[0] = '\0'; | ||
|
||
if (String(url).substring(0, 6) == "https:") { | ||
wifiClient = new WiFiClientSecure; | ||
((WiFiClientSecure*)wifiClient)->setInsecure(); | ||
} else { | ||
wifiClient = new WiFiClient; | ||
} | ||
|
||
if (!httpClient.begin(*wifiClient, url)) { | ||
snprintf_P(error, errorSize, "httpClient.begin failed"); | ||
delete wifiClient; | ||
return false; | ||
} | ||
|
||
httpClient.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); | ||
httpClient.setUserAgent("OpenDTU-OnBattery"); | ||
httpClient.setConnectTimeout(timeout); | ||
httpClient.setTimeout(timeout); | ||
httpClient.addHeader("Content-Type", "application/json"); | ||
httpClient.addHeader("Accept", "application/json"); | ||
|
||
if (strlen(httpHeader) > 0) { | ||
httpClient.addHeader(httpHeader, httpValue); | ||
} | ||
|
||
int httpCode = httpClient.GET(); | ||
|
||
|
||
if (httpCode == HTTP_CODE_OK) { | ||
if (httpClient.getSize() > (responseSize - 1)) { | ||
snprintf_P(error, errorSize, "Response too large!"); | ||
} else { | ||
strcpy(response, httpClient.getString().c_str()); | ||
} | ||
} else if (httpCode <= 0) { | ||
snprintf_P(error, errorSize, "Error: %s", httpClient.errorToString(httpCode).c_str()); | ||
} else if (httpCode != HTTP_CODE_OK) { | ||
snprintf_P(error, errorSize, "Bad HTTP code: %d", httpCode); | ||
} | ||
|
||
httpClient.end(); | ||
delete wifiClient; | ||
|
||
if (error[0] != '\0') { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
float HttpPowerMeterClass::getFloatValueByJsonPath(const char* jsonString, const char* jsonPath, float& value) | ||
{ | ||
FirebaseJson firebaseJson; | ||
firebaseJson.setJsonData(jsonString); | ||
|
||
FirebaseJsonData firebaseJsonResult; | ||
if (!firebaseJson.get(firebaseJsonResult, jsonPath)) { | ||
return false; | ||
} | ||
|
||
value = firebaseJsonResult.to<float>(); | ||
|
||
firebaseJson.clear(); | ||
|
||
return true; | ||
} | ||
|
||
HttpPowerMeterClass HttpPowerMeter; |
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
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
Oops, something went wrong.