diff --git a/MqttTopics.h b/MqttTopics.h index 46b460a..f58b5ed 100644 --- a/MqttTopics.h +++ b/MqttTopics.h @@ -54,6 +54,10 @@ #define mqtt_topic_keypad_json_action "/keypad/actionJson" #define mqtt_topic_keypad_json_command_result "/keypad/commandResultJson" +#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 6e6e8eb..21ed905 100644 --- a/NetworkLock.cpp +++ b/NetworkLock.cpp @@ -84,6 +84,12 @@ void NetworkLock::initialize() _network->initTopic(_mqttPath, mqtt_topic_keypad_json_action, "--"); } + 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; @@ -218,6 +224,18 @@ void NetworkLock::onMqttDataReceived(const char* topic, byte* payload, const uns publishString(mqtt_topic_keypad_json_action, "--"); } + + 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) @@ -611,6 +629,78 @@ void NetworkLock::publishKeypad(const std::list& entries, } } +void NetworkLock::publishTimeControl(const std::list& timeControlEntries) +{ + char str[50]; + JsonDocument json; + + for(const auto& entry : timeControlEntries) + { + 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); @@ -621,6 +711,11 @@ void NetworkLock::publishKeypadJsonCommandResult(const char* result) publishString(mqtt_topic_keypad_json_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; @@ -641,6 +736,11 @@ void NetworkLock::setKeypadJsonCommandReceivedCallback(void (*keypadJsonCommandR _keypadJsonCommandReceivedReceivedCallback = keypadJsonCommandReceivedReceivedCallback; } +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 0e62271..f259f38 100644 --- a/NetworkLock.h +++ b/NetworkLock.h @@ -37,14 +37,16 @@ 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& timeControlEntries); void publishKeypadCommandResult(const char* result); void publishKeypadJsonCommandResult(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 setKeypadJsonCommandReceivedCallback(void (*keypadJsonCommandReceivedReceivedCallback)(const char* value)); - + 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: 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 (*_keypadJsonCommandReceivedReceivedCallback)(const char* value) = nullptr; -}; + void (*_timeControlCommandReceivedReceivedCallback)(const char* value) = nullptr; +}; \ No newline at end of file diff --git a/NetworkOpener.cpp b/NetworkOpener.cpp index f257439..1daa850 100644 --- a/NetworkOpener.cpp +++ b/NetworkOpener.cpp @@ -72,6 +72,12 @@ void NetworkOpener::initialize() _network->initTopic(_mqttPath, mqtt_topic_keypad_json_action, "--"); } + 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; @@ -207,6 +213,18 @@ void NetworkOpener::onMqttDataReceived(const char* topic, byte* payload, const u publishString(mqtt_topic_keypad_json_action, "--"); } + + 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) @@ -380,24 +398,24 @@ void NetworkOpener::publishAuthorizationInfo(const std::list& entrie } } +void NetworkOpener::publishTimeControl(const std::list& timeControlEntries) +{ + char str[50]; + JsonDocument json; + + for(const auto& entry : timeControlEntries) + { + 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)); + NukiOpener::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); @@ -677,6 +767,11 @@ void NetworkOpener::publishKeypadJsonCommandResult(const char* result) publishString(mqtt_topic_keypad_json_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; @@ -697,6 +792,11 @@ void NetworkOpener::setKeypadJsonCommandReceivedCallback(void (*keypadJsonComman _keypadJsonCommandReceivedReceivedCallback = keypadJsonCommandReceivedReceivedCallback; } +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 84b8bac..e300db8 100644 --- a/NetworkOpener.h +++ b/NetworkOpener.h @@ -34,14 +34,16 @@ 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& timeControlEntries); void publishKeypadCommandResult(const char* result); void publishKeypadJsonCommandResult(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 setKeypadJsonCommandReceivedCallback(void (*keypadJsonCommandReceivedReceivedCallback)(const char* value)); - + void setTimeControlCommandReceivedCallback(void (*timeControlCommandReceivedReceivedCallback)(const char* value)); void onMqttDataReceived(const char* topic, byte* payload, const unsigned int length) override; bool reconnected(); @@ -96,4 +98,5 @@ private: 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 (*_keypadJsonCommandReceivedReceivedCallback)(const char* value) = nullptr; + void (*_timeControlCommandReceivedReceivedCallback)(const char* value) = nullptr; }; diff --git a/NukiOpenerWrapper.cpp b/NukiOpenerWrapper.cpp index a53ff1e..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(); @@ -406,7 +411,9 @@ 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(false); + const int pinStatus = _preferences->getInt(preference_opener_pin_status, 4); if(isPinSet()) { @@ -501,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; }); @@ -524,6 +534,43 @@ void NukiOpenerWrapper::updateKeypad() postponeBleWatchdog(); } +void NukiOpenerWrapper::updateTimeControl(bool retrieved) +{ + if(!_preferences->getBool(preference_timecontrol_info_enabled)) return; + + if(!retrieved) + { + 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()); + + timeControlEntries.sort([](const NukiOpener::TimeControlEntry& a, const NukiOpener::TimeControlEntry& b) { return a.entryId < b.entryId; }); + + _network->publishTimeControl(timeControlEntries); + + _timeControlIds.clear(); + _timeControlIds.reserve(timeControlEntries.size()); + for(const auto& entry : timeControlEntries) + { + _timeControlIds.push_back(entry.entryId); + } + } + + postponeBleWatchdog(); +} + void NukiOpenerWrapper::postponeBleWatchdog() { _disableBleWatchdogTs = millis() + 15000; @@ -531,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); @@ -964,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; @@ -973,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; @@ -1094,6 +1148,181 @@ void NukiOpenerWrapper::onKeypadJsonCommandReceived(const char *value) } } +void NukiOpenerWrapper::onTimeControlCommandReceived(const char *value) +{ + if(_nukiOpener.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(); + const char *lockAct = json["lockAction"].as(); + NukiOpener::LockAction timeControlLockAction; + + if(strlen(lockAct) > 0) + { + + timeControlLockAction = nukiOpenerInst->lockActionToEnum(lockAct); + + if((int)timeControlLockAction == 0xff) + { + _network->publishTimeControlCommandResult("invalidLockAction"); + return; + } + } + else + { + _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->publishTimeControlCommandResult("invalidTime"); + return; + } + } + else + { + _network->publishTimeControlCommandResult("invalidTime"); + return; + } + } + else + { + _network->publishTimeControlCommandResult("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) + { + NukiOpener::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.addTimeControlEntry(entry); + Log->print("Add time control: "); + Log->println((int)result); + } + else if (strcmp(action, "update") == 0) + { + NukiOpener::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)); + NukiOpener::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 0061420..b37f8f5 100644 --- a/NukiOpenerWrapper.h +++ b/NukiOpenerWrapper.h @@ -50,16 +50,19 @@ private: 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 onKeypadJsonCommandReceivedCallback(const char* value); + 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 onKeypadJsonCommandReceived(const char* value); + void onTimeControlCommandReceived(const char* value); void updateKeyTurnerState(); void updateBatteryState(); void updateConfig(); void updateAuthData(); void updateKeypad(); + void updateTimeControl(bool retrieved); void postponeBleWatchdog(); void updateGpioOutputs(); @@ -94,6 +97,7 @@ private: int _retryLockstateCount = 0; unsigned long _nextRetryTs = 0; std::vector _keypadCodeIds; + std::vector _timeControlIds; NukiOpener::OpenerState _lastKeyTurnerState; NukiOpener::OpenerState _keyTurnerState; @@ -118,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 fc9661f..81fbe90 100644 --- a/NukiWrapper.cpp +++ b/NukiWrapper.cpp @@ -33,6 +33,7 @@ NukiWrapper::NukiWrapper(const std::string& deviceName, NukiDeviceId* deviceId, network->setConfigUpdateReceivedCallback(nukiInst->onConfigUpdateReceivedCallback); network->setKeypadCommandReceivedCallback(nukiInst->onKeypadCommandReceivedCallback); network->setKeypadJsonCommandReceivedCallback(nukiInst->onKeypadJsonCommandReceivedCallback); + network->setTimeControlCommandReceivedCallback(nukiInst->onTimeControlCommandReceivedCallback); _gpio->addCallback(NukiWrapper::gpioActionCallback); } @@ -183,6 +184,11 @@ void NukiWrapper::update() setupHASS(); } } + if(_nextTimeControlUpdateTs != 0 && ts > _nextTimeControlUpdateTs) + { + _nextTimeControlUpdateTs = 0; + updateTimeControl(true); + } if(_hassEnabled && _configRead && _network->reconnected()) { setupHASS(); @@ -379,7 +385,9 @@ 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(false); + const int pinStatus = _preferences->getInt(preference_lock_pin_status, 4); if(isPinSet()) { @@ -474,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; }); @@ -497,6 +508,43 @@ void NukiWrapper::updateKeypad() postponeBleWatchdog(); } +void NukiWrapper::updateTimeControl(bool retrieved) +{ + if(!_preferences->getBool(preference_timecontrol_info_enabled)) return; + + if(!retrieved) + { + 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()); + + timeControlEntries.sort([](const NukiLock::TimeControlEntry& a, const NukiLock::TimeControlEntry& b) { return a.entryId < b.entryId; }); + + _network->publishTimeControl(timeControlEntries); + + _timeControlIds.clear(); + _timeControlIds.reserve(timeControlEntries.size()); + for(const auto& entry : timeControlEntries) + { + _timeControlIds.push_back(entry.entryId); + } + } + + postponeBleWatchdog(); +} + void NukiWrapper::postponeBleWatchdog() { _disableBleWatchdogTs = millis() + 15000; @@ -504,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); @@ -556,6 +610,11 @@ void NukiWrapper::onKeypadJsonCommandReceivedCallback(const char *value) nukiInst->onKeypadJsonCommandReceived(value); } +void NukiWrapper::onTimeControlCommandReceivedCallback(const char *value) +{ + nukiInst->onTimeControlCommandReceived(value); +} + void NukiWrapper::gpioActionCallback(const GpioAction &action, const int& pin) { switch(action) @@ -1087,6 +1146,181 @@ void NukiWrapper::onKeypadJsonCommandReceived(const char *value) } } +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(); + const char *lockAct = json["lockAction"].as(); + NukiLock::LockAction timeControlLockAction; + + if(strlen(lockAct) > 0) + { + + timeControlLockAction = nukiInst->lockActionToEnum(lockAct); + + if((int)timeControlLockAction == 0xff) + { + _network->publishTimeControlCommandResult("invalidLockAction"); + return; + } + } + else + { + _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->publishTimeControlCommandResult("invalidTime"); + return; + } + } + else + { + _network->publishTimeControlCommandResult("invalidTime"); + return; + } + } + else + { + _network->publishTimeControlCommandResult("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.addTimeControlEntry(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 a5f7a12..c122cbf 100644 --- a/NukiWrapper.h +++ b/NukiWrapper.h @@ -48,17 +48,20 @@ private: 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 onKeypadJsonCommandReceivedCallback(const char* value); + 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 onKeypadJsonCommandReceived(const char* value); + void onTimeControlCommandReceived(const char* value); void updateKeyTurnerState(); void updateBatteryState(); void updateConfig(); void updateAuthData(); void updateKeypad(); + void updateTimeControl(bool retrieved); void postponeBleWatchdog(); void updateGpioOutputs(); @@ -87,6 +90,7 @@ private: bool _publishAuthData = false; bool _clearAuthData = false; std::vector _keypadCodeIds; + std::vector _timeControlIds; NukiLock::KeyTurnerState _lastKeyTurnerState; NukiLock::KeyTurnerState _keyTurnerState; @@ -117,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/PreferencesKeys.h b/PreferencesKeys.h index 3e164ef..7adf00a 100644 --- a/PreferencesKeys.h +++ b/PreferencesKeys.h @@ -50,6 +50,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 @@ -79,9 +81,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, @@ -98,7 +101,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 1b81eea..f23a787 100644 --- a/README.md +++ b/README.md @@ -153,6 +153,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 @@ -254,6 +256,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. @@ -379,6 +385,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". 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" }` + ## GPIO lock control (optional) The lock can be controlled via GPIO.
diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index c39aadb..209df27 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")); @@ -983,6 +993,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)