Browse Source

started to convert for and while loop to async loops

pull/935/head
lumapu 2 years ago
parent
commit
1103374abd
  1. 117
      src/publisher/pubMqtt.h

117
src/publisher/pubMqtt.h

@ -34,13 +34,6 @@ struct alarm_t {
alarm_t(uint16_t c, uint32_t s, uint32_t e) : code(c), start(s), end(e) {}
};
typedef struct {
bool running;
uint8_t lastIvId;
uint8_t sub;
uint8_t foundIvCnt;
} discovery_t;
template<class HMSYSTEM>
class PubMqtt {
public:
@ -64,6 +57,7 @@ class PubMqtt {
mIntervalTimeout = 1;
mDiscovery.running = false;
mSendIvData.running = false;
snprintf(mLwtTopic, MQTT_TOPIC_LEN + 5, "%s/mqtt", mCfgMqtt->topic);
@ -93,6 +87,11 @@ class PubMqtt {
yield();
#endif
if(mSendIvData.running) {
sendIvDataLoop();
return;
}
if(mDiscovery.running)
discoveryConfigLoop();
}
@ -107,13 +106,14 @@ class PubMqtt {
return; // next try in a second
}
if(0 == mCfgMqtt->interval) // no fixed interval, publish once new data were received (from inverter)
sendIvData();
if(0 == mCfgMqtt->interval) { // no fixed interval, publish once new data were received (from inverter)
sendIvDataStart();
}
else { // send mqtt data in a fixed interval
if(mIntervalTimeout == 0) {
mIntervalTimeout = mCfgMqtt->interval;
mSendList.push(RealTimeRunData_Debug);
sendIvData();
sendIvDataStart();
}
}
}
@ -554,55 +554,65 @@ class PubMqtt {
}
}
void sendIvData() {
void sendIvDataStart() {
mSendIvData.RTRDataHasBeenSent = false;
memset(mSendIvData.total, 0, sizeof(float) * 4);
for (uint8_t id = 0; id < mSys->getNumInverters(); id++) {
Inverter<> *iv = mSys->getInverterByPos(id);
if (NULL != iv) {
if (iv->config->enabled) {
mSendIvData.lastIvId = id;
mSendIvData.running = true;
mSendIvData.lastIvReached = false;
mSendIvData.sendTotals = false;
break;
}
}
}
}
void sendIvDataLoop(void) {
bool anyAvail = processIvStatus();
if (mLastAnyAvail != anyAvail)
mSendList.push(RealTimeRunData_Debug); // makes shure that total values are calculated
mSendList.push(RealTimeRunData_Debug); // makes sure that total values are calculated
if(mSendList.empty())
if(mSendList.empty()) {
mSendIvData.running = false;
return;
}
float total[4];
bool RTRDataHasBeenSent = false;
while(!mSendList.empty()) {
memset(total, 0, sizeof(float) * 4);
//while(!mSendList.empty()) {
uint8_t curInfoCmd = mSendList.front();
if ((curInfoCmd != RealTimeRunData_Debug) || !mSendIvData.RTRDataHasBeenSent) { // send RTR Data only once
mSendIvData.sendTotals = (curInfoCmd == RealTimeRunData_Debug);
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
if(!mSendIvData.lastIvReached) {
Inverter<> *iv = mSys->getInverterByPos(mSendIvData.lastIvId);
// send RTR Data only if status is available
if ((curInfoCmd != RealTimeRunData_Debug) || (MQTT_STATUS_NOT_AVAIL_NOT_PROD != mLastIvState[id]))
if ((curInfoCmd != RealTimeRunData_Debug) || (MQTT_STATUS_NOT_AVAIL_NOT_PROD != mLastIvState[mSendIvData.lastIvId]))
sendData(iv, curInfoCmd);
// calculate total values for RealTimeRunData_Debug
if (sendTotals) {
if (mSendIvData.sendTotals) {
record_t<> *rec = iv->getRecordStruct(curInfoCmd);
sendTotals &= (iv->getLastTs(rec) > 0);
if (sendTotals) {
mSendIvData.sendTotals &= (iv->getLastTs(rec) > 0);
if (mSendIvData.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);
mSendIvData.total[0] += iv->getValue(i, rec);
break;
case FLD_YT:
total[1] += iv->getValue(i, rec);
mSendIvData.total[1] += iv->getValue(i, rec);
break;
case FLD_YD:
total[2] += iv->getValue(i, rec);
mSendIvData.total[2] += iv->getValue(i, rec);
break;
case FLD_PDC:
total[3] += iv->getValue(i, rec);
mSendIvData.total[3] += iv->getValue(i, rec);
break;
}
}
@ -610,9 +620,21 @@ class PubMqtt {
}
}
yield();
// get next inverter
for (uint8_t id = mSendIvData.lastIvId; id < mSys->getNumInverters(); id++) {
Inverter<> *iv = mSys->getInverterByPos(id);
if (NULL != iv) {
if (iv->config->enabled) {
mSendIvData.lastIvId = id;
return;
}
}
}
mSendIvData.lastIvReached = true;
}
if (sendTotals) {
if (mSendIvData.sendTotals) {
uint8_t fieldId;
for (uint8_t i = 0; i < 4; i++) {
bool retained = true;
@ -634,20 +656,36 @@ class PubMqtt {
break;
}
snprintf(mSubTopic, 32 + MAX_NAME_LENGTH, "total/%s", fields[fieldId]);
snprintf(mVal, 40, "%g", ah::round3(total[i]));
snprintf(mVal, 40, "%g", ah::round3(mSendIvData.total[i]));
publish(mSubTopic, mVal, retained);
}
RTRDataHasBeenSent = true;
mSendIvData.RTRDataHasBeenSent = true;
yield();
}
}
mSendList.pop(); // remove from list once all inverters were processed
}
//} // end while
mLastAnyAvail = anyAvail;
}
typedef struct {
bool running;
uint8_t lastIvId;
bool lastIvReached;
bool sendTotals;
float total[4];
bool RTRDataHasBeenSent;
} publish_t;
typedef struct {
bool running;
uint8_t lastIvId;
uint8_t sub;
uint8_t foundIvCnt;
} discovery_t;
espMqttClient mClient;
cfgMqtt_t *mCfgMqtt;
#if defined(ESP8266)
@ -674,6 +712,7 @@ class PubMqtt {
char mSubTopic[32 + MAX_NAME_LENGTH + 1];
char mVal[40];
discovery_t mDiscovery;
publish_t mSendIvData;
};
#endif /*__PUB_MQTT_H__*/

Loading…
Cancel
Save