From 2f2825eeedd3dc74d2d570383adc29ff684b975a Mon Sep 17 00:00:00 2001 From: technyon Date: Fri, 12 Aug 2022 20:12:21 +0200 Subject: [PATCH] add config entries for keypad --- NukiWrapper.cpp | 30 ++++++++++++++++++++++++++---- NukiWrapper.h | 3 +++ PreferencesKeys.h | 2 ++ WebCfgServer.cpp | 15 +++++++++++++++ main.cpp | 1 + 5 files changed, 47 insertions(+), 4 deletions(-) diff --git a/NukiWrapper.cpp b/NukiWrapper.cpp index 7feff6b..5d11b5d 100644 --- a/NukiWrapper.cpp +++ b/NukiWrapper.cpp @@ -41,12 +41,14 @@ void NukiWrapper::initialize() _intervalLockstate = _preferences->getInt(preference_query_interval_lockstate); _intervalBattery = _preferences->getInt(preference_query_interval_battery); + _intervalKeypad = _preferences->getInt(preference_query_interval_keypad); + _keypadEnabled = _preferences->getBool(preference_keypad_control_enabled); _publishAuthData = _preferences->getBool(preference_publish_authdata); _maxKeypadCodeCount = _preferences->getUInt(preference_max_keypad_code_count); if(_intervalLockstate == 0) { - _intervalLockstate = 60 * 5; + _intervalLockstate = 60 * 30; _preferences->putInt(preference_query_interval_lockstate, _intervalLockstate); } if(_intervalBattery == 0) @@ -54,6 +56,11 @@ void NukiWrapper::initialize() _intervalBattery = 60 * 30; _preferences->putInt(preference_query_interval_battery, _intervalBattery); } + if(_intervalKeypad == 0) + { + _intervalKeypad = 60 * 30; + _preferences->putInt(preference_query_interval_keypad, _intervalKeypad); + } _nukiLock.setEventHandler(this); @@ -107,9 +114,9 @@ void NukiWrapper::update() _nextConfigUpdateTs = ts + _intervalConfig * 1000; updateConfig(); } - if(_hasKeypad && (_nextKeypadUpdateTs == 0 || ts > _nextKeypadUpdateTs)) + if(_hasKeypad && _keypadEnabled && (_nextKeypadUpdateTs == 0 || ts > _nextKeypadUpdateTs)) { - _nextKeypadUpdateTs = ts + 60 * 60 * 1000; + _nextKeypadUpdateTs = ts + _intervalKeypad * 1000; updateKeypad(); } @@ -346,13 +353,18 @@ void NukiWrapper::onConfigUpdateReceived(const char *topic, const char *value) void NukiWrapper::onKeypadCommandReceived(const char *command, const uint &id, const String &name, const String &code, const int& enabled) { - if(!_hasKeypad) + if(!_hasKeypad || !_keypadEnabled) { return; } if(strcmp(command, "add") == 0) { + if(name == "") + { + return; + } + NukiLock::NewKeypadEntry entry; memset(&entry, 0, sizeof(entry)); size_t nameLen = name.length(); @@ -370,6 +382,11 @@ void NukiWrapper::onKeypadCommandReceived(const char *command, const uint &id, c } if(strcmp(command, "update") == 0) { + if(name == "") + { + return; + } + NukiLock::UpdatedKeypadEntry entry; memset(&entry, 0, sizeof(entry)); entry.codeId = id; @@ -393,6 +410,11 @@ const bool NukiWrapper::isPaired() return _paired; } +const bool NukiWrapper::hasKeypad() +{ + return _hasKeypad; +} + void NukiWrapper::notify(Nuki::EventType eventType) { if(eventType == Nuki::EventType::KeyTurnerStatusUpdated) diff --git a/NukiWrapper.h b/NukiWrapper.h index 48addc6..b4b7fb5 100644 --- a/NukiWrapper.h +++ b/NukiWrapper.h @@ -26,6 +26,7 @@ public: const NukiLock::KeyTurnerState& keyTurnerState(); const bool isPaired(); + const bool hasKeypad(); void notify(Nuki::EventType eventType) override; @@ -57,6 +58,7 @@ private: int _intervalLockstate = 0; // seconds int _intervalBattery = 0; // seconds int _intervalConfig = 60 * 60; // seconds + int _intervalKeypad = 0; // seconds bool _publishAuthData = false; bool _clearAuthData = false; @@ -76,6 +78,7 @@ private: bool _paired = false; bool _statusUpdated = false; bool _hasKeypad = false; + bool _keypadEnabled = false; uint _maxKeypadCodeCount = 0; unsigned long _nextLockStateUpdateTs = 0; unsigned long _nextBatteryReportTs = 0; diff --git a/PreferencesKeys.h b/PreferencesKeys.h index 3b72102..1f00e7c 100644 --- a/PreferencesKeys.h +++ b/PreferencesKeys.h @@ -21,6 +21,8 @@ #define preference_restart_timer "resttmr" #define preference_query_interval_lockstate "lockStInterval" #define preference_query_interval_battery "batInterval" +#define preference_query_interval_keypad "kpInterval" +#define preference_keypad_control_enabled "kpEnabled" #define preference_cred_user "crdusr" #define preference_cred_password "crdpass" #define preference_publish_authdata "pubauth" diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index 993eca6..c223f98 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -286,6 +286,16 @@ bool WebCfgServer::processArgs(String& message) _preferences->putInt(preference_query_interval_battery, value.toInt()); configChanged = true; } + else if(key == "KPINT") + { + _preferences->putInt(preference_query_interval_keypad, value.toInt()); + configChanged = true; + } + else if(key == "KPENA") + { + _preferences->putBool(preference_keypad_control_enabled, (value == "1")); + configChanged = true; + } else if(key == "PRDTMO") { _preferences->putInt(preference_presence_detection_timeout, value.toInt()); @@ -589,6 +599,11 @@ void WebCfgServer::buildNukiConfigHtml(String &response) } 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); + if(_nuki->hasKeypad()) + { + 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)); + } 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); diff --git a/main.cpp b/main.cpp index 69e8d25..63b68bc 100644 --- a/main.cpp +++ b/main.cpp @@ -148,6 +148,7 @@ void initPreferences() { preferences->putBool(preference_started_befores, true); preferences->putBool(preference_lock_enabled, true); + preferences->putBool(preference_keypad_control_enabled, true); } if(preferences->getInt(preference_restart_timer) == 0)