Browse Source

ZE MQTT

pull/1581/head
Patrick Amrhein 1 year ago
parent
commit
58a5cc43ab
  1. 131
      src/plugins/zeroExport/zeroExport.h

131
src/plugins/zeroExport/zeroExport.h

@ -387,7 +387,8 @@ class ZeroExport {
// Calculate // Calculate
int32_t ivLp = iv->actPowerLimit; int32_t ivLp = iv->actPowerLimit;
int32_t ivPm = iv->getMaxPower();; int32_t ivPm = iv->getMaxPower();
;
int32_t ivL = (ivPm * ivLp) / 100; int32_t ivL = (ivPm * ivLp) / 100;
int32_t zeL = mCfg->groups[group].inverters[inv].limit; int32_t zeL = mCfg->groups[group].inverters[inv].limit;
@ -432,11 +433,13 @@ class ZeroExport {
if (obj["path"] == "zero" && obj["cmd"] == "set") { if (obj["path"] == "zero" && obj["cmd"] == "set") {
int8_t topicGroup = getGroupFromTopic(topic.c_str()); int8_t topicGroup = getGroupFromTopic(topic.c_str());
mLog["topicGroup"] = topicGroup;
int8_t topicInverter = getInverterFromTopic(topic.c_str()); int8_t topicInverter = getInverterFromTopic(topic.c_str());
mLog["topicInverter"] = topicInverter;
if((topicGroup == -1) && (topicInverter == -1)) { if ((topicGroup == -1) && (topicInverter == -1)) {
// "topic":"???/zero/set/enabled" // "topic":"???/zero/set/enabled"
if (topic.indexOf("zero/set/enabled") != -1) { if (topic.indexOf("/zero/set/enabled") != -1) {
mCfg->enabled = (bool)obj["val"]; mCfg->enabled = (bool)obj["val"];
mLog["mCfg->enabled"] = mCfg->enabled; mLog["mCfg->enabled"] = mCfg->enabled;
// Initialize groups // Initialize groups
@ -445,48 +448,54 @@ class ZeroExport {
mCfg->groups[group].wait = 0; mCfg->groups[group].wait = 0;
} }
} }
else
// "topic":"???/zero/set/sleep" // "topic":"???/zero/set/sleep"
if (topic.indexOf("zero/set/sleep") != -1) { if (topic.indexOf("/zero/set/sleep") != -1) {
mCfg->sleep = (bool)obj["val"]; mCfg->sleep = (bool)obj["val"];
mLog["mCfg->sleep"] = mCfg->sleep; mLog["mCfg->sleep"] = mCfg->sleep;
} }
} } else if ((topicGroup != -1) && (topicInverter == -1)) {
else
if((topicGroup != -1) && (topicInverter == -1)) {
uint8_t group = topicGroup;
// "topic":"???/zero/set/groups/0/???" // "topic":"???/zero/set/groups/0/???"
// if (topic.indexOf("zero/set/groups/") != -1) { mLog["g"] = topicGroup;
// String i = topic.substring(topic.length() - 10, topic.length() - 8);
// uint8_t group = i.toInt();
mLog["g"] = group;
// "topic":"???/zero/set/groups/0/enabled" // "topic":"???/zero/set/groups/0/enabled"
if (topic.indexOf("enabled") != -1) { if (topic.indexOf("enabled") != -1) {
mCfg->groups[group].enabled = (bool)obj["val"]; mCfg->groups[topicGroup].enabled = (bool)obj["val"];
// Initialize group // Initialize group
mCfg->groups[group].state = zeroExportState::INIT; mCfg->groups[topicGroup].state = zeroExportState::INIT;
mCfg->groups[group].wait = 0; mCfg->groups[topicGroup].wait = 0;
} }
// "topic":"???/zero/set/groups/0/sleep" // "topic":"???/zero/set/groups/0/sleep"
if (topic.indexOf("sleep") != -1) { if (topic.indexOf("sleep") != -1) {
mCfg->groups[group].sleep = (bool)obj["val"]; mCfg->groups[topicGroup].sleep = (bool)obj["val"];
} }
// } // "topic":"???/zero/set/groups/0/battery/switch"
// Battery if (topic.indexOf("battery/switch") != -1) {
// - switch mCfg->groups[topicGroup].battSwitch = (bool)obj["val"];
}
// Advanced // "topic":"???/zero/set/groups/0/advanced/setPoint"
// - setpoint if (topic.indexOf("advanced/setPoint") != -1) {
// - powerTolerance mCfg->groups[topicGroup].setPoint = (int32_t)obj["val"];
// - powerMax }
// "topic":"???/zero/set/groups/0/advanced/powerTolerance"
if (topic.indexOf("advanced/powerTolerance") != -1) {
mCfg->groups[topicGroup].powerTolerance = (uint16_t)obj["val"];
}
// "topic":"???/zero/set/groups/0/advanced/powerMax"
if (topic.indexOf("advanced/powerMax") != -1) {
mCfg->groups[topicGroup].powerMax = (uint16_t)obj["val"];
}
} else if ((topicGroup != -1) && (topicInverter != -1)) {
// "topic":"???/zero/set/groups/0/inverter/0/enabled"
if (topic.indexOf("enabled") != -1) {
mCfg->groups[topicGroup].inverters[topicInverter].enabled = (bool)obj["val"];
}
// "topic":"???/zero/set/groups/0/inverter/0/powerMin"
if (topic.indexOf("powerMin") != -1) {
mCfg->groups[topicGroup].inverters[topicInverter].powerMin = (uint16_t)obj["val"];
}
// "topic":"???/zero/set/groups/0/inverter/0/powerMax"
if (topic.indexOf("powerMax") != -1) {
mCfg->groups[topicGroup].inverters[topicInverter].powerMax = (uint16_t)obj["val"];
} }
else
if((topicGroup != -1) && (topicInverter != -1)) {
// Inverter
// - enabled
// - powerMin
// - powerMax
} }
} }
@ -507,25 +516,35 @@ class ZeroExport {
return ((!mCfg->groups[group].inverters[inv].enabled) || (mCfg->groups[group].inverters[inv].id < 0)); return ((!mCfg->groups[group].inverters[inv].enabled) || (mCfg->groups[group].inverters[inv].id < 0));
} }
int8_t getGroupFromTopic(const char* topic) { /** getGroupFromTopic
const char* pGroupSection = strstr(topic, "groups/"); *
* @param
* @returns
*/
int8_t getGroupFromTopic(const char *topic) {
const char *pGroupSection = strstr(topic, "groups/");
if (pGroupSection == NULL) return -1; if (pGroupSection == NULL) return -1;
pGroupSection += 7; pGroupSection += 7;
char strGroup[3]; char strGroup[3];
uint8_t digitsCopied = 0; uint8_t digitsCopied = 0;
while(*pGroupSection != '/' && digitsCopied < 2) strGroup[digitsCopied++] = *pGroupSection++; while (*pGroupSection != '/' && digitsCopied < 2) strGroup[digitsCopied++] = *pGroupSection++;
strGroup[digitsCopied] = '\0'; strGroup[digitsCopied] = '\0';
int8_t group = atoi(strGroup); int8_t group = atoi(strGroup);
return group; return group;
} }
int8_t getInverterFromTopic(const char* topic) { /** getInverterFromTopic
const char* pInverterSection = strstr(topic, "inverters/"); *
* @param
* @returns
*/
int8_t getInverterFromTopic(const char *topic) {
const char *pInverterSection = strstr(topic, "inverters/");
if (pInverterSection == NULL) return -1; if (pInverterSection == NULL) return -1;
pInverterSection += 10; pInverterSection += 10;
char strInverter[3]; char strInverter[3];
uint8_t digitsCopied = 0; uint8_t digitsCopied = 0;
while(*pInverterSection != '/' && digitsCopied < 2) strInverter[digitsCopied++] = *pInverterSection++; while (*pInverterSection != '/' && digitsCopied < 2) strInverter[digitsCopied++] = *pInverterSection++;
strInverter[digitsCopied] = '\0'; strInverter[digitsCopied] = '\0';
int8_t inverter = atoi(strInverter); int8_t inverter = atoi(strInverter);
return inverter; return inverter;
@ -537,7 +556,6 @@ class ZeroExport {
* @returns true/false * @returns true/false
* @todo getInverterById statt getInverterByPos, dann würde die Variable *iv und die Schleife nicht gebraucht. * @todo getInverterById statt getInverterByPos, dann würde die Variable *iv und die Schleife nicht gebraucht.
*/ */
bool groupInit(uint8_t group, unsigned long *tsp, bool *doLog) { bool groupInit(uint8_t group, unsigned long *tsp, bool *doLog) {
uint8_t result = false; uint8_t result = false;
@ -1129,7 +1147,7 @@ class ZeroExport {
// Reset // Reset
if ((cfgGroupInv->doReboot == 2) && (cfgGroupInv->waitRebootAck == 0)) { if ((cfgGroupInv->doReboot == 2) && (cfgGroupInv->waitRebootAck == 0)) {
/// result = false; /// result = false;
cfgGroupInv->doReboot = 0; cfgGroupInv->doReboot = 0;
logObj["act"] = "done"; logObj["act"] = "done";
continue; continue;
@ -1210,7 +1228,7 @@ class ZeroExport {
// Reset // Reset
if ((cfgGroupInv->doPower != -1) && (cfgGroupInv->waitPowerAck == 0)) { if ((cfgGroupInv->doPower != -1) && (cfgGroupInv->waitPowerAck == 0)) {
/// result = false; /// result = false;
cfgGroupInv->doPower = -1; cfgGroupInv->doPower = -1;
logObj["act"] = "done"; logObj["act"] = "done";
continue; continue;
@ -1309,7 +1327,7 @@ class ZeroExport {
// Reset // Reset
if ((cfgGroupInv->doLimit) && (cfgGroupInv->waitLimitAck == 0)) { if ((cfgGroupInv->doLimit) && (cfgGroupInv->waitLimitAck == 0)) {
/// result = false; /// result = false;
cfgGroupInv->doLimit = false; cfgGroupInv->doLimit = false;
logObj["act"] = "done"; logObj["act"] = "done";
continue; continue;
@ -1358,13 +1376,13 @@ class ZeroExport {
} }
// Nothing todo // Nothing todo
// if (cfgGroupInv->doLimit == false) { // if (cfgGroupInv->doLimit == false) {
// logObj["act"] = "nothing to do"; // logObj["act"] = "nothing to do";
// continue; // continue;
// } // }
if (cfgGroupInv->limit == cfgGroupInv->limitNew) { if (cfgGroupInv->limit == cfgGroupInv->limitNew) {
/// logObj["act"] = "nothing to do"; /// logObj["act"] = "nothing to do";
continue; continue;
} }
@ -1414,16 +1432,16 @@ class ZeroExport {
String gr; String gr;
// Init // Init
// TODO: Init wird fälschlicherweise hier nur ausgeführt wenn zeroExport 1x aktiviert war. // TODO: Init wird fälschlicherweise hier nur ausgeführt wenn zeroExport 1x aktiviert war.
// BUG: Wenn zeroExport deaktiviert wurde und dann rebootet, lässt sich zeroExport nicht mehr einschalten. // BUG: Wenn zeroExport deaktiviert wurde und dann rebootet, lässt sich zeroExport nicht mehr einschalten.
if (!mIsSubscribed) { if (!mIsSubscribed) {
mIsSubscribed = true; mIsSubscribed = true;
// Global (zeroExport) // Global (zeroExport)
// TODO: Global wird fälschlicherweise hier je nach anzahl der aktivierten Gruppen bis zu 6x ausgeführt. // TODO: Global wird fälschlicherweise hier je nach anzahl der aktivierten Gruppen bis zu 6x ausgeführt.
mMqtt->publish("zero/set/enabled", ((mCfg->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]), false); mMqtt->publish("zero/set/enabled", ((mCfg->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]), false);
mMqtt->subscribe("zero/set/enabled", QOS_2); mMqtt->subscribe("zero/set/enabled", QOS_2);
// TODO: Global wird fälschlicherweise hier je nach anzahl der aktivierten Gruppen bis zu 6x ausgeführt. // TODO: Global wird fälschlicherweise hier je nach anzahl der aktivierten Gruppen bis zu 6x ausgeführt.
mMqtt->publish("zero/set/sleep", ((mCfg->sleep) ? dict[STR_TRUE] : dict[STR_FALSE]), false); mMqtt->publish("zero/set/sleep", ((mCfg->sleep) ? dict[STR_TRUE] : dict[STR_FALSE]), false);
mMqtt->subscribe("zero/set/sleep", QOS_2); mMqtt->subscribe("zero/set/sleep", QOS_2);
@ -1469,9 +1487,9 @@ class ZeroExport {
} }
// Global (zeroExport) // Global (zeroExport)
// TODO: Global wird fälschlicherweise hier je nach anzahl der aktivierten Gruppen bis zu 6x ausgeführt. // TODO: Global wird fälschlicherweise hier je nach anzahl der aktivierten Gruppen bis zu 6x ausgeführt.
mMqtt->publish("zero/state/enabled", ((mCfg->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]), false); mMqtt->publish("zero/state/enabled", ((mCfg->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]), false);
// TODO: Global wird fälschlicherweise hier je nach anzahl der aktivierten Gruppen bis zu 6x ausgeführt. // TODO: Global wird fälschlicherweise hier je nach anzahl der aktivierten Gruppen bis zu 6x ausgeführt.
mMqtt->publish("zero/state/sleep", ((mCfg->sleep) ? dict[STR_TRUE] : dict[STR_FALSE]), false); mMqtt->publish("zero/state/sleep", ((mCfg->sleep) ? dict[STR_TRUE] : dict[STR_FALSE]), false);
// General // General
@ -1485,15 +1503,15 @@ class ZeroExport {
mMqtt->publish(gr.c_str(), cfgGroup->name, false); mMqtt->publish(gr.c_str(), cfgGroup->name, false);
// Powermeter // Powermeter
// if (cfgGroup->publishPower) { // if (cfgGroup->publishPower) {
// cfgGroup->publishPower = false; // cfgGroup->publishPower = false;
obj["Sum"] = cfgGroup->pm_P; obj["Sum"] = cfgGroup->pm_P;
obj["L1"] = cfgGroup->pm_P1; obj["L1"] = cfgGroup->pm_P1;
obj["L2"] = cfgGroup->pm_P2; obj["L2"] = cfgGroup->pm_P2;
obj["L3"] = cfgGroup->pm_P3; obj["L3"] = cfgGroup->pm_P3;
mMqtt->publish("zero/state/powermeter/P", doc.as<std::string>().c_str(), false); mMqtt->publish("zero/state/powermeter/P", doc.as<std::string>().c_str(), false);
doc.clear(); doc.clear();
// } // }
// if (cfgGroup->pm_Publish_W) { // if (cfgGroup->pm_Publish_W) {
// cfgGroup->pm_Publish_W = false; // cfgGroup->pm_Publish_W = false;
@ -1539,7 +1557,6 @@ class ZeroExport {
obj["Kd"] = cfgGroup->Kd; obj["Kd"] = cfgGroup->Kd;
mMqtt->publish(gr.c_str(), doc.as<std::string>().c_str(), false); mMqtt->publish(gr.c_str(), doc.as<std::string>().c_str(), false);
doc.clear(); doc.clear();
} }
return true; return true;

Loading…
Cancel
Save