added return codes. Try to publish. If it fails, run MQTT init and publish again. Increased Keep Alive timeout to make sure it is greater than the flow interval

This commit is contained in:
CaCO3
2022-09-04 17:55:22 +02:00
parent 3023cd139d
commit 395b471700
4 changed files with 128 additions and 44 deletions

View File

@@ -32,9 +32,7 @@ void ClassFlowMQTT::SetInitialParameter(void)
ListFlowControll = NULL;
disabled = false;
MQTTenable = false;
keepAlive = 600; // TODO This must be greater than the Flow Interval!
}
ClassFlowMQTT::ClassFlowMQTT()
@@ -125,15 +123,50 @@ bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph)
printf("InitMQTTInit\n");
mainerrortopic = maintopic + "/connection";
printf("Init MQTT with uri: %s, clientname: %s, user: %s, password: %s, maintopic: %s\n", uri.c_str(), clientname.c_str(), user.c_str(), password.c_str(), mainerrortopic.c_str());
MQTTInit(uri, clientname, user, password, mainerrortopic, 60);
if (MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) {
MQTTenable = true;
}
else {
MQTTenable = true;
if (!MQTTInit(uri, clientname, user, password, mainerrortopic, keepAlive))
{ // Failed
MQTTenable = false;
return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
}
}
// Try sending mainerrortopic. If it fails, re-run init
if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
{ // Failed
LogFile.WriteToFile("MQTT - Re-running init...!");
if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->mainerrortopic, keepAlive))
{ // Failed
MQTTenable = false;
return false;
}
}
// Try again and quit if it fails
if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
{ // Failed
MQTTenable = false;
return false;
}
/* if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
{ // Failed
LogFile.WriteToFile("MQTT - Could not publish connection status!");
MQTTenable = false;
return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
}*/
/* if(!MQTTPublish(_LWTContext, "", 1))
{
LogFile.WriteToFile("MQTT - Could not publish LWT!");
MQTTenable = false;
return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
}*/
MQTTenable = true;
return true;
}
@@ -146,18 +179,43 @@ string ClassFlowMQTT::GetMQTTMainTopic()
bool ClassFlowMQTT::doFlow(string zwtime)
{
if (!MQTTenable) {
LogFile.WriteToFile("MQTT not enabled!");
// if (!MQTTenable) {
// LogFile.WriteToFile("MQTT not enabled!");
//
// // Try again to init it
// if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->mainerrortopic, keepAlive))
// { // Failed
// MQTTenable = false;
// return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
// }
//
// if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
// { // Failed
// MQTTenable = false;
// return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
// }
//
// LogFile.WriteToFile("MQTT is now enabled");
// MQTTenable = true;
// }
// Try again to init it
MQTTInit(this->uri, this->clientname, this->user, this->password, this->mainerrortopic, 60);
if (MQTTPublish(mainerrortopic, "connected", SetRetainFlag)) {
MQTTenable = true;
}
else { // Failed
// Try sending mainerrortopic. If it fails, re-run init
if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
{ // Failed
LogFile.WriteToFile("MQTT - Re-running init...!");
if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->mainerrortopic, keepAlive))
{ // Failed
MQTTenable = false;
return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
}
LogFile.WriteToFile("MQTT is now enabled");
}
}
// Try again and quit if it fails
if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
{ // Failed
MQTTenable = false;
return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
}
std::string result;
@@ -169,12 +227,10 @@ bool ClassFlowMQTT::doFlow(string zwtime)
string zw = "";
string namenumber = "";
if (MQTTPublish(mainerrortopic, "connected")) {
MQTTenable = true;
}
else { // Failed, skip other topics
return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
}
// if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
//{ // Failed, skip other topics
// return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
//}
zw = maintopic + "/" + "uptime";
char uptimeStr[11];
@@ -184,7 +240,10 @@ bool ClassFlowMQTT::doFlow(string zwtime)
zw = maintopic + "/" + "freeMem";
char freeheapmem[11];
sprintf(freeheapmem, "%zu", esp_get_free_heap_size());
MQTTPublish(zw, freeheapmem, SetRetainFlag);
if (!MQTTPublish(zw, freeheapmem, SetRetainFlag))
{ // Failed, skip other topics
return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
}
zw = maintopic + "/" + "wifiRSSI";
char rssi[11];