Skip to content

Commit

Permalink
Feature: HTTP power meter: support changing sign
Browse files Browse the repository at this point in the history
  • Loading branch information
schlimmchen committed Apr 18, 2024
1 parent ede1abb commit abe01ae
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions include/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ struct POWERMETER_HTTP_PHASE_CONFIG_T {
uint16_t Timeout;
char JsonPath[POWERMETER_MAX_HTTP_JSON_PATH_STRLEN + 1];
Unit PowerUnit;
bool SignInverted;
};
using PowerMeterHttpConfig = struct POWERMETER_HTTP_PHASE_CONFIG_T;

Expand Down
2 changes: 1 addition & 1 deletion include/HttpPowerMeter.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class HttpPowerMeterClass {
String extractParam(String& authReq, const String& param, const char delimit);
String getcNonce(const int len);
String getDigestAuth(String& authReq, const String& username, const String& password, const String& method, const String& uri, unsigned int counter);
bool tryGetFloatValueForPhase(int phase, const char* jsonPath, Unit_t unit);
bool tryGetFloatValueForPhase(int phase, const char* jsonPath, Unit_t unit, bool signInverted);
void prepareRequest(uint32_t timeout, const char* httpHeader, const char* httpValue);
String sha256(const String& data);
};
Expand Down
2 changes: 2 additions & 0 deletions src/Configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ bool ConfigurationClass::write()
powermeter_phase["timeout"] = config.PowerMeter.Http_Phase[i].Timeout;
powermeter_phase["json_path"] = config.PowerMeter.Http_Phase[i].JsonPath;
powermeter_phase["unit"] = config.PowerMeter.Http_Phase[i].PowerUnit;
powermeter_phase["sign_inverted"] = config.PowerMeter.Http_Phase[i].SignInverted;
}

JsonObject powerlimiter = doc.createNestedObject("powerlimiter");
Expand Down Expand Up @@ -429,6 +430,7 @@ bool ConfigurationClass::read()
config.PowerMeter.Http_Phase[i].Timeout = powermeter_phase["timeout"] | POWERMETER_HTTP_TIMEOUT;
strlcpy(config.PowerMeter.Http_Phase[i].JsonPath, powermeter_phase["json_path"] | "", sizeof(config.PowerMeter.Http_Phase[i].JsonPath));
config.PowerMeter.Http_Phase[i].PowerUnit = powermeter_phase["unit"] | PowerMeterHttpConfig::Unit::Watts;
config.PowerMeter.Http_Phase[i].SignInverted = powermeter_phase["sign_inverted"] | false;
}

JsonObject powerlimiter = doc["powerlimiter"];
Expand Down
13 changes: 9 additions & 4 deletions src/HttpPowerMeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ bool HttpPowerMeterClass::updateValues()
continue;
}

