From 633b7e8423aa00938e47c04b91ddcbf7207b5b35 Mon Sep 17 00:00:00 2001 From: technyon Date: Sat, 14 Jan 2023 10:09:35 +0100 Subject: [PATCH] add retry mechanic --- NukiWrapper.cpp | 41 ++++++++++++++++++++++++++++++++++++++--- NukiWrapper.h | 3 +++ PreferencesKeys.h | 2 ++ Version.h | 2 +- WebCfgServer.cpp | 12 ++++++++++++ 5 files changed, 56 insertions(+), 4 deletions(-) diff --git a/NukiWrapper.cpp b/NukiWrapper.cpp index 46921a5..0ce774c 100644 --- a/NukiWrapper.cpp +++ b/NukiWrapper.cpp @@ -48,6 +48,14 @@ void NukiWrapper::initialize() _maxKeypadCodeCount = _preferences->getUInt(preference_max_keypad_code_count); _restartBeaconTimeout = _preferences->getInt(preference_restart_ble_beacon_lost); _hassEnabled = _preferences->getString(preference_mqtt_hass_discovery) != ""; + _nrOfRetries = _preferences->getInt(preference_command_nr_of_retries); + _retryDelay = _preferences->getInt(preference_command_retry_delay); + + if(_retryDelay <= 100) + { + _retryDelay = 100; + _preferences->putInt(preference_command_retry_delay, _retryDelay); + } if(_intervalLockstate == 0) { @@ -175,10 +183,37 @@ void NukiWrapper::update() Log->print(F("Lock action result: ")); Log->println(resultStr); - _nextLockAction = (NukiLock::LockAction)0xff; - if(_intervalLockstate > 10) + if(cmdResult == Nuki::CmdResult::Success) { - _nextLockStateUpdateTs = ts + 10 * 1000; + _nextLockAction = (NukiLock::LockAction) 0xff; + if (_intervalLockstate > 10) + { + _nextLockStateUpdateTs = ts + 10 * 1000; + } + } + else + { + if(_retryCount == -1) + { + _retryCount = _nrOfRetries; + } + else if(_retryCount == 0) + { + _nextLockAction = (NukiLock::LockAction) 0xff; + _retryCount = -1; + if (_intervalLockstate > 10) + { + _nextLockStateUpdateTs = ts + 10 * 1000; + } + return; + } + + Log->print(F("Last command failed, retrying after ")); + Log->print(_retryDelay); + Log->print(F(" milliseconds.")); + + --_retryCount; + _nextLockStateUpdateTs = millis() + _retryDelay; } } diff --git a/NukiWrapper.h b/NukiWrapper.h index 9e7c669..ef054b5 100644 --- a/NukiWrapper.h +++ b/NukiWrapper.h @@ -84,6 +84,9 @@ private: bool _keypadEnabled = false; bool _configRead = false; uint _maxKeypadCodeCount = 0; + int _nrOfRetries = 0; + int _retryDelay = 0; + int _retryCount = -1; unsigned long _nextLockStateUpdateTs = 0; unsigned long _nextBatteryReportTs = 0; unsigned long _nextConfigUpdateTs = 0; diff --git a/PreferencesKeys.h b/PreferencesKeys.h index c45f3ef..890bca2 100644 --- a/PreferencesKeys.h +++ b/PreferencesKeys.h @@ -27,6 +27,8 @@ #define preference_query_interval_keypad "kpInterval" #define preference_keypad_control_enabled "kpEnabled" #define preference_register_as_app "regAsApp" // true = register as hub; false = register as app +#define preference_command_nr_of_retries "nrRetry" +#define preference_command_retry_delay "nrRetry" #define preference_cred_user "crdusr" #define preference_cred_password "crdpass" #define preference_publish_authdata "pubauth" diff --git a/Version.h b/Version.h index 394f420..807b224 100644 --- a/Version.h +++ b/Version.h @@ -1,3 +1,3 @@ #pragma once -#define nuki_hub_version "6.10" \ No newline at end of file +#define nuki_hub_version "6.10-retry-1" \ No newline at end of file diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index a87bfaa..679fbc3 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -307,6 +307,16 @@ bool WebCfgServer::processArgs(String& message) _preferences->putBool(preference_keypad_control_enabled, (value == "1")); configChanged = true; } + else if(key == "NRTRY") + { + _preferences->putInt(preference_command_nr_of_retries, value.toInt()); + configChanged = true; + } + else if(key == "TRYDLY") + { + _preferences->putInt(preference_command_retry_delay, value.toInt()); + configChanged = true; + } else if(key == "PRDTMO") { _preferences->putInt(preference_presence_detection_timeout, value.toInt()); @@ -637,6 +647,8 @@ void WebCfgServer::buildNukiConfigHtml(String &response) printInputField(response, "KPINT", "Query interval keypad (seconds)", _preferences->getInt(preference_query_interval_keypad), 10); printCheckBox(response, "KPENA", "Enabled keypad control via MQTT", _preferences->getBool(preference_keypad_control_enabled)); } + printInputField(response, "NRTRY", "Number of retries if command failed", _preferences->getInt(preference_command_nr_of_retries), 10); + printInputField(response, "TRYDLY", "Delay between retries", _preferences->getInt(preference_command_retry_delay), 10); printCheckBox(response, "PUBAUTH", "Publish auth data (May reduce battery life)", _preferences->getBool(preference_publish_authdata)); printCheckBox(response, "GPLCK", "Enable control via GPIO", _preferences->getBool(preference_gpio_locking_enabled)); printInputField(response, "PRDTMO", "Presence detection timeout (seconds; -1 to disable)", _preferences->getInt(preference_presence_detection_timeout), 10);