From 5f65ed15317ddea0217d11944dae24b2cc271c73 Mon Sep 17 00:00:00 2001 From: technyon Date: Sun, 3 Jul 2022 21:09:27 +0200 Subject: [PATCH] implement restart timer --- NetworkLock.h | 3 ++- PreferencesKeys.h | 1 + WebCfgServer.cpp | 6 ++++++ main.cpp | 32 ++++++++++++++++++++++---------- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/NetworkLock.h b/NetworkLock.h index b8226a4..aa993bb 100644 --- a/NetworkLock.h +++ b/NetworkLock.h @@ -42,7 +42,8 @@ private: void publishInt(const char* topic, const int value); void publishUInt(const char* topic, const unsigned int value); void publishBool(const char* topic, const bool value); - bool publishString(const char* topic, const char* value); bool comparePrefixedPath(const char* fullPath, const char* subPath); + bool publishString(const char* topic, const char* value); + bool comparePrefixedPath(const char* fullPath, const char* subPath); void buildMqttPath(const char* path, char* outPath); diff --git a/PreferencesKeys.h b/PreferencesKeys.h index ea2ff8a..c220810 100644 --- a/PreferencesKeys.h +++ b/PreferencesKeys.h @@ -17,6 +17,7 @@ #define preference_hostname "hostname" #define preference_network_timeout "nettmout" #define preference_restart_on_disconnect "restdisc" +#define preference_restart_timer "resttmr" #define preference_query_interval_lockstate "lockStInterval" #define preference_query_interval_battery "batInterval" #define preference_cred_user "crdusr" diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index 65190cb..a431b8f 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -254,6 +254,11 @@ bool WebCfgServer::processArgs(String& message) _preferences->putBool(preference_restart_on_disconnect, (value == "1")); configChanged = true; } + else if(key == "RSTTMR") + { + _preferences->putInt(preference_restart_timer, value.toInt()); + configChanged = true; + } else if(key == "LSTINT") { _preferences->putInt(preference_query_interval_lockstate, value.toInt()); @@ -520,6 +525,7 @@ void WebCfgServer::buildMqttConfigHtml(String &response) printInputField(response, "HASSDISCOVERY", "Home Assistant discovery topic (empty to disable)", _preferences->getString(preference_mqtt_hass_discovery).c_str(), 30); printInputField(response, "NETTIMEOUT", "Network Timeout until restart (seconds; -1 to disable)", _preferences->getInt(preference_network_timeout), 5); printCheckBox(response, "RSTDISC", "Restart on disconnect", _preferences->getBool(preference_restart_on_disconnect)); + printInputField(response, "RSTTMR", "Restart timer (minutes; -1 to disable)", _preferences->getInt(preference_restart_timer), 10); response.concat(""); response.concat("* If no encryption is configured for the MQTT broker, leave empty.
"); diff --git a/main.cpp b/main.cpp index daf9de3..9fd1e5f 100644 --- a/main.cpp +++ b/main.cpp @@ -24,6 +24,7 @@ EthServer* ethServer = nullptr; bool lockEnabled = false; bool openerEnabled = false; +unsigned long restartTs = (2^32) - 5 * 60000; void networkTask(void *pvParameters) { @@ -91,12 +92,13 @@ void checkMillisTask(void *pvParameters) { while(true) { - delay(60000); + delay(30000); + // millis() is about to overflow. Restart device to prevent problems with overflow - if(millis() > (2^32) - 5 * 60000) + if(millis() > restartTs) { - Serial.println(F("millis() is about to overflow. Restarting device.")); - vTaskDelay( 2000 / portTICK_PERIOD_MS); + Serial.println(F("Restart timer expired, restarting device.")); + delay(2000); ESP.restart(); } } @@ -141,9 +143,21 @@ void initEthServer(const NetworkDeviceType device) } } -void initNuki() +void initPreferences() { + preferences = new Preferences(); + preferences->begin("nukihub", false); + if(!preferences->getBool(preference_started_befores)) + { + preferences->putBool(preference_started_befores, true); + preferences->putBool(preference_lock_enabled, true); + } + + if(preferences->getInt(preference_restart_timer) == 0) + { + preferences->putInt(preference_restart_timer, -1); + } } void setup() @@ -152,13 +166,11 @@ void setup() Serial.begin(115200); - preferences = new Preferences(); - preferences->begin("nukihub", false); + initPreferences(); - if(!preferences->getBool(preference_started_befores)) + if(preferences->getInt(preference_restart_timer) > 0) { - preferences->putBool(preference_started_befores, true); - preferences->putBool(preference_lock_enabled, true); + restartTs = preferences->getInt(preference_restart_timer) * 60 * 1000; } // const NetworkDeviceType networkDevice = NetworkDeviceType::WiFi;