mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-06 05:35:54 +02:00
pub mqtt loop in extra class
This commit is contained in:
parent
c004c83bf9
commit
2108a13fa7
2 changed files with 33 additions and 93 deletions
|
@ -64,7 +64,10 @@ class PubMqtt {
|
||||||
mUtcTimestamp = utcTs;
|
mUtcTimestamp = utcTs;
|
||||||
mIntervalTimeout = 1;
|
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;
|
mDiscovery.running = false;
|
||||||
|
|
||||||
snprintf(mLwtTopic, MQTT_TOPIC_LEN + 5, "%s/mqtt", mCfgMqtt->topic);
|
snprintf(mLwtTopic, MQTT_TOPIC_LEN + 5, "%s/mqtt", mCfgMqtt->topic);
|
||||||
|
@ -566,89 +569,7 @@ class PubMqtt {
|
||||||
if(mSendList.empty())
|
if(mSendList.empty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
float total[4];
|
mSendIvData.start();
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
mLastAnyAvail = anyAvail;
|
mLastAnyAvail = anyAvail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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__
|
#ifndef __PUB_MQTT_IV_DATA_H__
|
||||||
#define __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>
|
template<class HMSYSTEM>
|
||||||
class PubMqttIvData {
|
class PubMqttIvData {
|
||||||
public:
|
public:
|
||||||
void setup(HMSYSTEM *sys, uint32_t *utcTs) {
|
void setup(HMSYSTEM *sys, uint32_t *utcTs, std::queue<uint8_t> *sendList) {
|
||||||
mSys = sys;
|
mSys = sys;
|
||||||
mUtcTimestamp = utcTs;
|
mUtcTimestamp = utcTs;
|
||||||
|
mSendList = sendList;
|
||||||
mState = IDLE;
|
mState = IDLE;
|
||||||
|
|
||||||
memset(mIvLastRTRpub, 0, MAX_NUM_INVERTERS * 4);
|
memset(mIvLastRTRpub, 0, MAX_NUM_INVERTERS * 4);
|
||||||
|
mRTRDataHasBeenSent = false;
|
||||||
|
|
||||||
mTable[IDLE] = &PubMqttIvData::stateIdle;
|
mTable[IDLE] = &PubMqttIvData::stateIdle;
|
||||||
mTable[START] = &PubMqttIvData::stateStart;
|
mTable[START] = &PubMqttIvData::stateStart;
|
||||||
|
@ -29,11 +36,11 @@ class PubMqttIvData {
|
||||||
yield();
|
yield();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool start(uint8_t cmd) {
|
bool start(void) {
|
||||||
if(IDLE != mState)
|
if(IDLE != mState)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
mCmd = cmd;
|
mRTRDataHasBeenSent = false;
|
||||||
mState = START;
|
mState = START;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -52,10 +59,17 @@ class PubMqttIvData {
|
||||||
|
|
||||||
void stateStart() {
|
void stateStart() {
|
||||||
mLastIvId = 0;
|
mLastIvId = 0;
|
||||||
mSendTotals = (RealTimeRunData_Debug == mCmd);
|
if(!mSendList->empty()) {
|
||||||
memset(mTotal, 0, sizeof(float) * 4);
|
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() {
|
void stateFindNxtIv() {
|
||||||
|
@ -159,10 +173,12 @@ class PubMqttIvData {
|
||||||
snprintf(mVal, 40, "%g", ah::round3(mTotal[mPos]));
|
snprintf(mVal, 40, "%g", ah::round3(mTotal[mPos]));
|
||||||
mPublish(mSubTopic, mVal, retained);
|
mPublish(mSubTopic, mVal, retained);
|
||||||
mPos++;
|
mPos++;
|
||||||
} else
|
} else {
|
||||||
mState = IDLE;
|
mSendList->pop();
|
||||||
|
mState = START;
|
||||||
|
}
|
||||||
|
|
||||||
RTRDataHasBeenSent = true;
|
mRTRDataHasBeenSent = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
HMSYSTEM *mSys;
|
HMSYSTEM *mSys;
|
||||||
|
@ -179,9 +195,12 @@ class PubMqttIvData {
|
||||||
Inverter<> *mIv;
|
Inverter<> *mIv;
|
||||||
uint8_t mPos;
|
uint8_t mPos;
|
||||||
uint32_t mIvLastRTRpub[MAX_NUM_INVERTERS];
|
uint32_t mIvLastRTRpub[MAX_NUM_INVERTERS];
|
||||||
|
bool mRTRDataHasBeenSent;
|
||||||
|
|
||||||
char mSubTopic[32 + MAX_NAME_LENGTH + 1];
|
char mSubTopic[32 + MAX_NAME_LENGTH + 1];
|
||||||
char mVal[40];
|
char mVal[40];
|
||||||
|
|
||||||
|
std::queue<uint8_t> *mSendList;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /*__PUB_MQTT_IV_DATA_H__*/
|
#endif /*__PUB_MQTT_IV_DATA_H__*/
|
||||||
|
|
Loading…
Add table
Reference in a new issue