Merge pull request #289 from lucaoliano/refactor_network_devices
Refactor network devices
This commit is contained in:
2
.github/workflows/build.yml
vendored
2
.github/workflows/build.yml
vendored
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -8,4 +8,4 @@ category=Communication
|
||||
url=https://github.com/androbi-com/MqttLogger
|
||||
architectures=*
|
||||
includes=MqttLogger.h
|
||||
depends=PubSubClient
|
||||
depends=espMqttClient
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <Print.h>
|
||||
#include "../../../networkDevices/NetworkDevice.h"
|
||||
#include <espMqttClient.h>
|
||||
|
||||
#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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
@@ -163,187 +143,3 @@ String EthLan8720Device::localIP()
|
||||
{
|
||||
return ETH.localIP().toString();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,57 +28,20 @@ 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;
|
||||
|
||||
String localIP() 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();
|
||||
|
||||
espMqttClient* _mqttClient = nullptr;
|
||||
espMqttClientSecure* _mqttClientSecure = nullptr;
|
||||
|
||||
bool _restartOnDisconnect = false;
|
||||
bool _startAp = false;
|
||||
char* _path;
|
||||
bool _useEncryption = false;
|
||||
bool _hardwareInitialized = false;
|
||||
bool _lastConnected = false;
|
||||
|
||||
@@ -90,7 +53,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};
|
||||
|
||||
161
networkDevices/NetworkDevice.cpp
Normal file
161
networkDevices/NetworkDevice.cpp
Normal file
@@ -0,0 +1,161 @@
|
||||
#include <Arduino.h>
|
||||
#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;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "MqttClient.h"
|
||||
#include "espMqttClient.h"
|
||||
#include "MqttClientSetup.h"
|
||||
#include "IPConfiguration.h"
|
||||
|
||||
@@ -24,34 +24,42 @@ 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 String localIP() = 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;
|
||||
};
|
||||
@@ -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(this, _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,10 +213,7 @@ void W5500Device::initializeMacAddress(byte *mac)
|
||||
void W5500Device::update()
|
||||
{
|
||||
_maintainResult = Ethernet.maintain();
|
||||
if(_mqttEnabled)
|
||||
{
|
||||
_mqttClient.loop();
|
||||
}
|
||||
NetworkDevice::update();
|
||||
}
|
||||
|
||||
int8_t W5500Device::signalStrength()
|
||||
@@ -233,79 +225,3 @@ String W5500Device::localIP()
|
||||
{
|
||||
return Ethernet.localIP().toString();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -23,11 +23,10 @@ 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();
|
||||
|
||||
@@ -35,41 +34,10 @@ public:
|
||||
|
||||
String localIP() 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;
|
||||
@@ -78,7 +46,6 @@ private:
|
||||
char* _path;
|
||||
W5500Variant _variant;
|
||||
bool _lastConnected = false;
|
||||
bool _mqttEnabled = true;
|
||||
|
||||
byte _mac[6];
|
||||
};
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
@@ -189,186 +163,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;
|
||||
}
|
||||
|
||||
@@ -18,62 +18,25 @@ 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;
|
||||
|
||||
String localIP() 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();
|
||||
|
||||
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};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "espMqttClientW5500.h"
|
||||
|
||||
espMqttClientW5500::espMqttClientW5500()
|
||||
: MqttClientSetup(espMqttClientTypes::UseInternalTask::NO),
|
||||
: espMqttClient(espMqttClientTypes::UseInternalTask::NO),
|
||||
_client()
|
||||
{
|
||||
_transport = &_client;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "MqttClientSetup.h"
|
||||
#include "espMqttClient.h"
|
||||
#include "ClientSyncW5500.h"
|
||||
|
||||
class espMqttClientW5500 : public MqttClientSetup<espMqttClientW5500> {
|
||||
class espMqttClientW5500 : public espMqttClient {
|
||||
public:
|
||||
#if defined(ARDUINO_ARCH_ESP32)
|
||||
explicit espMqttClientW5500();
|
||||
|
||||
Reference in New Issue
Block a user