MQTT Device refractor
This commit is contained in:
@@ -1,17 +1,13 @@
|
|||||||
#include "EthernetDevice.h"
|
#include "EthernetDevice.h"
|
||||||
#include "../PreferencesKeys.h"
|
#include "../PreferencesKeys.h"
|
||||||
#include "../Logger.h"
|
#include "../Logger.h"
|
||||||
#ifndef NUKI_HUB_UPDATER
|
|
||||||
#include "../MqttTopics.h"
|
|
||||||
#include "espMqttClient.h"
|
|
||||||
#endif
|
|
||||||
#include "../RestartReason.h"
|
#include "../RestartReason.h"
|
||||||
|
|
||||||
extern bool ethCriticalFailure;
|
extern bool ethCriticalFailure;
|
||||||
extern bool wifiFallback;
|
extern bool wifiFallback;
|
||||||
|
|
||||||
EthernetDevice::EthernetDevice(const String& hostname, Preferences* preferences, const IPConfiguration* ipConfiguration, const std::string& deviceName, uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_type_t ethtype, eth_clock_mode_t clock_mode)
|
EthernetDevice::EthernetDevice(const String& hostname, Preferences* preferences, const IPConfiguration* ipConfiguration, const std::string& deviceName, uint8_t phy_addr, int power, int mdc, int mdio, eth_phy_type_t ethtype, eth_clock_mode_t clock_mode)
|
||||||
: NetworkDevice(hostname, ipConfiguration),
|
: NetworkDevice(hostname, preferences, ipConfiguration),
|
||||||
_deviceName(deviceName),
|
_deviceName(deviceName),
|
||||||
_phy_addr(phy_addr),
|
_phy_addr(phy_addr),
|
||||||
_power(power),
|
_power(power),
|
||||||
@@ -22,7 +18,9 @@ EthernetDevice::EthernetDevice(const String& hostname, Preferences* preferences,
|
|||||||
_useSpi(false),
|
_useSpi(false),
|
||||||
_preferences(preferences)
|
_preferences(preferences)
|
||||||
{
|
{
|
||||||
init();
|
#ifndef NUKI_HUB_UPDATER
|
||||||
|
NetworkDevice::init();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
EthernetDevice::EthernetDevice(const String &hostname,
|
EthernetDevice::EthernetDevice(const String &hostname,
|
||||||
@@ -37,7 +35,7 @@ EthernetDevice::EthernetDevice(const String &hostname,
|
|||||||
int spi_miso,
|
int spi_miso,
|
||||||
int spi_mosi,
|
int spi_mosi,
|
||||||
eth_phy_type_t ethtype)
|
eth_phy_type_t ethtype)
|
||||||
: NetworkDevice(hostname, ipConfiguration),
|
: NetworkDevice(hostname, preferences, ipConfiguration),
|
||||||
_deviceName(deviceName),
|
_deviceName(deviceName),
|
||||||
_phy_addr(phy_addr),
|
_phy_addr(phy_addr),
|
||||||
_cs(cs),
|
_cs(cs),
|
||||||
@@ -50,52 +48,7 @@ EthernetDevice::EthernetDevice(const String &hostname,
|
|||||||
_useSpi(true),
|
_useSpi(true),
|
||||||
_preferences(preferences)
|
_preferences(preferences)
|
||||||
{
|
{
|
||||||
init();
|
NetworkDevice::init();
|
||||||
}
|
|
||||||
|
|
||||||
void EthernetDevice::init()
|
|
||||||
{
|
|
||||||
#ifndef NUKI_HUB_UPDATER
|
|
||||||
size_t caLength = _preferences->getString(preference_mqtt_ca, _ca, TLS_CA_MAX_SIZE);
|
|
||||||
size_t crtLength = _preferences->getString(preference_mqtt_crt, _cert, TLS_CERT_MAX_SIZE);
|
|
||||||
size_t keyLength = _preferences->getString(preference_mqtt_key, _key, TLS_KEY_MAX_SIZE);
|
|
||||||
|
|
||||||
_useEncryption = caLength > 1; // length is 1 when empty
|
|
||||||
|
|
||||||
if(_useEncryption)
|
|
||||||
{
|
|
||||||
Log->println(F("MQTT over TLS."));
|
|
||||||
_mqttClientSecure = new espMqttClientSecure(espMqttClientTypes::UseInternalTask::NO);
|
|
||||||
_mqttClientSecure->setCACert(_ca);
|
|
||||||
if(crtLength > 1 && keyLength > 1) // length is 1 when empty
|
|
||||||
{
|
|
||||||
Log->println(F("MQTT with client certificate."));
|
|
||||||
_mqttClientSecure->setCertificate(_cert);
|
|
||||||
_mqttClientSecure->setPrivateKey(_key);
|
|
||||||
}
|
|
||||||
} else
|
|
||||||
{
|
|
||||||
Log->println(F("MQTT without TLS."));
|
|
||||||
_mqttClient = new espMqttClient(espMqttClientTypes::UseInternalTask::NO);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(_preferences->getBool(preference_mqtt_log_enabled, false) || _preferences->getBool(preference_webserial_enabled, false))
|
|
||||||
{
|
|
||||||
MqttLoggerMode mode;
|
|
||||||
|
|
||||||
if(_preferences->getBool(preference_mqtt_log_enabled, false) && _preferences->getBool(preference_webserial_enabled, false)) mode = MqttLoggerMode::MqttAndSerialAndWeb;
|
|
||||||
else if (_preferences->getBool(preference_webserial_enabled, false)) mode = MqttLoggerMode::SerialAndWeb;
|
|
||||||
else mode = MqttLoggerMode::MqttAndSerial;
|
|
||||||
|
|
||||||
_path = new char[200];
|
|
||||||
memset(_path, 0, sizeof(_path));
|
|
||||||
|
|
||||||
String pathStr = _preferences->getString(preference_mqtt_lock_path);
|
|
||||||
pathStr.concat(mqtt_topic_log);
|
|
||||||
strcpy(_path, pathStr.c_str());
|
|
||||||
Log = new MqttLogger(*getMqttClient(), _path, mode);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const String EthernetDevice::deviceName() const
|
const String EthernetDevice::deviceName() const
|
||||||
|
|||||||
@@ -61,7 +61,6 @@ public:
|
|||||||
private:
|
private:
|
||||||
Preferences* _preferences;
|
Preferences* _preferences;
|
||||||
|
|
||||||
void init();
|
|
||||||
void onDisconnected();
|
void onDisconnected();
|
||||||
void onNetworkEvent(arduino_event_id_t event, arduino_event_info_t info);
|
void onNetworkEvent(arduino_event_id_t event, arduino_event_info_t info);
|
||||||
|
|
||||||
@@ -90,10 +89,4 @@ private:
|
|||||||
eth_phy_type_t _type;
|
eth_phy_type_t _type;
|
||||||
eth_clock_mode_t _clock_mode;
|
eth_clock_mode_t _clock_mode;
|
||||||
bool _useSpi = false;
|
bool _useSpi = false;
|
||||||
|
|
||||||
#ifndef NUKI_HUB_UPDATER
|
|
||||||
char _ca[TLS_CA_MAX_SIZE] = {0};
|
|
||||||
char _cert[TLS_CERT_MAX_SIZE] = {0};
|
|
||||||
char _key[TLS_KEY_MAX_SIZE] = {0};
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
@@ -2,13 +2,52 @@
|
|||||||
#include "NetworkDevice.h"
|
#include "NetworkDevice.h"
|
||||||
#include "../Logger.h"
|
#include "../Logger.h"
|
||||||
|
|
||||||
void NetworkDevice::printError()
|
|
||||||
{
|
|
||||||
Log->print(F("Free Heap: "));
|
|
||||||
Log->println(ESP.getFreeHeap());
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifndef NUKI_HUB_UPDATER
|
#ifndef NUKI_HUB_UPDATER
|
||||||
|
#include "../MqttTopics.h"
|
||||||
|
#include "espMqttClient.h"
|
||||||
|
|
||||||
|
void NetworkDevice::init()
|
||||||
|
{
|
||||||
|
size_t caLength = _preferences->getString(preference_mqtt_ca, _ca, TLS_CA_MAX_SIZE);
|
||||||
|
size_t crtLength = _preferences->getString(preference_mqtt_crt, _cert, TLS_CERT_MAX_SIZE);
|
||||||
|
size_t keyLength = _preferences->getString(preference_mqtt_key, _key, TLS_KEY_MAX_SIZE);
|
||||||
|
|
||||||
|
_useEncryption = caLength > 1; // length is 1 when empty
|
||||||
|
|
||||||
|
if(_useEncryption)
|
||||||
|
{
|
||||||
|
Log->println(F("MQTT over TLS."));
|
||||||
|
_mqttClientSecure = new espMqttClientSecure(espMqttClientTypes::UseInternalTask::NO);
|
||||||
|
_mqttClientSecure->setCACert(_ca);
|
||||||
|
if(crtLength > 1 && keyLength > 1) // length is 1 when empty
|
||||||
|
{
|
||||||
|
Log->println(F("MQTT with client certificate."));
|
||||||
|
_mqttClientSecure->setCertificate(_cert);
|
||||||
|
_mqttClientSecure->setPrivateKey(_key);
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
Log->println(F("MQTT without TLS."));
|
||||||
|
_mqttClient = new espMqttClient(espMqttClientTypes::UseInternalTask::NO);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(_preferences->getBool(preference_mqtt_log_enabled, false) || _preferences->getBool(preference_webserial_enabled, false))
|
||||||
|
{
|
||||||
|
MqttLoggerMode mode;
|
||||||
|
|
||||||
|
if(_preferences->getBool(preference_mqtt_log_enabled, false) && _preferences->getBool(preference_webserial_enabled, false)) mode = MqttLoggerMode::MqttAndSerialAndWeb;
|
||||||
|
else if (_preferences->getBool(preference_webserial_enabled, false)) mode = MqttLoggerMode::SerialAndWeb;
|
||||||
|
else mode = MqttLoggerMode::MqttAndSerial;
|
||||||
|
|
||||||
|
_path = new char[200];
|
||||||
|
memset(_path, 0, sizeof(_path));
|
||||||
|
|
||||||
|
String pathStr = _preferences->getString(preference_mqtt_lock_path);
|
||||||
|
pathStr.concat(mqtt_topic_log);
|
||||||
|
strcpy(_path, pathStr.c_str());
|
||||||
|
Log = new MqttLogger(*getMqttClient(), _path, mode);
|
||||||
|
}
|
||||||
|
}
|
||||||
void NetworkDevice::update()
|
void NetworkDevice::update()
|
||||||
{
|
{
|
||||||
if (_mqttEnabled)
|
if (_mqttEnabled)
|
||||||
@@ -19,38 +58,17 @@ void NetworkDevice::update()
|
|||||||
|
|
||||||
void NetworkDevice::mqttSetClientId(const char *clientId)
|
void NetworkDevice::mqttSetClientId(const char *clientId)
|
||||||
{
|
{
|
||||||
if (_useEncryption)
|
getMqttClient()->setClientId(clientId);
|
||||||
{
|
|
||||||
_mqttClientSecure->setClientId(clientId);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_mqttClient->setClientId(clientId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkDevice::mqttSetCleanSession(bool cleanSession)
|
void NetworkDevice::mqttSetCleanSession(bool cleanSession)
|
||||||
{
|
{
|
||||||
if (_useEncryption)
|
getMqttClient()->setCleanSession(cleanSession);
|
||||||
{
|
|
||||||
_mqttClientSecure->setCleanSession(cleanSession);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_mqttClient->setCleanSession(cleanSession);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkDevice::mqttSetKeepAlive(uint16_t keepAlive)
|
void NetworkDevice::mqttSetKeepAlive(uint16_t keepAlive)
|
||||||
{
|
{
|
||||||
if (_useEncryption)
|
getMqttClient()->setKeepAlive(keepAlive);
|
||||||
{
|
|
||||||
_mqttClientSecure->setKeepAlive(keepAlive);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_mqttClient->setKeepAlive(keepAlive);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t NetworkDevice::mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload)
|
uint16_t NetworkDevice::mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload)
|
||||||
@@ -70,14 +88,7 @@ bool NetworkDevice::mqttConnected() const
|
|||||||
|
|
||||||
void NetworkDevice::mqttSetServer(const char *host, uint16_t port)
|
void NetworkDevice::mqttSetServer(const char *host, uint16_t port)
|
||||||
{
|
{
|
||||||
if (_useEncryption)
|
getMqttClient()->setServer(host, port);
|
||||||
{
|
|
||||||
_mqttClientSecure->setServer(host, port);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_mqttClient->setServer(host, port);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool NetworkDevice::mqttConnect()
|
bool NetworkDevice::mqttConnect()
|
||||||
@@ -92,62 +103,27 @@ bool NetworkDevice::mqttDisconnect(bool force)
|
|||||||
|
|
||||||
void NetworkDevice::setWill(const char *topic, uint8_t qos, bool retain, const char *payload)
|
void NetworkDevice::setWill(const char *topic, uint8_t qos, bool retain, const char *payload)
|
||||||
{
|
{
|
||||||
if (_useEncryption)
|
getMqttClient()->setWill(topic, qos, retain, payload);
|
||||||
{
|
|
||||||
_mqttClientSecure->setWill(topic, qos, retain, payload);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_mqttClient->setWill(topic, qos, retain, payload);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkDevice::mqttSetCredentials(const char *username, const char *password)
|
void NetworkDevice::mqttSetCredentials(const char *username, const char *password)
|
||||||
{
|
{
|
||||||
if (_useEncryption)
|
getMqttClient()->setCredentials(username, password);
|
||||||
{
|
|
||||||
_mqttClientSecure->setCredentials(username, password);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_mqttClient->setCredentials(username, password);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkDevice::mqttOnMessage(espMqttClientTypes::OnMessageCallback callback)
|
void NetworkDevice::mqttOnMessage(espMqttClientTypes::OnMessageCallback callback)
|
||||||
{
|
{
|
||||||
if (_useEncryption)
|
getMqttClient()->onMessage(callback);
|
||||||
{
|
|
||||||
_mqttClientSecure->onMessage(callback);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_mqttClient->onMessage(callback);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkDevice::mqttOnConnect(espMqttClientTypes::OnConnectCallback callback)
|
void NetworkDevice::mqttOnConnect(espMqttClientTypes::OnConnectCallback callback)
|
||||||
{
|
{
|
||||||
if(_useEncryption)
|
getMqttClient()->onConnect(callback);
|
||||||
{
|
|
||||||
_mqttClientSecure->onConnect(callback);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_mqttClient->onConnect(callback);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkDevice::mqttOnDisconnect(espMqttClientTypes::OnDisconnectCallback callback)
|
void NetworkDevice::mqttOnDisconnect(espMqttClientTypes::OnDisconnectCallback callback)
|
||||||
{
|
{
|
||||||
if (_useEncryption)
|
getMqttClient()->onDisconnect(callback);
|
||||||
{
|
|
||||||
_mqttClientSecure->onDisconnect(callback);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_mqttClient->onDisconnect(callback);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t NetworkDevice::mqttSubscribe(const char *topic, uint8_t qos)
|
uint16_t NetworkDevice::mqttSubscribe(const char *topic, uint8_t qos)
|
||||||
|
|||||||
@@ -10,8 +10,9 @@
|
|||||||
class NetworkDevice
|
class NetworkDevice
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit NetworkDevice(const String& hostname, const IPConfiguration* ipConfiguration)
|
explicit NetworkDevice(const String& hostname, Preferences* preferences, const IPConfiguration* ipConfiguration)
|
||||||
: _hostname(hostname),
|
: _hostname(hostname),
|
||||||
|
_preferences(preferences),
|
||||||
_ipConfiguration(ipConfiguration)
|
_ipConfiguration(ipConfiguration)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
@@ -19,7 +20,6 @@ public:
|
|||||||
|
|
||||||
virtual void initialize() = 0;
|
virtual void initialize() = 0;
|
||||||
virtual void reconfigure() = 0;
|
virtual void reconfigure() = 0;
|
||||||
virtual void printError();
|
|
||||||
|
|
||||||
virtual void update();
|
virtual void update();
|
||||||
virtual void scan(bool passive = false, bool async = true) = 0;
|
virtual void scan(bool passive = false, bool async = true) = 0;
|
||||||
@@ -57,8 +57,14 @@ protected:
|
|||||||
|
|
||||||
bool _useEncryption = false;
|
bool _useEncryption = false;
|
||||||
bool _mqttEnabled = true;
|
bool _mqttEnabled = true;
|
||||||
|
|
||||||
|
void init();
|
||||||
|
|
||||||
MqttClient *getMqttClient() const;
|
MqttClient *getMqttClient() const;
|
||||||
|
|
||||||
|
char _ca[TLS_CA_MAX_SIZE] = {0};
|
||||||
|
char _cert[TLS_CERT_MAX_SIZE] = {0};
|
||||||
|
char _key[TLS_KEY_MAX_SIZE] = {0};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
const String _hostname;
|
const String _hostname;
|
||||||
|
|||||||
@@ -3,59 +3,17 @@
|
|||||||
#include "WifiDevice.h"
|
#include "WifiDevice.h"
|
||||||
#include "../PreferencesKeys.h"
|
#include "../PreferencesKeys.h"
|
||||||
#include "../Logger.h"
|
#include "../Logger.h"
|
||||||
#ifndef NUKI_HUB_UPDATER
|
|
||||||
#include "../MqttTopics.h"
|
|
||||||
#include "espMqttClient.h"
|
|
||||||
#endif
|
|
||||||
#include "../RestartReason.h"
|
#include "../RestartReason.h"
|
||||||
|
|
||||||
WifiDevice::WifiDevice(const String& hostname, Preferences* preferences, const IPConfiguration* ipConfiguration)
|
WifiDevice::WifiDevice(const String& hostname, Preferences* preferences, const IPConfiguration* ipConfiguration)
|
||||||
: NetworkDevice(hostname, ipConfiguration),
|
: NetworkDevice(hostname, preferences, ipConfiguration),
|
||||||
_preferences(preferences)
|
_preferences(preferences)
|
||||||
{
|
{
|
||||||
#ifndef NUKI_HUB_UPDATER
|
#ifndef NUKI_HUB_UPDATER
|
||||||
size_t caLength = preferences->getString(preference_mqtt_ca, _ca, TLS_CA_MAX_SIZE);
|
NetworkDevice::init();
|
||||||
size_t crtLength = preferences->getString(preference_mqtt_crt, _cert, TLS_CERT_MAX_SIZE);
|
#endif
|
||||||
size_t keyLength = preferences->getString(preference_mqtt_key, _key, TLS_KEY_MAX_SIZE);
|
|
||||||
|
|
||||||
_useEncryption = caLength > 1; // length is 1 when empty
|
|
||||||
|
|
||||||
if(_useEncryption)
|
|
||||||
{
|
|
||||||
Log->println(F("MQTT over TLS."));
|
|
||||||
_mqttClientSecure = new espMqttClientSecure(espMqttClientTypes::UseInternalTask::NO);
|
|
||||||
_mqttClientSecure->setCACert(_ca);
|
|
||||||
if(crtLength > 1 && keyLength > 1) // length is 1 when empty
|
|
||||||
{
|
|
||||||
Log->println(F("MQTT with client certificate."));
|
|
||||||
_mqttClientSecure->setCertificate(_cert);
|
|
||||||
_mqttClientSecure->setPrivateKey(_key);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Log->println(F("MQTT without TLS."));
|
|
||||||
_mqttClient = new espMqttClient(espMqttClientTypes::UseInternalTask::NO);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(preferences->getBool(preference_mqtt_log_enabled, false) || preferences->getBool(preference_webserial_enabled, false))
|
|
||||||
{
|
|
||||||
MqttLoggerMode mode;
|
|
||||||
|
|
||||||
if(preferences->getBool(preference_mqtt_log_enabled, false) && preferences->getBool(preference_webserial_enabled, false)) mode = MqttLoggerMode::MqttAndSerialAndWeb;
|
|
||||||
else if (preferences->getBool(preference_webserial_enabled, false)) mode = MqttLoggerMode::SerialAndWeb;
|
|
||||||
else mode = MqttLoggerMode::MqttAndSerial;
|
|
||||||
_path = new char[200];
|
|
||||||
memset(_path, 0, sizeof(_path));
|
|
||||||
String pathStr = preferences->getString(preference_mqtt_lock_path);
|
|
||||||
pathStr.concat(mqtt_topic_log);
|
|
||||||
strcpy(_path, pathStr.c_str());
|
|
||||||
Log = new MqttLogger(*getMqttClient(), _path, mode);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const String WifiDevice::deviceName() const
|
const String WifiDevice::deviceName() const
|
||||||
{
|
{
|
||||||
return "Built-in Wi-Fi";
|
return "Built-in Wi-Fi";
|
||||||
|
|||||||
@@ -45,10 +45,4 @@ private:
|
|||||||
uint8_t _connectedChannel = 0;
|
uint8_t _connectedChannel = 0;
|
||||||
uint8_t* _connectedBSSID;
|
uint8_t* _connectedBSSID;
|
||||||
int64_t _disconnectTs = 0;
|
int64_t _disconnectTs = 0;
|
||||||
|
|
||||||
#ifndef NUKI_HUB_UPDATER
|
|
||||||
char _ca[TLS_CA_MAX_SIZE] = {0};
|
|
||||||
char _cert[TLS_CERT_MAX_SIZE] = {0};
|
|
||||||
char _key[TLS_KEY_MAX_SIZE] = {0};
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user