mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2026-09-13 21:54:07 +00:00
move reconnect count to onConnect. always show MQTT disconnect error
This commit is contained in:
+13
-17
@@ -51,7 +51,7 @@ bool Mqtt::publish_single2cmd_;
|
|||||||
std::vector<Mqtt::MQTTSubFunction, AllocatorPSRAM<Mqtt::MQTTSubFunction>> Mqtt::mqtt_subfunctions_;
|
std::vector<Mqtt::MQTTSubFunction, AllocatorPSRAM<Mqtt::MQTTSubFunction>> Mqtt::mqtt_subfunctions_;
|
||||||
|
|
||||||
uint32_t Mqtt::mqtt_publish_fails_ = 0;
|
uint32_t Mqtt::mqtt_publish_fails_ = 0;
|
||||||
bool Mqtt::connecting_ = false;
|
bool Mqtt::connected_ = false;
|
||||||
bool Mqtt::initialized_ = false;
|
bool Mqtt::initialized_ = false;
|
||||||
uint16_t Mqtt::queuecount_ = 0;
|
uint16_t Mqtt::queuecount_ = 0;
|
||||||
uint8_t Mqtt::connectcount_ = 0;
|
uint8_t Mqtt::connectcount_ = 0;
|
||||||
@@ -473,29 +473,24 @@ bool Mqtt::get_publish_onchange(uint8_t device_type) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
void Mqtt::on_disconnect(espMqttClientTypes::DisconnectReason reason) {
|
void Mqtt::on_disconnect(espMqttClientTypes::DisconnectReason reason) {
|
||||||
// only show the error once on the first connect failure
|
connected_ = false;
|
||||||
if (!connecting_) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
connecting_ = false;
|
|
||||||
connectcount_++; // count # reconnects
|
|
||||||
|
|
||||||
if (reason == espMqttClientTypes::DisconnectReason::TCP_DISCONNECTED) {
|
if (reason == espMqttClientTypes::DisconnectReason::TCP_DISCONNECTED) {
|
||||||
LOG_WARNING("MQTT disconnected: TCP");
|
LOG_ERROR("MQTT connection failure: TCP");
|
||||||
} else if (reason == espMqttClientTypes::DisconnectReason::MQTT_UNACCEPTABLE_PROTOCOL_VERSION) {
|
} else if (reason == espMqttClientTypes::DisconnectReason::MQTT_UNACCEPTABLE_PROTOCOL_VERSION) {
|
||||||
LOG_WARNING("MQTT disconnected: Unacceptable protocol version");
|
LOG_ERROR("MQTT connection failure: Unaccepted protocol version");
|
||||||
} else if (reason == espMqttClientTypes::DisconnectReason::MQTT_IDENTIFIER_REJECTED) {
|
} else if (reason == espMqttClientTypes::DisconnectReason::MQTT_IDENTIFIER_REJECTED) {
|
||||||
LOG_WARNING("MQTT disconnected: Identifier Rejected");
|
LOG_ERROR("MQTT connection failure: Identifier Rejected");
|
||||||
} else if (reason == espMqttClientTypes::DisconnectReason::MQTT_SERVER_UNAVAILABLE) {
|
} else if (reason == espMqttClientTypes::DisconnectReason::MQTT_SERVER_UNAVAILABLE) {
|
||||||
LOG_WARNING("MQTT disconnected: Server unavailable");
|
LOG_ERROR("MQTT connection failure: Server unavailable");
|
||||||
} else if (reason == espMqttClientTypes::DisconnectReason::MQTT_MALFORMED_CREDENTIALS) {
|
} else if (reason == espMqttClientTypes::DisconnectReason::MQTT_MALFORMED_CREDENTIALS) {
|
||||||
LOG_WARNING("MQTT disconnected: Malformed credentials");
|
LOG_ERROR("MQTT connection failure: Malformed credentials");
|
||||||
} else if (reason == espMqttClientTypes::DisconnectReason::MQTT_NOT_AUTHORIZED) {
|
} else if (reason == espMqttClientTypes::DisconnectReason::MQTT_NOT_AUTHORIZED) {
|
||||||
LOG_WARNING("MQTT disconnected: Not authorized");
|
LOG_ERROR("MQTT connection failure: Not authorized");
|
||||||
} else if (reason == espMqttClientTypes::DisconnectReason::TLS_BAD_FINGERPRINT) {
|
} else if (reason == espMqttClientTypes::DisconnectReason::TLS_BAD_FINGERPRINT) {
|
||||||
LOG_WARNING("MQTT disconnected: Server fingerprint invalid");
|
LOG_ERROR("MQTT connection failure: Server fingerprint invalid");
|
||||||
} else {
|
} else {
|
||||||
LOG_WARNING("MQTT disconnected: code %d", reason);
|
LOG_ERROR("MQTT connection failure: code %d", reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
mqttClient_->clearQueue(true);
|
mqttClient_->clearQueue(true);
|
||||||
@@ -503,13 +498,14 @@ void Mqtt::on_disconnect(espMqttClientTypes::DisconnectReason reason) {
|
|||||||
|
|
||||||
// MQTT on_connect - when an MQTT connect is established
|
// MQTT on_connect - when an MQTT connect is established
|
||||||
void Mqtt::on_connect() {
|
void Mqtt::on_connect() {
|
||||||
if (connecting_) {
|
if (connected_) {
|
||||||
return; // prevent duplicated connections
|
return; // prevent duplicated connections
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_INFO("MQTT connected");
|
LOG_INFO("MQTT connected");
|
||||||
|
|
||||||
connecting_ = true;
|
connected_ = true;
|
||||||
|
connectcount_++;
|
||||||
queuecount_ = mqttClient_->queueSize();
|
queuecount_ = mqttClient_->queueSize();
|
||||||
|
|
||||||
if (ha_enabled_) {
|
if (ha_enabled_) {
|
||||||
|
|||||||
Reference in New Issue
Block a user