From 0cb42a6424d1732bf1d6b677262c8c844584a9cd Mon Sep 17 00:00:00 2001 From: MalteSchm <88371275+MalteSchm@users.noreply.github.com> Date: Tue, 12 Sep 2023 19:14:55 +0200 Subject: [PATCH] Reworked wifiClient handling in Power Meter httpRequest and smaller update to Power Meter updateValue method (#430) --- src/HttpPowerMeter.cpp | 44 ++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/src/HttpPowerMeter.cpp b/src/HttpPowerMeter.cpp index bf7318479..bae9f14cd 100644 --- a/src/HttpPowerMeter.cpp +++ b/src/HttpPowerMeter.cpp @@ -7,6 +7,7 @@ #include #include #include +#include void HttpPowerMeterClass::init() { @@ -23,39 +24,36 @@ bool HttpPowerMeterClass::updateValues() 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) { + if (!phaseConfig.Enabled) { power[i] = 0.0; - continue; - } + continue; + } 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))) { + if (!getFloatValueByJsonPath(response, phaseConfig.JsonPath, power[i])) { + MessageOutput.printf("[HttpPowerMeter] Couldn't find a value with Json query \"%s\"\r\n", phaseConfig.JsonPath); + return false; + } + } else { MessageOutput.printf("[HttpPowerMeter] Getting the power of phase %d failed. Error: %s\r\n", i + 1, errorMessage); - success = false; + return false; } } - - if (!getFloatValueByJsonPath(response, phaseConfig.JsonPath, power[i])) { - MessageOutput.printf("[HttpPowerMeter] Couldn't find a value with Json query \"%s\"\r\n", phaseConfig.JsonPath); - success = 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, char* response, size_t responseSize, char* error, size_t errorSize) { - WiFiClient* wifiClient = NULL; - HTTPClient httpClient; String newUrl = url; String urlProtocol; @@ -77,17 +75,22 @@ bool HttpPowerMeterClass::httpRequest(const char* url, Auth authType, const char newUrl += urlUri; } + // secureWifiClient MUST be created before HTTPClient + // see discussion: https://github.com/helgeerbe/OpenDTU-OnBattery/issues/381 + std::unique_ptr wifiClient; + if (urlProtocol == "https") { - wifiClient = new WiFiClientSecure; - reinterpret_cast(wifiClient)->setInsecure(); + auto secureWifiClient = std::make_unique(); + secureWifiClient->setInsecure(); + wifiClient = std::move(secureWifiClient); } else { - wifiClient = new WiFiClient; + wifiClient = std::make_unique(); } + HTTPClient httpClient; if (!httpClient.begin(*wifiClient, newUrl)) { - snprintf_P(error, errorSize, "httpClient.begin(%s) failed", newUrl.c_str()); - delete wifiClient; - return false; + snprintf_P(error, errorSize, "httpClient.begin(%s) failed", newUrl.c_str()); + return false; } httpClient.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); @@ -179,7 +182,6 @@ bool HttpPowerMeterClass::httpRequest(const char* url, Auth authType, const char } httpClient.end(); - delete wifiClient; if (error[0] != '\0') { return false;