From 8452e5613c714ee2311b02fb8756c0e645b89102 Mon Sep 17 00:00:00 2001 From: Patrick Amrhein Date: Thu, 28 Mar 2024 09:21:16 +0100 Subject: [PATCH] 0.8.970008-zero --- src/app.cpp | 3 + src/config/settings.h | 3 + src/defines.h | 2 +- src/plugins/zeroExport/zeroExport.h | 102 +++++++++++++++++++++++++--- 4 files changed, 98 insertions(+), 12 deletions(-) diff --git a/src/app.cpp b/src/app.cpp index 69626606..4c096e83 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -630,6 +630,9 @@ void app::resetSystem(void) { //----------------------------------------------------------------------------- void app::mqttSubRxCb(JsonObject obj) { mApi.ctrlRequest(obj); +#if defined(PLUGIN_ZEROEXPORT) + mZeroExport.onMqttMessage(obj); +#endif } //----------------------------------------------------------------------------- diff --git a/src/config/settings.h b/src/config/settings.h index c6bea7ea..397bd627 100644 --- a/src/config/settings.h +++ b/src/config/settings.h @@ -204,6 +204,7 @@ typedef struct { enum class zeroExportState : uint8_t { INIT, WAIT, + PUBLISH, WAITREFRESH, GETINVERTERACKS, GETINVERTERDATA, @@ -325,6 +326,8 @@ uint8_t pm_iOut = 0; float pmPowerL1; float pmPowerL2; float pmPowerL3; +bool publishPower = false; + bool battSwitch; float grpPower; float grpPowerL1; diff --git a/src/defines.h b/src/defines.h index 6df27ecd..26050953 100644 --- a/src/defines.h +++ b/src/defines.h @@ -13,7 +13,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 8 -#define VERSION_PATCH 970007 +#define VERSION_PATCH 970008 //------------------------------------- typedef struct { diff --git a/src/plugins/zeroExport/zeroExport.h b/src/plugins/zeroExport/zeroExport.h index de3626f9..ca268094 100644 --- a/src/plugins/zeroExport/zeroExport.h +++ b/src/plugins/zeroExport/zeroExport.h @@ -75,6 +75,9 @@ class ZeroExport { case zeroExportState::WAIT: if (groupWait(group)) sendLog(); break; + case zeroExportState::PUBLISH: + if (groupPublish(group)) sendLog(); + break; case zeroExportState::WAITREFRESH: if (groupWaitRefresh(group)) sendLog(); break; @@ -288,6 +291,18 @@ class ZeroExport { } } + /** onMqttMessage + * + */ + void onMqttMessage(JsonObject obj) { + if ((!mIsInitialized) || (!mCfg->enabled)) { + return; + } + + mLog["MQTT"] = obj; + sendLog(); + } + private: /** groupInit * initialisiert die Gruppe und sucht die ivPointer @@ -414,6 +429,71 @@ class ZeroExport { return doLog; } + /** groupPublish + * + */ + bool groupPublish(uint8_t group) { + bool doLog = false; + unsigned long bTsp = millis(); + + mLog["t"] = "groupPublish"; + mLog["g"] = group; + + mCfg->groups[group].stateLast = zeroExportState::PUBLISH; + + if (mMqtt->isConnected()) { + DynamicJsonDocument doc(512); + JsonObject obj = doc.to(); + + doLog = true; + + // Init + if (!mIsSubscribed) { + mIsSubscribed = true; + mMqtt->publish("zero/set/enabled", ((mCfg->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]), false); + mMqtt->subscribe("zero/set/enabled", QOS_2); + } + + mMqtt->publish("zero/state/enabled", ((mCfg->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]), false); + + // if (mCfg->groups[group].publishPower) { + // mCfg->groups[group].publishPower = false; + obj["L1"] = mCfg->groups[group].pmPowerL1; + obj["L2"] = mCfg->groups[group].pmPowerL2; + obj["L2"] = mCfg->groups[group].pmPowerL3; + obj["Sum"] = mCfg->groups[group].pmPower; + mMqtt->publish("zero/state/powermeter/P", doc.as().c_str(), false); + doc.clear(); + // } + + // if (mCfg->groups[group].pm_Publish_W) { + // mCfg->groups[group].pm_Publish_W = false; + // obj["todo"] = "true"; + // obj["L1"] = mCfg->groups[group].pm_P1; + // obj["L2"] = mCfg->groups[group].pm_P2; + // obj["L2"] = mCfg->groups[group].pm_P3; + // obj["Sum"] = mCfg->groups[group].pm_P; + // mMqtt->publish("zero/powermeter/W", doc.as().c_str(), false); + // doc.clear(); + // } + + for (uint8_t inv = 0; inv < ZEROEXPORT_GROUP_MAX_INVERTERS; inv++) { + } + } + + mCfg->groups[group].state = zeroExportState::WAITREFRESH; + mCfg->groups[group].stateNext = zeroExportState::WAITREFRESH; + + unsigned long eTsp = millis(); + mLog["B"] = bTsp; + mLog["E"] = eTsp; + mLog["D"] = eTsp - bTsp; + + mCfg->groups[group].lastRun = eTsp; + + return doLog; + } + /** groupWaitRefresh * pausiert die Gruppe * @param group @@ -791,8 +871,8 @@ class ZeroExport { // Next #if defined(ZEROEXPORT_DEV_POWERMETER) - mCfg->groups[group].state = zeroExportState::WAITREFRESH; - mCfg->groups[group].stateNext = zeroExportState::WAITREFRESH; + mCfg->groups[group].state = zeroExportState::PUBLISH; + mCfg->groups[group].stateNext = zeroExportState::PUBLISH; mCfg->groups[group].lastRefresh = millis(); #else if (result) { @@ -1219,9 +1299,8 @@ class ZeroExport { // Reject limit if difference < 5 W if ( (mCfg->groups[group].inverters[inv].limitNew > (mCfg->groups[group].inverters[inv].limit + ZEROEXPORT_GROUP_WR_LIMIT_MIN_DIFF)) && - (mCfg->groups[group].inverters[inv].limitNew < (mCfg->groups[group].inverters[inv].limit - ZEROEXPORT_GROUP_WR_LIMIT_MIN_DIFF)) - ) { - mLog["err"] = String("Diff < ") + String (ZEROEXPORT_GROUP_WR_LIMIT_MIN_DIFF) + String("W"); + (mCfg->groups[group].inverters[inv].limitNew < (mCfg->groups[group].inverters[inv].limit - ZEROEXPORT_GROUP_WR_LIMIT_MIN_DIFF))) { + mLog["err"] = String("Diff < ") + String(ZEROEXPORT_GROUP_WR_LIMIT_MIN_DIFF) + String("W"); return false; } @@ -1252,8 +1331,8 @@ class ZeroExport { } // Next - mCfg->groups[group].state = zeroExportState::WAITREFRESH; - mCfg->groups[group].stateNext = zeroExportState::WAITREFRESH; + mCfg->groups[group].state = zeroExportState::PUBLISH; + mCfg->groups[group].stateNext = zeroExportState::PUBLISH; unsigned long eTsp = millis(); mLog["B"] = bTsp; @@ -1326,8 +1405,8 @@ class ZeroExport { } // Next - mCfg->groups[group].state = zeroExportState::WAITREFRESH; - mCfg->groups[group].stateNext = zeroExportState::WAITREFRESH; + mCfg->groups[group].state = zeroExportState::PUBLISH; + mCfg->groups[group].stateNext = zeroExportState::PUBLISH; mCfg->groups[group].lastRefresh = millis(); unsigned long eTsp = millis(); @@ -1393,8 +1472,8 @@ class ZeroExport { } // Next - mCfg->groups[group].state = zeroExportState::WAITREFRESH; - mCfg->groups[group].stateNext = zeroExportState::WAITREFRESH; + mCfg->groups[group].state = zeroExportState::PUBLISH; + mCfg->groups[group].stateNext = zeroExportState::PUBLISH; mCfg->groups[group].lastRefresh = millis(); unsigned long eTsp = millis(); @@ -1434,6 +1513,7 @@ class ZeroExport { JsonObject mLog = mDocLog.to(); PubMqttType *mMqtt; powermeter mPowermeter; + bool mIsSubscribed = false; Inverter<> *mIv[ZEROEXPORT_MAX_GROUPS][ZEROEXPORT_GROUP_MAX_INVERTERS]; };