From d9bed88ca6889425892de5dd09657f2788b34c50 Mon Sep 17 00:00:00 2001 From: iranl Date: Sat, 13 Apr 2024 21:45:12 +0200 Subject: [PATCH 1/2] Add Time Control --- MqttTopics.h | 4 + NetworkLock.cpp | 100 +++++++++++++++++++++ NetworkLock.h | 5 +- NetworkOpener.cpp | 102 +++++++++++++++++++++- NetworkOpener.h | 5 +- NukiOpenerWrapper.cpp | 190 ++++++++++++++++++++++++++++++++++++++++ NukiOpenerWrapper.h | 4 + NukiWrapper.cpp | 196 ++++++++++++++++++++++++++++++++++++++++++ NukiWrapper.h | 4 + PreferencesKeys.h | 7 +- README.md | 30 +++++++ WebCfgServer.cpp | 13 +++ 12 files changed, 655 insertions(+), 5 deletions(-) diff --git a/MqttTopics.h b/MqttTopics.h index 903042a..bc41a0a 100644 --- a/MqttTopics.h +++ b/MqttTopics.h @@ -52,6 +52,10 @@ #define mqtt_topic_keypad_command_result "/keypad/command/commandResult" #define mqtt_topic_keypad_json "/keypad/json" +#define mqtt_topic_timecontrol_json "/timecontrol/json" +#define mqtt_topic_timecontrol_action "/timecontrol/action" +#define mqtt_topic_timecontrol_command_result "/timecontrol/commandResult" + #define mqtt_topic_info_hardware_version "/info/hardwareVersion" #define mqtt_topic_info_firmware_version "/info/firmwareVersion" #define mqtt_topic_info_nuki_hub_version "/info/nukiHubVersion" diff --git a/NetworkLock.cpp b/NetworkLock.cpp index f6faf69..270e3b7 100644 --- a/NetworkLock.cpp +++ b/NetworkLock.cpp @@ -82,6 +82,12 @@ void NetworkLock::initialize() _network->initTopic(_mqttPath, mqtt_topic_query_keypad, "0"); } + if(_preferences->getBool(preference_timecontrol_control_enabled)) + { + _network->subscribe(_mqttPath, mqtt_topic_timecontrol_action); + _network->initTopic(_mqttPath, mqtt_topic_timecontrol_action, "--"); + } + _network->addReconnectedCallback([&]() { _reconnected = true; @@ -204,6 +210,18 @@ void NetworkLock::onMqttDataReceived(const char* topic, byte* payload, const uns } } } + + if(comparePrefixedPath(topic, mqtt_topic_timecontrol_action)) + { + if(strcmp(value, "") == 0 || strcmp(value, "--") == 0) return; + + if(_timeControlCommandReceivedReceivedCallback != NULL) + { + _timeControlCommandReceivedReceivedCallback(value); + } + + publishString(mqtt_topic_timecontrol_action, "--"); + } } void NetworkLock::publishKeyTurnerState(const NukiLock::KeyTurnerState& keyTurnerState, const NukiLock::KeyTurnerState& lastKeyTurnerState) @@ -535,11 +553,88 @@ void NetworkLock::publishKeypad(const std::list& entries, } } +void NetworkLock::publishTimeControl(const std::list& entries) +{ + char str[50]; + JsonDocument json; + + for(const auto& entry : entries) + { + auto jsonEntry = json.add(); + + jsonEntry["entryId"] = entry.entryId; + jsonEntry["enabled"] = entry.enabled; + uint8_t weekdaysInt = entry.weekdays; + JsonArray weekdays = jsonEntry["weekdays"].to(); + + while(weekdaysInt > 0) { + if(weekdaysInt >= 64) + { + weekdays.add("mon"); + weekdaysInt -= 64; + continue; + } + if(weekdaysInt >= 32) + { + weekdays.add("tue"); + weekdaysInt -= 32; + continue; + } + if(weekdaysInt >= 16) + { + weekdays.add("wed"); + weekdaysInt -= 16; + continue; + } + if(weekdaysInt >= 8) + { + weekdays.add("thu"); + weekdaysInt -= 8; + continue; + } + if(weekdaysInt >= 4) + { + weekdays.add("fri"); + weekdaysInt -= 4; + continue; + } + if(weekdaysInt >= 2) + { + weekdays.add("sat"); + weekdaysInt -= 2; + continue; + } + if(weekdaysInt >= 1) + { + weekdays.add("sun"); + weekdaysInt -= 1; + continue; + } + } + + char timeT[5]; + sprintf(timeT, "%02d:%02d", entry.timeHour, entry.timeMin); + jsonEntry["time"] = timeT; + + memset(str, 0, sizeof(str)); + NukiLock::lockactionToString(entry.lockAction, str); + jsonEntry["lockAction"] = str; + } + + serializeJson(json, _buffer, _bufferSize); + publishString(mqtt_topic_timecontrol_json, _buffer); +} + void NetworkLock::publishKeypadCommandResult(const char* result) { publishString(mqtt_topic_keypad_command_result, result); } +void NetworkLock::publishTimeControlCommandResult(const char* result) +{ + publishString(mqtt_topic_timecontrol_command_result, result); +} + void NetworkLock::setLockActionReceivedCallback(LockActionResult (*lockActionReceivedCallback)(const char *)) { _lockActionReceivedCallback = lockActionReceivedCallback; @@ -555,6 +650,11 @@ void NetworkLock::setKeypadCommandReceivedCallback(void (*keypadCommandReceivedR _keypadCommandReceivedReceivedCallback = keypadCommandReceivedReceivedCallback; } +void NetworkLock::setTimeControlCommandReceivedCallback(void (*timeControlCommandReceivedReceivedCallback)(const char *)) +{ + _timeControlCommandReceivedReceivedCallback = timeControlCommandReceivedReceivedCallback; +} + void NetworkLock::buildMqttPath(const char* path, char* outPath) { int offset = 0; diff --git a/NetworkLock.h b/NetworkLock.h index 977aaba..7db15ed 100644 --- a/NetworkLock.h +++ b/NetworkLock.h @@ -37,12 +37,14 @@ public: void publishHASSConfig(char* deviceType, const char* baseTopic, char* name, char* uidString, const bool& hasDoorSensor, const bool& hasKeypad, const bool& publishAuthData, char* lockAction, char* unlockAction, char* openAction); void removeHASSConfig(char* uidString); void publishKeypad(const std::list& entries, uint maxKeypadCodeCount); + void publishTimeControl(const std::list& entries); void publishKeypadCommandResult(const char* result); + void publishTimeControlCommandResult(const char* result); void setLockActionReceivedCallback(LockActionResult (*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, const int& enabled)); - + void setTimeControlCommandReceivedCallback(void (*timeControlCommandReceivedReceivedCallback)(const char* value)); void onMqttDataReceived(const char* topic, byte* payload, const unsigned int length) override; bool reconnected(); @@ -90,4 +92,5 @@ private: LockActionResult (*_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, const int& enabled) = nullptr; + void (*_timeControlCommandReceivedReceivedCallback)(const char* value) = nullptr; }; diff --git a/NetworkOpener.cpp b/NetworkOpener.cpp index 04817be..e927b5d 100644 --- a/NetworkOpener.cpp +++ b/NetworkOpener.cpp @@ -70,6 +70,12 @@ void NetworkOpener::initialize() _network->initTopic(_mqttPath, mqtt_topic_query_keypad, "0"); } + if(_preferences->getBool(preference_timecontrol_control_enabled)) + { + _network->subscribe(_mqttPath, mqtt_topic_timecontrol_action); + _network->initTopic(_mqttPath, mqtt_topic_timecontrol_action, "--"); + } + _network->addReconnectedCallback([&]() { _reconnected = true; @@ -193,6 +199,18 @@ void NetworkOpener::onMqttDataReceived(const char* topic, byte* payload, const u } } } + + if(comparePrefixedPath(topic, mqtt_topic_timecontrol_action)) + { + if(strcmp(value, "") == 0 || strcmp(value, "--") == 0) return; + + if(_timeControlCommandReceivedReceivedCallback != NULL) + { + _timeControlCommandReceivedReceivedCallback(value); + } + + publishString(mqtt_topic_timecontrol_action, "--"); + } } void NetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& keyTurnerState, const NukiOpener::OpenerState& lastKeyTurnerState) @@ -282,7 +300,7 @@ void NetworkOpener::publishRing(const bool locked) { publishString(mqtt_topic_lock_ring, "ring"); } - + publishString(mqtt_topic_lock_binary_ring, "ring"); _resetRingStateTs = millis() + 2000; } @@ -591,11 +609,88 @@ void NetworkOpener::publishKeypad(const std::list& entrie } } +void NetworkOpener::publishTimeControl(const std::list& entries) +{ + char str[50]; + JsonDocument json; + + for(const auto& entry : entries) + { + auto jsonEntry = json.add(); + + jsonEntry["entryId"] = entry.entryId; + jsonEntry["enabled"] = entry.enabled; + uint8_t weekdaysInt = entry.weekdays; + JsonArray weekdays = jsonEntry["weekdays"].to(); + + while(weekdaysInt > 0) { + if(weekdaysInt >= 64) + { + weekdays.add("mon"); + weekdaysInt -= 64; + continue; + } + if(weekdaysInt >= 32) + { + weekdays.add("tue"); + weekdaysInt -= 32; + continue; + } + if(weekdaysInt >= 16) + { + weekdays.add("wed"); + weekdaysInt -= 16; + continue; + } + if(weekdaysInt >= 8) + { + weekdays.add("thu"); + weekdaysInt -= 8; + continue; + } + if(weekdaysInt >= 4) + { + weekdays.add("fri"); + weekdaysInt -= 4; + continue; + } + if(weekdaysInt >= 2) + { + weekdays.add("sat"); + weekdaysInt -= 2; + continue; + } + if(weekdaysInt >= 1) + { + weekdays.add("sun"); + weekdaysInt -= 1; + continue; + } + } + + char timeT[5]; + sprintf(timeT, "%02d:%02d", entry.timeHour, entry.timeMin); + jsonEntry["time"] = timeT; + + memset(str, 0, sizeof(str)); + NetworkOpener::lockactionToString(entry.lockAction, str); + jsonEntry["lockAction"] = str; + } + + serializeJson(json, _buffer, _bufferSize); + publishString(mqtt_topic_timecontrol_json, _buffer); +} + void NetworkOpener::publishKeypadCommandResult(const char* result) { publishString(mqtt_topic_keypad_command_result, result); } +void NetworkOpener::publishTimeControlCommandResult(const char* result) +{ + publishString(mqtt_topic_timecontrol_command_result, result); +} + void NetworkOpener::setLockActionReceivedCallback(LockActionResult (*lockActionReceivedCallback)(const char *)) { _lockActionReceivedCallback = lockActionReceivedCallback; @@ -611,6 +706,11 @@ void NetworkOpener::setKeypadCommandReceivedCallback(void (*keypadCommandReceive _keypadCommandReceivedReceivedCallback = keypadCommandReceivedReceivedCallback; } +void NetworkOpener::setTimeControlCommandReceivedCallback(void (*timeControlCommandReceivedReceivedCallback)(const char *)) +{ + _timeControlCommandReceivedReceivedCallback = timeControlCommandReceivedReceivedCallback; +} + void NetworkOpener::publishFloat(const char *topic, const float value, const uint8_t precision) { _network->publishFloat(_mqttPath, topic, value, precision); diff --git a/NetworkOpener.h b/NetworkOpener.h index ec84dc7..1b97788 100644 --- a/NetworkOpener.h +++ b/NetworkOpener.h @@ -34,12 +34,14 @@ public: void publishHASSConfig(char* deviceType, const char* baseTopic, char* name, char* uidString, char* lockAction, char* unlockAction, char* openAction); void removeHASSConfig(char* uidString); void publishKeypad(const std::list& entries, uint maxKeypadCodeCount); + void publishTimeControl(const std::list& entries); void publishKeypadCommandResult(const char* result); + void publishTimeControlCommandResult(const char* result); void setLockActionReceivedCallback(LockActionResult (*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, const int& enabled)); - + void setTimeControlCommandReceivedCallback(void (*timeControlCommandReceivedReceivedCallback)(const char* value)); void onMqttDataReceived(const char* topic, byte* payload, const unsigned int length) override; bool reconnected(); @@ -93,4 +95,5 @@ private: LockActionResult (*_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, const int& enabled) = nullptr; + void (*_timeControlCommandReceivedReceivedCallback)(const char* value) = nullptr; }; diff --git a/NukiOpenerWrapper.cpp b/NukiOpenerWrapper.cpp index b1f5aa5..4b14fe7 100644 --- a/NukiOpenerWrapper.cpp +++ b/NukiOpenerWrapper.cpp @@ -412,6 +412,8 @@ void NukiOpenerWrapper::updateConfig() _hardwareVersion = std::to_string(_nukiConfig.hardwareRevision[0]) + "." + std::to_string(_nukiConfig.hardwareRevision[1]); _network->publishConfig(_nukiConfig); _retryConfigCount = 0; + + if(_preferences->getBool(preference_timecontrol_info_enabled)) updateTimeControl(); } else { @@ -505,6 +507,31 @@ void NukiOpenerWrapper::updateKeypad() postponeBleWatchdog(); } +void NukiOpenerWrapper::updateTimeControl() +{ + if(!_preferences->getBool(preference_timecontrol_info_enabled)) return; + + Log->print(F("Querying lock time control: ")); + Nuki::CmdResult result = _nukiOpener.retrieveTimeControlEntries(); + printCommandResult(result); + if(result == Nuki::CmdResult::Success) + { + std::list entries; + _nukiOpener.getTimeControlEntries(&entries); + + entries.sort([](const NukiLock::TimeControlEntry& a, const NukiLock::TimeControlEntry& b) { return a.entryId < b.entryId; }); + + _timeControlIds.clear(); + _timeControlIds.reserve(entries.size()); + for(const auto& entry : entries) + { + _timeControlIds.push_back(entry.entryId); + } + } + + postponeBleWatchdog(); +} + void NukiOpenerWrapper::postponeBleWatchdog() { _disableBleWatchdogTs = millis() + 15000; @@ -726,6 +753,169 @@ void NukiOpenerWrapper::onKeypadCommandReceived(const char *command, const uint } } +void NukiOpenerWrapper::onTimeControlCommandReceived(const char *value) +{ + if(_nukiLock.getSecurityPincode() == 0) + { + _network->publishTimeControlCommandResult("noPinSet"); + return; + } + + if(!_preferences->getBool(preference_timecontrol_control_enabled)) + { + _network->publishTimeControlCommandResult("timeControlControlDisabled"); + return; + } + + JsonDocument json; + DeserializationError jsonError = deserializeJson(json, value); + + if(jsonError) + { + _network->publishTimeControlCommandResult("invalidJson"); + return; + } + + Nuki::CmdResult result = (Nuki::CmdResult)-1; + + const char *action = json["action"].as(); + uint8_t entryId = json["entryId"].as(); + uint8_t enabled = json["enabled"].as(); + String weekdays = json["weekdays"].as(); + const char *time = json["time"].as(); + NukiOpener::LockAction timeControlLockAction = nukiOpenerInst->lockActionToEnum(json["lockAction"].as()); + + if((int)timeControlLockAction == 0xff) + { + _network->publishTimeControlCommandResult("invalidLockAction"); + return; + } + + if(action) + { + bool idExists = false; + + if(entryId) + { + idExists = std::find(_timeControlIds.begin(), _timeControlIds.end(), entryId) != _timeControlIds.end(); + } + + if(strcmp(action, "delete") == 0) { + if(idExists) + { + result = _nukiOpener.removeTimeControlEntry(entryId); + Log->print("Delete time control "); + Log->println((int)result); + } + else + { + _network->publishTimeControlCommandResult("noExistingEntryIdSet"); + return; + } + } + else if(strcmp(action, "add") == 0 || strcmp(action, "update") == 0) + { + uint8_t timeHour; + uint8_t timeMin; + uint8_t weekdaysInt = 0; + unsigned int timeAr[2]; + + if(time) + { + if(strlen(time) == 5) + { + String timeStr = time; + timeAr[0] = (uint8_t)timeStr.substring(0, 2).toInt(); + timeAr[1] = (uint8_t)timeStr.substring(3, 5).toInt(); + + if(timeAr[0] < 0 || timeAr[0] > 23 || timeAr[1] < 0 || timeAr[1] > 59) + { + _network->publishKeypadJsonCommandResult("invalidTime"); + return; + } + } + else + { + _network->publishKeypadJsonCommandResult("invalidTime"); + return; + } + } + else + { + _network->publishKeypadJsonCommandResult("invalidTime"); + return; + } + + if(weekdays.indexOf("mon") >= 0) weekdaysInt += 64; + if(weekdays.indexOf("tue") >= 0) weekdaysInt += 32; + if(weekdays.indexOf("wed") >= 0) weekdaysInt += 16; + if(weekdays.indexOf("thu") >= 0) weekdaysInt += 8; + if(weekdays.indexOf("fri") >= 0) weekdaysInt += 4; + if(weekdays.indexOf("sat") >= 0) weekdaysInt += 2; + if(weekdays.indexOf("sun") >= 0) weekdaysInt += 1; + + if(strcmp(action, "add") == 0) + { + NukiLock::NewTimeControlEntry entry; + memset(&entry, 0, sizeof(entry)); + entry.weekdays = weekdaysInt; + + if(time) + { + entry.timeHour = timeAr[0]; + entry.timeMin = timeAr[1]; + } + + entry.lockAction = timeControlLockAction; + + result = _nukiOpener.addKeypadEntry(entry); + Log->print("Add time control: "); + Log->println((int)result); + } + else if (strcmp(action, "update") == 0) + { + NukiLock::TimeControlEntry entry; + memset(&entry, 0, sizeof(entry)); + entry.entryId = entryId; + entry.enabled = enabled == 0 ? 0 : 1; + entry.weekdays = weekdaysInt; + + if(time) + { + entry.timeHour = timeAr[0]; + entry.timeMin = timeAr[1]; + } + + entry.lockAction = timeControlLockAction; + + result = _nukiOpener.updateTimeControlEntry(entry); + Log->print("Update time control: "); + Log->println((int)result); + } + } + else + { + _network->publishTimeControlCommandResult("invalidAction"); + return; + } + + if((int)result != -1) + { + char resultStr[15]; + memset(&resultStr, 0, sizeof(resultStr)); + NukiLock::cmdResultToString(result, resultStr); + _network->publishTimeControlCommandResult(resultStr); + } + + _nextConfigUpdateTs = millis() + 300; + } + else + { + _network->publishTimeControlCommandResult("noActionSet"); + return; + } +} + const NukiOpener::OpenerState &NukiOpenerWrapper::keyTurnerState() { return _keyTurnerState; diff --git a/NukiOpenerWrapper.h b/NukiOpenerWrapper.h index 657fe2b..ddaf642 100644 --- a/NukiOpenerWrapper.h +++ b/NukiOpenerWrapper.h @@ -49,15 +49,18 @@ private: static LockActionResult 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, const int& enabled); + static void onTimeControlCommandReceivedCallback(const char* value); static void gpioActionCallback(const GpioAction& action, const int& pin); void onConfigUpdateReceived(const char* topic, const char* value); void onKeypadCommandReceived(const char* command, const uint& id, const String& name, const String& code, const int& enabled); + void onTimeControlCommandReceived(const char* value); void updateKeyTurnerState(); void updateBatteryState(); void updateConfig(); void updateAuthData(); void updateKeypad(); + void updateTimeControl(); void postponeBleWatchdog(); void updateGpioOutputs(); @@ -92,6 +95,7 @@ private: int _retryLockstateCount = 0; unsigned long _nextRetryTs = 0; std::vector _keypadCodeIds; + std::vector _timeControlIds; NukiOpener::OpenerState _lastKeyTurnerState; NukiOpener::OpenerState _keyTurnerState; diff --git a/NukiWrapper.cpp b/NukiWrapper.cpp index 35a4c3f..3f73d76 100644 --- a/NukiWrapper.cpp +++ b/NukiWrapper.cpp @@ -32,6 +32,7 @@ NukiWrapper::NukiWrapper(const std::string& deviceName, NukiDeviceId* deviceId, network->setLockActionReceivedCallback(nukiInst->onLockActionReceivedCallback); network->setConfigUpdateReceivedCallback(nukiInst->onConfigUpdateReceivedCallback); network->setKeypadCommandReceivedCallback(nukiInst->onKeypadCommandReceivedCallback); + network->setTimeControlCommandReceivedCallback(nukiInst->onTimeControlCommandReceivedCallback); _gpio->addCallback(NukiWrapper::gpioActionCallback); } @@ -378,6 +379,8 @@ void NukiWrapper::updateConfig() _hardwareVersion = std::to_string(_nukiConfig.hardwareRevision[0]) + "." + std::to_string(_nukiConfig.hardwareRevision[1]); _network->publishConfig(_nukiConfig); _retryConfigCount = 0; + + if(_preferences->getBool(preference_timecontrol_info_enabled)) updateTimeControl(); } else { @@ -471,6 +474,31 @@ void NukiWrapper::updateKeypad() postponeBleWatchdog(); } +void NukiWrapper::updateTimeControl() +{ + if(!_preferences->getBool(preference_timecontrol_info_enabled)) return; + + Log->print(F("Querying lock time control: ")); + Nuki::CmdResult result = _nukiLock.retrieveTimeControlEntries(); + printCommandResult(result); + if(result == Nuki::CmdResult::Success) + { + std::list entries; + _nukiLock.getTimeControlEntries(&entries); + + entries.sort([](const NukiLock::TimeControlEntry& a, const NukiLock::TimeControlEntry& b) { return a.entryId < b.entryId; }); + + _timeControlIds.clear(); + _timeControlIds.reserve(entries.size()); + for(const auto& entry : entries) + { + _timeControlIds.push_back(entry.entryId); + } + } + + postponeBleWatchdog(); +} + void NukiWrapper::postponeBleWatchdog() { _disableBleWatchdogTs = millis() + 15000; @@ -525,6 +553,11 @@ void NukiWrapper::onKeypadCommandReceivedCallback(const char *command, const uin nukiInst->onKeypadCommandReceived(command, id, name, code, enabled); } +void NukiWrapper::onTimeControlCommandReceivedCallback(const char *value) +{ + nukiInst->onTimeControlCommandReceived(value); +} + void NukiWrapper::gpioActionCallback(const GpioAction &action, const int& pin) { switch(action) @@ -712,6 +745,169 @@ void NukiWrapper::onKeypadCommandReceived(const char *command, const uint &id, c } } +void NukiWrapper::onTimeControlCommandReceived(const char *value) +{ + if(_nukiLock.getSecurityPincode() == 0) + { + _network->publishTimeControlCommandResult("noPinSet"); + return; + } + + if(!_preferences->getBool(preference_timecontrol_control_enabled)) + { + _network->publishTimeControlCommandResult("timeControlControlDisabled"); + return; + } + + JsonDocument json; + DeserializationError jsonError = deserializeJson(json, value); + + if(jsonError) + { + _network->publishTimeControlCommandResult("invalidJson"); + return; + } + + Nuki::CmdResult result = (Nuki::CmdResult)-1; + + const char *action = json["action"].as(); + uint8_t entryId = json["entryId"].as(); + uint8_t enabled = json["enabled"].as(); + String weekdays = json["weekdays"].as(); + const char *time = json["time"].as(); + NukiLock::LockAction timeControlLockAction = nukiInst->lockActionToEnum(json["lockAction"].as()); + + if((int)timeControlLockAction == 0xff) + { + _network->publishTimeControlCommandResult("invalidLockAction"); + return; + } + + if(action) + { + bool idExists = false; + + if(entryId) + { + idExists = std::find(_timeControlIds.begin(), _timeControlIds.end(), entryId) != _timeControlIds.end(); + } + + if(strcmp(action, "delete") == 0) { + if(idExists) + { + result = _nukiLock.removeTimeControlEntry(entryId); + Log->print("Delete time control "); + Log->println((int)result); + } + else + { + _network->publishTimeControlCommandResult("noExistingEntryIdSet"); + return; + } + } + else if(strcmp(action, "add") == 0 || strcmp(action, "update") == 0) + { + uint8_t timeHour; + uint8_t timeMin; + uint8_t weekdaysInt = 0; + unsigned int timeAr[2]; + + if(time) + { + if(strlen(time) == 5) + { + String timeStr = time; + timeAr[0] = (uint8_t)timeStr.substring(0, 2).toInt(); + timeAr[1] = (uint8_t)timeStr.substring(3, 5).toInt(); + + if(timeAr[0] < 0 || timeAr[0] > 23 || timeAr[1] < 0 || timeAr[1] > 59) + { + _network->publishKeypadJsonCommandResult("invalidTime"); + return; + } + } + else + { + _network->publishKeypadJsonCommandResult("invalidTime"); + return; + } + } + else + { + _network->publishKeypadJsonCommandResult("invalidTime"); + return; + } + + if(weekdays.indexOf("mon") >= 0) weekdaysInt += 64; + if(weekdays.indexOf("tue") >= 0) weekdaysInt += 32; + if(weekdays.indexOf("wed") >= 0) weekdaysInt += 16; + if(weekdays.indexOf("thu") >= 0) weekdaysInt += 8; + if(weekdays.indexOf("fri") >= 0) weekdaysInt += 4; + if(weekdays.indexOf("sat") >= 0) weekdaysInt += 2; + if(weekdays.indexOf("sun") >= 0) weekdaysInt += 1; + + if(strcmp(action, "add") == 0) + { + NukiLock::NewTimeControlEntry entry; + memset(&entry, 0, sizeof(entry)); + entry.weekdays = weekdaysInt; + + if(time) + { + entry.timeHour = timeAr[0]; + entry.timeMin = timeAr[1]; + } + + entry.lockAction = timeControlLockAction; + + result = _nukiLock.addKeypadEntry(entry); + Log->print("Add time control: "); + Log->println((int)result); + } + else if (strcmp(action, "update") == 0) + { + NukiLock::TimeControlEntry entry; + memset(&entry, 0, sizeof(entry)); + entry.entryId = entryId; + entry.enabled = enabled == 0 ? 0 : 1; + entry.weekdays = weekdaysInt; + + if(time) + { + entry.timeHour = timeAr[0]; + entry.timeMin = timeAr[1]; + } + + entry.lockAction = timeControlLockAction; + + result = _nukiLock.updateTimeControlEntry(entry); + Log->print("Update time control: "); + Log->println((int)result); + } + } + else + { + _network->publishTimeControlCommandResult("invalidAction"); + return; + } + + if((int)result != -1) + { + char resultStr[15]; + memset(&resultStr, 0, sizeof(resultStr)); + NukiLock::cmdResultToString(result, resultStr); + _network->publishTimeControlCommandResult(resultStr); + } + + _nextConfigUpdateTs = millis() + 300; + } + else + { + _network->publishTimeControlCommandResult("noActionSet"); + return; + } +} + const NukiLock::KeyTurnerState &NukiWrapper::keyTurnerState() { return _keyTurnerState; diff --git a/NukiWrapper.h b/NukiWrapper.h index 2f68988..6cc9531 100644 --- a/NukiWrapper.h +++ b/NukiWrapper.h @@ -47,16 +47,19 @@ private: static LockActionResult 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, const int& enabled); + static void onTimeControlCommandReceivedCallback(const char* value); static void gpioActionCallback(const GpioAction& action, const int& pin); void onConfigUpdateReceived(const char* topic, const char* value); void onKeypadCommandReceived(const char* command, const uint& id, const String& name, const String& code, const int& enabled); + void onTimeControlCommandReceived(const char* value); void updateKeyTurnerState(); void updateBatteryState(); void updateConfig(); void updateAuthData(); void updateKeypad(); + void updateTimeControl(); void postponeBleWatchdog(); void updateGpioOutputs(); @@ -85,6 +88,7 @@ private: bool _publishAuthData = false; bool _clearAuthData = false; std::vector _keypadCodeIds; + std::vector _timeControlIds; NukiLock::KeyTurnerState _lastKeyTurnerState; NukiLock::KeyTurnerState _keyTurnerState; diff --git a/PreferencesKeys.h b/PreferencesKeys.h index 18edb41..7bcb049 100644 --- a/PreferencesKeys.h +++ b/PreferencesKeys.h @@ -48,6 +48,8 @@ #define preference_admin_enabled "aclConfig" #define preference_keypad_info_enabled "kpInfoEnabled" #define preference_keypad_control_enabled "kpCntrlEnabled" +#define preference_timecontrol_control_enabled "tcCntrlEnabled" +#define preference_timecontrol_info_enabled "tcInfoEnabled" #define preference_publish_authdata "pubAuth" #define preference_acl "aclLckOpn" #define preference_register_as_app "regAsApp" // true = register as hub; false = register as app @@ -77,9 +79,10 @@ private: preference_ip_dhcp_enabled, preference_ip_address, preference_ip_subnet, preference_ip_gateway, preference_ip_dns_server, preference_network_hardware, preference_network_wifi_fallback_disabled, preference_rssi_publish_interval, preference_hostname, preference_network_timeout, preference_restart_on_disconnect, - preference_restart_ble_beacon_lost, preference_query_interval_lockstate, + preference_restart_ble_beacon_lost, preference_query_interval_lockstate, preference_query_interval_configuration, preference_query_interval_battery, preference_query_interval_keypad, preference_keypad_control_enabled, preference_admin_enabled, preference_keypad_info_enabled, preference_acl, + preference_timecontrol_control_enabled, preference_timecontrol_info_enabled, preference_access_level, preference_register_as_app, preference_command_nr_of_retries, preference_command_retry_delay, preference_cred_user, preference_cred_password, preference_publish_authdata, preference_publish_debug_info, preference_presence_detection_timeout, @@ -96,7 +99,7 @@ private: { preference_started_before, preference_mqtt_log_enabled, preference_check_updates, preference_lock_enabled, preference_opener_enabled, preference_opener_continuous_mode, preference_restart_on_disconnect, preference_keypad_control_enabled, preference_admin_enabled, preference_keypad_info_enabled, - preference_register_as_app, preference_ip_dhcp_enabled, + preference_timecontrol_control_enabled, preference_timecontrol_info_enabled, preference_register_as_app, preference_ip_dhcp_enabled, preference_publish_authdata, preference_has_mac_saved, preference_publish_debug_info, preference_network_wifi_fallback_disabled }; diff --git a/README.md b/README.md index f8d13db..da18bda 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,8 @@ In a browser navigate to the IP address assigned to the ESP32. - Change Lock/Opener configuration: Allows changing the Nuki Lock/Opener configuration through MQTT. - Publish keypad codes information (Only available when a Keypad is detected): Enable to publish information about keypad codes through MQTT, see the "Keypad control" section of this README - Add, modify and delete keypad codes (Only available when a Keypad is detected): Enable to allow configuration of keypad codes through MQTT, see the "Keypad control" section of this README +- Publish time control information: Enable to publish information about time control entries through MQTT, see the "Time control" section of this README +- Add, modify and delete time control entries: Enable to allow configuration of time control entries through MQTT, see the "Time control" section of this README - Publish auth data: Enable to publish authorization data to the MQTT topic lock/log. Requires the Nuki security code / PIN to be set, see "Nuki Lock PIN / Nuki Opener PIN" below. #### Nuki Lock/Opener Access Control @@ -252,6 +254,10 @@ In a browser navigate to the IP address assigned to the ESP32. - See the "Keypad control" section of this README. +### Time Control + +- See the "Time control" section of this README. + ### Info - info/nukiHubVersion: Set to the current version number of the Nuki Hub firmware. @@ -343,6 +349,30 @@ For example, to add a code: - write 1 to enabled - write "add" to action +## Time control using JSON (optional) + +Time control entries can be added, updated and removed. This has to enabled first in the configuration portal. Check "Add, modify and delete time control entries" under "Access Level Configuration" and save the configuration. + +Information about current time control entries is published as JSON data to the "timecontrol/json" MQTT topic.
+This needs to be enabled separately by checking "Publish time control entries information" under "Access Level Configuration" and saving the configuration. + +To change Nuki Lock/Opener time control settings set the `timecontrol/actionJson` topic to a JSON formatted value containing the following nodes. + +| Node | Delete | Add | Update | Usage | Possible values | +|------------------|----------|----------|----------|------------------------------------------------------------------------------------------|----------------------------------------------------------------| +| action | Required | Required | Required | The action to execute | "delete", "add", "update" | +| entryId | Required | Not used | Required | The entry ID of the existing entry to delete or update | Integer | +| enabled | Not used | Not used | Optional | Enable or disable the entry, enabled if not set | 1 = enabled, 0 = disabled | +| weekdays | Not used | Optional | Optional | Weekdays on which the chosen lock action should be exectued | Array of days: "mon", "tue", "wed", "thu" , "fri" "sat", "sun" | +| time | Not used | Required | Required | The time on which the chosen lock action should be executed | "HH:MM" | +| lockaction | Not used | Required | Required | The lock action that should be executed on the chosen weekdays at the chosen time | For the Nuki lock: "Unlock", "Lock", "Unlatch", "LockNgo", "LockNgoUnlatch", "FullLock", "FobAction1", "FobAction2", "FobAction3. For the Nuki Opener: "ActivateRTO", "DeactivateRTO", "ElectricStrikeActuation", "ActivateCM", "DeactivateCM", "FobAction1", "FobAction2", "FobAction3" | + +Example usage:
+Examples: +- Delete: `{ "action": "delete", "entryId": "1234" }` +- Add: `{ "action": "add", "weekdays": [ "wed", "thu", "fri" ], "time": "08:00", "lockaction": "unlock" }` +- Update: `{ "action": "update", "entryId": "1234", "enabled": "1", "weekdays": [ "mon", "tue", "sat", "sun" ], "time": "08:00", "lockaction": "lock" }` + ## GPIO lock control (optional) The lock can be controlled via GPIO.
diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index c7c5389..25168b4 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -480,6 +480,16 @@ bool WebCfgServer::processArgs(String& message) _preferences->putBool(preference_keypad_control_enabled, (value == "1")); configChanged = true; } + else if(key == "TCPUB") + { + _preferences->putBool(preference_timecontrol_info_enabled, (value == "1")); + configChanged = true; + } + else if(key == "TCENA") + { + _preferences->putBool(preference_timecontrol_control_enabled, (value == "1")); + configChanged = true; + } else if(key == "PUBAUTH") { _preferences->putBool(preference_publish_authdata, (value == "1")); @@ -944,6 +954,9 @@ void WebCfgServer::buildAccLvlHtml(String &response) printCheckBox(response, "KPPUB", "Publish keypad codes information", _preferences->getBool(preference_keypad_info_enabled)); printCheckBox(response, "KPENA", "Add, modify and delete keypad codes", _preferences->getBool(preference_keypad_control_enabled)); } + + printCheckBox(response, "TCPUB", "Publish time control entries information", _preferences->getBool(preference_timecontrol_info_enabled)); + printCheckBox(response, "TCENA", "Add, modify and delete time control entries", _preferences->getBool(preference_timecontrol_control_enabled)); printCheckBox(response, "PUBAUTH", "Publish authorisation log (may reduce battery life)", _preferences->getBool(preference_publish_authdata)); response.concat("
"); if(_nuki != nullptr) From ab8841c81f7b58ec2db35b20b9ef53dea6ce689a Mon Sep 17 00:00:00 2001 From: iranl Date: Sun, 21 Apr 2024 14:01:58 +0200 Subject: [PATCH 2/2] Time Control --- NetworkLock.cpp | 4 +- NetworkLock.h | 2 +- NetworkOpener.cpp | 16 +++--- NetworkOpener.h | 2 +- NukiOpenerWrapper.cpp | 113 ++++++++++++++++++++++++++++-------------- NukiOpenerWrapper.h | 3 +- NukiWrapper.cpp | 106 ++++++++++++++++++++++++++------------- NukiWrapper.h | 3 +- README.md | 6 +-- 9 files changed, 167 insertions(+), 88 deletions(-) diff --git a/NetworkLock.cpp b/NetworkLock.cpp index 42beb14..21ed905 100644 --- a/NetworkLock.cpp +++ b/NetworkLock.cpp @@ -629,12 +629,12 @@ void NetworkLock::publishKeypad(const std::list& entries, } } -void NetworkLock::publishTimeControl(const std::list& entries) +void NetworkLock::publishTimeControl(const std::list& timeControlEntries) { char str[50]; JsonDocument json; - for(const auto& entry : entries) + for(const auto& entry : timeControlEntries) { auto jsonEntry = json.add(); diff --git a/NetworkLock.h b/NetworkLock.h index 1100fed..f259f38 100644 --- a/NetworkLock.h +++ b/NetworkLock.h @@ -37,7 +37,7 @@ public: void publishHASSConfig(char* deviceType, const char* baseTopic, char* name, char* uidString, const bool& hasDoorSensor, const bool& hasKeypad, const bool& publishAuthData, char* lockAction, char* unlockAction, char* openAction); void removeHASSConfig(char* uidString); void publishKeypad(const std::list& entries, uint maxKeypadCodeCount); - void publishTimeControl(const std::list& entries); + void publishTimeControl(const std::list& timeControlEntries); void publishKeypadCommandResult(const char* result); void publishKeypadJsonCommandResult(const char* result); void publishTimeControlCommandResult(const char* result); diff --git a/NetworkOpener.cpp b/NetworkOpener.cpp index 2d11225..1daa850 100644 --- a/NetworkOpener.cpp +++ b/NetworkOpener.cpp @@ -398,24 +398,24 @@ void NetworkOpener::publishAuthorizationInfo(const std::list& entrie } } -void NetworkOpener::publishTimeControl(const std::list& entries) +void NetworkOpener::publishTimeControl(const std::list& timeControlEntries) { char str[50]; JsonDocument json; - for(const auto& entry : entries) + for(const auto& entry : timeControlEntries) { auto jsonEntry = json.add(); @@ -749,7 +749,7 @@ void NetworkOpener::publishTimeControl(const std::list& entries, uint maxKeypadCodeCount); - void publishTimeControl(const std::list& entries); + void publishTimeControl(const std::list& timeControlEntries); void publishKeypadCommandResult(const char* result); void publishKeypadJsonCommandResult(const char* result); void publishTimeControlCommandResult(const char* result); diff --git a/NukiOpenerWrapper.cpp b/NukiOpenerWrapper.cpp index 84eebd3..f934bf5 100644 --- a/NukiOpenerWrapper.cpp +++ b/NukiOpenerWrapper.cpp @@ -172,6 +172,11 @@ void NukiOpenerWrapper::update() setupHASS(); } } + if(_nextTimeControlUpdateTs != 0 && ts > _nextTimeControlUpdateTs) + { + _nextTimeControlUpdateTs = 0; + updateTimeControl(true); + } if(_hassEnabled && _configRead && _network->reconnected()) { setupHASS(); @@ -407,8 +412,8 @@ void NukiOpenerWrapper::updateConfig() _network->publishConfig(_nukiConfig); _retryConfigCount = 0; - if(_preferences->getBool(preference_timecontrol_info_enabled)) updateTimeControl(); - + if(_preferences->getBool(preference_timecontrol_info_enabled)) updateTimeControl(false); + const int pinStatus = _preferences->getInt(preference_opener_pin_status, 4); if(isPinSet()) { @@ -503,6 +508,9 @@ void NukiOpenerWrapper::updateKeypad() { std::list entries; _nukiOpener.getKeypadEntries(&entries); + + Log->print(F("Opener keypad codes: ")); + Log->println(entries.size()); entries.sort([](const NukiLock::KeypadEntry& a, const NukiLock::KeypadEntry& b) { return a.codeId < b.codeId; }); @@ -526,23 +534,35 @@ void NukiOpenerWrapper::updateKeypad() postponeBleWatchdog(); } -void NukiOpenerWrapper::updateTimeControl() +void NukiOpenerWrapper::updateTimeControl(bool retrieved) { if(!_preferences->getBool(preference_timecontrol_info_enabled)) return; - Log->print(F("Querying lock time control: ")); - Nuki::CmdResult result = _nukiOpener.retrieveTimeControlEntries(); - printCommandResult(result); - if(result == Nuki::CmdResult::Success) + if(!retrieved) { - std::list entries; - _nukiOpener.getTimeControlEntries(&entries); + Log->print(F("Querying opener time control: ")); + Nuki::CmdResult result = _nukiOpener.retrieveTimeControlEntries(); + printCommandResult(result); + if(result == Nuki::CmdResult::Success) + { + _nextTimeControlUpdateTs = millis() + 5000; + } + } + else + { + std::list timeControlEntries; + _nukiOpener.getTimeControlEntries(&timeControlEntries); + + Log->print(F("Opener time control entries: ")); + Log->println(timeControlEntries.size()); - entries.sort([](const NukiLock::TimeControlEntry& a, const NukiLock::TimeControlEntry& b) { return a.entryId < b.entryId; }); + timeControlEntries.sort([](const NukiOpener::TimeControlEntry& a, const NukiOpener::TimeControlEntry& b) { return a.entryId < b.entryId; }); + + _network->publishTimeControl(timeControlEntries); _timeControlIds.clear(); - _timeControlIds.reserve(entries.size()); - for(const auto& entry : entries) + _timeControlIds.reserve(timeControlEntries.size()); + for(const auto& entry : timeControlEntries) { _timeControlIds.push_back(entry.entryId); } @@ -558,24 +578,31 @@ void NukiOpenerWrapper::postponeBleWatchdog() NukiOpener::LockAction NukiOpenerWrapper::lockActionToEnum(const char *str) { - if(strcmp(str, "activateRTO") == 0) return NukiOpener::LockAction::ActivateRTO; - else if(strcmp(str, "deactivateRTO") == 0) return NukiOpener::LockAction::DeactivateRTO; - else if(strcmp(str, "electricStrikeActuation") == 0) return NukiOpener::LockAction::ElectricStrikeActuation; - else if(strcmp(str, "activateCM") == 0) return NukiOpener::LockAction::ActivateCM; - else if(strcmp(str, "deactivateCM") == 0) return NukiOpener::LockAction::DeactivateCM; - else if(strcmp(str, "fobAction2") == 0) return NukiOpener::LockAction::FobAction2; - else if(strcmp(str, "fobAction1") == 0) return NukiOpener::LockAction::FobAction1; - else if(strcmp(str, "fobAction3") == 0) return NukiOpener::LockAction::FobAction3; + if(strcmp(str, "activateRTO") == 0 || strcmp(str, "ActivateRTO") == 0) return NukiOpener::LockAction::ActivateRTO; + else if(strcmp(str, "deactivateRTO") == 0 || strcmp(str, "DeactivateRTO") == 0) return NukiOpener::LockAction::DeactivateRTO; + else if(strcmp(str, "electricStrikeActuation") == 0 || strcmp(str, "ElectricStrikeActuation") == 0) return NukiOpener::LockAction::ElectricStrikeActuation; + else if(strcmp(str, "activateCM") == 0 || strcmp(str, "ActivateCM") == 0) return NukiOpener::LockAction::ActivateCM; + else if(strcmp(str, "deactivateCM") == 0 || strcmp(str, "DeactivateCM") == 0) return NukiOpener::LockAction::DeactivateCM; + else if(strcmp(str, "fobAction2") == 0 || strcmp(str, "FobAction2") == 0) return NukiOpener::LockAction::FobAction2; + else if(strcmp(str, "fobAction1") == 0 || strcmp(str, "FobAction1") == 0) return NukiOpener::LockAction::FobAction1; + else if(strcmp(str, "fobAction3") == 0 || strcmp(str, "FobAction3") == 0) return NukiOpener::LockAction::FobAction3; return (NukiOpener::LockAction)0xff; } LockActionResult NukiOpenerWrapper::onLockActionReceivedCallback(const char *value) { - NukiOpener::LockAction action = nukiOpenerInst->lockActionToEnum(value); - if((int)action == 0xff) + NukiOpener::LockAction action; + + if(strlen(value) > 0) { - return LockActionResult::UnknownAction; + action = nukiOpenerInst->lockActionToEnum(value); + + if((int)action == 0xff) + { + return LockActionResult::UnknownAction; + } } + else return LockActionResult::UnknownAction; nukiOpenerPreferences = new Preferences(); nukiOpenerPreferences->begin("nukihub", true); @@ -822,7 +849,7 @@ void NukiOpenerWrapper::onKeypadJsonCommandReceived(const char *value) return; } - DynamicJsonDocument json(2048); + JsonDocument json; DeserializationError jsonError = deserializeJson(json, value); if(jsonError) @@ -991,7 +1018,7 @@ void NukiOpenerWrapper::onKeypadJsonCommandReceived(const char *value) return; } } - + if(allowedWeekdays.indexOf("mon") >= 0) allowedWeekdaysInt += 64; if(allowedWeekdays.indexOf("tue") >= 0) allowedWeekdaysInt += 32; if(allowedWeekdays.indexOf("wed") >= 0) allowedWeekdaysInt += 16; @@ -1000,7 +1027,7 @@ void NukiOpenerWrapper::onKeypadJsonCommandReceived(const char *value) if(allowedWeekdays.indexOf("sat") >= 0) allowedWeekdaysInt += 2; if(allowedWeekdays.indexOf("sun") >= 0) allowedWeekdaysInt += 1; } - + if(strcmp(action, "add") == 0) { NukiOpener::NewKeypadEntry entry; @@ -1123,7 +1150,7 @@ void NukiOpenerWrapper::onKeypadJsonCommandReceived(const char *value) void NukiOpenerWrapper::onTimeControlCommandReceived(const char *value) { - if(_nukiLock.getSecurityPincode() == 0) + if(_nukiOpener.getSecurityPincode() == 0) { _network->publishTimeControlCommandResult("noPinSet"); return; @@ -1151,9 +1178,21 @@ void NukiOpenerWrapper::onTimeControlCommandReceived(const char *value) uint8_t enabled = json["enabled"].as(); String weekdays = json["weekdays"].as(); const char *time = json["time"].as(); - NukiOpener::LockAction timeControlLockAction = nukiOpenerInst->lockActionToEnum(json["lockAction"].as()); + const char *lockAct = json["lockAction"].as(); + NukiOpener::LockAction timeControlLockAction; - if((int)timeControlLockAction == 0xff) + if(strlen(lockAct) > 0) + { + + timeControlLockAction = nukiOpenerInst->lockActionToEnum(lockAct); + + if((int)timeControlLockAction == 0xff) + { + _network->publishTimeControlCommandResult("invalidLockAction"); + return; + } + } + else { _network->publishTimeControlCommandResult("invalidLockAction"); return; @@ -1198,19 +1237,19 @@ void NukiOpenerWrapper::onTimeControlCommandReceived(const char *value) if(timeAr[0] < 0 || timeAr[0] > 23 || timeAr[1] < 0 || timeAr[1] > 59) { - _network->publishKeypadJsonCommandResult("invalidTime"); + _network->publishTimeControlCommandResult("invalidTime"); return; } } else { - _network->publishKeypadJsonCommandResult("invalidTime"); + _network->publishTimeControlCommandResult("invalidTime"); return; } } else { - _network->publishKeypadJsonCommandResult("invalidTime"); + _network->publishTimeControlCommandResult("invalidTime"); return; } @@ -1224,7 +1263,7 @@ void NukiOpenerWrapper::onTimeControlCommandReceived(const char *value) if(strcmp(action, "add") == 0) { - NukiLock::NewTimeControlEntry entry; + NukiOpener::NewTimeControlEntry entry; memset(&entry, 0, sizeof(entry)); entry.weekdays = weekdaysInt; @@ -1236,13 +1275,13 @@ void NukiOpenerWrapper::onTimeControlCommandReceived(const char *value) entry.lockAction = timeControlLockAction; - result = _nukiOpener.addKeypadEntry(entry); + result = _nukiOpener.addTimeControlEntry(entry); Log->print("Add time control: "); Log->println((int)result); } else if (strcmp(action, "update") == 0) { - NukiLock::TimeControlEntry entry; + NukiOpener::TimeControlEntry entry; memset(&entry, 0, sizeof(entry)); entry.entryId = entryId; entry.enabled = enabled == 0 ? 0 : 1; @@ -1271,10 +1310,10 @@ void NukiOpenerWrapper::onTimeControlCommandReceived(const char *value) { char resultStr[15]; memset(&resultStr, 0, sizeof(resultStr)); - NukiLock::cmdResultToString(result, resultStr); + NukiOpener::cmdResultToString(result, resultStr); _network->publishTimeControlCommandResult(resultStr); } - + _nextConfigUpdateTs = millis() + 300; } else diff --git a/NukiOpenerWrapper.h b/NukiOpenerWrapper.h index d0a21d1..b37f8f5 100644 --- a/NukiOpenerWrapper.h +++ b/NukiOpenerWrapper.h @@ -62,7 +62,7 @@ private: void updateConfig(); void updateAuthData(); void updateKeypad(); - void updateTimeControl(); + void updateTimeControl(bool retrieved); void postponeBleWatchdog(); void updateGpioOutputs(); @@ -122,6 +122,7 @@ private: unsigned long _nextLockStateUpdateTs = 0; unsigned long _nextBatteryReportTs = 0; unsigned long _nextConfigUpdateTs = 0; + unsigned long _nextTimeControlUpdateTs = 0; unsigned long _nextKeypadUpdateTs = 0; unsigned long _nextPairTs = 0; long _nextRssiTs = 0; diff --git a/NukiWrapper.cpp b/NukiWrapper.cpp index 4b5a6c6..81fbe90 100644 --- a/NukiWrapper.cpp +++ b/NukiWrapper.cpp @@ -184,6 +184,11 @@ void NukiWrapper::update() setupHASS(); } } + if(_nextTimeControlUpdateTs != 0 && ts > _nextTimeControlUpdateTs) + { + _nextTimeControlUpdateTs = 0; + updateTimeControl(true); + } if(_hassEnabled && _configRead && _network->reconnected()) { setupHASS(); @@ -381,8 +386,8 @@ void NukiWrapper::updateConfig() _network->publishConfig(_nukiConfig); _retryConfigCount = 0; - if(_preferences->getBool(preference_timecontrol_info_enabled)) updateTimeControl(); - + if(_preferences->getBool(preference_timecontrol_info_enabled)) updateTimeControl(false); + const int pinStatus = _preferences->getInt(preference_lock_pin_status, 4); if(isPinSet()) { @@ -477,6 +482,9 @@ void NukiWrapper::updateKeypad() { std::list entries; _nukiLock.getKeypadEntries(&entries); + + Log->print(F("Lock keypad codes: ")); + Log->println(entries.size()); entries.sort([](const NukiLock::KeypadEntry& a, const NukiLock::KeypadEntry& b) { return a.codeId < b.codeId; }); @@ -500,28 +508,40 @@ void NukiWrapper::updateKeypad() postponeBleWatchdog(); } -void NukiWrapper::updateTimeControl() +void NukiWrapper::updateTimeControl(bool retrieved) { if(!_preferences->getBool(preference_timecontrol_info_enabled)) return; - Log->print(F("Querying lock time control: ")); - Nuki::CmdResult result = _nukiLock.retrieveTimeControlEntries(); - printCommandResult(result); - if(result == Nuki::CmdResult::Success) + if(!retrieved) { - std::list entries; - _nukiLock.getTimeControlEntries(&entries); + Log->print(F("Querying lock time control: ")); + Nuki::CmdResult result = _nukiLock.retrieveTimeControlEntries(); + printCommandResult(result); + if(result == Nuki::CmdResult::Success) + { + _nextTimeControlUpdateTs = millis() + 5000; + } + } + else + { + std::list timeControlEntries; + _nukiLock.getTimeControlEntries(&timeControlEntries); + + Log->print(F("Lock time control entries: ")); + Log->println(timeControlEntries.size()); - entries.sort([](const NukiLock::TimeControlEntry& a, const NukiLock::TimeControlEntry& b) { return a.entryId < b.entryId; }); + timeControlEntries.sort([](const NukiLock::TimeControlEntry& a, const NukiLock::TimeControlEntry& b) { return a.entryId < b.entryId; }); + + _network->publishTimeControl(timeControlEntries); _timeControlIds.clear(); - _timeControlIds.reserve(entries.size()); - for(const auto& entry : entries) + _timeControlIds.reserve(timeControlEntries.size()); + for(const auto& entry : timeControlEntries) { _timeControlIds.push_back(entry.entryId); } } - + postponeBleWatchdog(); } @@ -532,26 +552,32 @@ void NukiWrapper::postponeBleWatchdog() NukiLock::LockAction NukiWrapper::lockActionToEnum(const char *str) { - if(strcmp(str, "unlock") == 0) return NukiLock::LockAction::Unlock; - else if(strcmp(str, "lock") == 0) return NukiLock::LockAction::Lock; - else if(strcmp(str, "unlatch") == 0) return NukiLock::LockAction::Unlatch; - else if(strcmp(str, "lockNgo") == 0) return NukiLock::LockAction::LockNgo; - else if(strcmp(str, "lockNgoUnlatch") == 0) return NukiLock::LockAction::LockNgoUnlatch; - else if(strcmp(str, "fullLock") == 0) return NukiLock::LockAction::FullLock; - else if(strcmp(str, "fobAction2") == 0) return NukiLock::LockAction::FobAction2; - else if(strcmp(str, "fobAction1") == 0) return NukiLock::LockAction::FobAction1; - else if(strcmp(str, "fobAction3") == 0) return NukiLock::LockAction::FobAction3; + if(strcmp(str, "unlock") == 0 || strcmp(str, "Unlock") == 0) return NukiLock::LockAction::Unlock; + else if(strcmp(str, "lock") == 0 || strcmp(str, "Lock") == 0) return NukiLock::LockAction::Lock; + else if(strcmp(str, "unlatch") == 0 || strcmp(str, "Unlatch") == 0) return NukiLock::LockAction::Unlatch; + else if(strcmp(str, "lockNgo") == 0 || strcmp(str, "LockNgo") == 0) return NukiLock::LockAction::LockNgo; + else if(strcmp(str, "lockNgoUnlatch") == 0 || strcmp(str, "LockNgoUnlatch") == 0) return NukiLock::LockAction::LockNgoUnlatch; + else if(strcmp(str, "fullLock") == 0 || strcmp(str, "FullLock") == 0) return NukiLock::LockAction::FullLock; + else if(strcmp(str, "fobAction2") == 0 || strcmp(str, "FobAction2") == 0) return NukiLock::LockAction::FobAction2; + else if(strcmp(str, "fobAction1") == 0 || strcmp(str, "FobAction1") == 0) return NukiLock::LockAction::FobAction1; + else if(strcmp(str, "fobAction3") == 0 || strcmp(str, "FobAction3") == 0) return NukiLock::LockAction::FobAction3; return (NukiLock::LockAction)0xff; } LockActionResult NukiWrapper::onLockActionReceivedCallback(const char *value) { - NukiLock::LockAction action = nukiInst->lockActionToEnum(value); - - if((int)action == 0xff) + NukiLock::LockAction action; + + if(strlen(value) > 0) { - return LockActionResult::UnknownAction; + action = nukiInst->lockActionToEnum(value); + + if((int)action == 0xff) + { + return LockActionResult::UnknownAction; + } } + else return LockActionResult::UnknownAction; nukiLockPreferences = new Preferences(); nukiLockPreferences->begin("nukihub", true); @@ -821,7 +847,7 @@ void NukiWrapper::onKeypadJsonCommandReceived(const char *value) return; } - DynamicJsonDocument json(2048); + JsonDocument json; DeserializationError jsonError = deserializeJson(json, value); if(jsonError) @@ -1150,9 +1176,21 @@ void NukiWrapper::onTimeControlCommandReceived(const char *value) uint8_t enabled = json["enabled"].as(); String weekdays = json["weekdays"].as(); const char *time = json["time"].as(); - NukiLock::LockAction timeControlLockAction = nukiInst->lockActionToEnum(json["lockAction"].as()); + const char *lockAct = json["lockAction"].as(); + NukiLock::LockAction timeControlLockAction; - if((int)timeControlLockAction == 0xff) + if(strlen(lockAct) > 0) + { + + timeControlLockAction = nukiInst->lockActionToEnum(lockAct); + + if((int)timeControlLockAction == 0xff) + { + _network->publishTimeControlCommandResult("invalidLockAction"); + return; + } + } + else { _network->publishTimeControlCommandResult("invalidLockAction"); return; @@ -1197,19 +1235,19 @@ void NukiWrapper::onTimeControlCommandReceived(const char *value) if(timeAr[0] < 0 || timeAr[0] > 23 || timeAr[1] < 0 || timeAr[1] > 59) { - _network->publishKeypadJsonCommandResult("invalidTime"); + _network->publishTimeControlCommandResult("invalidTime"); return; } } else { - _network->publishKeypadJsonCommandResult("invalidTime"); + _network->publishTimeControlCommandResult("invalidTime"); return; } } else { - _network->publishKeypadJsonCommandResult("invalidTime"); + _network->publishTimeControlCommandResult("invalidTime"); return; } @@ -1235,7 +1273,7 @@ void NukiWrapper::onTimeControlCommandReceived(const char *value) entry.lockAction = timeControlLockAction; - result = _nukiLock.addKeypadEntry(entry); + result = _nukiLock.addTimeControlEntry(entry); Log->print("Add time control: "); Log->println((int)result); } @@ -1273,7 +1311,7 @@ void NukiWrapper::onTimeControlCommandReceived(const char *value) NukiLock::cmdResultToString(result, resultStr); _network->publishTimeControlCommandResult(resultStr); } - + _nextConfigUpdateTs = millis() + 300; } else diff --git a/NukiWrapper.h b/NukiWrapper.h index 6ab984b..c122cbf 100644 --- a/NukiWrapper.h +++ b/NukiWrapper.h @@ -61,7 +61,7 @@ private: void updateConfig(); void updateAuthData(); void updateKeypad(); - void updateTimeControl(); + void updateTimeControl(bool retrieved); void postponeBleWatchdog(); void updateGpioOutputs(); @@ -121,6 +121,7 @@ private: unsigned long _nextLockStateUpdateTs = 0; unsigned long _nextBatteryReportTs = 0; unsigned long _nextConfigUpdateTs = 0; + unsigned long _nextTimeControlUpdateTs = 0; unsigned long _nextKeypadUpdateTs = 0; unsigned long _nextRssiTs = 0; unsigned long _lastRssi = 0; diff --git a/README.md b/README.md index 0b3d377..bef2487 100644 --- a/README.md +++ b/README.md @@ -399,13 +399,13 @@ To change Nuki Lock/Opener time control settings set the `timecontrol/actionJson | enabled | Not used | Not used | Optional | Enable or disable the entry, enabled if not set | 1 = enabled, 0 = disabled | | weekdays | Not used | Optional | Optional | Weekdays on which the chosen lock action should be exectued | Array of days: "mon", "tue", "wed", "thu" , "fri" "sat", "sun" | | time | Not used | Required | Required | The time on which the chosen lock action should be executed | "HH:MM" | -| lockaction | Not used | Required | Required | The lock action that should be executed on the chosen weekdays at the chosen time | For the Nuki lock: "Unlock", "Lock", "Unlatch", "LockNgo", "LockNgoUnlatch", "FullLock", "FobAction1", "FobAction2", "FobAction3. For the Nuki Opener: "ActivateRTO", "DeactivateRTO", "ElectricStrikeActuation", "ActivateCM", "DeactivateCM", "FobAction1", "FobAction2", "FobAction3" | +| lockAction | Not used | Required | Required | The lock action that should be executed on the chosen weekdays at the chosen time | For the Nuki lock: "Unlock", "Lock", "Unlatch", "LockNgo", "LockNgoUnlatch", "FullLock". For the Nuki Opener: "ActivateRTO", "DeactivateRTO", "ElectricStrikeActuation", "ActivateCM", "DeactivateCM" | Example usage:
Examples: - Delete: `{ "action": "delete", "entryId": "1234" }` -- Add: `{ "action": "add", "weekdays": [ "wed", "thu", "fri" ], "time": "08:00", "lockaction": "unlock" }` -- Update: `{ "action": "update", "entryId": "1234", "enabled": "1", "weekdays": [ "mon", "tue", "sat", "sun" ], "time": "08:00", "lockaction": "lock" }` +- Add: `{ "action": "add", "weekdays": [ "wed", "thu", "fri" ], "time": "08:00", "lockAction": "Unlock" }` +- Update: `{ "action": "update", "entryId": "1234", "enabled": "1", "weekdays": [ "mon", "tue", "sat", "sun" ], "time": "08:00", "lockAction": "Lock" }` ## GPIO lock control (optional)