CaCO3 3 лет назад
Родитель
Сommit
7a280bc7c6

+ 29 - 31
code/components/jomjol_flowcontroll/ClassFlowMQTT.cpp

@@ -16,7 +16,31 @@
 static const char *TAG = "class_flow_MQTT";
 
 
-extern ClassFlowControll tfliteflow;
+void publishRuntimeData(std::string maintopic, int SetRetainFlag) {
+    char tmp_char[50];
+
+    sprintf(tmp_char, "%ld", (long)getUpTime());
+    MQTTPublish(maintopic + "/" + "uptime", std::string(tmp_char), SetRetainFlag);
+    
+    sprintf(tmp_char, "%zu", esp_get_free_heap_size());
+    MQTTPublish(maintopic + "/" + "freeMem", std::string(tmp_char), SetRetainFlag);
+
+    sprintf(tmp_char, "%d", get_WIFI_RSSI());
+    MQTTPublish(maintopic + "/" + "wifiRSSI", std::string(tmp_char), SetRetainFlag);
+
+    sprintf(tmp_char, "%d", (int)temperatureRead());
+    MQTTPublish(maintopic + "/" + "CPUtemp", std::string(tmp_char), SetRetainFlag);
+}
+
+void GotConnected(std::string maintopic, int SetRetainFlag) {
+    MQTTPublish(maintopic + "/" + "mac", getMac(), SetRetainFlag);
+    MQTTPublish(maintopic + "/" + "ip", *getIPAddress(), SetRetainFlag);
+    MQTTPublish(maintopic + "/" + "hostname", hostname, SetRetainFlag);
+
+    publishRuntimeData(maintopic, SetRetainFlag);
+}
+
+
 
 void ClassFlowMQTT::SetInitialParameter(void)
 {
@@ -25,7 +49,7 @@ void ClassFlowMQTT::SetInitialParameter(void)
     topicError = "";
     topicRate = "";
     topicTimeStamp = "";
-    maintopic = "";
+    maintopic = hostname;
 
     topicUptime = "";
     topicFreeMem = "";
@@ -133,25 +157,15 @@ bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph)
         {
             maintopic = zerlegt[1];
         }
-        else { // Main topic not set, use the hostname
-            maintopic = hostname;
-        }
     }
-       
-    MQTT_Configure(uri, clientname, user, password, maintopic, "connection", keepAlive);
+
+    MQTT_Configure(uri, clientname, user, password, maintopic, "connection", keepAlive, SetRetainFlag, (void *)&GotConnected);
 
     if (!MQTT_Init()) {
         if (!MQTT_Init()) { // Retry
             return false;
         }
     }
-
-    MQTTPublish(maintopic + "/" + "mac", getMac(), SetRetainFlag);
-    MQTTPublish(maintopic + "/" + "ip", *getIPAddress(), SetRetainFlag);
-    MQTTPublish(maintopic + "/" + "hostname", hostname, SetRetainFlag);
-
-    publishRuntimeData();
-
     return true;
 }
 
@@ -161,22 +175,6 @@ string ClassFlowMQTT::GetMQTTMainTopic()
     return maintopic;
 }
 
-void ClassFlowMQTT::publishRuntimeData() {
-    char tmp_char[50];
-
-    sprintf(tmp_char, "%ld", (long)getUpTime());
-    MQTTPublish(maintopic + "/" + "uptime", std::string(tmp_char), SetRetainFlag);
-    
-    sprintf(tmp_char, "%zu", esp_get_free_heap_size());
-    MQTTPublish(maintopic + "/" + "freeMem", std::string(tmp_char), SetRetainFlag);
-
-    sprintf(tmp_char, "%d", get_WIFI_RSSI());
-    MQTTPublish(maintopic + "/" + "wifiRSSI", std::string(tmp_char), SetRetainFlag);
-
-    sprintf(tmp_char, "%d", (int)temperatureRead());
-    MQTTPublish(maintopic + "/" + "CPUtemp", std::string(tmp_char), SetRetainFlag);
-}
-
 
 bool ClassFlowMQTT::doFlow(string zwtime)
 {
@@ -189,7 +187,7 @@ bool ClassFlowMQTT::doFlow(string zwtime)
     string zw = "";
     string namenumber = "";
 
-    publishRuntimeData();
+    publishRuntimeData(maintopic, SetRetainFlag);
 
     if (flowpostprocessing)
     {

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

@@ -27,7 +27,6 @@ public:
     string GetMQTTMainTopic();
 
     bool ReadParameter(FILE* pfile, string& aktparamgraph);
-    void publishRuntimeData();
     bool doFlow(string time);
     string name(){return "ClassFlowMQTT";};
 };

+ 21 - 12
code/components/jomjol_mqtt/interface_mqtt.cpp

@@ -19,8 +19,9 @@ esp_mqtt_event_id_t esp_mmqtt_ID = MQTT_EVENT_ANY;
 
 bool mqtt_connected = false;
 esp_mqtt_client_handle_t client = NULL;
-std::string uri, client_id, lwt_topic, user, password;
-int keepalive;
+std::string uri, client_id, lwt_topic, user, password, maintopic;
+int keepalive, SetRetainFlag;
+void (*callbackOnConnected)(std::string, int) = NULL;
 
 
 bool MQTTPublish(std::string _key, std::string _content, int retained_flag) {
@@ -115,13 +116,14 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
 }
 
 
-void MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password, std::string _maintopic, std::string _lwt, int _keepalive){
+void MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password,
+        std::string _maintopic, std::string _lwt, int _keepalive, int _SetRetainFlag, void *_callbackOnConnected){
 #ifdef __HIDE_PASSWORD
     LogFile.WriteToFile(ESP_LOG_INFO, "MQTT Configuration: uri: " + _mqttURI + ", clientname: " + _clientid + 
-            ", user: " + _user + ", password: XXXXXXXX, last-will-topic: " + _maintopic + "/" + _lwt + ", keepAlive: " + std::to_string(_keepalive)); 
+            ", user: " + _user + ", password: XXXXXXXX, maintopic: " + _maintopic + ", last-will-topic: " + _maintopic + "/" + _lwt + ", keepAlive: " + std::to_string(_keepalive)); 
 #else
     LogFile.WriteToFile(ESP_LOG_INFO, "MQTT Configuration: uri: " + _mqttURI + ", clientname: " + _clientid + 
-            ", user: " + _user + ", password: " + _password + ", last-will-topic: " + _maintopic + "/" + _lwt + ", keepAlive: " + std::to_string(_keepalive)); 
+            ", user: " + _user + ", password: " + _password + ", maintopic: " + _maintopic + ", last-will-topic: " + _maintopic + "/" + _lwt + ", keepAlive: " + std::to_string(_keepalive)); 
 #endif
 
 
@@ -129,17 +131,18 @@ void MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _us
     client_id = _clientid;
     lwt_topic = _maintopic + "/" + _lwt;
     keepalive = _keepalive;
+    SetRetainFlag = _SetRetainFlag;
+    maintopic = _maintopic;
+    callbackOnConnected = ( void (*)(std::string, int) )(_callbackOnConnected);
 
     if (_user.length() && _password.length()){
         user = _user;
         password = _password;
     }
-
-
-    LogFile.WriteToFile(ESP_LOG_INFO, "MQTT maintopic: " + _maintopic);
 }
 
 bool MQTT_Init() {
+    esp_err_t ret;
     LogFile.WriteToFile(ESP_LOG_INFO, "MQTT - Init");
 
     MQTTdestroy_client();
@@ -164,14 +167,16 @@ bool MQTT_Init() {
     client = esp_mqtt_client_init(&mqtt_cfg);
     if (client)
     {
-        if (esp_mqtt_client_register_event(client, esp_mmqtt_ID, mqtt_event_handler, client) != ESP_OK)
+        ret = esp_mqtt_client_register_event(client, esp_mmqtt_ID, mqtt_event_handler, client);
+        if (ret != ESP_OK)
         {
-            LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Could not register event!");
+            LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Could not register event (ret=" + std::to_string(ret) + ")!");
             return false;
         }
-        if (esp_mqtt_client_start(client) != ESP_OK)
+        ret = esp_mqtt_client_start(client);
+        if (ret != ESP_OK)
         {
-            LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Could not start client!");
+            LogFile.WriteToFile(ESP_LOG_ERROR, "MQTT - Could not start client (ret=" + std::to_string(ret) + ")!");
             return false;
         }
     }
@@ -260,6 +265,10 @@ void MQTTconnected(){
                 LogFile.WriteToFile(ESP_LOG_INFO, "MQTT - topic " + it->first + " subscribe successful, msg_id=" + std::to_string(msg_id));
             }
         }
+
+        if (callbackOnConnected) {
+            callbackOnConnected(maintopic, SetRetainFlag);
+        }
     }
 }
 

+ 2 - 1
code/components/jomjol_mqtt/interface_mqtt.h

@@ -5,7 +5,8 @@
 #include <map>
 #include <functional>
 
-void MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password, std::string _maintopic, std::string _lwt, int _keepalive);
+void MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password,
+        std::string _maintopic, std::string _lwt, int _keepalive, int SetRetainFlag, void *callbackOnConnected);
 bool MQTT_Init();
 void MQTTdestroy_client();