Browse Source

Powermeter refresh hinzugefügt

pull/1615/head
Patrick Amrhein 9 months ago
parent
commit
d60e65b538
  1. 7
      src/config/settings.h
  2. 96
      src/plugins/zeroExport/powermeter.h
  3. 4
      src/web/RestApi.h
  4. 11
      src/web/html/setup.html
  5. 5
      src/web/lang.json

7
src/config/settings.h

@ -283,6 +283,8 @@ typedef struct {
bool sleep; bool sleep;
char name[ZEROEXPORT_GROUP_MAX_LEN_NAME]; char name[ZEROEXPORT_GROUP_MAX_LEN_NAME];
// Powermeter // Powermeter
uint8_t pm_refresh;
unsigned long pm_peviousTsp;
uint8_t pm_type; uint8_t pm_type;
char pm_url[ZEROEXPORT_GROUP_MAX_LEN_PM_URL]; char pm_url[ZEROEXPORT_GROUP_MAX_LEN_PM_URL];
char pm_jsonPath[ZEROEXPORT_GROUP_MAX_LEN_PM_JSONPATH]; char pm_jsonPath[ZEROEXPORT_GROUP_MAX_LEN_PM_JSONPATH];
@ -670,6 +672,8 @@ class settings {
mCfg.plugin.zeroExport.groups[group].sleep = false; mCfg.plugin.zeroExport.groups[group].sleep = false;
snprintf(mCfg.plugin.zeroExport.groups[group].name, ZEROEXPORT_GROUP_MAX_LEN_NAME, "%s", DEF_ZEXPORT); snprintf(mCfg.plugin.zeroExport.groups[group].name, ZEROEXPORT_GROUP_MAX_LEN_NAME, "%s", DEF_ZEXPORT);
// Powermeter // Powermeter
mCfg.plugin.zeroExport.groups[group].pm_refresh = 5;
mCfg.plugin.zeroExport.groups[group].pm_peviousTsp = 0;
mCfg.plugin.zeroExport.groups[group].pm_type = zeroExportPowermeterType_t::None; mCfg.plugin.zeroExport.groups[group].pm_type = zeroExportPowermeterType_t::None;
snprintf(mCfg.plugin.zeroExport.groups[group].pm_url, ZEROEXPORT_GROUP_MAX_LEN_PM_URL, "%s", DEF_ZEXPORT); snprintf(mCfg.plugin.zeroExport.groups[group].pm_url, ZEROEXPORT_GROUP_MAX_LEN_PM_URL, "%s", DEF_ZEXPORT);
snprintf(mCfg.plugin.zeroExport.groups[group].pm_jsonPath, ZEROEXPORT_GROUP_MAX_LEN_PM_JSONPATH, "%s", DEF_ZEXPORT); snprintf(mCfg.plugin.zeroExport.groups[group].pm_jsonPath, ZEROEXPORT_GROUP_MAX_LEN_PM_JSONPATH, "%s", DEF_ZEXPORT);
@ -1031,6 +1035,7 @@ class settings {
obj[F("enabled")] = mCfg.plugin.zeroExport.groups[group].enabled; obj[F("enabled")] = mCfg.plugin.zeroExport.groups[group].enabled;
obj[F("name")] = mCfg.plugin.zeroExport.groups[group].name; obj[F("name")] = mCfg.plugin.zeroExport.groups[group].name;
// Powermeter // Powermeter
obj[F("pm_refresh")] = mCfg.plugin.zeroExport.groups[group].pm_refresh;
obj[F("pm_type")] = mCfg.plugin.zeroExport.groups[group].pm_type; obj[F("pm_type")] = mCfg.plugin.zeroExport.groups[group].pm_type;
obj[F("pm_url")] = mCfg.plugin.zeroExport.groups[group].pm_url; obj[F("pm_url")] = mCfg.plugin.zeroExport.groups[group].pm_url;
obj[F("pm_jsonPath")] = mCfg.plugin.zeroExport.groups[group].pm_jsonPath; obj[F("pm_jsonPath")] = mCfg.plugin.zeroExport.groups[group].pm_jsonPath;
@ -1061,6 +1066,8 @@ class settings {
if (obj.containsKey(F("name"))) if (obj.containsKey(F("name")))
getChar(obj, F("name"), mCfg.plugin.zeroExport.groups[group].name, ZEXPORT_ADDR_LEN); getChar(obj, F("name"), mCfg.plugin.zeroExport.groups[group].name, ZEXPORT_ADDR_LEN);
// Powermeter // Powermeter
if (obj.containsKey(F("pm_refresh")))
getVal<uint8_t>(obj, F("pm_refresh"), &mCfg.plugin.zeroExport.groups[group].pm_refresh);
if (obj.containsKey(F("pm_type"))) if (obj.containsKey(F("pm_type")))
getVal<uint8_t>(obj, F("pm_type"), &mCfg.plugin.zeroExport.groups[group].pm_type); getVal<uint8_t>(obj, F("pm_type"), &mCfg.plugin.zeroExport.groups[group].pm_type);
if (obj.containsKey(F("pm_url"))) if (obj.containsKey(F("pm_url")))

96
src/plugins/zeroExport/powermeter.h

@ -75,6 +75,9 @@ class powermeter {
for (u_short group = 0; group < ZEROEXPORT_MAX_GROUPS; group++) { for (u_short group = 0; group < ZEROEXPORT_MAX_GROUPS; group++) {
if ((!mCfg->groups[group].enabled) || (mCfg->groups[group].sleep)) continue; if ((!mCfg->groups[group].enabled) || (mCfg->groups[group].sleep)) continue;
if ((millis() - mCfg->groups[group].pm_peviousTsp) <= ((uint16_t)mCfg->groups[group].pm_refresh * 1000)) continue;
mCfg->groups[group].pm_peviousTsp = millis();
switch (mCfg->groups[group].pm_type) { switch (mCfg->groups[group].pm_type) {
#if defined(ZEROEXPORT_POWERMETER_SHELLY) #if defined(ZEROEXPORT_POWERMETER_SHELLY)
case zeroExportPowermeterType_t::Shelly: case zeroExportPowermeterType_t::Shelly:
@ -88,8 +91,8 @@ class powermeter {
#endif #endif
#if defined(ZEROEXPORT_POWERMETER_MQTT) #if defined(ZEROEXPORT_POWERMETER_MQTT)
case zeroExportPowermeterType_t::Mqtt: case zeroExportPowermeterType_t::Mqtt:
/// power = getPowermeterWattsMqtt(*mLog, group); /// power = getPowermeterWattsMqtt(*mLog, group);
continue; continue;
break; break;
#endif #endif
#if defined(ZEROEXPORT_POWERMETER_HICHI) #if defined(ZEROEXPORT_POWERMETER_HICHI)
@ -158,7 +161,6 @@ continue;
* *
*/ */
void onMqttConnect(void) { void onMqttConnect(void) {
#if defined(ZEROEXPORT_POWERMETER_MQTT) #if defined(ZEROEXPORT_POWERMETER_MQTT)
for (uint8_t group = 0; group < ZEROEXPORT_MAX_GROUPS; group++) { for (uint8_t group = 0; group < ZEROEXPORT_MAX_GROUPS; group++) {
@ -167,13 +169,11 @@ continue;
if (!mCfg->groups[group].enabled) continue; if (!mCfg->groups[group].enabled) continue;
if (mCfg->groups[group].pm_type == zeroExportPowermeterType_t::Mqtt) { if (mCfg->groups[group].pm_type == zeroExportPowermeterType_t::Mqtt) {
mMqtt->subscribeExtern(String(mCfg->groups[group].pm_jsonPath).c_str(), QOS_2); mMqtt->subscribeExtern(String(mCfg->groups[group].pm_jsonPath).c_str(), QOS_2);
} }
} }
#endif /*defined(ZEROEXPORT_POWERMETER_MQTT)*/ #endif /*defined(ZEROEXPORT_POWERMETER_MQTT)*/
} }
/** /**
@ -183,14 +183,42 @@ continue;
String topic = String(obj["topic"]); String topic = String(obj["topic"]);
#if defined(ZEROEXPORT_POWERMETER_MQTT) #if defined(ZEROEXPORT_POWERMETER_MQTT)
// topic for powermeter?
// for (uint8_t group = 0; group < ZEROEXPORT_MAX_GROUPS; group++) { for (uint8_t group = 0; group < ZEROEXPORT_MAX_GROUPS; group++) {
// if (mCfg->groups[group].pm_type == zeroExportPowermeterType_t::Mqtt) { if (!mCfg->groups[group].enabled) continue;
// // mLog["mqttDevice"] = "topicInverter";
// if (!topic.equals(mCfg->groups[group].pm_jsonPath)) return; if (!mCfg->groups[group].pm_type == zeroExportPowermeterType_t::Mqtt) continue;
// mCfg->groups[group].pm_P = (int32_t)obj["val"];
// } if (!strcmp(mCfg->groups[group].pm_jsonPath, "")) continue;
// }
if (strcmp(mCfg->groups[group].pm_jsonPath, String(topic).c_str())) continue;
PowermeterBuffer_t power;
power.P = (uint16_t)obj["val"];
power.P1 = power.P2 = power.P3 = (uint16_t)obj["val"] / 3;
if ((power.P == 0) and (power.P1 == 0) && (power.P2 == 0) && (power.P3 == 0)) return;
bufferWrite(power, group);
// MQTT - Powermeter
if (mCfg->debug) {
if (mMqtt->isConnected()) {
// P
mqttObj["Sum"] = ah::round1(power.P);
mqttObj["L1"] = ah::round1(power.P1);
mqttObj["L2"] = ah::round1(power.P2);
mqttObj["L3"] = ah::round1(power.P3);
mMqtt->publish(String("zero/state/groups/" + String(group) + "/powermeter/P").c_str(), mqttDoc.as<std::string>().c_str(), false);
mqttDoc.clear();
// W (TODO)
}
}
return;
}
#endif /*defined(ZEROEXPORT_POWERMETER_MQTT)*/ #endif /*defined(ZEROEXPORT_POWERMETER_MQTT)*/
} }
@ -202,7 +230,7 @@ continue;
* @returns void * @returns void
*/ */
void mqttSubscribe(String gr, String payload) { void mqttSubscribe(String gr, String payload) {
// mqttPublish(gr, payload); // mqttPublish(gr, payload);
mMqtt->subscribe(gr.c_str(), QOS_2); mMqtt->subscribe(gr.c_str(), QOS_2);
} }
@ -435,26 +463,26 @@ continue;
} }
#endif #endif
///#if defined(ZEROEXPORT_POWERMETER_MQTT) /// #if defined(ZEROEXPORT_POWERMETER_MQTT)
/// /** getPowermeterWattsMqtt /// /** getPowermeterWattsMqtt
/// * ... /// * ...
/// * @param logObj /// * @param logObj
/// * @param group /// * @param group
/// * @returns true/false /// * @returns true/false
/// */ /// */
/// PowermeterBuffer_t getPowermeterWattsMqtt(JsonObject logObj, uint8_t group) { /// PowermeterBuffer_t getPowermeterWattsMqtt(JsonObject logObj, uint8_t group) {
/// PowermeterBuffer_t result; /// PowermeterBuffer_t result;
/// result.P = result.P1 = result.P2 = result.P3 = 0; /// result.P = result.P1 = result.P2 = result.P3 = 0;
/// ///
/// logObj["mod"] = "getPowermeterWattsMqtt"; /// logObj["mod"] = "getPowermeterWattsMqtt";
/// ///
/// // topic for powermeter? /// // topic for powermeter?
/// result.P = mCfg->groups[group].pm_P; /// result.P = mCfg->groups[group].pm_P;
/// result.P1 = result.P2 = result.P3 = mCfg->groups[group].pm_P / 3; /// result.P1 = result.P2 = result.P3 = mCfg->groups[group].pm_P / 3;
/// ///
/// return result; /// return result;
/// } /// }
///#endif /// #endif
#if defined(ZEROEXPORT_POWERMETER_HICHI) #if defined(ZEROEXPORT_POWERMETER_HICHI)
/** getPowermeterWattsHichi /** getPowermeterWattsHichi

4
src/web/RestApi.h

@ -242,7 +242,7 @@ class RestApi {
if((len + index) != total) if((len + index) != total)
return; // not last frame - nothing to do return; // not last frame - nothing to do
DynamicJsonDocument json(1000); DynamicJsonDocument json(2000);
AsyncJsonResponse* response = new AsyncJsonResponse(false, 200); AsyncJsonResponse* response = new AsyncJsonResponse(false, 200);
JsonObject root = response->getRoot(); JsonObject root = response->getRoot();
@ -837,6 +837,7 @@ class RestApi {
objGroup[F("enabled")] = (bool)mConfig->plugin.zeroExport.groups[group].enabled; objGroup[F("enabled")] = (bool)mConfig->plugin.zeroExport.groups[group].enabled;
objGroup[F("name")] = String(mConfig->plugin.zeroExport.groups[group].name); objGroup[F("name")] = String(mConfig->plugin.zeroExport.groups[group].name);
// Powermeter // Powermeter
objGroup[F("pm_refresh")] = (uint8_t)mConfig->plugin.zeroExport.groups[group].pm_refresh;
objGroup[F("pm_type")] = (uint8_t)mConfig->plugin.zeroExport.groups[group].pm_type; objGroup[F("pm_type")] = (uint8_t)mConfig->plugin.zeroExport.groups[group].pm_type;
objGroup[F("pm_url")] = String(mConfig->plugin.zeroExport.groups[group].pm_url); objGroup[F("pm_url")] = String(mConfig->plugin.zeroExport.groups[group].pm_url);
objGroup[F("pm_jsonPath")] = String(mConfig->plugin.zeroExport.groups[group].pm_jsonPath); objGroup[F("pm_jsonPath")] = String(mConfig->plugin.zeroExport.groups[group].pm_jsonPath);
@ -1156,6 +1157,7 @@ class RestApi {
mConfig->plugin.zeroExport.groups[group].enabled = jsonIn[F("enabled")]; mConfig->plugin.zeroExport.groups[group].enabled = jsonIn[F("enabled")];
snprintf(mConfig->plugin.zeroExport.groups[group].name, ZEROEXPORT_GROUP_MAX_LEN_NAME, "%s", jsonIn[F("name")].as<const char*>()); snprintf(mConfig->plugin.zeroExport.groups[group].name, ZEROEXPORT_GROUP_MAX_LEN_NAME, "%s", jsonIn[F("name")].as<const char*>());
// Powermeter // Powermeter
mConfig->plugin.zeroExport.groups[group].pm_refresh = jsonIn[F("pm_refresh")];
mConfig->plugin.zeroExport.groups[group].pm_type = jsonIn[F("pm_type")]; mConfig->plugin.zeroExport.groups[group].pm_type = jsonIn[F("pm_type")];
snprintf(mConfig->plugin.zeroExport.groups[group].pm_url, ZEROEXPORT_GROUP_MAX_LEN_PM_URL, "%s", jsonIn[F("pm_url")].as<const char*>()); snprintf(mConfig->plugin.zeroExport.groups[group].pm_url, ZEROEXPORT_GROUP_MAX_LEN_PM_URL, "%s", jsonIn[F("pm_url")].as<const char*>());
snprintf(mConfig->plugin.zeroExport.groups[group].pm_jsonPath, ZEROEXPORT_GROUP_MAX_LEN_PM_JSONPATH, "%s", jsonIn[F("pm_jsonPath")].as<const char*>()); snprintf(mConfig->plugin.zeroExport.groups[group].pm_jsonPath, ZEROEXPORT_GROUP_MAX_LEN_PM_JSONPATH, "%s", jsonIn[F("pm_jsonPath")].as<const char*>());

11
src/web/html/setup.html

@ -1390,10 +1390,9 @@
]), ]),
// Powermeter // Powermeter
ml("div", {id: "div{#ZE_GROUP_TAB_POWERMETER}", class: "tab-content hide"}, [ ml("div", {id: "div{#ZE_GROUP_TAB_POWERMETER}", class: "tab-content hide"}, [
// TODO: add divRow("{#ZE_GROUP_TAB_POWERMETER_REFRESH}",
//divRow("{#ZE_GROUP_TAB_POWERMETER_REFRESH}", ml("input", {name: "pm_refresh", class: "text", type: "number", min: "1", max: "30", step: "1", value: obj.pm_refresh}, null),
//ml("select", {name: "pm_refresh", class: "text", type: "text", value: obj.pm_refresh}, null), ),
//),
divRow("{#ZE_GROUP_TAB_POWERMETER_TYPE}", divRow("{#ZE_GROUP_TAB_POWERMETER_TYPE}",
ml("select", {name: "pm_type", class: "text", id: "pm_type"}, null), ml("select", {name: "pm_type", class: "text", id: "pm_type"}, null),
), ),
@ -1556,7 +1555,7 @@
o.enabled = document.getElementsByName("enabled")[0].checked; o.enabled = document.getElementsByName("enabled")[0].checked;
o.name = document.getElementsByName("name")[0].value; o.name = document.getElementsByName("name")[0].value;
// Powermeter // Powermeter
//o.pm_type = document.getElementsByName("pm_type")[0].selectedIndex; o.pm_refresh = document.getElementsByName("pm_refresh")[0].value;
var e = document.getElementsByName("pm_type")[0]; var e = document.getElementsByName("pm_type")[0];
o.pm_type = e.options[e.selectedIndex].value; o.pm_type = e.options[e.selectedIndex].value;
o.pm_url = document.getElementsByName("pm_url")[0].value; o.pm_url = document.getElementsByName("pm_url")[0].value;
@ -1623,6 +1622,7 @@
o.enabled = false; o.enabled = false;
o.name = ""; o.name = "";
// Powermeter // Powermeter
o.pm_refresh = 5;
o.pm_type = 0; o.pm_type = 0;
o.pm_url = ""; o.pm_url = "";
o.pm_jsonPath = ""; o.pm_jsonPath = "";
@ -1640,6 +1640,7 @@
q.target = -1; q.target = -1;
q.powerMin = 0; q.powerMin = 0;
q.powerMax = 0; q.powerMax = 0;
q.turnOff = false;
o.inverters.push(q); o.inverters.push(q);
} }
// Battery // Battery

5
src/web/lang.json

@ -883,6 +883,11 @@
"en": "Powermeter", "en": "Powermeter",
"de": "Leistungsmessung" "de": "Leistungsmessung"
}, },
{
"token": "ZE_GROUP_TAB_POWERMETER_REFRESH",
"en": "Refresh (s):",
"de": "Zykluszeit (s):"
},
{ {
"token": "ZE_GROUP_TAB_POWERMETER_TYPE", "token": "ZE_GROUP_TAB_POWERMETER_TYPE",
"en": "Type:", "en": "Type:",

Loading…
Cancel
Save