Fixes/Enhancement for MQTT and WiFi reconnect + network timout + MQTT retain (#419)

* Retain

* Reconnect
This commit is contained in:
iranl
2024-07-09 13:17:31 +02:00
committed by GitHub
parent 8f679c45a5
commit 137b99b291
8 changed files with 90 additions and 66 deletions

View File

@@ -321,18 +321,18 @@ void NukiNetwork::initialize()
if(rebGpio)
{
buildMqttPath(gpioPath, {mqtt_topic_gpio_prefix, (mqtt_topic_gpio_pin + std::to_string(pinEntry.pin)).c_str(), mqtt_topic_gpio_role});
publishString(_lockPath.c_str(), gpioPath, "input");
publishString(_lockPath.c_str(), gpioPath, "input", false);
buildMqttPath(gpioPath, {mqtt_topic_gpio_prefix, (mqtt_topic_gpio_pin + std::to_string(pinEntry.pin)).c_str(), mqtt_topic_gpio_state});
publishString(_lockPath.c_str(), gpioPath, std::to_string(digitalRead(pinEntry.pin)).c_str());
publishString(_lockPath.c_str(), gpioPath, std::to_string(digitalRead(pinEntry.pin)).c_str(), false);
}
break;
case PinRole::GeneralOutput:
if(rebGpio)
{
buildMqttPath(gpioPath, {mqtt_topic_gpio_prefix, (mqtt_topic_gpio_pin + std::to_string(pinEntry.pin)).c_str(), mqtt_topic_gpio_role});
publishString(_lockPath.c_str(), gpioPath, "output");
publishString(_lockPath.c_str(), gpioPath, "output", false);
buildMqttPath(gpioPath, {mqtt_topic_gpio_prefix, (mqtt_topic_gpio_pin + std::to_string(pinEntry.pin)).c_str(), mqtt_topic_gpio_state});
publishString(_lockPath.c_str(), gpioPath, "0");
publishString(_lockPath.c_str(), gpioPath, "0", false);
}
buildMqttPath(gpioPath, {mqtt_topic_gpio_prefix, (mqtt_topic_gpio_pin + std::to_string(pinEntry.pin)).c_str(), mqtt_topic_gpio_state});
subscribe(_lockPath.c_str(), gpioPath);
@@ -360,8 +360,11 @@ bool NukiNetwork::update()
if(!_device->isConnected())
{
_device->mqttDisconnect(true);
if(_firstDisconnected) {
_firstDisconnected = false;
_device->mqttDisconnect(true);
}
if(_restartOnDisconnect && millis() > 60000)
{
restartEsp(RestartReason::RestartOnDisconnectWatchdog);
@@ -394,17 +397,11 @@ bool NukiNetwork::update()
_logIp = false;
Log->print(F("IP: "));
Log->println(_device->localIP());
_firstDisconnected = true;
}
if(!_device->mqttConnected() && _device->isConnected())
{
if(_networkTimeout > 0 && (ts - _lastConnectedTs > _networkTimeout * 1000) && ts > 60000)
{
Log->println("Network timeout has been reached, restarting ...");
delay(200);
restartEsp(RestartReason::NetworkTimeoutWatchdog);
}
bool success = reconnect();
if(!success)
{
@@ -413,8 +410,19 @@ bool NukiNetwork::update()
delay(2000);
}
if(!_device->mqttConnected() || !_device->isConnected()) return false;
if(!_device->mqttConnected() || !_device->isConnected())
{
if(_networkTimeout > 0 && (ts - _lastConnectedTs > _networkTimeout * 1000) && ts > 60000)
{
Log->println("Network timeout has been reached, restarting ...");
delay(200);
restartEsp(RestartReason::NetworkTimeoutWatchdog);
}
delay(2000);
return false;
}
_lastConnectedTs = ts;
if(_presenceDetection != nullptr && (_lastPresenceTs == 0 || (ts - _lastPresenceTs) > 3000))
@@ -504,7 +512,7 @@ bool NukiNetwork::update()
uint8_t pinState = digitalRead(pin) == HIGH ? 1 : 0;
char gpioPath[250];
buildMqttPath(gpioPath, {mqtt_topic_gpio_prefix, (mqtt_topic_gpio_pin + std::to_string(pin)).c_str(), mqtt_topic_gpio_state});
publishInt(_lockPath.c_str(), gpioPath, pinState);
publishInt(_lockPath.c_str(), gpioPath, pinState, false);
Log->print(F("GPIO "));
Log->print(pin);
@@ -774,7 +782,7 @@ bool NukiNetwork::encryptionSupported()
return _device->supportsEncryption();
}
void NukiNetwork::publishFloat(const char* prefix, const char* topic, const float value, const uint8_t precision, bool retain)
void NukiNetwork::publishFloat(const char* prefix, const char* topic, const float value, bool retain, const uint8_t precision)
{
char str[30];
dtostrf(value, 0, precision, str);