mirror of
https://github.com/Laxilef/OTGateway.git
synced 2025-12-11 02:34:29 +05:00
refactor: optimization of connection to MQTT
This commit is contained in:
@@ -160,22 +160,19 @@ protected:
|
|||||||
fsSettings.update();
|
fsSettings.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this->client->connected() && this->connected) {
|
if (this->connected && !this->client->connected()) {
|
||||||
this->connected = false;
|
this->connected = false;
|
||||||
this->onDisconnect();
|
this->onDisconnect();
|
||||||
}
|
|
||||||
|
} else if (!this->connected && millis() - this->lastReconnectTime >= MQTT_RECONNECT_INTERVAL) {
|
||||||
if (this->wifiClient == nullptr || (!this->connected && millis() - this->lastReconnectTime >= MQTT_RECONNECT_INTERVAL)) {
|
|
||||||
Log.sinfoln(FPSTR(L_MQTT), F("Connecting to %s:%u..."), settings.mqtt.server, settings.mqtt.port);
|
Log.sinfoln(FPSTR(L_MQTT), F("Connecting to %s:%u..."), settings.mqtt.server, settings.mqtt.port);
|
||||||
|
|
||||||
this->client->setId(networkSettings.hostname);
|
this->client->setId(networkSettings.hostname);
|
||||||
this->client->setUsernamePassword(settings.mqtt.user, settings.mqtt.password);
|
this->client->setUsernamePassword(settings.mqtt.user, settings.mqtt.password);
|
||||||
this->client->connect(settings.mqtt.server, settings.mqtt.port);
|
this->client->connect(settings.mqtt.server, settings.mqtt.port);
|
||||||
|
|
||||||
this->lastReconnectTime = millis();
|
this->lastReconnectTime = millis();
|
||||||
}
|
|
||||||
|
|
||||||
if (this->client->connected() && !this->connected) {
|
} else if (!this->connected && this->client->connected()) {
|
||||||
this->connected = true;
|
this->connected = true;
|
||||||
this->onConnect();
|
this->onConnect();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user