add network timeout to restart option

This commit is contained in:
technyon
2022-05-15 20:09:56 +02:00
parent 2875eb3421
commit ec277b1013
3 changed files with 31 additions and 12 deletions

View File

@@ -116,6 +116,13 @@ void Network::initialize()
_device->mqttClient()->setServer(_mqttBrokerAddr, port); _device->mqttClient()->setServer(_mqttBrokerAddr, port);
_device->mqttClient()->setCallback(Network::onMqttDataReceivedCallback); _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() bool Network::reconnect()
@@ -165,23 +172,28 @@ void Network::update()
{ {
long ts = millis(); long ts = millis();
if((ts - _lastMaintain) > 1000) _device->update();
{
_device->update();
if(!_device->isConnected()) if(!_device->isConnected())
{ {
Serial.println(F("Network not connected. Trying reconnect.")); Serial.println(F("Network not connected. Trying reconnect."));
bool success = _device->reconnect(); bool success = _device->reconnect();
Serial.println(success ? F("Reconnect successful") : F("Reconnect failed")); Serial.println(success ? F("Reconnect successful") : F("Reconnect failed"));
}
} }
if(!_device->isConnected()) if(!_device->isConnected())
{ {
if(ts - _lastConnectedTs > _networkTimeout * 1000)
{
Serial.println("Network timeout has been reached, restarting ...");
delay(200);
ESP.restart();
}
return; return;
} }
_lastConnectedTs = ts;
if(!_device->mqttClient()->connected()) if(!_device->mqttClient()->connected())
{ {
bool success = reconnect(); bool success = reconnect();

View File

@@ -68,6 +68,9 @@ private:
char _mqttPath[181] = {0}; char _mqttPath[181] = {0};
char _mqttUser[31] = {0}; char _mqttUser[31] = {0};
char _mqttPass[31] = {0}; char _mqttPass[31] = {0};
int _networkTimeout = 0;
unsigned long _lastConnectedTs = 0;
char* _presenceCsv = nullptr; char* _presenceCsv = nullptr;
@@ -75,8 +78,6 @@ private:
bool _firstTunerStatePublish = true; bool _firstTunerStatePublish = true;
long _lastMaintain = 0;
bool (*_lockActionReceivedCallback)(const char* value) = nullptr; bool (*_lockActionReceivedCallback)(const char* value) = nullptr;
void (*_configUpdateReceivedCallback)(const char* path, const char* value) = nullptr; void (*_configUpdateReceivedCallback)(const char* path, const char* value) = nullptr;
}; };

View File

@@ -146,6 +146,11 @@ bool WebCfgServer::processArgs(String& message)
_preferences->putString(preference_hostname, value); _preferences->putString(preference_hostname, value);
configChanged = true; configChanged = true;
} }
else if(key == "NETTIMEOUT")
{
_preferences->putInt(preference_network_timeout, value.toInt());
configChanged = true;
}
else if(key == "LSTINT") else if(key == "LSTINT")
{ {
_preferences->putInt(preference_query_interval_lockstate, value.toInt()); _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, "MQTTPASS", "MQTT Password", "*", 30, true);
printInputField(response, "MQTTPATH", "MQTT Path", _preferences->getString(preference_mqtt_path).c_str(), 180); 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, "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, "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); 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)); 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("</table>"); response.concat("</table>");
response.concat("<br><INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"Save\">"); response.concat("<br><INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"Save\">");