Revert ESP-MQTT (#508)

* Revert ESP-MQTT

* Revert ESP-MQTT

* Update sdkconfig.defaults
This commit is contained in:
iranl
2024-11-02 17:07:00 +01:00
committed by GitHub
parent 81a91c4157
commit 7454208230
115 changed files with 9965 additions and 962 deletions

View File

@@ -1,10 +1,14 @@
#include "EthernetDevice.h"
#include "../PreferencesKeys.h"
#include "../Logger.h"
#ifndef NUKI_HUB_UPDATER
#include "../MqttTopics.h"
#include "espMqttClient.h"
#endif
#include "../RestartReason.h"
RTC_NOINIT_ATTR bool criticalEthFailure;
extern char WiFi_fallbackDetect[14];
extern bool ethCriticalFailure;
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)
: NetworkDevice(hostname, ipConfiguration),
@@ -49,6 +53,51 @@ EthernetDevice::EthernetDevice(const String &hostname,
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
{
return _deviceName.c_str();
@@ -57,12 +106,12 @@ const String EthernetDevice::deviceName() const
void EthernetDevice::initialize()
{
delay(250);
if(criticalEthFailure)
if(ethCriticalFailure)
{
criticalEthFailure = false;
ethCriticalFailure = false;
Log->println(F("Failed to initialize ethernet hardware"));
Log->println("Network device has a critical failure, enable fallback to Wi-Fi and reboot.");
strcpy(WiFi_fallbackDetect, "wifi_fallback");
wifiFallback = true;
delay(200);
restartEsp(RestartReason::NetworkDeviceCriticalFailure);
return;
@@ -73,18 +122,18 @@ void EthernetDevice::initialize()
if(_useSpi)
{
Log->println(F("Use SPI"));
criticalEthFailure = true;
ethCriticalFailure = true;
SPI.begin(_spi_sck, _spi_miso, _spi_mosi);
_hardwareInitialized = ETH.begin(_type, _phy_addr, _cs, _irq, _rst, SPI);
criticalEthFailure = false;
ethCriticalFailure = false;
}
#ifdef CONFIG_IDF_TARGET_ESP32
else
{
Log->println(F("Use RMII"));
criticalEthFailure = true;
ethCriticalFailure = true;
_hardwareInitialized = ETH.begin(_type, _phy_addr, _mdc, _mdio, _power, _clock_mode);
criticalEthFailure = false;
ethCriticalFailure = false;
if(!_ipConfiguration->dhcpEnabled())
{
_checkIpTs = espMillis() + 2000;
@@ -95,7 +144,7 @@ void EthernetDevice::initialize()
if(_hardwareInitialized)
{
Log->println(F("Ethernet hardware Initialized"));
memset(WiFi_fallbackDetect, 0, sizeof(WiFi_fallbackDetect));
wifiFallback = false;
if(_useSpi && !_ipConfiguration->dhcpEnabled())
{
@@ -111,7 +160,7 @@ void EthernetDevice::initialize()
{
Log->println(F("Failed to initialize ethernet hardware"));
Log->println("Network device has a critical failure, enable fallback to Wi-Fi and reboot.");
strcpy(WiFi_fallbackDetect, "wifi_fallback");
wifiFallback = true;
delay(200);
restartEsp(RestartReason::NetworkDeviceCriticalFailure);
return;
@@ -120,18 +169,19 @@ void EthernetDevice::initialize()
void EthernetDevice::update()
{
if(_checkIpTs != -1)
NetworkDevice::update();
if(_checkIpTs != -1 && _checkIpTs < espMillis())
{
if(_ipConfiguration->ipAddress() != ETH.localIP())
{
Log->println(F("ETH Set static IP"));
ETH.config(_ipConfiguration->ipAddress(), _ipConfiguration->defaultGateway(), _ipConfiguration->subnet(), _ipConfiguration->dnsServer());
_checkIpTs = espMillis() + 2000;
}
else
{
_checkIpTs = -1;
_checkIpTs = espMillis() + 5000;
return;
}
_checkIpTs = -1;
}
}