HTTP power meter: remove trailing whitespace

this commit is empty when inspecting it with --ignore-all-space.
This commit is contained in:
Bernhard Kirchen 2024-03-23 21:33:34 +01:00
parent 054a677575
commit c41d6f5138

View File

@ -21,18 +21,18 @@ float HttpPowerMeterClass::getPower(int8_t phase)
bool HttpPowerMeterClass::updateValues() bool HttpPowerMeterClass::updateValues()
{ {
const CONFIG_T& config = Configuration.get(); const CONFIG_T& config = Configuration.get();
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) { 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 (!queryPhase(i, phaseConfig.Url, phaseConfig.AuthType, phaseConfig.Username, phaseConfig.Password, phaseConfig.HeaderKey, phaseConfig.HeaderValue, phaseConfig.Timeout, if (!queryPhase(i, phaseConfig.Url, phaseConfig.AuthType, phaseConfig.Username, phaseConfig.Password, phaseConfig.HeaderKey, phaseConfig.HeaderValue, phaseConfig.Timeout,
phaseConfig.JsonPath)) { phaseConfig.JsonPath)) {
MessageOutput.printf("[HttpPowerMeter] Getting the power of phase %d failed.\r\n", i + 1); MessageOutput.printf("[HttpPowerMeter] Getting the power of phase %d failed.\r\n", i + 1);
MessageOutput.printf("%s\r\n", httpPowerMeterError); MessageOutput.printf("%s\r\n", httpPowerMeterError);
@ -43,10 +43,10 @@ bool HttpPowerMeterClass::updateValues()
return true; return true;
} }
bool HttpPowerMeterClass::queryPhase(int phase, const String& url, Auth authType, const char* username, const char* password, bool HttpPowerMeterClass::queryPhase(int phase, const String& url, Auth authType, const char* username, const char* password,
const char* httpHeader, const char* httpValue, uint32_t timeout, const char* jsonPath) const char* httpHeader, const char* httpValue, uint32_t timeout, const char* jsonPath)
{ {
//hostByName in WiFiGeneric fails to resolve local names. issue described in //hostByName in WiFiGeneric fails to resolve local names. issue described in
//https://github.com/espressif/arduino-esp32/issues/3822 //https://github.com/espressif/arduino-esp32/issues/3822
//and in depth analyzed in https://github.com/espressif/esp-idf/issues/2507#issuecomment-761836300 //and in depth analyzed in https://github.com/espressif/esp-idf/issues/2507#issuecomment-761836300
//in conclusion: we cannot rely on httpClient.begin(*wifiClient, url) to resolve IP adresses. //in conclusion: we cannot rely on httpClient.begin(*wifiClient, url) to resolve IP adresses.
@ -59,7 +59,7 @@ bool HttpPowerMeterClass::queryPhase(int phase, const String& url, Auth authType
extractUrlComponents(url, protocol, host, uri, port, base64Authorization); extractUrlComponents(url, protocol, host, uri, port, base64Authorization);
IPAddress ipaddr((uint32_t)0); IPAddress ipaddr((uint32_t)0);
//first check if "host" is already an IP adress //first check if "host" is already an IP adress
if (!ipaddr.fromString(host)) if (!ipaddr.fromString(host))
{ {
//"host"" is not an IP address so try to resolve the IP adress //"host"" is not an IP address so try to resolve the IP adress
@ -69,20 +69,20 @@ bool HttpPowerMeterClass::queryPhase(int phase, const String& url, Auth authType
snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("Error resolving host %s via DNS, try to enable mDNS in Network Settings"), host.c_str()); snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("Error resolving host %s via DNS, try to enable mDNS in Network Settings"), host.c_str());
//ensure we try resolving via DNS even if mDNS is disabled //ensure we try resolving via DNS even if mDNS is disabled
if(!WiFiGenericClass::hostByName(host.c_str(), ipaddr)){ if(!WiFiGenericClass::hostByName(host.c_str(), ipaddr)){
snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("Error resolving host %s via DNS"), host.c_str()); snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("Error resolving host %s via DNS"), host.c_str());
} }
} }
else else
{ {
ipaddr = MDNS.queryHost(host); ipaddr = MDNS.queryHost(host);
if (ipaddr == INADDR_NONE){ if (ipaddr == INADDR_NONE){
snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("Error resolving host %s via mDNS"), host.c_str()); snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("Error resolving host %s via mDNS"), host.c_str());
//when we cannot find local server via mDNS, try resolving via DNS //when we cannot find local server via mDNS, try resolving via DNS
if(!WiFiGenericClass::hostByName(host.c_str(), ipaddr)){ if(!WiFiGenericClass::hostByName(host.c_str(), ipaddr)){
snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("Error resolving host %s via DNS"), host.c_str()); snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("Error resolving host %s via DNS"), host.c_str());
} }
} }
} }
} }
// secureWifiClient MUST be created before HTTPClient // secureWifiClient MUST be created before HTTPClient
@ -97,19 +97,19 @@ bool HttpPowerMeterClass::queryPhase(int phase, const String& url, Auth authType
} else { } else {
wifiClient = std::make_unique<WiFiClient>(); wifiClient = std::make_unique<WiFiClient>();
} }
return httpRequest(phase, *wifiClient, ipaddr.toString(), port, uri, https, authType, username, password, httpHeader, httpValue, timeout, jsonPath); return httpRequest(phase, *wifiClient, ipaddr.toString(), port, uri, https, authType, username, password, httpHeader, httpValue, timeout, jsonPath);
} }
bool HttpPowerMeterClass::httpRequest(int phase, WiFiClient &wifiClient, const String& host, uint16_t port, const String& uri, bool https, Auth authType, const char* username, bool HttpPowerMeterClass::httpRequest(int phase, WiFiClient &wifiClient, const String& host, uint16_t port, const String& uri, bool https, Auth authType, const char* username,
const char* password, const char* httpHeader, const char* httpValue, uint32_t timeout, const char* jsonPath) const char* password, const char* httpHeader, const char* httpValue, uint32_t timeout, const char* jsonPath)
{ {
if(!httpClient.begin(wifiClient, host, port, uri, https)){ if(!httpClient.begin(wifiClient, host, port, uri, https)){
snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("httpClient.begin() failed for %s://%s"), (https ? "https" : "http"), host.c_str()); snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("httpClient.begin() failed for %s://%s"), (https ? "https" : "http"), host.c_str());
return false; return false;
} }
prepareRequest(timeout, httpHeader, httpValue); prepareRequest(timeout, httpHeader, httpValue);
if (authType == Auth::digest) { if (authType == Auth::digest) {
const char *headers[1] = {"WWW-Authenticate"}; const char *headers[1] = {"WWW-Authenticate"};
httpClient.collectHeaders(headers, 1); httpClient.collectHeaders(headers, 1);
@ -129,8 +129,8 @@ bool HttpPowerMeterClass::httpRequest(int phase, WiFiClient &wifiClient, const S
String authReq = httpClient.header("WWW-Authenticate"); String authReq = httpClient.header("WWW-Authenticate");
String authorization = getDigestAuth(authReq, String(username), String(password), "GET", String(uri), 1); String authorization = getDigestAuth(authReq, String(username), String(password), "GET", String(uri), 1);
httpClient.end(); httpClient.end();
if(!httpClient.begin(wifiClient, host, port, uri, https)){ if(!httpClient.begin(wifiClient, host, port, uri, https)){
snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("httpClient.begin() failed for %s://%s using digest auth"), (https ? "https" : "http"), host.c_str()); snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("httpClient.begin() failed for %s://%s using digest auth"), (https ? "https" : "http"), host.c_str());
return false; return false;
} }
@ -176,11 +176,11 @@ String HttpPowerMeterClass::getDigestAuth(String& authReq, const String& usernam
//sha256 of method:uri //sha256 of method:uri
String ha2 = sha256(method + ":" + uri); String ha2 = sha256(method + ":" + uri);
//sha256 of h1:nonce:nc:cNonce:auth:h2 //sha256 of h1:nonce:nc:cNonce:auth:h2
String response = sha256(ha1 + ":" + nonce + ":" + String(nc) + ":" + cNonce + ":" + "auth" + ":" + ha2); String response = sha256(ha1 + ":" + nonce + ":" + String(nc) + ":" + cNonce + ":" + "auth" + ":" + ha2);
//Final authorization String; //Final authorization String;
String authorization = "Digest username=\""; String authorization = "Digest username=\"";
authorization += username; authorization += username;
authorization += "\", realm=\""; authorization += "\", realm=\"";
authorization += realm; authorization += realm;
@ -194,7 +194,7 @@ String HttpPowerMeterClass::getDigestAuth(String& authReq, const String& usernam
authorization += String(nc); authorization += String(nc);
authorization += ", qop=auth, response=\""; authorization += ", qop=auth, response=\"";
authorization += response; authorization += response;
authorization += "\", algorithm=SHA-256"; authorization += "\", algorithm=SHA-256";
return authorization; return authorization;
} }
@ -203,7 +203,7 @@ bool HttpPowerMeterClass::tryGetFloatValueForPhase(int phase, int httpCode, cons
{ {
bool success = false; bool success = false;
if (httpCode == HTTP_CODE_OK) { if (httpCode == HTTP_CODE_OK) {
httpResponse = httpClient.getString(); //very unfortunate that we cannot parse WifiClient stream directly httpResponse = httpClient.getString(); //very unfortunate that we cannot parse WifiClient stream directly
FirebaseJson json; FirebaseJson json;
json.setJsonData(httpResponse); json.setJsonData(httpResponse);
FirebaseJsonData value; FirebaseJsonData value;
@ -218,7 +218,7 @@ bool HttpPowerMeterClass::tryGetFloatValueForPhase(int phase, int httpCode, cons
snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("HTTP Error %s"), httpClient.errorToString(httpCode).c_str()); snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("HTTP Error %s"), httpClient.errorToString(httpCode).c_str());
} else if (httpCode != HTTP_CODE_OK) { } else if (httpCode != HTTP_CODE_OK) {
snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("Bad HTTP code: %d"), httpCode); snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("Bad HTTP code: %d"), httpCode);
} }
return success; return success;
} }
@ -234,7 +234,7 @@ bool HttpPowerMeterClass::extractUrlComponents(String url, String& _protocol, St
_protocol = url.substring(0, index); _protocol = url.substring(0, index);
//initialize port to default values for http or https. //initialize port to default values for http or https.
//port will be overwritten below in case port is explicitly defined //port will be overwritten below in case port is explicitly defined
_port = (_protocol == "https" ? 443 : 80); _port = (_protocol == "https" ? 443 : 80);