Granular Access Level Control

This commit is contained in:
iranl
2024-03-11 22:30:39 +01:00
parent 1d9787374c
commit a0dc425d3b
12 changed files with 403 additions and 184 deletions

View File

@@ -7,7 +7,7 @@
#include <NukiLockUtils.h>
NukiWrapper* nukiInst;
AccessLevel NukiWrapper::_accessLevel = AccessLevel::ReadOnly;
Preferences* nukiLockPreferences = nullptr;
NukiWrapper::NukiWrapper(const std::string& deviceName, NukiDeviceId* deviceId, BleScanner::Scanner* scanner, NetworkLock* network, Gpio* gpio, Preferences* preferences)
: _deviceName(deviceName),
@@ -53,7 +53,7 @@ void NukiWrapper::initialize(const bool& firstStart)
_intervalConfig = _preferences->getInt(preference_query_interval_configuration);
_intervalBattery = _preferences->getInt(preference_query_interval_battery);
_intervalKeypad = _preferences->getInt(preference_query_interval_keypad);
_keypadEnabled = _preferences->getBool(preference_keypad_control_enabled);
_keypadEnabled = _preferences->getBool(preference_keypad_info_enabled);
_publishAuthData = _preferences->getBool(preference_publish_authdata);
_maxKeypadCodeCount = _preferences->getUInt(preference_lock_max_keypad_code_count);
_restartBeaconTimeout = _preferences->getInt(preference_restart_ble_beacon_lost);
@@ -61,15 +61,32 @@ void NukiWrapper::initialize(const bool& firstStart)
_nrOfRetries = _preferences->getInt(preference_command_nr_of_retries);
_retryDelay = _preferences->getInt(preference_command_retry_delay);
_rssiPublishInterval = _preferences->getInt(preference_rssi_publish_interval) * 1000;
_accessLevel = (AccessLevel)_preferences->getInt(preference_access_level);
if(firstStart)
{
_preferences->putInt(preference_command_nr_of_retries, 3);
_preferences->putInt(preference_command_retry_delay, 1000);
_preferences->putInt(preference_restart_ble_beacon_lost, 60);
_preferences->putBool(preference_admin_config_enabled, true);
_preferences->putBool(preference_acl_lock, true);
_preferences->putBool(preference_acl_unlock, true);
_preferences->putBool(preference_acl_unlatch, true);
_preferences->putBool(preference_acl_lockngo, true);
_preferences->putBool(preference_acl_lockngo_unlatch, true);
_preferences->putBool(preference_acl_fulllock, true);
_preferences->putBool(preference_acl_lck_fob1, true);
_preferences->putBool(preference_acl_lck_fob2, true);
_preferences->putBool(preference_acl_lck_fob3, true);
_preferences->putBool(preference_acl_act_rto, true);
_preferences->putBool(preference_acl_deact_rto, true);
_preferences->putBool(preference_acl_act_esa, true);
_preferences->putBool(preference_acl_act_cm, true);
_preferences->putBool(preference_acl_deact_cm, true);
_preferences->putBool(preference_acl_opn_fob1, true);
_preferences->putBool(preference_acl_opn_fob2, true);
_preferences->putBool(preference_acl_opn_fob3, true);
}
if(_retryDelay <= 100)
{
_retryDelay = 100;
@@ -324,7 +341,7 @@ void NukiWrapper::updateKeyTurnerState()
}
return;
}
_retryLockstateCount = 0;
if(_publishAuthData)
@@ -375,7 +392,7 @@ void NukiWrapper::updateConfig()
void NukiWrapper::updateAuthData()
{
if(_nukiLock.getSecurityPincode() == 0) return;
Nuki::CmdResult result = _nukiLock.retrieveLogEntries(0, 0, 0, true);
if(result != Nuki::CmdResult::Success)
{
@@ -404,6 +421,8 @@ void NukiWrapper::updateAuthData()
void NukiWrapper::updateKeypad()
{
if(_preferences->getBool(preference_keypad_info_enabled)) return;
Log->print(F("Querying lock keypad: "));
Nuki::CmdResult result = _nukiLock.retrieveKeypadEntries(0, 0xffff);
printCommandResult(result);
@@ -462,33 +481,17 @@ LockActionResult NukiWrapper::onLockActionReceivedCallback(const char *value)
return LockActionResult::UnknownAction;
}
switch(_accessLevel)
nukiLockPreferences = new Preferences();
nukiLockPreferences->begin("nukihub", true);
if((action == NukiLock::LockAction::Lock && nukiLockPreferences->getBool(preference_acl_lock)) || (action == NukiLock::LockAction::Unlock && nukiLockPreferences->getBool(preference_acl_unlock)) || (action == NukiLock::LockAction::Unlatch && nukiLockPreferences->getBool(preference_acl_unlatch)) || (action == NukiLock::LockAction::LockNgo && nukiLockPreferences->getBool(preference_acl_lockngo)) || (action == NukiLock::LockAction::LockNgoUnlatch && nukiLockPreferences->getBool(preference_acl_lockngo_unlatch)) || (action == NukiLock::LockAction::FullLock && nukiLockPreferences->getBool(preference_acl_fulllock)) || (action == NukiLock::LockAction::FobAction1 && nukiLockPreferences->getBool(preference_acl_lck_fob1)) || (action == NukiLock::LockAction::FobAction2 && nukiLockPreferences->getBool(preference_acl_lck_fob2)) || (action == NukiLock::LockAction::FobAction3 && nukiLockPreferences->getBool(preference_acl_lck_fob3)))
{
case AccessLevel::Full:
nukiInst->_nextLockAction = action;
return LockActionResult::Success;
break;
case AccessLevel::LockAndUnlock:
if(action == NukiLock::LockAction::Lock || action == NukiLock::LockAction::Unlock || action == NukiLock::LockAction::LockNgo || action == NukiLock::LockAction::FullLock)
{
nukiInst->_nextLockAction = action;
return LockActionResult::Success;
}
return LockActionResult::AccessDenied;
break;
case AccessLevel::LockOnly:
if(action == NukiLock::LockAction::Lock)
{
nukiInst->_nextLockAction = action;
return LockActionResult::Success;
}
return LockActionResult::AccessDenied;
break;
case AccessLevel::ReadOnly:
default:
return LockActionResult::AccessDenied;
break;
nukiLockPreferences->end();
return LockActionResult::Success;
}
nukiLockPreferences->end();
return LockActionResult::AccessDenied;
}
void NukiWrapper::onConfigUpdateReceivedCallback(const char *topic, const char *value)
@@ -525,7 +528,7 @@ void NukiWrapper::gpioActionCallback(const GpioAction &action, const int& pin)
void NukiWrapper::onConfigUpdateReceived(const char *topic, const char *value)
{
if(_accessLevel != AccessLevel::Full) return;
if(!_preferences->getBool(preference_admin_config_enabled)) return;
if(strcmp(topic, mqtt_topic_config_button_enabled) == 0)
{
@@ -580,7 +583,11 @@ 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(_accessLevel != AccessLevel::Full) return;
if(!_preferences->getBool(preference_keypad_control_enabled))
{
_network->publishKeypadCommandResult("KeypadControlDisabled");
return;
}
if(!_hasKeypad)
{
@@ -742,7 +749,7 @@ void NukiWrapper::setupHASS()
char uidString[20];
itoa(_nukiConfig.nukiId, uidString, 16);
_network->publishHASSConfig("SmartLock", baseTopic.c_str(),(char*)_nukiConfig.name, uidString, hasDoorSensor(), _hasKeypad, _publishAuthData,"lock", "unlock", "unlatch");
_network->publishHASSConfig("SmartLock", baseTopic.c_str(),(char*)_nukiConfig.name, uidString, hasDoorSensor(), _hasKeypad, _publishAuthData, "lock", "unlock", "unlatch");
_hassSetupCompleted = true;
Log->println("HASS setup for lock completed.");