From 05f2c177129c3cbd05b435c95ed7d348a9cb1d4f Mon Sep 17 00:00:00 2001 From: Luca Oliano Date: Mon, 5 Feb 2024 10:48:18 +0100 Subject: [PATCH] refactor network devices hierarchy --- networkDevices/EthLan8720Device.cpp | 215 ------------------------- networkDevices/EthLan8720Device.h | 39 ----- networkDevices/NetworkDevice.cpp | 161 +++++++++++++++++++ networkDevices/NetworkDevice.h | 44 ++--- 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, 195 insertions(+), 656 deletions(-) create mode 100644 networkDevices/NetworkDevice.cpp diff --git a/networkDevices/EthLan8720Device.cpp b/networkDevices/EthLan8720Device.cpp index c7a6b46..d550fbb 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 93ca817..14fb742 100644 --- a/networkDevices/EthLan8720Device.h +++ b/networkDevices/EthLan8720Device.h @@ -28,56 +28,18 @@ public: 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 @@ private: 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 0000000..6ca5d30 --- /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 a850696..7c945ee 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 @@ public: 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 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) = 0; + 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 6a89a3e..d7b45d3 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 ca909eb..916198c 100644 --- a/networkDevices/W5500Device.h +++ b/networkDevices/W5500Device.h @@ -23,51 +23,19 @@ public: 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 @@ private: 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 e57a485..11139c4 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 c5f15b4..952b45f 100644 --- a/networkDevices/WifiDevice.h +++ b/networkDevices/WifiDevice.h @@ -18,61 +18,23 @@ public: 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 f4ca520..91bfd5a 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 55afc59..ba66dd9 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();