renamed mainerrortopic with lwt, only send it once on connecting, minor fixes and cleanups

This commit is contained in:
CaCO3
2022-10-26 10:34:15 +02:00
parent 6b47eef4cd
commit 98451c8fc7
2 changed files with 39 additions and 60 deletions

View File

@@ -22,7 +22,7 @@ void ClassFlowMQTT::SetInitialParameter(void)
topicRate = "";
topicTimeStamp = "";
maintopic = "";
mainerrortopic = "";
lwt = "";
topicUptime = "";
topicFreeMem = "";
@@ -121,65 +121,50 @@ bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph)
{
maintopic = zerlegt[1];
}
else { // Main topic not set, use the hostname
maintopic = hostname;
}
}
#ifdef __HIDE_PASSWORD
ESP_LOGD(TAG, "Init Read with uri: %s, clientname: %s, user: %s, password: XXXXXX, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), mainerrortopic.c_str());
ESP_LOGD(TAG, "Init Read with uri: %s, clientname: %s, user: %s, password: XXXXXX, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), maintopic.c_str());
#else
ESP_LOGD(TAG, "Init Read with uri: %s, clientname: %s, user: %s, password: %s, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), password.c_str(), mainerrortopic.c_str());
ESP_LOGD(TAG, "Init Read with uri: %s, clientname: %s, user: %s, password: %s, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), password.c_str(), maintopic.c_str());
#endif
if (!MQTTisConnected() && (uri.length() > 0) && (maintopic.length() > 0))
{
ESP_LOGD(TAG, "InitMQTTInit");
mainerrortopic = maintopic + "/connection";
lwt = maintopic + "/connection";
#ifdef __HIDE_PASSWORD
ESP_LOGD(TAG, "Init MQTT with uri: %s, clientname: %s, user: %s, password: XXXXXXXX, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), mainerrortopic.c_str());
ESP_LOGD(TAG, "Init MQTT with uri: %s, clientname: %s, user: %s, password: XXXXXXXX, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), maintopic.c_str());
#else
ESP_LOGD(TAG, "Init MQTT with uri: %s, clientname: %s, user: %s, password: %s, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), password.c_str(), mainerrortopic.c_str());
ESP_LOGD(TAG, "Init MQTT with uri: %s, clientname: %s, user: %s, password: %s, maintopic: %s", uri.c_str(), clientname.c_str(), user.c_str(), password.c_str(), maintopic.c_str());
#endif
if (!MQTTInit(uri, clientname, user, password, mainerrortopic, keepAlive))
if (!MQTTInit(uri, clientname, user, password, lwt, 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))
// Try sending LWT. If it fails, re-run init
if (!MQTTPublish(lwt, "connected", SetRetainFlag))
{ // Failed
LogFile.WriteToFile(ESP_LOG_WARN, "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))
if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->lwt, keepAlive))
{ // Failed
MQTTenable = false;
return false;
}
/* if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
// Try again sending LWT and quit if it fails
if (!MQTTPublish(lwt, "connected", SetRetainFlag))
{ // Failed
LogFile.WriteToFile(ESP_LOG_ERROR, "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(ESP_LOG_ERROR, "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!
}*/
return false;
}
}
MQTTenable = true;
return true;
@@ -194,24 +179,6 @@ string ClassFlowMQTT::GetMQTTMainTopic()
bool ClassFlowMQTT::doFlow(string zwtime)
{
// Try sending mainerrortopic. If it fails, re-run init
if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
{ // Failed
LogFile.WriteToFile(ESP_LOG_WARN, "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!
}
}
// 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;
std::string resulterror = "";
std::string resultraw = "";
@@ -221,15 +188,27 @@ bool ClassFlowMQTT::doFlow(string zwtime)
string zw = "";
string namenumber = "";
// 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];
sprintf(uptimeStr, "%ld", (long)getUpTime());
MQTTPublish(zw, uptimeStr, SetRetainFlag);
// Try sending uptime. If it fails, re-run init
if (!MQTTPublish(zw, uptimeStr, SetRetainFlag))
{ // Failed
LogFile.WriteToFile(ESP_LOG_WARN, "MQTT - Re-running init...!");
if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->lwt, keepAlive))
{ // Failed
MQTTenable = false;
return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
}
// Try again and quit if it fails
if (!MQTTPublish(zw, uptimeStr, SetRetainFlag))
{ // Failed
MQTTenable = false;
return true; // We need to return true despite we failed, else it will retry 5x and then reboot!
}
}
zw = maintopic + "/" + "freeMem";
char freeheapmem[11];

View File

@@ -17,7 +17,7 @@ protected:
bool MQTTenable;
int keepAlive;
std::string maintopic, mainerrortopic;
std::string maintopic, lwt;
void SetInitialParameter(void);
public: