From c716afb25420b1c1ae271b89659d33221fd55140 Mon Sep 17 00:00:00 2001 From: Luca Oliano Date: Sun, 4 Feb 2024 19:05:26 +0100 Subject: [PATCH 1/3] remove NetworkDevice dependency from MqttLogger library --- lib/MqttLogger/library.properties | 2 +- lib/MqttLogger/src/MqttLogger.cpp | 10 +++++----- lib/MqttLogger/src/MqttLogger.h | 8 ++++---- networkDevices/EthLan8720Device.cpp | 13 ++++++++++++- networkDevices/EthLan8720Device.h | 1 + networkDevices/W5500Device.cpp | 2 +- networkDevices/WifiDevice.cpp | 14 +++++++++++++- networkDevices/WifiDevice.h | 1 + 8 files changed, 38 insertions(+), 13 deletions(-) diff --git a/lib/MqttLogger/library.properties b/lib/MqttLogger/library.properties index 5dd5cc2a..a6ac783a 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 bae0bf2e..ada26d1c 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 795fd100..2be7d1e8 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 @@ class MqttLogger : public Print 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 def7bc68..c7a6b469 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 1453e1b3..93ca8177 100644 --- a/networkDevices/EthLan8720Device.h +++ b/networkDevices/EthLan8720Device.h @@ -69,6 +69,7 @@ class EthLan8720Device : public NetworkDevice private: void onDisconnected(); + MqttClient *getMqttClient() const; espMqttClient* _mqttClient = nullptr; espMqttClientSecure* _mqttClientSecure = nullptr; diff --git a/networkDevices/W5500Device.cpp b/networkDevices/W5500Device.cpp index e66b6bd6..6a89a3ef 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 a90ff822..e57a4856 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 d45b30e6..c5f15b45 100644 --- a/networkDevices/WifiDevice.h +++ b/networkDevices/WifiDevice.h @@ -61,6 +61,7 @@ class WifiDevice : public NetworkDevice static void clearRtcInitVar(WiFiManager*); void onDisconnected(); + MqttClient *getMqttClient() const; WiFiManager _wm; espMqttClient* _mqttClient = nullptr; From 05f2c177129c3cbd05b435c95ed7d348a9cb1d4f Mon Sep 17 00:00:00 2001 From: Luca Oliano Date: Mon, 5 Feb 2024 10:48:18 +0100 Subject: [PATCH 2/3] refactor network devices hierarchy --- networkDevices/EthLan8720Device.cpp | 215 ------------------------- networkDevices/EthLan8720Device.h | 39 ----- networkDevices/NetworkDevice.cpp | 161 +++++++++++++++++++ networkDevices/NetworkDevice.h | 46 +++--- networkDevices/W5500Device.cpp | 92 +---------- networkDevices/W5500Device.h | 35 +--- networkDevices/WifiDevice.cpp | 221 -------------------------- networkDevices/WifiDevice.h | 38 ----- networkDevices/espMqttClientW5500.cpp | 2 +- networkDevices/espMqttClientW5500.h | 4 +- 10 files changed, 196 insertions(+), 657 deletions(-) create mode 100644 networkDevices/NetworkDevice.cpp diff --git a/networkDevices/EthLan8720Device.cpp b/networkDevices/EthLan8720Device.cpp index c7a6b469..d550fbb1 100644 --- a/networkDevices/EthLan8720Device.cpp +++ b/networkDevices/EthLan8720Device.cpp @@ -96,12 +96,6 @@ void EthLan8720Device::reconfigure() restartEsp(RestartReason::ReconfigureLAN8720); } -void EthLan8720Device::printError() -{ - Log->print(F("Free Heap: ")); - Log->println(ESP.getFreeHeap()); -} - bool EthLan8720Device::supportsEncryption() { return true; @@ -132,20 +126,6 @@ ReconnectStatus EthLan8720Device::reconnect() return isConnected() ? ReconnectStatus::Success : ReconnectStatus::Failure; } -void EthLan8720Device::update() -{ - if(_mqttEnabled) - { - if (_useEncryption) - { - _mqttClientSecure->loop(); - } else - { - _mqttClient->loop(); - } - } -} - void EthLan8720Device::onDisconnected() { if(millis() > 60000) @@ -158,198 +138,3 @@ int8_t EthLan8720Device::signalStrength() { return -1; } - -void EthLan8720Device::mqttSetClientId(const char *clientId) -{ - if(_useEncryption) - { - _mqttClientSecure->setClientId(clientId); - } - else - { - _mqttClient->setClientId(clientId); - } -} - -void EthLan8720Device::mqttSetCleanSession(bool cleanSession) -{ - if(_useEncryption) - { - _mqttClientSecure->setCleanSession(cleanSession); - } - else - { - _mqttClient->setCleanSession(cleanSession); - } -} - -uint16_t EthLan8720Device::mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload) -{ - if(_useEncryption) - { - return _mqttClientSecure->publish(topic, qos, retain, payload); - } - else - { - return _mqttClient->publish(topic, qos, retain, payload); - } -} - -uint16_t EthLan8720Device::mqttPublish(const char *topic, uint8_t qos, bool retain, const uint8_t *payload, size_t length) -{ - if(_useEncryption) - { - return _mqttClientSecure->publish(topic, qos, retain, payload, length); - } - else - { - return _mqttClient->publish(topic, qos, retain, payload, length); - } -} - -bool EthLan8720Device::mqttConnected() const -{ - if(_useEncryption) - { - return _mqttClientSecure->connected(); - } - else - { - return _mqttClient->connected(); - } -} - -void EthLan8720Device::mqttSetServer(const char *host, uint16_t port) -{ - if(_useEncryption) - { - _mqttClientSecure->setServer(host, port); - } - else - { - _mqttClient->setServer(host, port); - } -} - -bool EthLan8720Device::mqttConnect() -{ - if(_useEncryption) - { - return _mqttClientSecure->connect(); - } - else - { - return _mqttClient->connect(); - } -} - -bool EthLan8720Device::mqttDisconnect(bool force) -{ - if(_useEncryption) - { - return _mqttClientSecure->disconnect(force); - } - else - { - return _mqttClient->disconnect(force); - } -} - -void EthLan8720Device::setWill(const char *topic, uint8_t qos, bool retain, const char *payload) -{ - if(_useEncryption) - { - _mqttClientSecure->setWill(topic, qos, retain, payload); - } - else - { - _mqttClient->setWill(topic, qos, retain, payload); - } -} - -void EthLan8720Device::mqttSetCredentials(const char *username, const char *password) -{ - if(_useEncryption) - { - _mqttClientSecure->setCredentials(username, password); - } - else - { - _mqttClient->setCredentials(username, password); - } -} - -void EthLan8720Device::mqttOnMessage(espMqttClientTypes::OnMessageCallback callback) -{ - if(_useEncryption) - { - _mqttClientSecure->onMessage(callback); - } - else - { - _mqttClient->onMessage(callback); - } -} - - -void EthLan8720Device::mqttOnConnect(espMqttClientTypes::OnConnectCallback callback) -{ - if(_useEncryption) - { - _mqttClientSecure->onConnect(callback); - } - else - { - _mqttClient->onConnect(callback); - } -} - -void EthLan8720Device::mqttOnDisconnect(espMqttClientTypes::OnDisconnectCallback callback) -{ - if(_useEncryption) - { - _mqttClientSecure->onDisconnect(callback); - } - else - { - _mqttClient->onDisconnect(callback); - } -} - - -uint16_t EthLan8720Device::mqttSubscribe(const char *topic, uint8_t qos) -{ - if(_useEncryption) - { - return _mqttClientSecure->subscribe(topic, qos); - } - else - { - return _mqttClient->subscribe(topic, qos); - } -} - -void EthLan8720Device::disableMqtt() -{ - if (_useEncryption) - { - _mqttClientSecure->disconnect(); - } else - { - _mqttClient->disconnect(); - } - - _mqttEnabled = false; -} - -MqttClient *EthLan8720Device::getMqttClient() const -{ - if (_useEncryption) - { - return _mqttClientSecure; - } - else - { - return _mqttClient; - } -} diff --git a/networkDevices/EthLan8720Device.h b/networkDevices/EthLan8720Device.h index 93ca8177..14fb7429 100644 --- a/networkDevices/EthLan8720Device.h +++ b/networkDevices/EthLan8720Device.h @@ -28,56 +28,18 @@ class EthLan8720Device : public NetworkDevice virtual void initialize(); virtual void reconfigure(); virtual ReconnectStatus reconnect(); - virtual void printError(); bool supportsEncryption() override; - virtual void update(); - virtual bool isConnected(); int8_t signalStrength() override; - void mqttSetClientId(const char *clientId) override; - - void mqttSetCleanSession(bool cleanSession) override; - - uint16_t mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload) override; - - uint16_t mqttPublish(const char *topic, uint8_t qos, bool retain, const uint8_t *payload, size_t length) override; - - bool mqttConnected() const override; - - void mqttSetServer(const char *host, uint16_t port) override; - - bool mqttConnect() override; - - bool mqttDisconnect(bool force) override; - - void setWill(const char *topic, uint8_t qos, bool retain, const char *payload) override; - - void mqttSetCredentials(const char *username, const char *password) override; - - void mqttOnMessage(espMqttClientTypes::OnMessageCallback callback) override; - - void mqttOnConnect(espMqttClientTypes::OnConnectCallback callback) override; - - void mqttOnDisconnect(espMqttClientTypes::OnDisconnectCallback callback) override; - - uint16_t mqttSubscribe(const char *topic, uint8_t qos) override; - - void disableMqtt() override; - private: void onDisconnected(); - MqttClient *getMqttClient() const; - - espMqttClient* _mqttClient = nullptr; - espMqttClientSecure* _mqttClientSecure = nullptr; bool _restartOnDisconnect = false; bool _startAp = false; char* _path; - bool _useEncryption = false; bool _hardwareInitialized = false; bool _lastConnected = false; @@ -89,7 +51,6 @@ class EthLan8720Device : public NetworkDevice eth_phy_type_t _type; eth_clock_mode_t _clock_mode; bool _use_mac_from_efuse; - bool _mqttEnabled = true; char _ca[TLS_CA_MAX_SIZE] = {0}; char _cert[TLS_CERT_MAX_SIZE] = {0}; diff --git a/networkDevices/NetworkDevice.cpp b/networkDevices/NetworkDevice.cpp new file mode 100644 index 00000000..6ca5d305 --- /dev/null +++ b/networkDevices/NetworkDevice.cpp @@ -0,0 +1,161 @@ +#include +#include "NetworkDevice.h" +#include "../Logger.h" + +void NetworkDevice::printError() +{ + Log->print(F("Free Heap: ")); + Log->println(ESP.getFreeHeap()); +} + +void NetworkDevice::update() +{ + if (_mqttEnabled) + { + getMqttClient()->loop(); + } +} + +void NetworkDevice::mqttSetClientId(const char *clientId) +{ + if (_useEncryption) + { + _mqttClientSecure->setClientId(clientId); + } + else + { + _mqttClient->setClientId(clientId); + } +} + +void NetworkDevice::mqttSetCleanSession(bool cleanSession) +{ + if (_useEncryption) + { + _mqttClientSecure->setCleanSession(cleanSession); + } + else + { + _mqttClient->setCleanSession(cleanSession); + } +} + +uint16_t NetworkDevice::mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload) +{ + return getMqttClient()->publish(topic, qos, retain, payload); +} + +uint16_t NetworkDevice::mqttPublish(const char *topic, uint8_t qos, bool retain, const uint8_t *payload, size_t length) +{ + return getMqttClient()->publish(topic, qos, retain, payload, length); +} + +bool NetworkDevice::mqttConnected() const +{ + return getMqttClient()->connected(); +} + +void NetworkDevice::mqttSetServer(const char *host, uint16_t port) +{ + if (_useEncryption) + { + _mqttClientSecure->setServer(host, port); + } + else + { + _mqttClient->setServer(host, port); + } +} + +bool NetworkDevice::mqttConnect() +{ + return getMqttClient()->connect(); +} + +bool NetworkDevice::mqttDisconnect(bool force) +{ + return getMqttClient()->disconnect(force); +} + +void NetworkDevice::setWill(const char *topic, uint8_t qos, bool retain, const char *payload) +{ + if (_useEncryption) + { + _mqttClientSecure->setWill(topic, qos, retain, payload); + } + else + { + _mqttClient->setWill(topic, qos, retain, payload); + } +} + +void NetworkDevice::mqttSetCredentials(const char *username, const char *password) +{ + if (_useEncryption) + { + _mqttClientSecure->setCredentials(username, password); + } + else + { + _mqttClient->setCredentials(username, password); + } +} + +void NetworkDevice::mqttOnMessage(espMqttClientTypes::OnMessageCallback callback) +{ + if (_useEncryption) + { + _mqttClientSecure->onMessage(callback); + } + else + { + _mqttClient->onMessage(callback); + } +} + +void NetworkDevice::mqttOnConnect(espMqttClientTypes::OnConnectCallback callback) +{ + if(_useEncryption) + { + _mqttClientSecure->onConnect(callback); + } + else + { + _mqttClient->onConnect(callback); + } +} + +void NetworkDevice::mqttOnDisconnect(espMqttClientTypes::OnDisconnectCallback callback) +{ + if (_useEncryption) + { + _mqttClientSecure->onDisconnect(callback); + } + else + { + _mqttClient->onDisconnect(callback); + } +} + +uint16_t NetworkDevice::mqttSubscribe(const char *topic, uint8_t qos) +{ + return getMqttClient()->subscribe(topic, qos); +} + +void NetworkDevice::disableMqtt() +{ + getMqttClient()->disconnect(); + _mqttEnabled = false; +} + +MqttClient *NetworkDevice::getMqttClient() const +{ + if (_useEncryption) + { + return _mqttClientSecure; + } + else + { + return _mqttClient; + } +} \ No newline at end of file diff --git a/networkDevices/NetworkDevice.h b/networkDevices/NetworkDevice.h index a850696b..7c945ee1 100644 --- a/networkDevices/NetworkDevice.h +++ b/networkDevices/NetworkDevice.h @@ -1,6 +1,6 @@ #pragma once -#include "MqttClient.h" +#include "espMqttClient.h" #include "MqttClientSetup.h" #include "IPConfiguration.h" @@ -24,32 +24,40 @@ class NetworkDevice virtual void initialize() = 0; virtual ReconnectStatus reconnect() = 0; virtual void reconfigure() = 0; - virtual void printError() = 0; + virtual void printError(); virtual bool supportsEncryption() = 0; - virtual void update() = 0; + virtual void update(); virtual bool isConnected() = 0; virtual int8_t signalStrength() = 0; - virtual void mqttSetClientId(const char* clientId) = 0; - virtual void mqttSetCleanSession(bool cleanSession) = 0; - virtual uint16_t mqttPublish(const char* topic, uint8_t qos, bool retain, const char* payload) = 0; - virtual uint16_t mqttPublish(const char* topic, uint8_t qos, bool retain, const uint8_t* payload, size_t length) = 0; - virtual bool mqttConnected() const = 0; - virtual void mqttSetServer(const char* host, uint16_t port) = 0; - virtual bool mqttConnect() = 0; - virtual bool mqttDisconnect(bool force) = 0; - virtual void setWill(const char* topic, uint8_t qos, bool retain, const char* payload) = 0; - virtual void mqttSetCredentials(const char* username, const char* password) = 0; - virtual void mqttOnMessage(espMqttClientTypes::OnMessageCallback callback) = 0; - virtual void mqttOnConnect(espMqttClientTypes::OnConnectCallback callback) = 0; - virtual void mqttOnDisconnect(espMqttClientTypes::OnDisconnectCallback callback) = 0; - virtual void disableMqtt() = 0; - - virtual uint16_t mqttSubscribe(const char* topic, uint8_t qos) = 0; + virtual void mqttSetClientId(const char* clientId); + virtual void mqttSetCleanSession(bool cleanSession); + virtual uint16_t mqttPublish(const char* topic, uint8_t qos, bool retain, const char* payload); + virtual uint16_t mqttPublish(const char* topic, uint8_t qos, bool retain, const uint8_t* payload, size_t length); + virtual bool mqttConnected() const; + virtual void mqttSetServer(const char* host, uint16_t port); + virtual bool mqttConnect(); + virtual bool mqttDisconnect(bool force); + virtual void setWill(const char* topic, uint8_t qos, bool retain, const char* payload); + virtual void mqttSetCredentials(const char* username, const char* password); + virtual void mqttOnMessage(espMqttClientTypes::OnMessageCallback callback); + virtual void mqttOnConnect(espMqttClientTypes::OnConnectCallback callback); + virtual void mqttOnDisconnect(espMqttClientTypes::OnDisconnectCallback callback); + virtual void disableMqtt(); + + virtual uint16_t mqttSubscribe(const char* topic, uint8_t qos); protected: + espMqttClient *_mqttClient = nullptr; + espMqttClientSecure *_mqttClientSecure = nullptr; + + bool _useEncryption = false; + bool _mqttEnabled = true; + const String _hostname; const IPConfiguration* _ipConfiguration = nullptr; + + MqttClient *getMqttClient() const; }; \ No newline at end of file diff --git a/networkDevices/W5500Device.cpp b/networkDevices/W5500Device.cpp index 6a89a3ef..d7b45d31 100644 --- a/networkDevices/W5500Device.cpp +++ b/networkDevices/W5500Device.cpp @@ -26,6 +26,8 @@ W5500Device::W5500Device(const String &hostname, Preferences* preferences, const } } Log->println(); + + _mqttClient = new espMqttClientW5500(); } W5500Device::~W5500Device() @@ -61,7 +63,7 @@ void W5500Device::initialize() _path = new char[pathStr.length() + 1]; memset(_path, 0, sizeof(_path)); strcpy(_path, pathStr.c_str()); - Log = new MqttLogger(_mqttClient, _path, MqttLoggerMode::MqttAndSerial); + Log = new MqttLogger(*getMqttClient(), _path, MqttLoggerMode::MqttAndSerial); } reconnect(); @@ -161,13 +163,6 @@ void W5500Device::resetDevice() delay(50); } - -void W5500Device::printError() -{ - Log->print(F("Free Heap: ")); - Log->println(ESP.getFreeHeap()); -} - bool W5500Device::supportsEncryption() { return false; @@ -218,89 +213,10 @@ void W5500Device::initializeMacAddress(byte *mac) void W5500Device::update() { _maintainResult = Ethernet.maintain(); - if(_mqttEnabled) - { - _mqttClient.loop(); - } + NetworkDevice::update(); } int8_t W5500Device::signalStrength() { return 127; } - -void W5500Device::mqttSetClientId(const char *clientId) -{ - _mqttClient.setClientId(clientId); -} - -void W5500Device::mqttSetCleanSession(bool cleanSession) -{ - _mqttClient.setCleanSession(cleanSession); -} - -uint16_t W5500Device::mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload) -{ - return _mqttClient.publish(topic, qos, retain, payload); -} - -bool W5500Device::mqttConnected() const -{ - return _mqttClient.connected(); -} - -void W5500Device::mqttSetServer(const char *host, uint16_t port) -{ - _mqttClient.setServer(host, port); -} - -bool W5500Device::mqttConnect() -{ - return _mqttClient.connect(); -} - -bool W5500Device::mqttDisconnect(bool force) -{ - return _mqttClient.disconnect(force); -} - -void W5500Device::setWill(const char *topic, uint8_t qos, bool retain, const char *payload) -{ - _mqttClient.setWill(topic, qos, retain, payload); -} - -void W5500Device::mqttSetCredentials(const char *username, const char *password) -{ - _mqttClient.setCredentials(username, password); -} - -void W5500Device::mqttOnMessage(espMqttClientTypes::OnMessageCallback callback) -{ - _mqttClient.onMessage(callback); -} - -void W5500Device::mqttOnConnect(espMqttClientTypes::OnConnectCallback callback) -{ - _mqttClient.onConnect(callback); -} - -void W5500Device::mqttOnDisconnect(espMqttClientTypes::OnDisconnectCallback callback) -{ - _mqttClient.onDisconnect(callback); -} - -uint16_t W5500Device::mqttSubscribe(const char *topic, uint8_t qos) -{ - return _mqttClient.subscribe(topic, qos); -} - -uint16_t W5500Device::mqttPublish(const char *topic, uint8_t qos, bool retain, const uint8_t *payload, size_t length) -{ - return _mqttClient.publish(topic, qos, retain, payload, length); -} - -void W5500Device::disableMqtt() -{ - _mqttClient.disconnect(); - _mqttEnabled = false; -} diff --git a/networkDevices/W5500Device.h b/networkDevices/W5500Device.h index ca909eb9..916198cc 100644 --- a/networkDevices/W5500Device.h +++ b/networkDevices/W5500Device.h @@ -23,51 +23,19 @@ class W5500Device : public NetworkDevice virtual void initialize(); virtual ReconnectStatus reconnect(); virtual void reconfigure(); - virtual void printError(); bool supportsEncryption() override; - virtual void update(); + virtual void update() override; virtual bool isConnected(); int8_t signalStrength() override; - void mqttSetClientId(const char *clientId) override; - - void mqttSetCleanSession(bool cleanSession) override; - - uint16_t mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload) override; - - uint16_t mqttPublish(const char *topic, uint8_t qos, bool retain, const uint8_t *payload, size_t length) override; - - bool mqttConnected() const override; - - void mqttSetServer(const char *host, uint16_t port) override; - - bool mqttConnect() override; - - bool mqttDisconnect(bool force) override; - - void setWill(const char *topic, uint8_t qos, bool retain, const char *payload) override; - - void mqttSetCredentials(const char *username, const char *password) override; - - void mqttOnMessage(espMqttClientTypes::OnMessageCallback callback) override; - - void mqttOnConnect(espMqttClientTypes::OnConnectCallback callback) override; - - void mqttOnDisconnect(espMqttClientTypes::OnDisconnectCallback callback) override; - - uint16_t mqttSubscribe(const char *topic, uint8_t qos) override; - - void disableMqtt() override; - private: void resetDevice(); void initializeMacAddress(byte* mac); - espMqttClientW5500 _mqttClient; Preferences* _preferences = nullptr; int _maintainResult = 0; @@ -76,7 +44,6 @@ class W5500Device : public NetworkDevice char* _path; W5500Variant _variant; bool _lastConnected = false; - bool _mqttEnabled = true; byte _mac[6]; }; \ No newline at end of file diff --git a/networkDevices/WifiDevice.cpp b/networkDevices/WifiDevice.cpp index e57a4856..11139c46 100644 --- a/networkDevices/WifiDevice.cpp +++ b/networkDevices/WifiDevice.cpp @@ -125,18 +125,6 @@ void WifiDevice::reconfigure() restartEsp(RestartReason::ReconfigureWifi); } -void WifiDevice::printError() -{ -// if(_wifiClientSecure != nullptr) -// { -// char lastError[100]; -// _wifiClientSecure->lastError(lastError,100); -// Log->println(lastError); -// } - Log->print(F("Free Heap: ")); - Log->println(ESP.getFreeHeap()); -} - bool WifiDevice::supportsEncryption() { return true; @@ -153,20 +141,6 @@ ReconnectStatus WifiDevice::reconnect() return isConnected() ? ReconnectStatus::Success : ReconnectStatus::Failure; } -void WifiDevice::update() -{ - if(_mqttEnabled) - { - if (_useEncryption) - { - _mqttClientSecure->loop(); - } else - { - _mqttClient->loop(); - } - } -} - void WifiDevice::onDisconnected() { if(millis() > 60000) @@ -184,198 +158,3 @@ void WifiDevice::clearRtcInitVar(WiFiManager *) { memset(WiFiDevice_reconfdetect, 0, sizeof WiFiDevice_reconfdetect); } - -void WifiDevice::mqttSetClientId(const char *clientId) -{ - if(_useEncryption) - { - _mqttClientSecure->setClientId(clientId); - } - else - { - _mqttClient->setClientId(clientId); - } -} - -void WifiDevice::mqttSetCleanSession(bool cleanSession) -{ - if(_useEncryption) - { - _mqttClientSecure->setCleanSession(cleanSession); - } - else - { - _mqttClient->setCleanSession(cleanSession); - } -} - -uint16_t WifiDevice::mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload) -{ - if(_useEncryption) - { - return _mqttClientSecure->publish(topic, qos, retain, payload); - } - else - { - return _mqttClient->publish(topic, qos, retain, payload); - } -} - -uint16_t WifiDevice::mqttPublish(const char *topic, uint8_t qos, bool retain, const uint8_t *payload, size_t length) -{ - if(_useEncryption) - { - return _mqttClientSecure->publish(topic, qos, retain, payload, length); - } - else - { - return _mqttClient->publish(topic, qos, retain, payload, length); - } -} - -bool WifiDevice::mqttConnected() const -{ - if(_useEncryption) - { - return _mqttClientSecure->connected(); - } - else - { - return _mqttClient->connected(); - } -} - -void WifiDevice::mqttSetServer(const char *host, uint16_t port) -{ - if(_useEncryption) - { - _mqttClientSecure->setServer(host, port); - } - else - { - _mqttClient->setServer(host, port); - } -} - -bool WifiDevice::mqttConnect() -{ - if(_useEncryption) - { - return _mqttClientSecure->connect(); - } - else - { - return _mqttClient->connect(); - } -} - -bool WifiDevice::mqttDisconnect(bool force) -{ - if(_useEncryption) - { - return _mqttClientSecure->disconnect(force); - } - else - { - return _mqttClient->disconnect(force); - } -} - -void WifiDevice::setWill(const char *topic, uint8_t qos, bool retain, const char *payload) -{ - if(_useEncryption) - { - _mqttClientSecure->setWill(topic, qos, retain, payload); - } - else - { - _mqttClient->setWill(topic, qos, retain, payload); - } -} - -void WifiDevice::mqttSetCredentials(const char *username, const char *password) -{ - if(_useEncryption) - { - _mqttClientSecure->setCredentials(username, password); - } - else - { - _mqttClient->setCredentials(username, password); - } -} - -void WifiDevice::mqttOnMessage(espMqttClientTypes::OnMessageCallback callback) -{ - if(_useEncryption) - { - _mqttClientSecure->onMessage(callback); - } - else - { - _mqttClient->onMessage(callback); - } -} - - -void WifiDevice::mqttOnConnect(espMqttClientTypes::OnConnectCallback callback) -{ - if(_useEncryption) - { - _mqttClientSecure->onConnect(callback); - } - else - { - _mqttClient->onConnect(callback); - } -} - -void WifiDevice::mqttOnDisconnect(espMqttClientTypes::OnDisconnectCallback callback) -{ - if(_useEncryption) - { - _mqttClientSecure->onDisconnect(callback); - } - else - { - _mqttClient->onDisconnect(callback); - } -} - - -uint16_t WifiDevice::mqttSubscribe(const char *topic, uint8_t qos) -{ - if(_useEncryption) - { - return _mqttClientSecure->subscribe(topic, qos); - } - else - { - return _mqttClient->subscribe(topic, qos); - } -} - -void WifiDevice::disableMqtt() -{ - if (_useEncryption) - { - _mqttClientSecure->disconnect(); - } else - { - _mqttClient->disconnect(); - } - - _mqttEnabled = false; -} - -MqttClient *WifiDevice::getMqttClient() const -{ - if (_useEncryption) - { - return _mqttClientSecure; - } - else - { - return _mqttClient; - } -} diff --git a/networkDevices/WifiDevice.h b/networkDevices/WifiDevice.h index c5f15b45..952b45f1 100644 --- a/networkDevices/WifiDevice.h +++ b/networkDevices/WifiDevice.h @@ -18,61 +18,23 @@ class WifiDevice : public NetworkDevice virtual void initialize(); virtual void reconfigure(); virtual ReconnectStatus reconnect(); - virtual void printError(); bool supportsEncryption() override; - virtual void update(); - virtual bool isConnected(); int8_t signalStrength() override; - void mqttSetClientId(const char *clientId) override; - - void mqttSetCleanSession(bool cleanSession) override; - - uint16_t mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload) override; - - uint16_t mqttPublish(const char *topic, uint8_t qos, bool retain, const uint8_t *payload, size_t length) override; - - bool mqttConnected() const override; - - void mqttSetServer(const char *host, uint16_t port) override; - - bool mqttConnect() override; - - bool mqttDisconnect(bool force) override; - - void setWill(const char *topic, uint8_t qos, bool retain, const char *payload) override; - - void mqttSetCredentials(const char *username, const char *password) override; - - void mqttOnMessage(espMqttClientTypes::OnMessageCallback callback) override; - - void mqttOnConnect(espMqttClientTypes::OnConnectCallback callback) override; - - void mqttOnDisconnect(espMqttClientTypes::OnDisconnectCallback callback) override; - - uint16_t mqttSubscribe(const char *topic, uint8_t qos) override; - - void disableMqtt() override; - private: static void clearRtcInitVar(WiFiManager*); void onDisconnected(); - MqttClient *getMqttClient() const; WiFiManager _wm; - espMqttClient* _mqttClient = nullptr; - espMqttClientSecure* _mqttClientSecure = nullptr; Preferences* _preferences = nullptr; bool _restartOnDisconnect = false; bool _startAp = false; char* _path; - bool _useEncryption = false; - bool _mqttEnabled = true; char _ca[TLS_CA_MAX_SIZE] = {0}; char _cert[TLS_CERT_MAX_SIZE] = {0}; diff --git a/networkDevices/espMqttClientW5500.cpp b/networkDevices/espMqttClientW5500.cpp index f4ca5202..91bfd5aa 100644 --- a/networkDevices/espMqttClientW5500.cpp +++ b/networkDevices/espMqttClientW5500.cpp @@ -1,7 +1,7 @@ #include "espMqttClientW5500.h" espMqttClientW5500::espMqttClientW5500() -: MqttClientSetup(espMqttClientTypes::UseInternalTask::NO), +: espMqttClient(espMqttClientTypes::UseInternalTask::NO), _client() { _transport = &_client; diff --git a/networkDevices/espMqttClientW5500.h b/networkDevices/espMqttClientW5500.h index 55afc592..ba66dd95 100644 --- a/networkDevices/espMqttClientW5500.h +++ b/networkDevices/espMqttClientW5500.h @@ -1,9 +1,9 @@ #pragma once -#include "MqttClientSetup.h" +#include "espMqttClient.h" #include "ClientSyncW5500.h" -class espMqttClientW5500 : public MqttClientSetup { +class espMqttClientW5500 : public espMqttClient { public: #if defined(ARDUINO_ARCH_ESP32) explicit espMqttClientW5500(); From 645f7afa9d8cd8f79b6ea009a2deb1a4e3862346 Mon Sep 17 00:00:00 2001 From: Luca Oliano Date: Mon, 5 Feb 2024 16:15:48 +0100 Subject: [PATCH 3/3] fix build break --- .github/workflows/build.yml | 2 +- CMakeLists.txt | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 169e88e3..7ad00f9f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -19,7 +19,7 @@ jobs: cd ~/arduino* ./install.sh ./arduino --pref "boardsmanager.additional.urls=https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json" --save-prefs - ./arduino --install-boards esp32:esp32 + ./arduino --install-boards esp32:esp32:2.0.9 - name: Install Arduino CMake Toolchain uses: actions/checkout@v2 with: diff --git a/CMakeLists.txt b/CMakeLists.txt index 5622c28d..03c54caf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,7 @@ set(SRCFILES NetworkLock.cpp NetworkOpener.cpp networkDevices/NetworkDevice.h + networkDevices/NetworkDevice.cpp networkDevices/WifiDevice.cpp networkDevices/W5500Device.cpp networkDevices/EthLan8720Device.cpp