From ac751fd5268f46859a99847e5f70a520cf34410a Mon Sep 17 00:00:00 2001 From: technyon Date: Fri, 12 Aug 2022 19:33:56 +0200 Subject: [PATCH] add keypad update command --- MqttTopics.h | 1 + NetworkLock.cpp | 23 +++++++++++++++++------ NetworkLock.h | 5 +++-- NukiWrapper.cpp | 25 +++++++++++++++++++++---- NukiWrapper.h | 4 ++-- 5 files changed, 44 insertions(+), 14 deletions(-) diff --git a/MqttTopics.h b/MqttTopics.h index 8917aa4..010412e 100644 --- a/MqttTopics.h +++ b/MqttTopics.h @@ -31,6 +31,7 @@ #define mqtt_topic_keypad_command_id "/keypad/command/id" #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_presence "/presence/devices" diff --git a/NetworkLock.cpp b/NetworkLock.cpp index 606af6c..a9a350c 100644 --- a/NetworkLock.cpp +++ b/NetworkLock.cpp @@ -58,10 +58,12 @@ void NetworkLock::initialize() _network->subscribe(_mqttPath, mqtt_topic_keypad_command_id); _network->subscribe(_mqttPath, mqtt_topic_keypad_command_name); _network->subscribe(_mqttPath, mqtt_topic_keypad_command_code); + _network->subscribe(_mqttPath, mqtt_topic_keypad_command_enabled); _network->initTopic(_mqttPath, mqtt_topic_keypad_command_action, "--"); _network->initTopic(_mqttPath, mqtt_topic_keypad_command_id, "0"); _network->initTopic(_mqttPath, mqtt_topic_keypad_command_name, "--"); _network->initTopic(_mqttPath, mqtt_topic_keypad_command_code, "000000"); + _network->initTopic(_mqttPath, mqtt_topic_keypad_command_enabled, "1"); } void NetworkLock::update() @@ -111,11 +113,17 @@ void NetworkLock::onMqttDataReceived(char *&topic, byte *&payload, unsigned int { if(_keypadCommandReceivedReceivedCallback != nullptr) { - _keypadCommandReceivedReceivedCallback(value, _keypadCommandId, _keypadCommandName, _keypadCommandCode); + _keypadCommandReceivedReceivedCallback(value, _keypadCommandId, _keypadCommandName, _keypadCommandCode, _keypadCommandEnabled); + + _keypadCommandId = 0; + _keypadCommandName = "--"; + _keypadCommandCode = "000000"; + _keypadCommandEnabled = 1; publishString(mqtt_topic_keypad_command_action, "--"); - publishInt(mqtt_topic_keypad_command_id, 0); - publishString(mqtt_topic_keypad_command_name, "--"); - publishString(mqtt_topic_keypad_command_code, "000000"); + publishInt(mqtt_topic_keypad_command_id, _keypadCommandId); + publishString(mqtt_topic_keypad_command_name, _keypadCommandName.c_str()); + publishString(mqtt_topic_keypad_command_code, _keypadCommandCode.c_str()); + publishInt(mqtt_topic_keypad_command_enabled, _keypadCommandEnabled); } } else if(comparePrefixedPath(topic, mqtt_topic_keypad_command_id)) @@ -130,6 +138,10 @@ void NetworkLock::onMqttDataReceived(char *&topic, byte *&payload, unsigned int { _keypadCommandCode = value; } + else if(comparePrefixedPath(topic, mqtt_topic_keypad_command_enabled)) + { + _keypadCommandEnabled = atoi(value); + } for(auto configTopic : _configTopics) { @@ -284,7 +296,7 @@ void NetworkLock::setConfigUpdateReceivedCallback(void (*configUpdateReceivedCal _configUpdateReceivedCallback = configUpdateReceivedCallback; } -void NetworkLock::setKeypadCommandReceivedCallback(void (*keypadCommandReceivedReceivedCallback)(const char* command, const uint& id, const String& name, const String& code)) +void NetworkLock::setKeypadCommandReceivedCallback(void (*keypadCommandReceivedReceivedCallback)(const char* command, const uint& id, const String& name, const String& code, const int& enabled)) { _keypadCommandReceivedReceivedCallback = keypadCommandReceivedReceivedCallback; } @@ -358,7 +370,6 @@ bool NetworkLock::publishString(const char *topic, const char *value) void NetworkLock::publishKeypadEntry(const String topic, NukiLock::KeypadEntry entry) { - char codeName[sizeof(entry.name) + 1]; memset(codeName, 0, sizeof(codeName)); memcpy(codeName, entry.name, sizeof(entry.name)); diff --git a/NetworkLock.h b/NetworkLock.h index 629e302..ff4a03f 100644 --- a/NetworkLock.h +++ b/NetworkLock.h @@ -33,7 +33,7 @@ public: void setLockActionReceivedCallback(bool (*lockActionReceivedCallback)(const char* value)); void setConfigUpdateReceivedCallback(void (*configUpdateReceivedCallback)(const char* path, const char* value)); - void setKeypadCommandReceivedCallback(void (*keypadCommandReceivedReceivedCallback)(const char* command, const uint& id, const String& name, const String& code)); + void setKeypadCommandReceivedCallback(void (*keypadCommandReceivedReceivedCallback)(const char* command, const uint& id, const String& name, const String& code, const int& enabled)); void onMqttDataReceived(char*& topic, byte*& payload, unsigned int& length) override; @@ -64,8 +64,9 @@ private: String _keypadCommandName = ""; String _keypadCommandCode = ""; uint _keypadCommandId = 0; + int _keypadCommandEnabled = 1; bool (*_lockActionReceivedCallback)(const char* value) = nullptr; void (*_configUpdateReceivedCallback)(const char* path, const char* value) = nullptr; - void (*_keypadCommandReceivedReceivedCallback)(const char* command, const uint& id, const String& name, const String& code) = nullptr; + void (*_keypadCommandReceivedReceivedCallback)(const char* command, const uint& id, const String& name, const String& code, const int& enabled) = nullptr; }; diff --git a/NukiWrapper.cpp b/NukiWrapper.cpp index 392be65..7feff6b 100644 --- a/NukiWrapper.cpp +++ b/NukiWrapper.cpp @@ -285,10 +285,9 @@ void NukiWrapper::onConfigUpdateReceivedCallback(const char *topic, const char * } -void NukiWrapper::onKeypadCommandReceivedCallback(const char *command, const uint &id, const String &name, - const String &code) +void NukiWrapper::onKeypadCommandReceivedCallback(const char *command, const uint &id, const String &name, const String &code, const int& enabled) { - nukiInst->onKeypadCommandReceived(command, id, name, code); + nukiInst->onKeypadCommandReceived(command, id, name, code, enabled); } @@ -345,8 +344,13 @@ void NukiWrapper::onConfigUpdateReceived(const char *topic, const char *value) } } -void NukiWrapper::onKeypadCommandReceived(const char *command, const uint &id, const String &name, const String &code) +void NukiWrapper::onKeypadCommandReceived(const char *command, const uint &id, const String &name, const String &code, const int& enabled) { + if(!_hasKeypad) + { + return; + } + if(strcmp(command, "add") == 0) { NukiLock::NewKeypadEntry entry; @@ -364,6 +368,19 @@ void NukiWrapper::onKeypadCommandReceived(const char *command, const uint &id, c Serial.print("Delete keypad code: "); Serial.println((int)r); updateKeypad(); } + if(strcmp(command, "update") == 0) + { + NukiLock::UpdatedKeypadEntry entry; + memset(&entry, 0, sizeof(entry)); + entry.codeId = id; + size_t nameLen = name.length(); + memcpy(&entry.name, name.c_str(), nameLen > 20 ? 20 : nameLen); + entry.code = code.toInt(); + entry.enabled = enabled == 0 ? 0 : 1; + const auto r = _nukiLock.updateKeypadEntry(entry); + Serial.print("Update keypad code: "); Serial.println((int)r); + updateKeypad(); + } } const NukiLock::KeyTurnerState &NukiWrapper::keyTurnerState() diff --git a/NukiWrapper.h b/NukiWrapper.h index 358fccc..48addc6 100644 --- a/NukiWrapper.h +++ b/NukiWrapper.h @@ -32,9 +32,9 @@ public: private: static bool onLockActionReceivedCallback(const char* value); static void onConfigUpdateReceivedCallback(const char* topic, const char* value); - static void onKeypadCommandReceivedCallback(const char* command, const uint& id, const String& name, const String& code); + static void onKeypadCommandReceivedCallback(const char* command, const uint& id, const String& name, const String& code, const int& enabled); void onConfigUpdateReceived(const char* topic, const char* value); - void onKeypadCommandReceived(const char* command, const uint& id, const String& name, const String& code); + void onKeypadCommandReceived(const char* command, const uint& id, const String& name, const String& code, const int& enabled); void updateKeyTurnerState(); void updateBatteryState();