Skip to content

Commit

Permalink
Merge branch 'helgeerbe/issue438' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
helgeerbe committed Sep 18, 2023
2 parents 9d6b459 + f289322 commit ed2a189
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
6 changes: 5 additions & 1 deletion include/HttpPowerMeter.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <stdint.h>
#include <Arduino.h>
#include <HTTPClient.h>

class HttpPowerMeterClass {
public:
Expand All @@ -14,9 +15,12 @@ class HttpPowerMeterClass {
float getFloatValueByJsonPath(const char* jsonString, const char* jsonPath, float &value);

private:
float power[POWERMETER_MAX_PHASES];
void extractUrlComponents(const String& url, String& protocol, String& hostname, String& uri);
void prepareRequest(uint32_t timeout, const char* httpHeader, const char* httpValue);
HTTPClient httpClient;
float power[POWERMETER_MAX_PHASES];
String sha256(const String& data);

};

extern HttpPowerMeterClass HttpPowerMeter;
37 changes: 22 additions & 15 deletions src/HttpPowerMeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "HttpPowerMeter.h"
#include "MessageOutput.h"
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include <FirebaseJson.h>
#include <Crypto.h>
#include <SHA256.h>
Expand Down Expand Up @@ -87,30 +86,19 @@ bool HttpPowerMeterClass::httpRequest(const char* url, Auth authType, const char
wifiClient = std::make_unique<WiFiClient>();
}

HTTPClient httpClient;

if (!httpClient.begin(*wifiClient, newUrl)) {
snprintf_P(error, errorSize, "httpClient.begin(%s) failed", newUrl.c_str());
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);
}

prepareRequest(timeout, httpHeader, httpValue);

if (authType == Auth::digest) {
const char *headers[1] = {"WWW-Authenticate"};
httpClient.collectHeaders(headers, 1);
}

int httpCode = httpClient.GET();

if (httpCode == HTTP_CODE_UNAUTHORIZED && authType == Auth::digest) {
// Handle authentication challenge
char realm[256]; // Buffer to store the realm received from the server
Expand Down Expand Up @@ -160,6 +148,12 @@ bool HttpPowerMeterClass::httpRequest(const char* url, Auth authType, const char
authorization += "\", nc=00000001, qop=auth, response=\"";
authorization += response;
authorization += "\", algorithm=SHA-256";
httpClient.end();
if (!httpClient.begin(*wifiClient, newUrl)) {
snprintf_P(error, errorSize, "httpClient.begin(%s) for digest auth failed", newUrl.c_str());
return false;
}
prepareRequest(timeout, httpHeader, httpValue);
httpClient.addHeader("Authorization", authorization);
httpCode = httpClient.GET();
}
Expand Down Expand Up @@ -259,4 +253,17 @@ String HttpPowerMeterClass::sha256(const String& data) {
return hashStr;
}

void HttpPowerMeterClass::prepareRequest(uint32_t timeout, const char* httpHeader, const char* httpValue) {
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);
}
}

HttpPowerMeterClass HttpPowerMeter;

0 comments on commit ed2a189

Please sign in to comment.