mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2026-09-13 13:44:10 +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_;
|
||||
|
||||
uint32_t Mqtt::mqtt_publish_fails_ = 0;
|
||||
bool Mqtt::connecting_ = false;
|
||||
bool Mqtt::connected_ = false;
|
||||
bool Mqtt::initialized_ = false;
|
||||
uint16_t Mqtt::queuecount_ = 0;
|
||||
uint8_t Mqtt::connectcount_ = 0;
|
||||
@@ -473,29 +473,24 @@ bool Mqtt::get_publish_onchange(uint8_t device_type) {
|
||||
return false;
|
||||
}
|
||||
void Mqtt::on_disconnect(espMqttClientTypes::DisconnectReason reason) {
|
||||
// only show the error once on the first connect failure
|
||||
if (!connecting_) {
|
||||
return;
|
||||
}
|
||||
connecting_ = false;
|
||||
connectcount_++; // count # reconnects
|
||||
connected_ = false;
|
||||
|
||||
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) {
|
||||
LOG_WARNING("MQTT disconnected: Unacceptable protocol version");
|
||||
LOG_ERROR("MQTT connection failure: Unaccepted protocol version");
|
||||
} 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) {
|
||||
LOG_WARNING("MQTT disconnected: Server unavailable");
|
||||
LOG_ERROR("MQTT connection failure: Server unavailable");
|
||||
} 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) {
|
||||
LOG_WARNING("MQTT disconnected: Not authorized");
|
||||
LOG_ERROR("MQTT connection failure: Not authorized");
|
||||
} else if (reason == espMqttClientTypes::DisconnectReason::TLS_BAD_FINGERPRINT) {
|
||||
LOG_WARNING("MQTT disconnected: Server fingerprint invalid");
|
||||
LOG_ERROR("MQTT connection failure: Server fingerprint invalid");
|
||||
} else {
|
||||
LOG_WARNING("MQTT disconnected: code %d", reason);
|
||||
LOG_ERROR("MQTT connection failure: code %d", reason);
|
||||
}
|
||||
|
||||
mqttClient_->clearQueue(true);
|
||||
@@ -503,13 +498,14 @@ void Mqtt::on_disconnect(espMqttClientTypes::DisconnectReason reason) {
|
||||
|
||||
// MQTT on_connect - when an MQTT connect is established
|
||||
void Mqtt::on_connect() {
|
||||
if (connecting_) {
|
||||
if (connected_) {
|
||||
return; // prevent duplicated connections
|
||||
}
|
||||
|
||||
LOG_INFO("MQTT connected");
|
||||
|
||||
connecting_ = true;
|
||||
connected_ = true;
|
||||
connectcount_++;
|
||||
queuecount_ = mqttClient_->queueSize();
|
||||
|
||||
if (ha_enabled_) {
|
||||
|
||||
Reference in New Issue
Block a user