Reworked wifiClient handling in Power Meter httpRequest and smaller update to Power Meter updateValue method (#430)

This commit is contained in:
MalteSchm 2023-09-12 19:14:55 +02:00 committed by GitHub
parent d984912d7c
commit 0cb42a6424
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,6 +7,7 @@
#include <FirebaseJson.h> #include <FirebaseJson.h>
#include <Crypto.h> #include <Crypto.h>
#include <SHA256.h> #include <SHA256.h>
#include <memory>
void HttpPowerMeterClass::init() void HttpPowerMeterClass::init()
{ {
@ -23,39 +24,36 @@ bool HttpPowerMeterClass::updateValues()
char response[2000], char response[2000],
errorMessage[256]; errorMessage[256];
bool success = true;
for (uint8_t i = 0; i < POWERMETER_MAX_PHASES; i++) { for (uint8_t i = 0; i < POWERMETER_MAX_PHASES; i++) {
POWERMETER_HTTP_PHASE_CONFIG_T phaseConfig = config.Powermeter_Http_Phase[i]; POWERMETER_HTTP_PHASE_CONFIG_T phaseConfig = config.Powermeter_Http_Phase[i];
if (!phaseConfig.Enabled || !success) { if (!phaseConfig.Enabled) {
power[i] = 0.0; power[i] = 0.0;
continue; continue;
} }
if (i == 0 || config.PowerMeter_HttpIndividualRequests) { if (i == 0 || config.PowerMeter_HttpIndividualRequests) {
if (!httpRequest(phaseConfig.Url, phaseConfig.AuthType, phaseConfig.Username, phaseConfig.Password, phaseConfig.HeaderKey, phaseConfig.HeaderValue, phaseConfig.Timeout, if (httpRequest(phaseConfig.Url, phaseConfig.AuthType, phaseConfig.Username, phaseConfig.Password, phaseConfig.HeaderKey, phaseConfig.HeaderValue, phaseConfig.Timeout,
response, sizeof(response), errorMessage, sizeof(errorMessage))) { response, sizeof(response), errorMessage, sizeof(errorMessage))) {
MessageOutput.printf("[HttpPowerMeter] Getting the power of phase %d failed. Error: %s\r\n",
i + 1, errorMessage);
success = false;
}
}
if (!getFloatValueByJsonPath(response, phaseConfig.JsonPath, power[i])) { if (!getFloatValueByJsonPath(response, phaseConfig.JsonPath, power[i])) {
MessageOutput.printf("[HttpPowerMeter] Couldn't find a value with Json query \"%s\"\r\n", phaseConfig.JsonPath); MessageOutput.printf("[HttpPowerMeter] Couldn't find a value with Json query \"%s\"\r\n", phaseConfig.JsonPath);
success = false; return false;
}
} else {
MessageOutput.printf("[HttpPowerMeter] Getting the power of phase %d failed. Error: %s\r\n",
i + 1, errorMessage);
return false;
}
} }
} }
return success; return true;
} }
bool HttpPowerMeterClass::httpRequest(const char* url, Auth authType, const char* username, const char* password, const char* httpHeader, const char* httpValue, uint32_t timeout, bool HttpPowerMeterClass::httpRequest(const char* url, Auth authType, const char* username, const char* password, const char* httpHeader, const char* httpValue, uint32_t timeout,
char* response, size_t responseSize, char* error, size_t errorSize) char* response, size_t responseSize, char* error, size_t errorSize)
{ {
WiFiClient* wifiClient = NULL;
HTTPClient httpClient;
String newUrl = url; String newUrl = url;
String urlProtocol; String urlProtocol;
@ -77,16 +75,21 @@ bool HttpPowerMeterClass::httpRequest(const char* url, Auth authType, const char
newUrl += urlUri; newUrl += urlUri;
} }
// secureWifiClient MUST be created before HTTPClient
// see discussion: https://github.com/helgeerbe/OpenDTU-OnBattery/issues/381
std::unique_ptr<WiFiClient> wifiClient;
if (urlProtocol == "https") { if (urlProtocol == "https") {
wifiClient = new WiFiClientSecure; auto secureWifiClient = std::make_unique<WiFiClientSecure>();
reinterpret_cast<WiFiClientSecure*>(wifiClient)->setInsecure(); secureWifiClient->setInsecure();
wifiClient = std::move(secureWifiClient);
} else { } else {
wifiClient = new WiFiClient; wifiClient = std::make_unique<WiFiClient>();
} }
HTTPClient httpClient;
if (!httpClient.begin(*wifiClient, newUrl)) { if (!httpClient.begin(*wifiClient, newUrl)) {
snprintf_P(error, errorSize, "httpClient.begin(%s) failed", newUrl.c_str()); snprintf_P(error, errorSize, "httpClient.begin(%s) failed", newUrl.c_str());
delete wifiClient;
return false; return false;
} }
@ -179,7 +182,6 @@ bool HttpPowerMeterClass::httpRequest(const char* url, Auth authType, const char
} }
httpClient.end(); httpClient.end();
delete wifiClient;
if (error[0] != '\0') { if (error[0] != '\0') {
return false; return false;