From 2108a13fa715cf4cb72e4e5719f70b3ed54c9a1c Mon Sep 17 00:00:00 2001 From: lumapu Date: Thu, 27 Apr 2023 21:38:01 +0200 Subject: [PATCH] pub mqtt loop in extra class --- src/publisher/pubMqtt.h | 89 ++--------------------------------- src/publisher/pubMqttIvData.h | 37 +++++++++++---- 2 files changed, 33 insertions(+), 93 deletions(-) diff --git a/src/publisher/pubMqtt.h b/src/publisher/pubMqtt.h index e2bb96f5..049554d7 100644 --- a/src/publisher/pubMqtt.h +++ b/src/publisher/pubMqtt.h @@ -64,7 +64,10 @@ class PubMqtt { mUtcTimestamp = utcTs; mIntervalTimeout = 1; - mSendIvData.setup(sys, utcTs); + mSendIvData.setup(sys, utcTs, &mSendList); + mSendIvData.setPublishFunc([this](const char *subTopic, const char *payload, bool retained) { + publish(subTopic, payload, retained); + }); mDiscovery.running = false; snprintf(mLwtTopic, MQTT_TOPIC_LEN + 5, "%s/mqtt", mCfgMqtt->topic); @@ -566,89 +569,7 @@ class PubMqtt { if(mSendList.empty()) return; - float total[4]; - bool RTRDataHasBeenSent = false; - - while(!mSendList.empty()) { - memset(total, 0, sizeof(float) * 4); - uint8_t curInfoCmd = mSendList.front(); - - if ((curInfoCmd != RealTimeRunData_Debug) || !RTRDataHasBeenSent) { // send RTR Data only once - bool sendTotals = (curInfoCmd == RealTimeRunData_Debug); - - for (uint8_t id = 0; id < mSys->getNumInverters(); id++) { - Inverter<> *iv = mSys->getInverterByPos(id); - if (NULL == iv) - continue; // skip to next inverter - if (!iv->config->enabled) - continue; // skip to next inverter - - // send RTR Data only if status is available - if ((curInfoCmd != RealTimeRunData_Debug) || (MQTT_STATUS_NOT_AVAIL_NOT_PROD != mLastIvState[id])) - sendData(iv, curInfoCmd); - - // calculate total values for RealTimeRunData_Debug - if (sendTotals) { - record_t<> *rec = iv->getRecordStruct(curInfoCmd); - - sendTotals &= (iv->getLastTs(rec) > 0); - if (sendTotals) { - for (uint8_t i = 0; i < rec->length; i++) { - if (CH0 == rec->assign[i].ch) { - switch (rec->assign[i].fieldId) { - case FLD_PAC: - total[0] += iv->getValue(i, rec); - break; - case FLD_YT: - total[1] += iv->getValue(i, rec); - break; - case FLD_YD: - total[2] += iv->getValue(i, rec); - break; - case FLD_PDC: - total[3] += iv->getValue(i, rec); - break; - } - } - } - } - } - yield(); - } - - if (sendTotals) { - uint8_t fieldId; - for (uint8_t i = 0; i < 4; i++) { - bool retained = true; - switch (i) { - default: - case 0: - fieldId = FLD_PAC; - retained = false; - break; - case 1: - fieldId = FLD_YT; - break; - case 2: - fieldId = FLD_YD; - break; - case 3: - fieldId = FLD_PDC; - retained = false; - break; - } - snprintf(mSubTopic, 32 + MAX_NAME_LENGTH, "total/%s", fields[fieldId]); - snprintf(mVal, 40, "%g", ah::round3(total[i])); - publish(mSubTopic, mVal, retained); - } - RTRDataHasBeenSent = true; - yield(); - } - } - - mSendList.pop(); // remove from list once all inverters were processed - } - + mSendIvData.start(); mLastAnyAvail = anyAvail; } diff --git a/src/publisher/pubMqttIvData.h b/src/publisher/pubMqttIvData.h index 361ce307..4c574118 100644 --- a/src/publisher/pubMqttIvData.h +++ b/src/publisher/pubMqttIvData.h @@ -1,3 +1,8 @@ +//----------------------------------------------------------------------------- +// 2023 Ahoy, https://ahoydtu.de +// Creative Commons - https://creativecommons.org/licenses/by-nc-sa/4.0/deed +//----------------------------------------------------------------------------- + #ifndef __PUB_MQTT_IV_DATA_H__ #define __PUB_MQTT_IV_DATA_H__ @@ -10,12 +15,14 @@ typedef std::function class PubMqttIvData { public: - void setup(HMSYSTEM *sys, uint32_t *utcTs) { + void setup(HMSYSTEM *sys, uint32_t *utcTs, std::queue *sendList) { mSys = sys; mUtcTimestamp = utcTs; + mSendList = sendList; mState = IDLE; memset(mIvLastRTRpub, 0, MAX_NUM_INVERTERS * 4); + mRTRDataHasBeenSent = false; mTable[IDLE] = &PubMqttIvData::stateIdle; mTable[START] = &PubMqttIvData::stateStart; @@ -29,11 +36,11 @@ class PubMqttIvData { yield(); } - bool start(uint8_t cmd) { + bool start(void) { if(IDLE != mState) return false; - mCmd = cmd; + mRTRDataHasBeenSent = false; mState = START; return true; } @@ -52,10 +59,17 @@ class PubMqttIvData { void stateStart() { mLastIvId = 0; - mSendTotals = (RealTimeRunData_Debug == mCmd); - memset(mTotal, 0, sizeof(float) * 4); + if(!mSendList->empty()) { + mCmd = mSendList->front(); - mState = FIND_NXT_IV; + if((RealTimeRunData_Debug != mCmd) || !mRTRDataHasBeenSent) { + mSendTotals = (RealTimeRunData_Debug == mCmd); + memset(mTotal, 0, sizeof(float) * 4); + mState = FIND_NXT_IV; + } else + mSendList->pop(); + } else + mState = IDLE; } void stateFindNxtIv() { @@ -159,10 +173,12 @@ class PubMqttIvData { snprintf(mVal, 40, "%g", ah::round3(mTotal[mPos])); mPublish(mSubTopic, mVal, retained); mPos++; - } else - mState = IDLE; + } else { + mSendList->pop(); + mState = START; + } - RTRDataHasBeenSent = true; + mRTRDataHasBeenSent = true; } HMSYSTEM *mSys; @@ -179,9 +195,12 @@ class PubMqttIvData { Inverter<> *mIv; uint8_t mPos; uint32_t mIvLastRTRpub[MAX_NUM_INVERTERS]; + bool mRTRDataHasBeenSent; char mSubTopic[32 + MAX_NAME_LENGTH + 1]; char mVal[40]; + + std::queue *mSendList; }; #endif /*__PUB_MQTT_IV_DATA_H__*/