implement restart timer

This commit is contained in:
technyon
2022-07-03 21:09:27 +02:00
parent 4f442d7b1d
commit 5f65ed1531
4 changed files with 31 additions and 11 deletions

View File

@@ -42,7 +42,8 @@ private:
void publishInt(const char* topic, const int value); void publishInt(const char* topic, const int value);
void publishUInt(const char* topic, const unsigned int value); void publishUInt(const char* topic, const unsigned int value);
void publishBool(const char* topic, const bool 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); void buildMqttPath(const char* path, char* outPath);

View File

@@ -17,6 +17,7 @@
#define preference_hostname "hostname" #define preference_hostname "hostname"
#define preference_network_timeout "nettmout" #define preference_network_timeout "nettmout"
#define preference_restart_on_disconnect "restdisc" #define preference_restart_on_disconnect "restdisc"
#define preference_restart_timer "resttmr"
#define preference_query_interval_lockstate "lockStInterval" #define preference_query_interval_lockstate "lockStInterval"
#define preference_query_interval_battery "batInterval" #define preference_query_interval_battery "batInterval"
#define preference_cred_user "crdusr" #define preference_cred_user "crdusr"

View File

@@ -254,6 +254,11 @@ bool WebCfgServer::processArgs(String& message)
_preferences->putBool(preference_restart_on_disconnect, (value == "1")); _preferences->putBool(preference_restart_on_disconnect, (value == "1"));
configChanged = true; configChanged = true;
} }
else if(key == "RSTTMR")
{
_preferences->putInt(preference_restart_timer, 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());
@@ -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, "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); 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)); 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("</table>"); response.concat("</table>");
response.concat("* If no encryption is configured for the MQTT broker, leave empty.<br>"); response.concat("* If no encryption is configured for the MQTT broker, leave empty.<br>");

View File

@@ -24,6 +24,7 @@ EthServer* ethServer = nullptr;
bool lockEnabled = false; bool lockEnabled = false;
bool openerEnabled = false; bool openerEnabled = false;
unsigned long restartTs = (2^32) - 5 * 60000;
void networkTask(void *pvParameters) void networkTask(void *pvParameters)
{ {
@@ -91,12 +92,13 @@ void checkMillisTask(void *pvParameters)
{ {
while(true) while(true)
{ {
delay(60000); delay(30000);
// millis() is about to overflow. Restart device to prevent problems with overflow // 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.")); Serial.println(F("Restart timer expired, restarting device."));
vTaskDelay( 2000 / portTICK_PERIOD_MS); delay(2000);
ESP.restart(); 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() void setup()
@@ -152,13 +166,11 @@ void setup()
Serial.begin(115200); Serial.begin(115200);
preferences = new Preferences(); initPreferences();
preferences->begin("nukihub", false);
if(!preferences->getBool(preference_started_befores)) if(preferences->getInt(preference_restart_timer) > 0)
{ {
preferences->putBool(preference_started_befores, true); restartTs = preferences->getInt(preference_restart_timer) * 60 * 1000;
preferences->putBool(preference_lock_enabled, true);
} }
// const NetworkDeviceType networkDevice = NetworkDeviceType::WiFi; // const NetworkDeviceType networkDevice = NetworkDeviceType::WiFi;