diff --git a/lib/MqttLogger/library.properties b/lib/MqttLogger/library.properties index 5dd5cc2..a6ac783 100644 --- a/lib/MqttLogger/library.properties +++ b/lib/MqttLogger/library.properties @@ -8,4 +8,4 @@ category=Communication url=https://github.com/androbi-com/MqttLogger architectures=* includes=MqttLogger.h -depends=PubSubClient +depends=espMqttClient diff --git a/lib/MqttLogger/src/MqttLogger.cpp b/lib/MqttLogger/src/MqttLogger.cpp index bae0bf2..ada26d1 100644 --- a/lib/MqttLogger/src/MqttLogger.cpp +++ b/lib/MqttLogger/src/MqttLogger.cpp @@ -7,7 +7,7 @@ MqttLogger::MqttLogger(MqttLoggerMode mode) this->setBufferSize(MQTT_MAX_PACKET_SIZE); } -MqttLogger::MqttLogger(NetworkDevice* client, const char* topic, MqttLoggerMode mode) +MqttLogger::MqttLogger(MqttClient& client, const char* topic, MqttLoggerMode mode) { this->setClient(client); this->setTopic(topic); @@ -19,9 +19,9 @@ MqttLogger::~MqttLogger() { } -void MqttLogger::setClient(NetworkDevice* client) +void MqttLogger::setClient(MqttClient& client) { - this->client = client; + this->client = &client; } void MqttLogger::setTopic(const char* topic) @@ -74,9 +74,9 @@ void MqttLogger::sendBuffer() if (this->bufferCnt > 0) { bool doSerial = this->mode==MqttLoggerMode::SerialOnly || this->mode==MqttLoggerMode::MqttAndSerial; - if (this->mode!=MqttLoggerMode::SerialOnly && this->client != NULL && this->client->mqttConnected()) + if (this->mode!=MqttLoggerMode::SerialOnly && this->client != NULL && this->client->connected()) { - this->client->mqttPublish(topic, 0, true, (uint8_t*)this->buffer, this->bufferCnt); + this->client->publish(topic, 0, true, this->buffer, this->bufferCnt); } else if (this->mode == MqttLoggerMode::MqttAndSerialFallback) { doSerial = true; diff --git a/lib/MqttLogger/src/MqttLogger.h b/lib/MqttLogger/src/MqttLogger.h index 795fd10..2be7d1e 100644 --- a/lib/MqttLogger/src/MqttLogger.h +++ b/lib/MqttLogger/src/MqttLogger.h @@ -11,7 +11,7 @@ #include #include -#include "../../../networkDevices/NetworkDevice.h" +#include #define MQTT_MAX_PACKET_SIZE 1024 @@ -29,16 +29,16 @@ private: uint8_t* buffer; uint8_t* bufferEnd; uint16_t bufferCnt = 0, bufferSize = 0; - NetworkDevice* client; + MqttClient* client; MqttLoggerMode mode; void sendBuffer(); public: MqttLogger(MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback); - MqttLogger(NetworkDevice* client, const char* topic, MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback); + MqttLogger(MqttClient& client, const char* topic, MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback); ~MqttLogger(); - void setClient(NetworkDevice* client); + void setClient(MqttClient& client); void setTopic(const char* topic); void setMode(MqttLoggerMode mode); void setRetained(boolean retained); diff --git a/networkDevices/EthLan8720Device.cpp b/networkDevices/EthLan8720Device.cpp index def7bc6..c7a6b46 100644 --- a/networkDevices/EthLan8720Device.cpp +++ b/networkDevices/EthLan8720Device.cpp @@ -57,7 +57,7 @@ EthLan8720Device::EthLan8720Device(const String& hostname, Preferences* preferen String pathStr = preferences->getString(preference_mqtt_lock_path); pathStr.concat(mqtt_topic_log); strcpy(_path, pathStr.c_str()); - Log = new MqttLogger(this, _path, MqttLoggerMode::MqttAndSerial); + Log = new MqttLogger(*getMqttClient(), _path, MqttLoggerMode::MqttAndSerial); } } @@ -342,3 +342,14 @@ void EthLan8720Device::disableMqtt() _mqttEnabled = false; } +MqttClient *EthLan8720Device::getMqttClient() const +{ + if (_useEncryption) + { + return _mqttClientSecure; + } + else + { + return _mqttClient; + } +} diff --git a/networkDevices/EthLan8720Device.h b/networkDevices/EthLan8720Device.h index 1453e1b..93ca817 100644 --- a/networkDevices/EthLan8720Device.h +++ b/networkDevices/EthLan8720Device.h @@ -69,6 +69,7 @@ public: private: void onDisconnected(); + MqttClient *getMqttClient() const; espMqttClient* _mqttClient = nullptr; espMqttClientSecure* _mqttClientSecure = nullptr; diff --git a/networkDevices/W5500Device.cpp b/networkDevices/W5500Device.cpp index e66b6bd..6a89a3e 100644 --- a/networkDevices/W5500Device.cpp +++ b/networkDevices/W5500Device.cpp @@ -61,7 +61,7 @@ void W5500Device::initialize() _path = new char[pathStr.length() + 1]; memset(_path, 0, sizeof(_path)); strcpy(_path, pathStr.c_str()); - Log = new MqttLogger(this, _path, MqttLoggerMode::MqttAndSerial); + Log = new MqttLogger(_mqttClient, _path, MqttLoggerMode::MqttAndSerial); } reconnect(); diff --git a/networkDevices/WifiDevice.cpp b/networkDevices/WifiDevice.cpp index a90ff82..e57a485 100644 --- a/networkDevices/WifiDevice.cpp +++ b/networkDevices/WifiDevice.cpp @@ -51,7 +51,7 @@ WifiDevice::WifiDevice(const String& hostname, Preferences* preferences, const I String pathStr = preferences->getString(preference_mqtt_lock_path); pathStr.concat(mqtt_topic_log); strcpy(_path, pathStr.c_str()); - Log = new MqttLogger(this, _path, MqttLoggerMode::MqttAndSerial); + Log = new MqttLogger(*getMqttClient(), _path, MqttLoggerMode::MqttAndSerial); } } @@ -367,3 +367,15 @@ void WifiDevice::disableMqtt() _mqttEnabled = false; } + +MqttClient *WifiDevice::getMqttClient() const +{ + if (_useEncryption) + { + return _mqttClientSecure; + } + else + { + return _mqttClient; + } +} diff --git a/networkDevices/WifiDevice.h b/networkDevices/WifiDevice.h index d45b30e..c5f15b4 100644 --- a/networkDevices/WifiDevice.h +++ b/networkDevices/WifiDevice.h @@ -61,6 +61,7 @@ private: static void clearRtcInitVar(WiFiManager*); void onDisconnected(); + MqttClient *getMqttClient() const; WiFiManager _wm; espMqttClient* _mqttClient = nullptr;