Merge branch 'helgeerbe/issue438' into development
This commit is contained in:
commit
ed2a189a61
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <HTTPClient.h>
|
||||||
|
|
||||||
class HttpPowerMeterClass {
|
class HttpPowerMeterClass {
|
||||||
public:
|
public:
|
||||||
@ -14,9 +15,12 @@ public:
|
|||||||
float getFloatValueByJsonPath(const char* jsonString, const char* jsonPath, float &value);
|
float getFloatValueByJsonPath(const char* jsonString, const char* jsonPath, float &value);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float power[POWERMETER_MAX_PHASES];
|
|
||||||
void extractUrlComponents(const String& url, String& protocol, String& hostname, String& uri);
|
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);
|
String sha256(const String& data);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
extern HttpPowerMeterClass HttpPowerMeter;
|
extern HttpPowerMeterClass HttpPowerMeter;
|
||||||
|
|||||||
@ -3,7 +3,6 @@
|
|||||||
#include "HttpPowerMeter.h"
|
#include "HttpPowerMeter.h"
|
||||||
#include "MessageOutput.h"
|
#include "MessageOutput.h"
|
||||||
#include <WiFiClientSecure.h>
|
#include <WiFiClientSecure.h>
|
||||||
#include <HTTPClient.h>
|
|
||||||
#include <FirebaseJson.h>
|
#include <FirebaseJson.h>
|
||||||
#include <Crypto.h>
|
#include <Crypto.h>
|
||||||
#include <SHA256.h>
|
#include <SHA256.h>
|
||||||
@ -87,30 +86,19 @@ bool HttpPowerMeterClass::httpRequest(const char* url, Auth authType, const char
|
|||||||
wifiClient = std::make_unique<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());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
prepareRequest(timeout, httpHeader, 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
int httpCode = httpClient.GET();
|
int httpCode = httpClient.GET();
|
||||||
|
|
||||||
if (httpCode == HTTP_CODE_UNAUTHORIZED && authType == Auth::digest) {
|
if (httpCode == HTTP_CODE_UNAUTHORIZED && authType == Auth::digest) {
|
||||||
// Handle authentication challenge
|
// Handle authentication challenge
|
||||||
char realm[256]; // Buffer to store the realm received from the server
|
char realm[256]; // Buffer to store the realm received from the server
|
||||||
@ -160,6 +148,12 @@ bool HttpPowerMeterClass::httpRequest(const char* url, Auth authType, const char
|
|||||||
authorization += "\", nc=00000001, qop=auth, response=\"";
|
authorization += "\", nc=00000001, qop=auth, response=\"";
|
||||||
authorization += response;
|
authorization += response;
|
||||||
authorization += "\", algorithm=SHA-256";
|
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);
|
httpClient.addHeader("Authorization", authorization);
|
||||||
httpCode = httpClient.GET();
|
httpCode = httpClient.GET();
|
||||||
}
|
}
|
||||||
@ -259,4 +253,17 @@ String HttpPowerMeterClass::sha256(const String& data) {
|
|||||||
return hashStr;
|
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;
|
HttpPowerMeterClass HttpPowerMeter;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user