add keypad update command

This commit is contained in:
technyon
2022-08-12 19:33:56 +02:00
parent 9fed6c0ce8
commit ac751fd526
5 changed files with 44 additions and 14 deletions

View File

@@ -31,6 +31,7 @@
#define mqtt_topic_keypad_command_id "/keypad/command/id" #define mqtt_topic_keypad_command_id "/keypad/command/id"
#define mqtt_topic_keypad_command_name "/keypad/command/name" #define mqtt_topic_keypad_command_name "/keypad/command/name"
#define mqtt_topic_keypad_command_code "/keypad/command/code" #define mqtt_topic_keypad_command_code "/keypad/command/code"
#define mqtt_topic_keypad_command_enabled "/keypad/command/enabled"
#define mqtt_topic_presence "/presence/devices" #define mqtt_topic_presence "/presence/devices"

View File

@@ -58,10 +58,12 @@ void NetworkLock::initialize()
_network->subscribe(_mqttPath, mqtt_topic_keypad_command_id); _network->subscribe(_mqttPath, mqtt_topic_keypad_command_id);
_network->subscribe(_mqttPath, mqtt_topic_keypad_command_name); _network->subscribe(_mqttPath, mqtt_topic_keypad_command_name);
_network->subscribe(_mqttPath, mqtt_topic_keypad_command_code); _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_action, "--");
_network->initTopic(_mqttPath, mqtt_topic_keypad_command_id, "0"); _network->initTopic(_mqttPath, mqtt_topic_keypad_command_id, "0");
_network->initTopic(_mqttPath, mqtt_topic_keypad_command_name, "--"); _network->initTopic(_mqttPath, mqtt_topic_keypad_command_name, "--");
_network->initTopic(_mqttPath, mqtt_topic_keypad_command_code, "000000"); _network->initTopic(_mqttPath, mqtt_topic_keypad_command_code, "000000");
_network->initTopic(_mqttPath, mqtt_topic_keypad_command_enabled, "1");
} }
void NetworkLock::update() void NetworkLock::update()
@@ -111,11 +113,17 @@ void NetworkLock::onMqttDataReceived(char *&topic, byte *&payload, unsigned int
{ {
if(_keypadCommandReceivedReceivedCallback != nullptr) 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, "--"); publishString(mqtt_topic_keypad_command_action, "--");
publishInt(mqtt_topic_keypad_command_id, 0); publishInt(mqtt_topic_keypad_command_id, _keypadCommandId);
publishString(mqtt_topic_keypad_command_name, "--"); publishString(mqtt_topic_keypad_command_name, _keypadCommandName.c_str());
publishString(mqtt_topic_keypad_command_code, "000000"); 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)) else if(comparePrefixedPath(topic, mqtt_topic_keypad_command_id))
@@ -130,6 +138,10 @@ void NetworkLock::onMqttDataReceived(char *&topic, byte *&payload, unsigned int
{ {
_keypadCommandCode = value; _keypadCommandCode = value;
} }
else if(comparePrefixedPath(topic, mqtt_topic_keypad_command_enabled))
{
_keypadCommandEnabled = atoi(value);
}
for(auto configTopic : _configTopics) for(auto configTopic : _configTopics)
{ {
@@ -284,7 +296,7 @@ void NetworkLock::setConfigUpdateReceivedCallback(void (*configUpdateReceivedCal
_configUpdateReceivedCallback = configUpdateReceivedCallback; _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; _keypadCommandReceivedReceivedCallback = keypadCommandReceivedReceivedCallback;
} }
@@ -358,7 +370,6 @@ bool NetworkLock::publishString(const char *topic, const char *value)
void NetworkLock::publishKeypadEntry(const String topic, NukiLock::KeypadEntry entry) void NetworkLock::publishKeypadEntry(const String topic, NukiLock::KeypadEntry entry)
{ {
char codeName[sizeof(entry.name) + 1]; char codeName[sizeof(entry.name) + 1];
memset(codeName, 0, sizeof(codeName)); memset(codeName, 0, sizeof(codeName));
memcpy(codeName, entry.name, sizeof(entry.name)); memcpy(codeName, entry.name, sizeof(entry.name));

View File

@@ -33,7 +33,7 @@ public:
void setLockActionReceivedCallback(bool (*lockActionReceivedCallback)(const char* value)); void setLockActionReceivedCallback(bool (*lockActionReceivedCallback)(const char* value));
void setConfigUpdateReceivedCallback(void (*configUpdateReceivedCallback)(const char* path, 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; void onMqttDataReceived(char*& topic, byte*& payload, unsigned int& length) override;
@@ -64,8 +64,9 @@ private:
String _keypadCommandName = ""; String _keypadCommandName = "";
String _keypadCommandCode = ""; String _keypadCommandCode = "";
uint _keypadCommandId = 0; uint _keypadCommandId = 0;
int _keypadCommandEnabled = 1;
bool (*_lockActionReceivedCallback)(const char* value) = nullptr; bool (*_lockActionReceivedCallback)(const char* value) = nullptr;
void (*_configUpdateReceivedCallback)(const char* path, 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;
}; };

View File

@@ -285,10 +285,9 @@ void NukiWrapper::onConfigUpdateReceivedCallback(const char *topic, const char *
} }
void NukiWrapper::onKeypadCommandReceivedCallback(const char *command, const uint &id, const String &name, void NukiWrapper::onKeypadCommandReceivedCallback(const char *command, const uint &id, const String &name, const String &code, const int& enabled)
const String &code)
{ {
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) if(strcmp(command, "add") == 0)
{ {
NukiLock::NewKeypadEntry entry; 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); Serial.print("Delete keypad code: "); Serial.println((int)r);
updateKeypad(); 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() const NukiLock::KeyTurnerState &NukiWrapper::keyTurnerState()

View File

@@ -32,9 +32,9 @@ public:
private: private:
static bool onLockActionReceivedCallback(const char* value); static bool onLockActionReceivedCallback(const char* value);
static void onConfigUpdateReceivedCallback(const char* topic, 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 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 updateKeyTurnerState();
void updateBatteryState(); void updateBatteryState();