Refractor
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#include "../PreferencesKeys.h"
|
||||
#include "../Logger.h"
|
||||
#include "../RestartReason.h"
|
||||
#include "../EspMillis.h"
|
||||
|
||||
extern bool ethCriticalFailure;
|
||||
extern bool wifiFallback;
|
||||
@@ -48,7 +49,9 @@ EthernetDevice::EthernetDevice(const String &hostname,
|
||||
_useSpi(true),
|
||||
_preferences(preferences)
|
||||
{
|
||||
#ifndef NUKI_HUB_UPDATER
|
||||
NetworkDevice::init();
|
||||
#endif
|
||||
}
|
||||
|
||||
const String EthernetDevice::deviceName() const
|
||||
|
||||
@@ -11,9 +11,6 @@
|
||||
#include <NetworkClientSecure.h>
|
||||
#include <Preferences.h>
|
||||
#include "NetworkDevice.h"
|
||||
#ifndef NUKI_HUB_UPDATER
|
||||
#include "espMqttClient.h"
|
||||
#endif
|
||||
|
||||
class EthernetDevice : public NetworkDevice
|
||||
{
|
||||
@@ -65,16 +62,20 @@ private:
|
||||
void onNetworkEvent(arduino_event_id_t event, arduino_event_info_t info);
|
||||
|
||||
bool _connected = false;
|
||||
char* _path;
|
||||
bool _hardwareInitialized = false;
|
||||
bool _useSpi = false;
|
||||
|
||||
int64_t _checkIpTs = -1;
|
||||
|
||||
const std::string _deviceName;
|
||||
uint8_t _phy_addr;
|
||||
eth_phy_type_t _type;
|
||||
|
||||
// LAN8720
|
||||
int _power;
|
||||
int _mdc;
|
||||
int _mdio;
|
||||
eth_clock_mode_t _clock_mode;
|
||||
|
||||
// W55000 and DM9051
|
||||
int _cs;
|
||||
@@ -83,10 +84,4 @@ private:
|
||||
int _spi_sck;
|
||||
int _spi_miso;
|
||||
int _spi_mosi;
|
||||
|
||||
int64_t _checkIpTs = -1;
|
||||
|
||||
eth_phy_type_t _type;
|
||||
eth_clock_mode_t _clock_mode;
|
||||
bool _useSpi = false;
|
||||
};
|
||||
@@ -2,19 +2,19 @@
|
||||
|
||||
#ifndef CONFIG_IDF_TARGET_ESP32
|
||||
typedef enum {
|
||||
ETH_CLOCK_GPIO0_IN = 0,
|
||||
ETH_CLOCK_GPIO16_OUT = 2,
|
||||
ETH_CLOCK_GPIO17_OUT = 3
|
||||
} eth_clock_mode_t;
|
||||
ETH_CLOCK_GPIO0_IN = 0,
|
||||
ETH_CLOCK_GPIO16_OUT = 2,
|
||||
ETH_CLOCK_GPIO17_OUT = 3
|
||||
} eth_clock_mode_t;
|
||||
|
||||
#define ETH_PHY_TYPE_LAN8720 ETH_PHY_MAX
|
||||
#define ETH_PHY_TYPE_LAN8720 ETH_PHY_MAX
|
||||
#else
|
||||
#define ETH_PHY_TYPE_LAN8720 ETH_PHY_LAN8720
|
||||
#endif
|
||||
|
||||
#define ETH_CLK_MODE_LAN8720 ETH_CLOCK_GPIO0_IN
|
||||
#define ETH_PHY_ADDR_LAN8720 0
|
||||
#define ETH_PHY_MDC_LAN8720 23
|
||||
#define ETH_PHY_MDIO_LAN8720 18
|
||||
#define ETH_PHY_POWER_LAN8720 -1
|
||||
#define ETH_RESET_PIN_LAN8720 1
|
||||
#define ETH_CLK_MODE_LAN8720 ETH_CLOCK_GPIO0_IN
|
||||
#define ETH_PHY_ADDR_LAN8720 0
|
||||
#define ETH_PHY_MDC_LAN8720 23
|
||||
#define ETH_PHY_MDIO_LAN8720 18
|
||||
#define ETH_PHY_POWER_LAN8720 -1
|
||||
#define ETH_RESET_PIN_LAN8720 1
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
#ifndef NUKI_HUB_UPDATER
|
||||
#include "../MqttTopics.h"
|
||||
#include "espMqttClient.h"
|
||||
#include "PreferencesKeys.h"
|
||||
|
||||
void NetworkDevice::init()
|
||||
{
|
||||
@@ -58,17 +58,38 @@ void NetworkDevice::update()
|
||||
|
||||
void NetworkDevice::mqttSetClientId(const char *clientId)
|
||||
{
|
||||
getMqttClient()->setClientId(clientId);
|
||||
if (_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->setClientId(clientId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->setClientId(clientId);
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkDevice::mqttSetCleanSession(bool cleanSession)
|
||||
{
|
||||
getMqttClient()->setCleanSession(cleanSession);
|
||||
if (_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->setCleanSession(cleanSession);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->setCleanSession(cleanSession);
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkDevice::mqttSetKeepAlive(uint16_t keepAlive)
|
||||
{
|
||||
getMqttClient()->setKeepAlive(keepAlive);
|
||||
if (_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->setKeepAlive(keepAlive);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->setKeepAlive(keepAlive);
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t NetworkDevice::mqttPublish(const char *topic, uint8_t qos, bool retain, const char *payload)
|
||||
@@ -88,7 +109,14 @@ bool NetworkDevice::mqttConnected() const
|
||||
|
||||
void NetworkDevice::mqttSetServer(const char *host, uint16_t port)
|
||||
{
|
||||
getMqttClient()->setServer(host, port);
|
||||
if (_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->setServer(host, port);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->setServer(host, port);
|
||||
}
|
||||
}
|
||||
|
||||
bool NetworkDevice::mqttConnect()
|
||||
@@ -101,29 +129,64 @@ bool NetworkDevice::mqttDisconnect(bool force)
|
||||
return getMqttClient()->disconnect(force);
|
||||
}
|
||||
|
||||
void NetworkDevice::setWill(const char *topic, uint8_t qos, bool retain, const char *payload)
|
||||
void NetworkDevice::mqttSetWill(const char *topic, uint8_t qos, bool retain, const char *payload)
|
||||
{
|
||||
getMqttClient()->setWill(topic, qos, retain, 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)
|
||||
{
|
||||
getMqttClient()->setCredentials(username, password);
|
||||
if (_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->setCredentials(username, password);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->setCredentials(username, password);
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkDevice::mqttOnMessage(espMqttClientTypes::OnMessageCallback callback)
|
||||
{
|
||||
getMqttClient()->onMessage(callback);
|
||||
if (_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->onMessage(callback);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->onMessage(callback);
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkDevice::mqttOnConnect(espMqttClientTypes::OnConnectCallback callback)
|
||||
{
|
||||
getMqttClient()->onConnect(callback);
|
||||
if(_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->onConnect(callback);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->onConnect(callback);
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkDevice::mqttOnDisconnect(espMqttClientTypes::OnDisconnectCallback callback)
|
||||
{
|
||||
getMqttClient()->onDisconnect(callback);
|
||||
if (_useEncryption)
|
||||
{
|
||||
_mqttClientSecure->onDisconnect(callback);
|
||||
}
|
||||
else
|
||||
{
|
||||
_mqttClient->onDisconnect(callback);
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t NetworkDevice::mqttSubscribe(const char *topic, uint8_t qos)
|
||||
@@ -131,7 +194,7 @@ uint16_t NetworkDevice::mqttSubscribe(const char *topic, uint8_t qos)
|
||||
return getMqttClient()->subscribe(topic, qos);
|
||||
}
|
||||
|
||||
void NetworkDevice::disableMqtt()
|
||||
void NetworkDevice::mqttDisable()
|
||||
{
|
||||
getMqttClient()->disconnect();
|
||||
_mqttEnabled = false;
|
||||
|
||||
@@ -2,10 +2,8 @@
|
||||
|
||||
#ifndef NUKI_HUB_UPDATER
|
||||
#include "espMqttClient.h"
|
||||
#include "MqttClientSetup.h"
|
||||
#endif
|
||||
#include "IPConfiguration.h"
|
||||
#include "../EspMillis.h"
|
||||
|
||||
class NetworkDevice
|
||||
{
|
||||
@@ -31,42 +29,45 @@ public:
|
||||
virtual String BSSIDstr() = 0;
|
||||
|
||||
#ifndef NUKI_HUB_UPDATER
|
||||
virtual bool mqttConnect();
|
||||
virtual bool mqttDisconnect(bool force);
|
||||
virtual void mqttDisable();
|
||||
virtual bool mqttConnected() const;
|
||||
|
||||
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 uint16_t mqttSubscribe(const char* topic, uint8_t qos);
|
||||
|
||||
virtual void mqttSetServer(const char* host, uint16_t port);
|
||||
virtual void mqttSetClientId(const char* clientId);
|
||||
virtual void mqttSetCleanSession(bool cleanSession);
|
||||
virtual void mqttSetKeepAlive(uint16_t keepAlive);
|
||||
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 mqttSetWill(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);
|
||||
#endif
|
||||
|
||||
protected:
|
||||
const IPConfiguration* _ipConfiguration = nullptr;
|
||||
Preferences* _preferences = nullptr;
|
||||
#ifndef NUKI_HUB_UPDATER
|
||||
espMqttClient *_mqttClient = nullptr;
|
||||
espMqttClientSecure *_mqttClientSecure = nullptr;
|
||||
|
||||
bool _useEncryption = false;
|
||||
bool _mqttEnabled = true;
|
||||
|
||||
void init();
|
||||
|
||||
MqttClient *getMqttClient() const;
|
||||
|
||||
bool _useEncryption = false;
|
||||
bool _mqttEnabled = true;
|
||||
char* _path;
|
||||
char _ca[TLS_CA_MAX_SIZE] = {0};
|
||||
char _cert[TLS_CERT_MAX_SIZE] = {0};
|
||||
char _key[TLS_KEY_MAX_SIZE] = {0};
|
||||
#endif
|
||||
|
||||
const String _hostname;
|
||||
const IPConfiguration* _ipConfiguration = nullptr;
|
||||
};
|
||||
@@ -1,9 +1,10 @@
|
||||
#include "WifiDevice.h"
|
||||
#include "esp_wifi.h"
|
||||
#include <WiFi.h>
|
||||
#include "WifiDevice.h"
|
||||
#include "../PreferencesKeys.h"
|
||||
#include "../Logger.h"
|
||||
#include "../RestartReason.h"
|
||||
#include "../EspMillis.h"
|
||||
|
||||
WifiDevice::WifiDevice(const String& hostname, Preferences* preferences, const IPConfiguration* ipConfiguration)
|
||||
: NetworkDevice(hostname, preferences, ipConfiguration),
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <WiFiClient.h>
|
||||
#include <NetworkClientSecure.h>
|
||||
#include <Preferences.h>
|
||||
#include "NetworkDevice.h"
|
||||
#include "IPConfiguration.h"
|
||||
@@ -29,10 +27,10 @@ private:
|
||||
void onDisconnected();
|
||||
void onConnected();
|
||||
bool connect();
|
||||
char* _path;
|
||||
|
||||
Preferences* _preferences = nullptr;
|
||||
|
||||
char* _path;
|
||||
int _foundNetworks = 0;
|
||||
int _disconnectCount = 0;
|
||||
bool _connectOnScanDone = false;
|
||||
@@ -45,4 +43,4 @@ private:
|
||||
uint8_t _connectedChannel = 0;
|
||||
uint8_t* _connectedBSSID;
|
||||
int64_t _disconnectTs = 0;
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user