From 99e0c1568f3afb1e84e9a9a9cb60601f45137a58 Mon Sep 17 00:00:00 2001 From: technyon Date: Fri, 23 Dec 2022 23:24:34 +0100 Subject: [PATCH] make mqtt logging configurable --- PreferencesKeys.h | 1 + WebCfgServer.cpp | 6 ++++++ networkDevices/W5500Device.cpp | 16 ++++++++++------ networkDevices/WifiDevice.cpp | 14 ++++++++++---- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/PreferencesKeys.h b/PreferencesKeys.h index 1b572bb..968d07c 100644 --- a/PreferencesKeys.h +++ b/PreferencesKeys.h @@ -6,6 +6,7 @@ #define preference_mqtt_broker_port "mqttport" #define preference_mqtt_user "mqttuser" #define preference_mqtt_password "mqttpass" +#define preference_mqtt_log_enabled "mqttlog" #define preference_lock_enabled "lockena" #define preference_mqtt_lock_path "mqttpath" #define preference_opener_enabled "openerena" diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index 8104969..b8568a7 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -277,6 +277,11 @@ bool WebCfgServer::processArgs(String& message) _preferences->putInt(preference_restart_timer, value.toInt()); configChanged = true; } + else if(key == "MQTTLOG") + { + _preferences->putBool(preference_mqtt_log_enabled, (value == "1")); + configChanged = true; + } else if(key == "LSTINT") { _preferences->putInt(preference_query_interval_lockstate, value.toInt()); @@ -586,6 +591,7 @@ void WebCfgServer::buildMqttConfigHtml(String &response) printInputField(response, "NETTIMEOUT", "Network Timeout until restart (seconds; -1 to disable)", _preferences->getInt(preference_network_timeout), 5); printCheckBox(response, "RSTDISC", "Restart on disconnect", _preferences->getBool(preference_restart_on_disconnect)); printInputField(response, "RSTTMR", "Restart timer (minutes; -1 to disable)", _preferences->getInt(preference_restart_timer), 10); + printCheckBox(response, "MQTTLOG", "Enable MQTT logging", _preferences->getBool(preference_mqtt_log_enabled)); response.concat(""); response.concat("* If no encryption is configured for the MQTT broker, leave empty. Only supported for WiFi connections.
"); diff --git a/networkDevices/W5500Device.cpp b/networkDevices/W5500Device.cpp index 4bfd2df..74baf64 100644 --- a/networkDevices/W5500Device.cpp +++ b/networkDevices/W5500Device.cpp @@ -42,13 +42,17 @@ void W5500Device::initialize() _mqttClient = new PubSubClient(*_ethClient); _mqttClient->setBufferSize(_mqttMaxBufferSize); - _path = new char[200]; - memset(_path, 0, sizeof(_path)); - String pathStr = _preferences->getString(preference_mqtt_lock_path); - pathStr.concat(mqtt_topic_log); - strcpy(_path, pathStr.c_str()); - Log = new MqttLogger(*_mqttClient, _path); + if(_preferences->getBool(preference_mqtt_log_enabled)) + { + _path = new char[200]; + memset(_path, 0, sizeof(_path)); + + String pathStr = _preferences->getString(preference_mqtt_lock_path); + pathStr.concat(mqtt_topic_log); + strcpy(_path, pathStr.c_str()); + Log = new MqttLogger(*_mqttClient, _path); + } reconnect(); } diff --git a/networkDevices/WifiDevice.cpp b/networkDevices/WifiDevice.cpp index 948aab0..5de0ff4 100644 --- a/networkDevices/WifiDevice.cpp +++ b/networkDevices/WifiDevice.cpp @@ -41,10 +41,16 @@ WifiDevice::WifiDevice(const String& hostname, Preferences* _preferences) _mqttClient = new PubSubClient(*_wifiClient); } - String pathStr = _preferences->getString(preference_mqtt_lock_path); - pathStr.concat(mqtt_topic_log); - strcpy(_path, pathStr.c_str()); - Log = new MqttLogger(*_mqttClient, _path); + if(_preferences->getBool(preference_mqtt_log_enabled)) + { + _path = new char[200]; + memset(_path, 0, sizeof(_path)); + + String pathStr = _preferences->getString(preference_mqtt_lock_path); + pathStr.concat(mqtt_topic_log); + strcpy(_path, pathStr.c_str()); + Log = new MqttLogger(*_mqttClient, _path); + } } PubSubClient *WifiDevice::mqttClient()