mirror of
https://github.com/lumapu/ahoy.git
synced 2025-06-09 14:11:39 +02:00
Update zeroExport.h
move some mqtt parts and add function to the loop for the init part.
This commit is contained in:
parent
2780b9ff33
commit
60386e251f
1 changed files with 102 additions and 101 deletions
|
@ -48,7 +48,9 @@ class ZeroExport {
|
||||||
mApi = api;
|
mApi = api;
|
||||||
mMqtt = mqtt;
|
mMqtt = mqtt;
|
||||||
|
|
||||||
|
mIsSubscribed = false;
|
||||||
mIsInitialized = mPowermeter.setup(mCfg, &mLog);
|
mIsInitialized = mPowermeter.setup(mCfg, &mLog);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** loop
|
/** loop
|
||||||
|
@ -59,6 +61,8 @@ class ZeroExport {
|
||||||
* @todo emergency
|
* @todo emergency
|
||||||
*/
|
*/
|
||||||
void loop(void) {
|
void loop(void) {
|
||||||
|
mqttInitTopic();
|
||||||
|
|
||||||
if ((!mIsInitialized) || (!mCfg->enabled)) return;
|
if ((!mIsInitialized) || (!mCfg->enabled)) return;
|
||||||
|
|
||||||
bool DoLog = false;
|
bool DoLog = false;
|
||||||
|
@ -864,6 +868,27 @@ class ZeroExport {
|
||||||
mLog["P2"] = mCfg->groups[group].pm_P2;
|
mLog["P2"] = mCfg->groups[group].pm_P2;
|
||||||
mLog["P3"] = mCfg->groups[group].pm_P3;
|
mLog["P3"] = mCfg->groups[group].pm_P3;
|
||||||
|
|
||||||
|
// Powermeter
|
||||||
|
// if (cfgGroup->publishPower) {
|
||||||
|
// cfgGroup->publishPower = false;
|
||||||
|
mqttObj["Sum"] = mCfg->groups[group].pm_P;
|
||||||
|
mqttObj["L1"] = mCfg->groups[group].pm_P1;
|
||||||
|
mqttObj["L2"] = mCfg->groups[group].pm_P2;
|
||||||
|
mqttObj["L3"] = mCfg->groups[group].pm_P3;
|
||||||
|
mMqtt->publish("zero/state/powermeter/P", mqttDoc.as<std::string>().c_str(), false);
|
||||||
|
mqttDoc.clear();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (cfgGroup->pm_Publish_W) {
|
||||||
|
// cfgGroup->pm_Publish_W = false;
|
||||||
|
// obj["todo"] = "true";
|
||||||
|
// obj["Sum"] = cfgGroup->pm_P;
|
||||||
|
// obj["L1"] = cfgGroup->pm_P1;
|
||||||
|
// obj["L2"] = cfgGroup->pm_P2;
|
||||||
|
// obj["L2"] = cfgGroup->pm_P3;
|
||||||
|
// mMqtt->publish("zero/powermeter/W", doc.as<std::string>().c_str(), false);
|
||||||
|
// doc.clear();
|
||||||
|
// }
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1476,6 +1501,58 @@ if (mCfg->groups[group].power > mCfg->groups[group].powerMax) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PubSubInit(String gr, String payload)
|
||||||
|
{
|
||||||
|
mMqtt->publish(gr.c_str(), payload.c_str(), false);
|
||||||
|
mMqtt->subscribe(gr.c_str(), QOS_2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mqttInitTopic()
|
||||||
|
{
|
||||||
|
if (mIsSubscribed) return;
|
||||||
|
if (!mMqtt->isConnected()) return;
|
||||||
|
|
||||||
|
mIsSubscribed = true;
|
||||||
|
|
||||||
|
// Global (zeroExport)
|
||||||
|
// 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->subscribe("zero/set/enabled", QOS_2);
|
||||||
|
// 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->subscribe("zero/set/sleep", QOS_2);
|
||||||
|
|
||||||
|
|
||||||
|
String gr;
|
||||||
|
for (uint8_t group = 0; group < ZEROEXPORT_MAX_GROUPS; group++)
|
||||||
|
{
|
||||||
|
zeroExportGroup_t *cfgGroup = &mCfg->groups[group];
|
||||||
|
gr = "zero/set/groups/" + String(group);
|
||||||
|
|
||||||
|
// General
|
||||||
|
PubSubInit(gr + "/enabled", ((cfgGroup->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]));
|
||||||
|
PubSubInit(gr + "/sleep", ((cfgGroup->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]));
|
||||||
|
|
||||||
|
// Powermeter
|
||||||
|
|
||||||
|
// Inverters
|
||||||
|
for (uint8_t inv = 0; inv < ZEROEXPORT_GROUP_MAX_INVERTERS; inv++) {
|
||||||
|
zeroExportGroupInverter_t *cfgGroupInv = &cfgGroup->inverters[inv];
|
||||||
|
|
||||||
|
PubSubInit(gr + "/inverters/" + String(inv) + "/enabled", ((cfgGroupInv->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]));
|
||||||
|
PubSubInit(gr + "/inverters/" + String(inv) + "/powerMin", String(cfgGroupInv->powerMin));
|
||||||
|
PubSubInit(gr + "/inverters/" + String(inv) + "/powerMax", String(cfgGroupInv->powerMax));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Battery
|
||||||
|
PubSubInit(gr + "/battery/switch", ((cfgGroup->battSwitch) ? dict[STR_TRUE] : dict[STR_FALSE]));
|
||||||
|
|
||||||
|
// Advanced
|
||||||
|
PubSubInit(gr + "/advanced/setPoint", String(cfgGroup->setPoint));
|
||||||
|
PubSubInit(gr + "/advanced/powerTolerance", String(cfgGroup->powerTolerance));
|
||||||
|
PubSubInit(gr + "/advanced/powerMax", String(cfgGroup->powerMax));
|
||||||
|
}
|
||||||
|
}
|
||||||
/** groupPublish
|
/** groupPublish
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
@ -1487,67 +1564,10 @@ if (mCfg->groups[group].power > mCfg->groups[group].powerMax) {
|
||||||
cfgGroup->lastRun = *tsp;
|
cfgGroup->lastRun = *tsp;
|
||||||
|
|
||||||
if (mMqtt->isConnected()) {
|
if (mMqtt->isConnected()) {
|
||||||
DynamicJsonDocument doc(512);
|
|
||||||
JsonObject obj = doc.to<JsonObject>();
|
|
||||||
|
|
||||||
// *doLog = true;
|
// *doLog = true;
|
||||||
String gr;
|
String gr;
|
||||||
|
|
||||||
// Init
|
|
||||||
// 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.
|
|
||||||
if (!mIsSubscribed) {
|
|
||||||
mIsSubscribed = true;
|
|
||||||
|
|
||||||
// Global (zeroExport)
|
|
||||||
// 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->subscribe("zero/set/enabled", QOS_2);
|
|
||||||
// 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->subscribe("zero/set/sleep", QOS_2);
|
|
||||||
|
|
||||||
// General
|
|
||||||
gr = "zero/set/groups/" + String(group) + "/enabled";
|
|
||||||
mMqtt->publish(gr.c_str(), ((cfgGroup->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]), false);
|
|
||||||
mMqtt->subscribe(gr.c_str(), QOS_2);
|
|
||||||
gr = "zero/set/groups/" + String(group) + "/sleep";
|
|
||||||
mMqtt->publish(gr.c_str(), ((cfgGroup->sleep) ? dict[STR_TRUE] : dict[STR_FALSE]), false);
|
|
||||||
mMqtt->subscribe(gr.c_str(), QOS_2);
|
|
||||||
|
|
||||||
// Powermeter
|
|
||||||
|
|
||||||
// Inverters
|
|
||||||
for (uint8_t inv = 0; inv < ZEROEXPORT_GROUP_MAX_INVERTERS; inv++) {
|
|
||||||
zeroExportGroupInverter_t *cfgGroupInv = &cfgGroup->inverters[inv];
|
|
||||||
gr = "zero/set/groups/" + String(group) + "/inverters/" + String(inv) + "/enabled";
|
|
||||||
mMqtt->publish(gr.c_str(), ((cfgGroupInv->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]), false);
|
|
||||||
mMqtt->subscribe(gr.c_str(), QOS_2);
|
|
||||||
gr = "zero/set/groups/" + String(group) + "/inverters/" + String(inv) + "/powerMin";
|
|
||||||
mMqtt->publish(gr.c_str(), String(cfgGroupInv->powerMin).c_str(), false);
|
|
||||||
mMqtt->subscribe(gr.c_str(), QOS_2);
|
|
||||||
gr = "zero/set/groups/" + String(group) + "/inverters/" + String(inv) + "/powerMax";
|
|
||||||
mMqtt->publish(gr.c_str(), String(cfgGroupInv->powerMax).c_str(), false);
|
|
||||||
mMqtt->subscribe(gr.c_str(), QOS_2);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Battery
|
|
||||||
gr = "zero/set/groups/" + String(group) + "/battery/switch";
|
|
||||||
mMqtt->publish(gr.c_str(), ((cfgGroup->battSwitch) ? dict[STR_TRUE] : dict[STR_FALSE]), false);
|
|
||||||
mMqtt->subscribe(gr.c_str(), QOS_2);
|
|
||||||
|
|
||||||
// Advanced
|
|
||||||
gr = "zero/set/groups/" + String(group) + "/advanced/setPoint";
|
|
||||||
mMqtt->publish(gr.c_str(), String(cfgGroup->setPoint).c_str(), false);
|
|
||||||
mMqtt->subscribe(gr.c_str(), QOS_2);
|
|
||||||
gr = "zero/set/groups/" + String(group) + "/advanced/powerTolerance";
|
|
||||||
mMqtt->publish(gr.c_str(), String(cfgGroup->powerTolerance).c_str(), false);
|
|
||||||
mMqtt->subscribe(gr.c_str(), QOS_2);
|
|
||||||
gr = "zero/set/groups/" + String(group) + "/advanced/powerMax";
|
|
||||||
mMqtt->publish(gr.c_str(), String(cfgGroup->powerMax).c_str(), false);
|
|
||||||
mMqtt->subscribe(gr.c_str(), QOS_2);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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);
|
||||||
|
@ -1564,61 +1584,39 @@ if (mCfg->groups[group].power > mCfg->groups[group].powerMax) {
|
||||||
gr = "zero/state/groups/" + String(group) + "/name";
|
gr = "zero/state/groups/" + String(group) + "/name";
|
||||||
mMqtt->publish(gr.c_str(), cfgGroup->name, false);
|
mMqtt->publish(gr.c_str(), cfgGroup->name, false);
|
||||||
|
|
||||||
// Powermeter
|
|
||||||
// if (cfgGroup->publishPower) {
|
|
||||||
// cfgGroup->publishPower = false;
|
|
||||||
obj["Sum"] = cfgGroup->pm_P;
|
|
||||||
obj["L1"] = cfgGroup->pm_P1;
|
|
||||||
obj["L2"] = cfgGroup->pm_P2;
|
|
||||||
obj["L3"] = cfgGroup->pm_P3;
|
|
||||||
mMqtt->publish("zero/state/powermeter/P", doc.as<std::string>().c_str(), false);
|
|
||||||
doc.clear();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (cfgGroup->pm_Publish_W) {
|
|
||||||
// cfgGroup->pm_Publish_W = false;
|
|
||||||
// obj["todo"] = "true";
|
|
||||||
// obj["Sum"] = cfgGroup->pm_P;
|
|
||||||
// obj["L1"] = cfgGroup->pm_P1;
|
|
||||||
// obj["L2"] = cfgGroup->pm_P2;
|
|
||||||
// obj["L2"] = cfgGroup->pm_P3;
|
|
||||||
// mMqtt->publish("zero/powermeter/W", doc.as<std::string>().c_str(), false);
|
|
||||||
// doc.clear();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Inverters
|
// Inverters
|
||||||
for (uint8_t inv = 0; inv < ZEROEXPORT_GROUP_MAX_INVERTERS; inv++) {
|
for (uint8_t inv = 0; inv < ZEROEXPORT_GROUP_MAX_INVERTERS; inv++) {
|
||||||
zeroExportGroupInverter_t *cfgGroupInv = &cfgGroup->inverters[inv];
|
zeroExportGroupInverter_t *cfgGroupInv = &cfgGroup->inverters[inv];
|
||||||
gr = "zero/state/groups/" + String(group) + "/inverters/" + String(inv);
|
gr = "zero/state/groups/" + String(group) + "/inverters/" + String(inv);
|
||||||
obj["enabled"] = cfgGroupInv->enabled;
|
mqttObj["enabled"] = cfgGroupInv->enabled;
|
||||||
obj["id"] = cfgGroupInv->id;
|
mqttObj["id"] = cfgGroupInv->id;
|
||||||
obj["target"] = cfgGroupInv->target;
|
mqttObj["target"] = cfgGroupInv->target;
|
||||||
obj["powerMin"] = cfgGroupInv->powerMin;
|
mqttObj["powerMin"] = cfgGroupInv->powerMin;
|
||||||
obj["powerMax"] = cfgGroupInv->powerMax;
|
mqttObj["powerMax"] = cfgGroupInv->powerMax;
|
||||||
mMqtt->publish(gr.c_str(), doc.as<std::string>().c_str(), false);
|
mMqtt->publish(gr.c_str(), mqttDoc.as<std::string>().c_str(), false);
|
||||||
doc.clear();
|
mqttDoc.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Battery
|
// Battery
|
||||||
gr = "zero/state/groups/" + String(group) + "/battery";
|
gr = "zero/state/groups/" + String(group) + "/battery";
|
||||||
obj["enabled"] = cfgGroup->battEnabled;
|
mqttObj["enabled"] = cfgGroup->battEnabled;
|
||||||
obj["voltageOn"] = cfgGroup->battVoltageOn;
|
mqttObj["voltageOn"] = cfgGroup->battVoltageOn;
|
||||||
obj["voltageOff"] = cfgGroup->battVoltageOff;
|
mqttObj["voltageOff"] = cfgGroup->battVoltageOff;
|
||||||
obj["switch"] = cfgGroup->battSwitch;
|
mqttObj["switch"] = cfgGroup->battSwitch;
|
||||||
mMqtt->publish(gr.c_str(), doc.as<std::string>().c_str(), false);
|
mMqtt->publish(gr.c_str(), mqttDoc.as<std::string>().c_str(), false);
|
||||||
doc.clear();
|
mqttDoc.clear();
|
||||||
|
|
||||||
// Advanced
|
// Advanced
|
||||||
gr = "zero/state/groups/" + String(group) + "/advanced";
|
gr = "zero/state/groups/" + String(group) + "/advanced";
|
||||||
obj["setPoint"] = cfgGroup->setPoint;
|
mqttObj["setPoint"] = cfgGroup->setPoint;
|
||||||
obj["refresh"] = cfgGroup->refresh;
|
mqttObj["refresh"] = cfgGroup->refresh;
|
||||||
obj["powerTolerance"] = cfgGroup->powerTolerance;
|
mqttObj["powerTolerance"] = cfgGroup->powerTolerance;
|
||||||
obj["powerMax"] = cfgGroup->powerMax;
|
mqttObj["powerMax"] = cfgGroup->powerMax;
|
||||||
obj["Kp"] = cfgGroup->Kp;
|
mqttObj["Kp"] = cfgGroup->Kp;
|
||||||
obj["Ki"] = cfgGroup->Ki;
|
mqttObj["Ki"] = cfgGroup->Ki;
|
||||||
obj["Kd"] = cfgGroup->Kd;
|
mqttObj["Kd"] = cfgGroup->Kd;
|
||||||
mMqtt->publish(gr.c_str(), doc.as<std::string>().c_str(), false);
|
mMqtt->publish(gr.c_str(), mqttDoc.as<std::string>().c_str(), false);
|
||||||
doc.clear();
|
mqttDoc.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -1681,6 +1679,9 @@ if (mCfg->groups[group].power > mCfg->groups[group].powerMax) {
|
||||||
powermeter mPowermeter;
|
powermeter mPowermeter;
|
||||||
bool mIsSubscribed = false;
|
bool mIsSubscribed = false;
|
||||||
|
|
||||||
|
StaticJsonDocument<512> mqttDoc; //DynamicJsonDocument mqttDoc(512);
|
||||||
|
JsonObject mqttObj = mqttDoc.to<JsonObject>();
|
||||||
|
|
||||||
Inverter<> *mIv[ZEROEXPORT_MAX_GROUPS][ZEROEXPORT_GROUP_MAX_INVERTERS];
|
Inverter<> *mIv[ZEROEXPORT_MAX_GROUPS][ZEROEXPORT_GROUP_MAX_INVERTERS];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue