From ca4cfb5dc5ece215f1bb5d410767d3bee589ab8b Mon Sep 17 00:00:00 2001
From: Jochen Vothknecht <jochen3120@gmail.com>
Date: Sun, 17 Apr 2022 22:03:41 +0200
Subject: [PATCH] Adding bespoken changes and fixing remaining errors

---
 qthing/mqtt.cpp | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)

diff --git a/qthing/mqtt.cpp b/qthing/mqtt.cpp
index 567d7d2..49a47fd 100644
--- a/qthing/mqtt.cpp
+++ b/qthing/mqtt.cpp
@@ -68,9 +68,7 @@ std::map<std::string, message_callback_t> callback_map;
 std::map<std::string, binary_message_callback_t> binary_callback_map;
 
 mqtt_connected_callback_t connected_callback = []() {};
-mqtt_message_info_callback_t message_info_callback = [](const mqtt_message_info_t& ignored) {
-  ESP_LOGW("MSGinfo", "Got a new message: length[ %d ]  direction[ %s ]", ignored.length, ignored.direction==inbound ? "inbound" : "outbound");
-};
+mqtt_message_info_callback_t message_info_callback = [](const mqtt_message_info_t& ignored) {};
 
 std::string multipart_topic;
 
@@ -87,22 +85,22 @@ void on_message(const std::string& topic, const std::string& message) {
 }
 
 void on_binary_message(const multipart_message_t& message) {
+  // let callbacks process the message
+  auto iterator = binary_callback_map.find(message.topic);
+  if (iterator != binary_callback_map.end()) {
+    auto callback = iterator->second;
+    callback(message);
+  }
+
   // inform metrics & co. that we got a message
-  if (message.length + message.offset == message.total_length) {  // only on last part
+  if (message.length + message.offset == message.total_length) {  // only at last part
     mqtt_message_info_t msg_info = {
-      .length = message.length,
+      .length = message.total_length,
       .direction = inbound
     };
 
     message_info_callback(msg_info);
   }
-
-  // let callbacks process the message
-  auto iterator = binary_callback_map.find(message.topic);
-  if (iterator != binary_callback_map.end()) {
-    auto callback = iterator->second;
-    callback(message);
-  }
 }
 
 esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) {
-- 
GitLab