if(!tryGetFloatValueForPhase(i, phaseConfig.JsonPath, phaseConfig.PowerUnit)) {
if(!tryGetFloatValueForPhase(i, phaseConfig.JsonPath, phaseConfig.PowerUnit, phaseConfig.SignInverted)) {
MessageOutput.printf("[HttpPowerMeter] Getting the power of phase %d (from JSON fetched with Phase 1 config) failed.\r\n", i + 1);
MessageOutput.printf("%s\r\n", httpPowerMeterError);
return false;
Expand Down Expand Up @@ -161,7 +161,7 @@ bool HttpPowerMeterClass::httpRequest(int phase, WiFiClient &wifiClient, const S

// TODO(schlimmchen): postpone calling tryGetFloatValueForPhase, as it
// will be called twice for each phase when doing separate requests.
return tryGetFloatValueForPhase(phase, config.JsonPath, config.PowerUnit);
return tryGetFloatValueForPhase(phase, config.JsonPath, config.PowerUnit, config.SignInverted);
}

String HttpPowerMeterClass::extractParam(String& authReq, const String& param, const char delimit) {
Expand Down Expand Up @@ -219,7 +219,7 @@ String HttpPowerMeterClass::getDigestAuth(String& authReq, const String& usernam
return authorization;
}

bool HttpPowerMeterClass::tryGetFloatValueForPhase(int phase, const char* jsonPath, Unit_t unit)
bool HttpPowerMeterClass::tryGetFloatValueForPhase(int phase, const char* jsonPath, Unit_t unit, bool signInverted)
{
FirebaseJson json;
json.setJsonData(httpResponse);
Expand All @@ -229,7 +229,9 @@ bool HttpPowerMeterClass::tryGetFloatValueForPhase(int phase, const char* jsonPa
return false;
}

power[phase] = value.to<float>(); // this is supposed to be in Watts
// this value is supposed to be in Watts and positive if energy is consumed.
power[phase] = value.to<float>();

switch (unit) {
case Unit_t::MilliWatts:
power[phase] /= 1000;
Expand All @@ -240,6 +242,9 @@ bool HttpPowerMeterClass::tryGetFloatValueForPhase(int phase, const char* jsonPa
default:
break;
}

if (signInverted) { power[phase] *= -1; }

return true;
}

Expand Down
2 changes: 2 additions & 0 deletions src/WebApi_powermeter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void WebApiPowerMeterClass::decodeJsonPhaseConfig(JsonObject const& json, PowerM
config.Timeout = json["timeout"].as<uint16_t>();
strlcpy(config.JsonPath, json["json_path"].as<String>().c_str(), sizeof(config.JsonPath));
config.PowerUnit = json["unit"].as<PowerMeterHttpConfig::Unit>();
config.SignInverted = json["sign_inverted"].as<bool>();
}

void WebApiPowerMeterClass::onStatus(AsyncWebServerRequest* request)
Expand Down Expand Up @@ -75,6 +76,7 @@ void WebApiPowerMeterClass::onStatus(AsyncWebServerRequest* request)
phaseObject["timeout"] = config.PowerMeter.Http_Phase[i].Timeout;
phaseObject["json_path"] = String(config.PowerMeter.Http_Phase[i].JsonPath);
phaseObject["unit"] = config.PowerMeter.Http_Phase[i].PowerUnit;
phaseObject["sign_inverted"] = config.PowerMeter.Http_Phase[i].SignInverted;
}

response->setLength();
Expand Down
2 changes: 2 additions & 0 deletions webapp/src/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,8 @@
"httpJsonPath": "JSON Pfad",
"httpJsonPathDescription": "JSON Pfad um den Leistungswert zu finden. Es verwendet die Selektions-Syntax von mobizt/FirebaseJson. Beispiele gibt es oben.",
"httpUnit": "Einheit",
"httpSignInverted": "Vorzeichen umkehren",
"httpSignInvertedHint": "Positive Werte werden als Leistungsabnahme aus dem Netz interpretiert. Diese Option muss aktiviert werden, wenn das Vorzeichen des Wertes die gegenteilige Bedeutung hat.",
"httpTimeout": "Timeout",
"testHttpRequest": "Testen"
},
Expand Down
2 changes: 2 additions & 0 deletions webapp/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,8 @@
"httpJsonPath": "JSON path",
"httpJsonPathDescription": "JSON path to find the power value in the response. This uses the JSON path query syntax from mobizt/FirebaseJson. See above for examples.",
"httpUnit": "Unit",
"httpSignInverted": "Change Sign",
"httpSignInvertedHint": "Is is expected that positive values denote power usage from the grid. Check this option if the sign of this value has the opposite meaning.",
"httpTimeout": "Timeout",
"testHttpRequest": "Run test",
"milliSeconds": "ms"
Expand Down
1 change: 1 addition & 0 deletions webapp/src/types/PowerMeterConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface PowerMeterHttpPhaseConfig {
json_path: string;
timeout: number;
unit: number;
sign_inverted: boolean;
}

export interface PowerMeterConfig {
Expand Down
6 changes: 6 additions & 0 deletions webapp/src/views/PowerMeterAdminView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@
</div>
</div>

<InputElement
:label="$t('powermeteradmin.httpSignInverted')"
v-model="http_phase.sign_inverted"
:tooltip="$t('powermeteradmin.httpSignInvertedHint')"
type="checkbox" />

<div class="text-center mb-3">
<button type="button" class="btn btn-danger" @click="testHttpRequest(index)">
{{ $t('powermeteradmin.testHttpRequest') }}
Expand Down

0 comments on commit abe01ae

Please sign in to comment.