HTTP power meter: refactor tryGetFloatValueForPhase
unindent code, prepare this method for re-use on the same HTTP response but with a different JSON path.
This commit is contained in:
parent
c41d6f5138
commit
b8d0998a49
@ -25,7 +25,7 @@ private:
|
|||||||
String extractParam(String& authReq, const String& param, const char delimit);
|
String extractParam(String& authReq, const String& param, const char delimit);
|
||||||
String getcNonce(const int len);
|
String getcNonce(const int len);
|
||||||
String getDigestAuth(String& authReq, const String& username, const String& password, const String& method, const String& uri, unsigned int counter);
|
String getDigestAuth(String& authReq, const String& username, const String& password, const String& method, const String& uri, unsigned int counter);
|
||||||
bool tryGetFloatValueForPhase(int phase, int httpCode, const char* jsonPath);
|
bool tryGetFloatValueForPhase(int phase, const char* jsonPath);
|
||||||
void prepareRequest(uint32_t timeout, const char* httpHeader, const char* httpValue);
|
void prepareRequest(uint32_t timeout, const char* httpHeader, const char* httpValue);
|
||||||
String sha256(const String& data);
|
String sha256(const String& data);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -139,9 +139,21 @@ bool HttpPowerMeterClass::httpRequest(int phase, WiFiClient &wifiClient, const S
|
|||||||
httpCode = httpClient.GET();
|
httpCode = httpClient.GET();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bool result = tryGetFloatValueForPhase(phase, httpCode, jsonPath);
|
|
||||||
|
if (httpCode <= 0) {
|
||||||
|
snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("HTTP Error %s"), httpClient.errorToString(httpCode).c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (httpCode != HTTP_CODE_OK) {
|
||||||
|
snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("Bad HTTP code: %d"), httpCode);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
httpResponse = httpClient.getString(); // very unfortunate that we cannot parse WifiClient stream directly
|
||||||
httpClient.end();
|
httpClient.end();
|
||||||
return result;
|
|
||||||
|
return tryGetFloatValueForPhase(phase, jsonPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
String HttpPowerMeterClass::extractParam(String& authReq, const String& param, const char delimit) {
|
String HttpPowerMeterClass::extractParam(String& authReq, const String& param, const char delimit) {
|
||||||
@ -199,27 +211,18 @@ String HttpPowerMeterClass::getDigestAuth(String& authReq, const String& usernam
|
|||||||
return authorization;
|
return authorization;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HttpPowerMeterClass::tryGetFloatValueForPhase(int phase, int httpCode, const char* jsonPath)
|
bool HttpPowerMeterClass::tryGetFloatValueForPhase(int phase, const char* jsonPath)
|
||||||
{
|
{
|
||||||
bool success = false;
|
FirebaseJson json;
|
||||||
if (httpCode == HTTP_CODE_OK) {
|
json.setJsonData(httpResponse);
|
||||||
httpResponse = httpClient.getString(); //very unfortunate that we cannot parse WifiClient stream directly
|
FirebaseJsonData value;
|
||||||
FirebaseJson json;
|
if (!json.get(value, jsonPath)) {
|
||||||
json.setJsonData(httpResponse);
|
snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("[HttpPowerMeter] Couldn't find a value for phase %i with Json query \"%s\""), phase, jsonPath);
|
||||||
FirebaseJsonData value;
|
return false;
|
||||||
if (!json.get(value, jsonPath)) {
|
|
||||||
snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("[HttpPowerMeter] Couldn't find a value for phase %i with Json query \"%s\""), phase, jsonPath);
|
|
||||||
}else {
|
|
||||||
power[phase] = value.to<float>();
|
|
||||||
//MessageOutput.printf("Power for Phase %i: %5.2fW\r\n", phase, power[phase]);
|
|
||||||
success = true;
|
|
||||||
}
|
|
||||||
} else if (httpCode <= 0) {
|
|
||||||
snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("HTTP Error %s"), httpClient.errorToString(httpCode).c_str());
|
|
||||||
} else if (httpCode != HTTP_CODE_OK) {
|
|
||||||
snprintf_P(httpPowerMeterError, sizeof(httpPowerMeterError), PSTR("Bad HTTP code: %d"), httpCode);
|
|
||||||
}
|
}
|
||||||
return success;
|
|
||||||
|
power[phase] = value.to<float>();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//extract url component as done by httpClient::begin(String url, const char* expectedProtocol) https://github.com/espressif/arduino-esp32/blob/da6325dd7e8e152094b19fe63190907f38ef1ff0/libraries/HTTPClient/src/HTTPClient.cpp#L250
|
//extract url component as done by httpClient::begin(String url, const char* expectedProtocol) https://github.com/espressif/arduino-esp32/blob/da6325dd7e8e152094b19fe63190907f38ef1ff0/libraries/HTTPClient/src/HTTPClient.cpp#L250
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user