From 76076d3907ec4e207efea83c6d0c05fa219abfd1 Mon Sep 17 00:00:00 2001 From: Matteo Giaccone Date: Sat, 5 Mar 2022 14:10:38 +0100 Subject: [PATCH] Send MQTT message when done processing one This allows waiting on the other end to avoid sending too many messages, and also sends ACK so that we can be sure that the message has been received properly. --- src/log.esp | 20 ++++++-------------- src/mqtt.esp | 34 ++++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/log.esp b/src/log.esp index 936e1939..cdd635ed 100644 --- a/src/log.esp +++ b/src/log.esp @@ -1,4 +1,4 @@ -void extern mqtt_publish_event(JsonDocument *root); +void extern mqttPublishEvent(JsonDocument *root); void ICACHE_FLASH_ATTR writeEvent(String type, String src, String desc, String data) { @@ -12,7 +12,7 @@ void ICACHE_FLASH_ATTR writeEvent(String type, String src, String desc, String d { root["cmd"] = "event"; root["door"] = config.deviceHostname; - mqtt_publish_event(&root); + mqttPublishEvent(&root); } else // log to file { @@ -33,18 +33,10 @@ void ICACHE_FLASH_ATTR writeLatest(String uid, String username, int acctype) root["username"] = username; root["acctype"] = acctype; root["timestamp"] = now(); - if ((config.mqttEvents) && (config.mqttEnabled == 1)) - { // log to MQTT - //root["cmd"] = "access"; - //mqtt_publish_event(&root); - } - else // log to file - { - File latestlog = SPIFFS.open("/latestlog.json", "a"); - serializeJson(root, latestlog); - latestlog.print("\n"); - latestlog.close(); - } + File latestlog = SPIFFS.open("/latestlog.json", "a"); + serializeJson(root, latestlog); + latestlog.print("\n"); + latestlog.close(); } size_t lastPos; // position counter for fast seek diff --git a/src/mqtt.esp b/src/mqtt.esp index b9b4823c..76a9a55c 100644 --- a/src/mqtt.esp +++ b/src/mqtt.esp @@ -55,6 +55,22 @@ void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) } } +void mqttPublishEvent(JsonDocument *root) +{ + if (mqttClient.connected()) + { + String stopic(mqttTopic); + stopic = stopic + "/send"; + String mqttBuffer; + serializeJson(*root, mqttBuffer); + mqttClient.publish(stopic.c_str(), 0, false, mqttBuffer.c_str()); +#ifdef DEBUG + Serial.print("[ INFO ] Mqtt Publish:"); + Serial.println(mqttBuffer); +#endif + } +} + void mqtt_publish_boot(time_t boot_time) { String stopic(config.mqttTopic); @@ -288,18 +304,6 @@ void mqtt_publish_io(String const &io, String const &state) } } -void mqtt_publish_event(JsonDocument *root) -{ - if (mqttClient.connected()) - { - String stopic(config.mqttTopic); - stopic = stopic + "/send"; - String mqttBuffer; - serializeJson(*root, mqttBuffer); - mqttClient.publish(stopic.c_str(), 0, false, mqttBuffer.c_str()); - } -} - void onMqttPublish(uint16_t packetId) { writeEvent("INFO", "mqtt", "MQTT publish acknowledged", String(packetId)); @@ -465,6 +469,12 @@ void onMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties lastMessage->nextMessage = incomingMessage; } + DynamicJsonDocument root(512); + root["type"] = mqttIncomingJson["cmd"]; + root["ip"] = WiFi.localIP().toString(); + root["hostname"] = deviceHostname; + mqttPublishEvent(&root); + return; }