Browse Source

pub mqtt loop in extra class

pull/935/head
lumapu 2 years ago
parent
commit
2108a13fa7
  1. 89
      src/publisher/pubMqtt.h
  2. 33
      src/publisher/pubMqttIvData.h

89
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;
}

33
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<void(const char *subTopic, const char *payload, bool retai
template<class HMSYSTEM>
class PubMqttIvData {
public:
void setup(HMSYSTEM *sys, uint32_t *utcTs) {
void setup(HMSYSTEM *sys, uint32_t *utcTs, std::queue<uint8_t> *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;
if(!mSendList->empty()) {
mCmd = mSendList->front();
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<uint8_t> *mSendList;
};
#endif /*__PUB_MQTT_IV_DATA_H__*/

Loading…
Cancel
Save