add keypad command sanity checks

This commit is contained in:
technyon
2022-08-12 23:17:03 +02:00
parent 54d4d8b341
commit 0060acefce
4 changed files with 29 additions and 7 deletions

View File

@@ -32,7 +32,7 @@
#define mqtt_topic_keypad_command_name "/keypad/command/name"
#define mqtt_topic_keypad_command_code "/keypad/command/code"
#define mqtt_topic_keypad_command_enabled "/keypad/command/enabled"
#define mqtt_topic_keypad_command_enabled "/keypad/command/commandResult"
#define mqtt_topic_keypad_command_result "/keypad/command/commandResult"
#define mqtt_topic_presence "/presence/devices"

View File

@@ -288,7 +288,7 @@ void NetworkLock::publishKeypad(const std::list<NukiLock::KeypadEntry>& entries,
void NetworkLock::publishKeypadCommandResult(const char* result)
{
publishString(mqtt_topic_keypad_command_enabled, result);
publishString(mqtt_topic_keypad_command_result, result);
}
void NetworkLock::setLockActionReceivedCallback(bool (*lockActionReceivedCallback)(const char *))

View File

@@ -262,6 +262,13 @@ void NukiWrapper::updateKeypad()
}
_network->publishKeypad(entries, _maxKeypadCodeCount);
_keypadCodeIds.clear();
_keypadCodeIds.reserve(entries.size());
for(const auto& entry : entries)
{
_keypadCodeIds.push_back(entry.codeId);
}
}
}
@@ -358,12 +365,14 @@ void NukiWrapper::onKeypadCommandReceived(const char *command, const uint &id, c
return;
}
bool idExists = std::find(_keypadCodeIds.begin(), _keypadCodeIds.end(), id) != _keypadCodeIds.end();
NukiLock::CmdResult result = (NukiLock::CmdResult)-1;
if(strcmp(command, "add") == 0)
{
if(name == "")
{
_network->publishKeypadCommandResult("MissingParameterName");
return;
}
@@ -378,14 +387,25 @@ void NukiWrapper::onKeypadCommandReceived(const char *command, const uint &id, c
}
else if(strcmp(command, "delete") == 0)
{
if(!idExists)
{
_network->publishKeypadCommandResult("UnknownId");
return;
}
result = _nukiLock.deleteKeypadEntry(id);
Serial.print("Delete keypad code: "); Serial.println((int)result);
updateKeypad();
}
if(strcmp(command, "update") == 0)
else if(strcmp(command, "update") == 0)
{
if(name == "")
{
_network->publishKeypadCommandResult("MissingParameterName");
return;
}
if(!idExists)
{
_network->publishKeypadCommandResult("UnknownId");
return;
}
@@ -400,6 +420,11 @@ void NukiWrapper::onKeypadCommandReceived(const char *command, const uint &id, c
Serial.print("Update keypad code: "); Serial.println((int)result);
updateKeypad();
}
else
{
_network->publishKeypadCommandResult("UnknownCommand");
return;
}
if((int)result != -1)
{
@@ -407,9 +432,6 @@ void NukiWrapper::onKeypadCommandReceived(const char *command, const uint &id, c
memset(&resultStr, 0, sizeof(resultStr));
NukiLock::cmdResultToString(result, resultStr);
_network->publishKeypadCommandResult(resultStr);
} else
{
_network->publishKeypadCommandResult("UnknownCommand");
}
}

View File

@@ -61,6 +61,7 @@ private:
int _intervalKeypad = 0; // seconds
bool _publishAuthData = false;
bool _clearAuthData = false;
std::vector<uint16_t> _keypadCodeIds;
NukiLock::KeyTurnerState _lastKeyTurnerState;
NukiLock::KeyTurnerState _keyTurnerState;
@@ -84,6 +85,5 @@ private:
unsigned long _nextBatteryReportTs = 0;
unsigned long _nextConfigUpdateTs = 0;
unsigned long _nextKeypadUpdateTs = 0;
unsigned long _nextPairTs = 0;
NukiLock::LockAction _nextLockAction = (NukiLock::LockAction)0xff;
};