diff --git a/MqttTopics.h b/MqttTopics.h index a342aac..a00474a 100644 --- a/MqttTopics.h +++ b/MqttTopics.h @@ -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" diff --git a/NetworkLock.cpp b/NetworkLock.cpp index c54a63e..5fb4c55 100644 --- a/NetworkLock.cpp +++ b/NetworkLock.cpp @@ -288,7 +288,7 @@ void NetworkLock::publishKeypad(const std::list& 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 *)) diff --git a/NukiWrapper.cpp b/NukiWrapper.cpp index 101f556..30176b5 100644 --- a/NukiWrapper.cpp +++ b/NukiWrapper.cpp @@ -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"); } } diff --git a/NukiWrapper.h b/NukiWrapper.h index b4b7fb5..4e7f047 100644 --- a/NukiWrapper.h +++ b/NukiWrapper.h @@ -61,6 +61,7 @@ private: int _intervalKeypad = 0; // seconds bool _publishAuthData = false; bool _clearAuthData = false; + std::vector _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; };