Skip to content

Commit

Permalink
Merge pull request #367 from iranl/code-optional-update-keypad
Browse files Browse the repository at this point in the history
Make the code optional when updating keypad using JSON
  • Loading branch information
technyon committed May 21, 2024
2 parents 8d03cd6 + c320d3b commit 9f00ab7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ To change Nuki Lock/Opener keypad settings set the `keypad/actionJson` topic to
|------------------|----------|----------|----------|------------------------------------------------------------------------------------------------------------------|----------------------------------------|
| action | Required | Required | Required | The action to execute | "delete", "add", "update" |
| codeId | Required | Not used | Required | The code ID of the existing code to delete or update | Integer |
| code | Not used | Required | Required | The code to create or update | 6-digit Integer without zero's, can't start with "12"|
| code | Not used | Required | Optional | The code to create or update | 6-digit Integer without zero's, can't start with "12"|
| enabled | Not used | Not used | Optional | Enable or disable the code, always enabled on add, disabled if not set on update | 1 = enabled, 0 = disabled |
| name | Not used | Required | Required | The name of the code to create or update | String, max 20 chars |
| timeLimited | Not used | Optional | Optional | If this authorization is restricted to access only at certain times, disabled if not set (requires enabled = 1) | 1 = enabled, 0 = disabled |
Expand All @@ -460,7 +460,7 @@ To change Nuki Lock/Opener keypad settings set the `keypad/actionJson` topic to
Examples:
- Delete: `{ "action": "delete", "codeId": "1234" }`
- Add: `{ "action": "add", "code": "589472", "name": "Test", "timeLimited": "1", "allowedFrom": "2024-04-12 10:00:00", "allowedUntil": "2034-04-12 10:00:00", "allowedWeekdays": [ "wed", "thu", "fri" ], "allowedFromTime": "08:00", "allowedUntilTime": "16:00" }`
- Update: `{ "action": "update", "codeId": "1234", "code": "589472", "enabled": "1", "name": "Test", "timeLimited": "1", "allowedFrom": "2024-04-12 10:00:00", "allowedUntil": "2034-04-12 10:00:00", "allowedWeekdays": [ "mon", "tue", "sat", "sun" ], "allowedFromTime": "08:00", "allowedUntilTime": "16:00" }`
- Update: `{ "action": "update", "codeId": "1234", "enabled": "1", "name": "Test", "timeLimited": "1", "allowedFrom": "2024-04-12 10:00:00", "allowedUntil": "2034-04-12 10:00:00", "allowedWeekdays": [ "mon", "tue", "sat", "sun" ], "allowedFromTime": "08:00", "allowedUntilTime": "16:00" }`

### Result of attempted keypad code changes

Expand Down
14 changes: 12 additions & 2 deletions src/NukiOpenerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,12 @@ void NukiOpenerWrapper::updateKeypad()

_keypadCodeIds.clear();
_keypadCodeIds.reserve(entries.size());
_keypadCodes.clear();
_keypadCodes.reserve(entries.size());
for(const auto& entry : entries)
{
_keypadCodeIds.push_back(entry.codeId);
_keypadCodes.push_back(entry.code);
}
}

Expand Down Expand Up @@ -1505,7 +1508,7 @@ void NukiOpenerWrapper::onKeypadJsonCommandReceived(const char *value)
return;
}
}
else
else if (strcmp(action, "update") != 0)
{
_network->publishKeypadJsonCommandResult("noCodeSet");
return;
Expand Down Expand Up @@ -1686,7 +1689,14 @@ void NukiOpenerWrapper::onKeypadJsonCommandReceived(const char *value)
entry.codeId = codeId;
size_t nameLen = strlen(name);
memcpy(&entry.name, name, nameLen > 20 ? 20 : nameLen);
entry.code = code;

if(code) entry.code = code;
else
{
auto it = std::find(_keypadCodeIds.begin(), _keypadCodeIds.end(), codeId);
entry.code = _keypadCodes[(it - _keypadCodeIds.begin())];
}

entry.enabled = enabled == 0 ? 0 : 1;
entry.timeLimited = timeLimited == 1 ? 1 : 0;

Expand Down
1 change: 1 addition & 0 deletions src/NukiOpenerWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class NukiOpenerWrapper : public NukiOpener::SmartlockEventHandler
int _retryLockstateCount = 0;
unsigned long _nextRetryTs = 0;
std::vector<uint16_t> _keypadCodeIds;
std::vector<uint32_t> _keypadCodes;
std::vector<uint8_t> _timeControlIds;

NukiOpener::OpenerState _lastKeyTurnerState;
Expand Down
14 changes: 12 additions & 2 deletions src/NukiWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,12 @@ void NukiWrapper::updateKeypad()

_keypadCodeIds.clear();
_keypadCodeIds.reserve(entries.size());
_keypadCodes.clear();
_keypadCodes.reserve(entries.size());
for(const auto& entry : entries)
{
_keypadCodeIds.push_back(entry.codeId);
_keypadCodes.push_back(entry.code);
}
}

Expand Down Expand Up @@ -1491,7 +1494,7 @@ void NukiWrapper::onKeypadJsonCommandReceived(const char *value)
return;
}
}
else
else if (strcmp(action, "update") != 0)
{
_network->publishKeypadJsonCommandResult("noCodeSet");
return;
Expand Down Expand Up @@ -1672,7 +1675,14 @@ void NukiWrapper::onKeypadJsonCommandReceived(const char *value)
entry.codeId = codeId;
size_t nameLen = strlen(name);
memcpy(&entry.name, name, nameLen > 20 ? 20 : nameLen);
entry.code = code;

if(code) entry.code = code;
else
{
auto it = std::find(_keypadCodeIds.begin(), _keypadCodeIds.end(), codeId);
entry.code = _keypadCodes[(it - _keypadCodeIds.begin())];
}

entry.enabled = enabled == 0 ? 0 : 1;
entry.timeLimited = timeLimited == 1 ? 1 : 0;

Expand Down
1 change: 1 addition & 0 deletions src/NukiWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class NukiWrapper : public Nuki::SmartlockEventHandler
bool _publishAuthData = false;
bool _clearAuthData = false;
std::vector<uint16_t> _keypadCodeIds;
std::vector<uint32_t> _keypadCodes;
std::vector<uint8_t> _timeControlIds;

NukiLock::KeyTurnerState _lastKeyTurnerState;
Expand Down

0 comments on commit 9f00ab7

Please sign in to comment.