refactor network devices hierarchy
This commit is contained in:
@@ -96,12 +96,6 @@ void EthLan8720Device::reconfigure()
|
|||||||
restartEsp(RestartReason::ReconfigureLAN8720);
|
restartEsp(RestartReason::ReconfigureLAN8720);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EthLan8720Device::printError()
|
|
||||||
{
|
|
||||||
Log->print(F("Free Heap: "));
|
|
||||||
Log->println(ESP.getFreeHeap());
|
|
||||||
}
|
|
||||||
|
|
||||||
bool EthLan8720Device::supportsEncryption()
|
bool EthLan8720Device::supportsEncryption()
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
@@ -132,20 +126,6 @@ ReconnectStatus EthLan8720Device::reconnect()
|
|||||||
return isConnected() ? ReconnectStatus::Success : ReconnectStatus::Failure;
|
return isConnected() ? ReconnectStatus::Success : ReconnectStatus::Failure;
|
||||||
}
|
}
|
||||||
|
|
||||||
void EthLan8720Device::update()
|
|
||||||
{
|
|
||||||
if(_mqttEnabled)
|
|
||||||
{
|
|
||||||
if (_useEncryption)
|
|
||||||
{
|
|
||||||
_mqttClientSecure->loop();
|
|
||||||
} else
|
|
||||||
{
|
|
||||||
_mqttClient->loop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void EthLan8720Device::onDisconnected()
|
void EthLan8720Device::onDisconnected()
|
||||||
{
|
{
|
||||||
if(millis() > 60000)
|
if(millis() > 60000)
|
||||||
@@ -158,198 +138,3 @@ int8_t EthLan8720Device::signalStrength()
|
|||||||
{
|
{
|
||||||
return -1;
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -28,56 +28,18 @@ public:
|
|||||||
virtual void initialize();
|
virtual void initialize();
|
||||||
virtual void reconfigure();
|
virtual void reconfigure();
|
||||||
virtual ReconnectStatus reconnect();
|
virtual ReconnectStatus reconnect();
|
||||||
virtual void printError();
|
|
||||||
bool supportsEncryption() override;
|
bool supportsEncryption() override;
|
||||||
|
|
||||||
virtual void update();
|
|
||||||
|
|
||||||
virtual bool isConnected();
|
virtual bool isConnected();
|
||||||
|
|
||||||
int8_t signalStrength() override;
|
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:
|
private:
|
||||||
void onDisconnected();
|
void onDisconnected();
|
||||||
MqttClient *getMqttClient() const;
|
|
||||||
|
|
||||||
espMqttClient* _mqttClient = nullptr;
|
|
||||||
espMqttClientSecure* _mqttClientSecure = nullptr;
|
|
||||||
|
|
||||||
bool _restartOnDisconnect = false;
|
bool _restartOnDisconnect = false;
|
||||||
bool _startAp = false;
|
bool _startAp = false;
|
||||||
char* _path;
|
char* _path;
|
||||||
bool _useEncryption = false;
|
|
||||||
bool _hardwareInitialized = false;
|
bool _hardwareInitialized = false;
|
||||||
bool _lastConnected = false;
|
bool _lastConnected = false;
|
||||||
|
|
||||||
@@ -89,7 +51,6 @@ private:
|
|||||||
eth_phy_type_t _type;
|
eth_phy_type_t _type;
|
||||||
eth_clock_mode_t _clock_mode;
|
eth_clock_mode_t _clock_mode;
|
||||||
bool _use_mac_from_efuse;
|
bool _use_mac_from_efuse;
|
||||||
bool _mqttEnabled = true;
|
|
||||||
|
|
||||||
char _ca[TLS_CA_MAX_SIZE] = {0};
|
char _ca[TLS_CA_MAX_SIZE] = {0};
|
||||||
char _cert[TLS_CERT_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
|
#pragma once
|
||||||
|
|
||||||
#include "MqttClient.h"
|
#include "espMqttClient.h"
|
||||||
#include "MqttClientSetup.h"
|
#include "MqttClientSetup.h"
|
||||||
#include "IPConfiguration.h"
|
#include "IPConfiguration.h"
|
||||||
|
|
||||||
@@ -24,32 +24,40 @@ public:
|
|||||||
virtual void initialize() = 0;
|
virtual void initialize() = 0;
|
||||||
virtual ReconnectStatus reconnect() = 0;
|
virtual ReconnectStatus reconnect() = 0;
|
||||||
virtual void reconfigure() = 0;
|
virtual void reconfigure() = 0;
|
||||||
virtual void printError() = 0;
|
virtual void printError();
|
||||||
virtual bool supportsEncryption() = 0;
|
virtual bool supportsEncryption() = 0;
|
||||||
|
|
||||||
virtual void update() = 0;
|
virtual void update();
|
||||||
|
|
||||||
virtual bool isConnected() = 0;
|
virtual bool isConnected() = 0;
|
||||||
virtual int8_t signalStrength() = 0;
|
virtual int8_t signalStrength() = 0;
|
||||||
|
|
||||||
virtual void mqttSetClientId(const char* clientId) = 0;
|
virtual void mqttSetClientId(const char* clientId);
|
||||||
virtual void mqttSetCleanSession(bool cleanSession) = 0;
|
virtual void mqttSetCleanSession(bool cleanSession);
|
||||||
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 char* payload);
|
||||||
virtual uint16_t mqttPublish(const char* topic, uint8_t qos, bool retain, const uint8_t* payload, size_t length) = 0;
|
virtual uint16_t mqttPublish(const char* topic, uint8_t qos, bool retain, const uint8_t* payload, size_t length);
|
||||||
virtual bool mqttConnected() const = 0;
|
virtual bool mqttConnected() const;
|
||||||
virtual void mqttSetServer(const char* host, uint16_t port) = 0;
|
virtual void mqttSetServer(const char* host, uint16_t port);
|
||||||
virtual bool mqttConnect() = 0;
|
virtual bool mqttConnect();
|
||||||
virtual bool mqttDisconnect(bool force) = 0;
|
virtual bool mqttDisconnect(bool force);
|
||||||
virtual void setWill(const char* topic, uint8_t qos, bool retain, const char* payload) = 0;
|
virtual void setWill(const char* topic, uint8_t qos, bool retain, const char* payload);
|
||||||
virtual void mqttSetCredentials(const char* username, const char* password) = 0;
|
virtual void mqttSetCredentials(const char* username, const char* password);
|
||||||
virtual void mqttOnMessage(espMqttClientTypes::OnMessageCallback callback) = 0;
|
virtual void mqttOnMessage(espMqttClientTypes::OnMessageCallback callback);
|
||||||
virtual void mqttOnConnect(espMqttClientTypes::OnConnectCallback callback) = 0;
|
virtual void mqttOnConnect(espMqttClientTypes::OnConnectCallback callback);
|
||||||
virtual void mqttOnDisconnect(espMqttClientTypes::OnDisconnectCallback callback) = 0;
|
virtual void mqttOnDisconnect(espMqttClientTypes::OnDisconnectCallback callback);
|
||||||
virtual void disableMqtt() = 0;
|
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:
|
protected:
|
||||||
|
espMqttClient *_mqttClient = nullptr;
|
||||||
|
espMqttClientSecure *_mqttClientSecure = nullptr;
|
||||||
|
|
||||||
|
bool _useEncryption = false;
|
||||||
|
bool _mqttEnabled = true;
|
||||||
|
|
||||||
const String _hostname;
|
const String _hostname;
|
||||||
const IPConfiguration* _ipConfiguration = nullptr;
|
const IPConfiguration* _ipConfiguration = nullptr;
|
||||||
|
|
||||||
|
MqttClient *getMqttClient() const;
|
||||||
};
|
};
|
||||||
@@ -26,6 +26,8 @@ W5500Device::W5500Device(const String &hostname, Preferences* preferences, const
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Log->println();
|
Log->println();
|
||||||
|
|
||||||
|
_mqttClient = new espMqttClientW5500();
|
||||||
}
|
}
|
||||||
|
|
||||||
W5500Device::~W5500Device()
|
W5500Device::~W5500Device()
|
||||||
@@ -61,7 +63,7 @@ void W5500Device::initialize()
|
|||||||
_path = new char[pathStr.length() + 1];
|
_path = new char[pathStr.length() + 1];
|
||||||
memset(_path, 0, sizeof(_path));
|
memset(_path, 0, sizeof(_path));
|
||||||
strcpy(_path, pathStr.c_str());
|
strcpy(_path, pathStr.c_str());
|
||||||
Log = new MqttLogger(_mqttClient, _path, MqttLoggerMode::MqttAndSerial);
|
Log = new MqttLogger(*getMqttClient(), _path, MqttLoggerMode::MqttAndSerial);
|
||||||
}
|
}
|
||||||
|
|
||||||
reconnect();
|
reconnect();
|
||||||
@@ -161,13 +163,6 @@ void W5500Device::resetDevice()
|
|||||||
delay(50);
|
delay(50);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void W5500Device::printError()
|
|
||||||
{
|
|
||||||
Log->print(F("Free Heap: "));
|
|
||||||
Log->println(ESP.getFreeHeap());
|
|
||||||
}
|
|
||||||
|
|
||||||
bool W5500Device::supportsEncryption()
|
bool W5500Device::supportsEncryption()
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@@ -218,89 +213,10 @@ void W5500Device::initializeMacAddress(byte *mac)
|
|||||||
void W5500Device::update()
|
void W5500Device::update()
|
||||||
{
|
{
|
||||||
_maintainResult = Ethernet.maintain();
|
_maintainResult = Ethernet.maintain();
|
||||||
if(_mqttEnabled)
|
NetworkDevice::update();
|
||||||
{
|
|
||||||
_mqttClient.loop();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int8_t W5500Device::signalStrength()
|
int8_t W5500Device::signalStrength()
|
||||||
{
|
{
|
||||||
return 127;
|
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;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -23,51 +23,19 @@ public:
|
|||||||
virtual void initialize();
|
virtual void initialize();
|
||||||
virtual ReconnectStatus reconnect();
|
virtual ReconnectStatus reconnect();
|
||||||
virtual void reconfigure();
|
virtual void reconfigure();
|
||||||
virtual void printError();
|
|
||||||
|
|
||||||
bool supportsEncryption() override;
|
bool supportsEncryption() override;
|
||||||
|
|
||||||
virtual void update();
|
virtual void update() override;
|
||||||
|
|
||||||
virtual bool isConnected();
|
virtual bool isConnected();
|
||||||
|
|
||||||
int8_t signalStrength() override;
|
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:
|
private:
|
||||||
void resetDevice();
|
void resetDevice();
|
||||||
void initializeMacAddress(byte* mac);
|
void initializeMacAddress(byte* mac);
|
||||||
|
|
||||||
espMqttClientW5500 _mqttClient;
|
|
||||||
Preferences* _preferences = nullptr;
|
Preferences* _preferences = nullptr;
|
||||||
|
|
||||||
int _maintainResult = 0;
|
int _maintainResult = 0;
|
||||||
@@ -76,7 +44,6 @@ private:
|
|||||||
char* _path;
|
char* _path;
|
||||||
W5500Variant _variant;
|
W5500Variant _variant;
|
||||||
bool _lastConnected = false;
|
bool _lastConnected = false;
|
||||||
bool _mqttEnabled = true;
|
|
||||||
|
|
||||||
byte _mac[6];
|
byte _mac[6];
|
||||||
};
|
};
|
||||||
@@ -125,18 +125,6 @@ void WifiDevice::reconfigure()
|
|||||||
restartEsp(RestartReason::ReconfigureWifi);
|
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()
|
bool WifiDevice::supportsEncryption()
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
@@ -153,20 +141,6 @@ ReconnectStatus WifiDevice::reconnect()
|
|||||||
return isConnected() ? ReconnectStatus::Success : ReconnectStatus::Failure;
|
return isConnected() ? ReconnectStatus::Success : ReconnectStatus::Failure;
|
||||||
}
|
}
|
||||||
|
|
||||||
void WifiDevice::update()
|
|
||||||
{
|
|
||||||
if(_mqttEnabled)
|
|
||||||
{
|
|
||||||
if (_useEncryption)
|
|
||||||
{
|
|
||||||
_mqttClientSecure->loop();
|
|
||||||
} else
|
|
||||||
{
|
|
||||||
_mqttClient->loop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void WifiDevice::onDisconnected()
|
void WifiDevice::onDisconnected()
|
||||||
{
|
{
|
||||||
if(millis() > 60000)
|
if(millis() > 60000)
|
||||||
@@ -184,198 +158,3 @@ void WifiDevice::clearRtcInitVar(WiFiManager *)
|
|||||||
{
|
{
|
||||||
memset(WiFiDevice_reconfdetect, 0, sizeof WiFiDevice_reconfdetect);
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -18,61 +18,23 @@ public:
|
|||||||
virtual void initialize();
|
virtual void initialize();
|
||||||
virtual void reconfigure();
|
virtual void reconfigure();
|
||||||
virtual ReconnectStatus reconnect();
|
virtual ReconnectStatus reconnect();
|
||||||
virtual void printError();
|
|
||||||
bool supportsEncryption() override;
|
bool supportsEncryption() override;
|
||||||
|
|
||||||
virtual void update();
|
|
||||||
|
|
||||||
virtual bool isConnected();
|
virtual bool isConnected();
|
||||||
|
|
||||||
int8_t signalStrength() override;
|
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:
|
private:
|
||||||
static void clearRtcInitVar(WiFiManager*);
|
static void clearRtcInitVar(WiFiManager*);
|
||||||
|
|
||||||
void onDisconnected();
|
void onDisconnected();
|
||||||
MqttClient *getMqttClient() const;
|
|
||||||
|
|
||||||
WiFiManager _wm;
|
WiFiManager _wm;
|
||||||
espMqttClient* _mqttClient = nullptr;
|
|
||||||
espMqttClientSecure* _mqttClientSecure = nullptr;
|
|
||||||
Preferences* _preferences = nullptr;
|
Preferences* _preferences = nullptr;
|
||||||
|
|
||||||
bool _restartOnDisconnect = false;
|
bool _restartOnDisconnect = false;
|
||||||
bool _startAp = false;
|
bool _startAp = false;
|
||||||
char* _path;
|
char* _path;
|
||||||
bool _useEncryption = false;
|
|
||||||
bool _mqttEnabled = true;
|
|
||||||
|
|
||||||
char _ca[TLS_CA_MAX_SIZE] = {0};
|
char _ca[TLS_CA_MAX_SIZE] = {0};
|
||||||
char _cert[TLS_CERT_MAX_SIZE] = {0};
|
char _cert[TLS_CERT_MAX_SIZE] = {0};
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#include "espMqttClientW5500.h"
|
#include "espMqttClientW5500.h"
|
||||||
|
|
||||||
espMqttClientW5500::espMqttClientW5500()
|
espMqttClientW5500::espMqttClientW5500()
|
||||||
: MqttClientSetup(espMqttClientTypes::UseInternalTask::NO),
|
: espMqttClient(espMqttClientTypes::UseInternalTask::NO),
|
||||||
_client()
|
_client()
|
||||||
{
|
{
|
||||||
_transport = &_client;
|
_transport = &_client;
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "MqttClientSetup.h"
|
#include "espMqttClient.h"
|
||||||
#include "ClientSyncW5500.h"
|
#include "ClientSyncW5500.h"
|
||||||
|
|
||||||
class espMqttClientW5500 : public MqttClientSetup<espMqttClientW5500> {
|
class espMqttClientW5500 : public espMqttClient {
|
||||||
public:
|
public:
|
||||||
#if defined(ARDUINO_ARCH_ESP32)
|
#if defined(ARDUINO_ARCH_ESP32)
|
||||||
explicit espMqttClientW5500();
|
explicit espMqttClientW5500();
|
||||||
|
|||||||
Reference in New Issue
Block a user