From ec277b1013378d79da05d41dd7130dc278e65c03 Mon Sep 17 00:00:00 2001 From: technyon Date: Sun, 15 May 2022 20:09:56 +0200 Subject: [PATCH] add network timeout to restart option --- Network.cpp | 30 +++++++++++++++++++++--------- Network.h | 5 +++-- WebCfgServer.cpp | 8 +++++++- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/Network.cpp b/Network.cpp index aad7a63..2114d03 100644 --- a/Network.cpp +++ b/Network.cpp @@ -116,6 +116,13 @@ void Network::initialize() _device->mqttClient()->setServer(_mqttBrokerAddr, port); _device->mqttClient()->setCallback(Network::onMqttDataReceivedCallback); + + _networkTimeout = _preferences->getInt(preference_network_timeout); + if(_networkTimeout == 0) + { + _networkTimeout = -1; + _preferences->putInt(preference_network_timeout, _networkTimeout); + } } bool Network::reconnect() @@ -165,23 +172,28 @@ void Network::update() { long ts = millis(); - if((ts - _lastMaintain) > 1000) - { - _device->update(); + _device->update(); - if(!_device->isConnected()) - { - Serial.println(F("Network not connected. Trying reconnect.")); - bool success = _device->reconnect(); - Serial.println(success ? F("Reconnect successful") : F("Reconnect failed")); - } + if(!_device->isConnected()) + { + Serial.println(F("Network not connected. Trying reconnect.")); + bool success = _device->reconnect(); + Serial.println(success ? F("Reconnect successful") : F("Reconnect failed")); } if(!_device->isConnected()) { + if(ts - _lastConnectedTs > _networkTimeout * 1000) + { + Serial.println("Network timeout has been reached, restarting ..."); + delay(200); + ESP.restart(); + } return; } + _lastConnectedTs = ts; + if(!_device->mqttClient()->connected()) { bool success = reconnect(); diff --git a/Network.h b/Network.h index 327549c..5d9e49d 100644 --- a/Network.h +++ b/Network.h @@ -68,6 +68,9 @@ private: char _mqttPath[181] = {0}; char _mqttUser[31] = {0}; char _mqttPass[31] = {0}; + int _networkTimeout = 0; + + unsigned long _lastConnectedTs = 0; char* _presenceCsv = nullptr; @@ -75,8 +78,6 @@ private: bool _firstTunerStatePublish = true; - long _lastMaintain = 0; - bool (*_lockActionReceivedCallback)(const char* value) = nullptr; void (*_configUpdateReceivedCallback)(const char* path, const char* value) = nullptr; }; diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index c742c2b..55299fe 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -146,6 +146,11 @@ bool WebCfgServer::processArgs(String& message) _preferences->putString(preference_hostname, value); configChanged = true; } + else if(key == "NETTIMEOUT") + { + _preferences->putInt(preference_network_timeout, value.toInt()); + configChanged = true; + } else if(key == "LSTINT") { _preferences->putInt(preference_query_interval_lockstate, value.toInt()); @@ -265,10 +270,11 @@ void WebCfgServer::buildHtml(String& response) printInputField(response, "MQTTPASS", "MQTT Password", "*", 30, true); printInputField(response, "MQTTPATH", "MQTT Path", _preferences->getString(preference_mqtt_path).c_str(), 180); printInputField(response, "HOSTNAME", "Host name", _preferences->getString(preference_hostname).c_str(), 100); + printInputField(response, "NETTIMEOUT", "Network Timeout until restart (seconds; -1 to disable)", _preferences->getInt(preference_network_timeout), 5); printInputField(response, "LSTINT", "Query interval lock state (seconds)", _preferences->getInt(preference_query_interval_lockstate), 10); printInputField(response, "BATINT", "Query interval battery (seconds)", _preferences->getInt(preference_query_interval_battery), 10); printCheckBox(response, "PUBAUTH", "Publish auth data (May reduce battery life)", _preferences->getBool(preference_publish_authdata)); - printInputField(response, "PRDTMO", "Presence detection timeout (seconds, -1 to disable)", _preferences->getInt(preference_presence_detection_timeout), 10); + printInputField(response, "PRDTMO", "Presence detection timeout (seconds; -1 to disable)", _preferences->getInt(preference_presence_detection_timeout), 10); response.concat(""); response.concat("
");