Просмотр исходного кода

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

CaCO3 3 лет назад
Родитель
Сommit
25ae9a045f

+ 37 - 58
code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp

@@ -22,7 +22,7 @@ void ClassFlowMQTT::SetInitialParameter(void)
     topicRate = "";
     topicTimeStamp = "";
     maintopic = "";
-    mainerrortopic = ""; 
+    lwt = ""; 
 
     topicUptime = "";
     topicFreeMem = "";
@@ -121,66 +121,51 @@ 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))
+        if (!MQTTInit(this->uri, this->clientname, this->user, this->password, this->lwt, keepAlive))
         { // Failed
             MQTTenable = false;
             return false;
         } 
-    }
 
-    // Try again and quit if it fails
-    if (!MQTTPublish(mainerrortopic, "connected", SetRetainFlag))
-    { // Failed
-        MQTTenable = false;
-        return false;
+        // Try again sending LWT and quit if it fails
+        if (!MQTTPublish(lwt, "connected", SetRetainFlag))
+        { // Failed
+            MQTTenable = false;
+            return false;
+        }
     }
 
-
-
-   
- /*   if (!MQTTPublish(mainerrortopic, "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!
-    }*/
-
-
     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,16 +188,28 @@ 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];
     sprintf(freeheapmem, "%zu", esp_get_free_heap_size());

+ 1 - 1
code/components/jomjol_flowcontroll/ClassFlowMQTT.h

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