some checks

This commit is contained in:
MichaelDvP
2025-07-10 12:09:17 +02:00
parent 6c42cbfb4b
commit 4b2468d616
3 changed files with 15 additions and 11 deletions

View File

@@ -23,6 +23,7 @@ static String generateClientId() {
MqttSettingsService::~MqttSettingsService() { MqttSettingsService::~MqttSettingsService() {
delete _mqttClient; delete _mqttClient;
_mqttClient = nullptr;
} }
void MqttSettingsService::begin() { void MqttSettingsService::begin() {
@@ -34,7 +35,7 @@ void MqttSettingsService::startClient() {
static bool isSecure = false; static bool isSecure = false;
if (_mqttClient != nullptr) { if (_mqttClient != nullptr) {
// do we need to change the client? // do we need to change the client?
if ((isSecure && _state.enableTLS) || (!isSecure && !_state.enableTLS)) { if (_state.enabled && ((isSecure && _state.enableTLS) || (!isSecure && !_state.enableTLS))) {
return; return;
} }
delete _mqttClient; delete _mqttClient;
@@ -43,10 +44,10 @@ void MqttSettingsService::startClient() {
#ifndef TASMOTA_SDK #ifndef TASMOTA_SDK
if (_state.enableTLS) { if (_state.enableTLS) {
isSecure = true; isSecure = true;
if (emsesp::EMSESP::system_.PSram() > 0) { if (emsesp::EMSESP::system_.PSram() == 0) {
_mqttClient = new espMqttClientSecure(EMSESP_MQTT_PRIORITY, EMSESP_MQTT_RUNNING_CORE);
} else {
_mqttClient = new espMqttClientSecure(espMqttClientTypes::UseInternalTask::NO); _mqttClient = new espMqttClientSecure(espMqttClientTypes::UseInternalTask::NO);
} else {
_mqttClient = new espMqttClientSecure(EMSESP_MQTT_PRIORITY, EMSESP_MQTT_RUNNING_CORE);
} }
if (!_mqttClient) { if (!_mqttClient) {
emsesp::EMSESP::logger().warning("MQTT Client alloc failed"); emsesp::EMSESP::logger().warning("MQTT Client alloc failed");
@@ -63,10 +64,10 @@ void MqttSettingsService::startClient() {
} }
#endif #endif
isSecure = false; isSecure = false;
if (emsesp::EMSESP::system_.PSram() > 0) { if (emsesp::EMSESP::system_.PSram() == 0) {
_mqttClient = new espMqttClient(EMSESP_MQTT_PRIORITY, EMSESP_MQTT_RUNNING_CORE);
} else {
_mqttClient = new espMqttClient(espMqttClientTypes::UseInternalTask::NO); _mqttClient = new espMqttClient(espMqttClientTypes::UseInternalTask::NO);
} else {
_mqttClient = new espMqttClient(EMSESP_MQTT_PRIORITY, EMSESP_MQTT_RUNNING_CORE);
} }
static_cast<espMqttClient *>(_mqttClient)->onConnect([this](bool sessionPresent) { onMqttConnect(sessionPresent); }); static_cast<espMqttClient *>(_mqttClient)->onConnect([this](bool sessionPresent) { onMqttConnect(sessionPresent); });
static_cast<espMqttClient *>(_mqttClient)->onDisconnect([this](espMqttClientTypes::DisconnectReason reason) { onMqttDisconnect(reason); }); static_cast<espMqttClient *>(_mqttClient)->onDisconnect([this](espMqttClientTypes::DisconnectReason reason) { onMqttDisconnect(reason); });
@@ -78,6 +79,9 @@ void MqttSettingsService::startClient() {
} }
void MqttSettingsService::loop() { void MqttSettingsService::loop() {
if (!_state.enabled || _mqttClient == nullptr || emsesp::EMSESP::system_.systemStatus() != 0) {
return;
}
if (_reconfigureMqtt || (_disconnectedAt && static_cast<uint32_t>(uuid::get_uptime() - _disconnectedAt) >= MQTT_RECONNECTION_DELAY)) { if (_reconfigureMqtt || (_disconnectedAt && static_cast<uint32_t>(uuid::get_uptime() - _disconnectedAt) >= MQTT_RECONNECTION_DELAY)) {
// reconfigure MQTT client // reconfigure MQTT client
_disconnectedAt = configureMqtt() ? 0 : uuid::get_uptime(); _disconnectedAt = configureMqtt() ? 0 : uuid::get_uptime();
@@ -93,11 +97,11 @@ bool MqttSettingsService::isEnabled() {
} }
bool MqttSettingsService::isConnected() { bool MqttSettingsService::isConnected() {
return _mqttClient->connected(); return _mqttClient ? _mqttClient->connected() : false;
} }
const char * MqttSettingsService::getClientId() { const char * MqttSettingsService::getClientId() {
return _mqttClient->getClientId(); return _mqttClient ? _mqttClient->getClientId() : "";
} }
void MqttSettingsService::onMqttMessage(const espMqttClientTypes::MessageProperties & properties, void MqttSettingsService::onMqttMessage(const espMqttClientTypes::MessageProperties & properties,

View File

@@ -25,7 +25,7 @@ void NTPSettingsService::WiFiEvent(WiFiEvent_t event) {
switch (event) { switch (event) {
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
case ARDUINO_EVENT_ETH_DISCONNECTED: case ARDUINO_EVENT_ETH_DISCONNECTED:
if (_connected) { if (_connected && emsesp::EMSESP::system_.ntp_connected()) {
emsesp::EMSESP::logger().info("WiFi connection dropped, stopping NTP"); emsesp::EMSESP::logger().info("WiFi connection dropped, stopping NTP");
_connected = false; _connected = false;
configureNTP(); configureNTP();

View File

@@ -293,7 +293,7 @@ enum {
#ifndef STRINGIZE #ifndef STRINGIZE
#define STRINGIZE(s) #s #define STRINGIZE(s) #s
#endif #endif
#if TASMOTA_SDK #ifdef TASMOTA_SDK
#define ARDUINO_VERSION_STR(major, minor, patch) "Tasmota Arduino v" STRINGIZE(major) "." STRINGIZE(minor) "." STRINGIZE(patch) #define ARDUINO_VERSION_STR(major, minor, patch) "Tasmota Arduino v" STRINGIZE(major) "." STRINGIZE(minor) "." STRINGIZE(patch)
#else #else
#define ARDUINO_VERSION_STR(major, minor, patch) "ESP32 Arduino v" STRINGIZE(major) "." STRINGIZE(minor) "." STRINGIZE(patch) #define ARDUINO_VERSION_STR(major, minor, patch) "ESP32 Arduino v" STRINGIZE(major) "." STRINGIZE(minor) "." STRINGIZE(patch)