From 0b05f374f54c79aae5e3bc727a1775bdc5a3bd61 Mon Sep 17 00:00:00 2001 From: iranl Date: Fri, 16 Feb 2024 21:30:03 +0100 Subject: [PATCH 1/5] MQTT JSON NukiOpener --- MqttTopics.h | 1 + NetworkOpener.cpp | 47 ++++++++++++++++++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/MqttTopics.h b/MqttTopics.h index 78fc7a03..eaa1bcd3 100644 --- a/MqttTopics.h +++ b/MqttTopics.h @@ -10,6 +10,7 @@ #define mqtt_topic_battery_keypad_critical "/battery/keypadCritical" #define mqtt_topic_lock_state "/lock/state" +#define mqtt_topic_lock_json "/lock/json" #define mqtt_topic_query_config "/lock/query/config" #define mqtt_topic_query_lockstate "/lock/query/lockstate" #define mqtt_topic_query_keypad "/lock/query/keypad" diff --git a/NetworkOpener.cpp b/NetworkOpener.cpp index 89e5a4ff..38dc9470 100644 --- a/NetworkOpener.cpp +++ b/NetworkOpener.cpp @@ -202,18 +202,20 @@ void NetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& keyTurn _currentLockState = keyTurnerState.lockState; char str[50]; + memset(&str, 0, sizeof(str)); - if((_firstTunerStatePublish || keyTurnerState.lockState != lastKeyTurnerState.lockState || keyTurnerState.nukiState != lastKeyTurnerState.nukiState) && keyTurnerState.lockState != NukiOpener::LockState::Undefined) - { - memset(&str, 0, sizeof(str)); + DynamicJsonDocument json(_bufferSize); + + lockstateToString(keyTurnerState.lockState, str); + if((_firstTunerStatePublish || keyTurnerState.lockState != lastKeyTurnerState.lockState || keyTurnerState.nukiState != lastKeyTurnerState.nukiState) && keyTurnerState.lockState != NukiOpener::LockState::Undefined) + { if(keyTurnerState.nukiState == NukiOpener::State::ContinuousMode) { publishString(mqtt_topic_lock_state, "ContinuousMode"); } else { - lockstateToString(keyTurnerState.lockState, str); publishString(mqtt_topic_lock_state, str); } @@ -222,35 +224,58 @@ void NetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& keyTurn publishBinaryState(keyTurnerState); } } + + json["lock_state"] = str; + + if(keyTurnerState.nukiState == NukiOpener::State::ContinuousMode) + { + json["continuous_mode"] = true; + } else { + json["continuous_mode"] = false; + } + + memset(&str, 0, sizeof(str)); + triggerToString(keyTurnerState.trigger, str); if(_firstTunerStatePublish || keyTurnerState.trigger != lastKeyTurnerState.trigger) { - memset(&str, 0, sizeof(str)); - triggerToString(keyTurnerState.trigger, str); publishString(mqtt_topic_lock_trigger, str); } + + json["trigger"] = str; + memset(&str, 0, sizeof(str)); + completionStatusToString(keyTurnerState.lastLockActionCompletionStatus, str); if(_firstTunerStatePublish || keyTurnerState.lastLockActionCompletionStatus != lastKeyTurnerState.lastLockActionCompletionStatus) { - memset(&str, 0, sizeof(str)); - completionStatusToString(keyTurnerState.lastLockActionCompletionStatus, str); publishString(mqtt_topic_lock_completionStatus, str); } + + json["lock_completion_status"] = str; + + memset(&str, 0, sizeof(str)); + NukiOpener::doorSensorStateToString(keyTurnerState.doorSensorState, str); if(_firstTunerStatePublish || keyTurnerState.doorSensorState != lastKeyTurnerState.doorSensorState) { - memset(&str, 0, sizeof(str)); - NukiOpener::doorSensorStateToString(keyTurnerState.doorSensorState, str); publishString(mqtt_topic_lock_door_sensor_state, str); } + + json["door_sensor_state"] = str; + + bool critical = (keyTurnerState.criticalBatteryState & 0b00000001) > 0; if(_firstTunerStatePublish || keyTurnerState.criticalBatteryState != lastKeyTurnerState.criticalBatteryState) { - bool critical = (keyTurnerState.criticalBatteryState & 0b00000001) > 0; publishBool(mqtt_topic_battery_critical, critical); } + json["battery_critical"] = critical; + + serializeJson(json, _buffer, _bufferSize); + publishString(mqtt_topic_lock_json, _buffer); + _firstTunerStatePublish = false; } From 8757197c367181c8f4f5575f304a2c42242b0f0f Mon Sep 17 00:00:00 2001 From: iranl Date: Fri, 16 Feb 2024 21:37:47 +0100 Subject: [PATCH 2/5] MQTT JSON NukiLock --- NetworkLock.cpp | 71 ++++++++++++++++++++++++++++++++--------------- NetworkOpener.cpp | 2 +- 2 files changed, 50 insertions(+), 23 deletions(-) diff --git a/NetworkLock.cpp b/NetworkLock.cpp index 549afc5e..59c74d2f 100644 --- a/NetworkLock.cpp +++ b/NetworkLock.cpp @@ -209,11 +209,15 @@ void NetworkLock::onMqttDataReceived(const char* topic, byte* payload, const uns void NetworkLock::publishKeyTurnerState(const NukiLock::KeyTurnerState& keyTurnerState, const NukiLock::KeyTurnerState& lastKeyTurnerState) { char str[50]; + memset(&str, 0, sizeof(str)); + + DynamicJsonDocument json(_bufferSize); + + lockstateToString(keyTurnerState.lockState, str); if((_firstTunerStatePublish || keyTurnerState.lockState != lastKeyTurnerState.lockState) && keyTurnerState.lockState != NukiLock::LockState::Undefined) { - memset(&str, 0, sizeof(str)); - lockstateToString(keyTurnerState.lockState, str); + publishString(mqtt_topic_lock_state, str); if(_haEnabled) @@ -222,57 +226,80 @@ void NetworkLock::publishKeyTurnerState(const NukiLock::KeyTurnerState& keyTurne } } + json["lock_state"] = str; + + memset(&str, 0, sizeof(str)); + triggerToString(keyTurnerState.trigger, str); + if(_firstTunerStatePublish || keyTurnerState.trigger != lastKeyTurnerState.trigger) { - memset(&str, 0, sizeof(str)); - triggerToString(keyTurnerState.trigger, str); publishString(mqtt_topic_lock_trigger, str); } + json["trigger"] = str; + + memset(&str, 0, sizeof(str)); + lockactionToString(keyTurnerState.lastLockAction, str); + if(_firstTunerStatePublish || keyTurnerState.lastLockAction != lastKeyTurnerState.lastLockAction) { - memset(&str, 0, sizeof(str)); - lockactionToString(keyTurnerState.lastLockAction, str); publishString(mqtt_topic_lock_last_lock_action, str); } + json["last_lock_action"] = str; + + memset(&str, 0, sizeof(str)); + NukiLock::completionStatusToString(keyTurnerState.lastLockActionCompletionStatus, str); + if(_firstTunerStatePublish || keyTurnerState.lastLockActionCompletionStatus != lastKeyTurnerState.lastLockActionCompletionStatus) { - memset(&str, 0, sizeof(str)); - NukiLock::completionStatusToString(keyTurnerState.lastLockActionCompletionStatus, str); publishString(mqtt_topic_lock_completionStatus, str); } + json["lock_completion_status"] = str; + + memset(&str, 0, sizeof(str)); + NukiLock::doorSensorStateToString(keyTurnerState.doorSensorState, str); + if(_firstTunerStatePublish || keyTurnerState.doorSensorState != lastKeyTurnerState.doorSensorState) { - memset(&str, 0, sizeof(str)); - NukiLock::doorSensorStateToString(keyTurnerState.doorSensorState, str); publishString(mqtt_topic_lock_door_sensor_state, str); } + json["door_sensor_state"] = str; + + bool critical = (keyTurnerState.criticalBatteryState & 0b00000001) > 0; + bool charging = (keyTurnerState.criticalBatteryState & 0b00000010) > 0; + uint8_t level = (keyTurnerState.criticalBatteryState & 0b11111100) >> 1; + if(_firstTunerStatePublish || keyTurnerState.criticalBatteryState != lastKeyTurnerState.criticalBatteryState) { - bool critical = (keyTurnerState.criticalBatteryState & 0b00000001) > 0; publishBool(mqtt_topic_battery_critical, critical); - - bool charging = (keyTurnerState.criticalBatteryState & 0b00000010) > 0; publishBool(mqtt_topic_battery_charging, charging); - - uint8_t level = (keyTurnerState.criticalBatteryState & 0b11111100) >> 1; publishInt(mqtt_topic_battery_level, level); } + json["battery_critical"] = critical; + json["battery_charging"] = charging; + json["battery_level"] = level; + + bool keypadBatteryCritical; + + if ((keyTurnerState.accessoryBatteryState & (1 << 7)) != 0) { + keypadBatteryCritical = (keyTurnerState.accessoryBatteryState & (1 << 6)) != 0); + } + else + { + keypadBatteryCritical = false; + } + if(_firstTunerStatePublish || keyTurnerState.accessoryBatteryState != lastKeyTurnerState.accessoryBatteryState) { - if ((keyTurnerState.accessoryBatteryState & (1 << 7)) != 0) { - publishBool(mqtt_topic_battery_keypad_critical, (keyTurnerState.accessoryBatteryState & (1 << 6)) != 0); - } - else - { - publishBool(mqtt_topic_battery_keypad_critical, false); - } + publishBool(mqtt_topic_battery_keypad_critical, keypadBatteryCritical); } + json["keypad_battery_critical"] = keypadBatteryCritical; + _firstTunerStatePublish = false; } diff --git a/NetworkOpener.cpp b/NetworkOpener.cpp index 38dc9470..71c6788a 100644 --- a/NetworkOpener.cpp +++ b/NetworkOpener.cpp @@ -271,7 +271,7 @@ void NetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& keyTurn publishBool(mqtt_topic_battery_critical, critical); } - json["battery_critical"] = critical; + json["battery_critical"] = critical; serializeJson(json, _buffer, _bufferSize); publishString(mqtt_topic_lock_json, _buffer); From c2615e537a26382bd17e3b26a7a5ee9530823059 Mon Sep 17 00:00:00 2001 From: iranl Date: Fri, 16 Feb 2024 21:40:44 +0100 Subject: [PATCH 3/5] Update NetworkLock.cpp --- NetworkLock.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NetworkLock.cpp b/NetworkLock.cpp index 59c74d2f..f1026b4e 100644 --- a/NetworkLock.cpp +++ b/NetworkLock.cpp @@ -286,7 +286,7 @@ void NetworkLock::publishKeyTurnerState(const NukiLock::KeyTurnerState& keyTurne bool keypadBatteryCritical; if ((keyTurnerState.accessoryBatteryState & (1 << 7)) != 0) { - keypadBatteryCritical = (keyTurnerState.accessoryBatteryState & (1 << 6)) != 0); + keypadBatteryCritical = ((keyTurnerState.accessoryBatteryState & (1 << 6)) != 0); } else { From 7937ec8c65c881aa0c17222210b4812a10b6f0c8 Mon Sep 17 00:00:00 2001 From: iranl Date: Sat, 17 Feb 2024 11:42:03 +0100 Subject: [PATCH 4/5] Add auth info --- NetworkLock.cpp | 4 ++-- NetworkLock.h | 6 ++++-- NetworkOpener.cpp | 4 ++-- NetworkOpener.h | 3 +++ NukiWrapper.cpp | 11 ++++++----- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/NetworkLock.cpp b/NetworkLock.cpp index f1026b4e..c87ed302 100644 --- a/NetworkLock.cpp +++ b/NetworkLock.cpp @@ -299,6 +299,8 @@ void NetworkLock::publishKeyTurnerState(const NukiLock::KeyTurnerState& keyTurne } json["keypad_battery_critical"] = keypadBatteryCritical; + json["auth_id"] = authId; + json["auth_name"] = authName; _firstTunerStatePublish = false; } @@ -328,8 +330,6 @@ void NetworkLock::publishAuthorizationInfo(const std::list& char str[50]; bool authFound = false; - uint32_t authId = 0; - char authName[33]; memset(authName, 0, sizeof(authName)); DynamicJsonDocument json(_bufferSize); diff --git a/NetworkLock.h b/NetworkLock.h index 95f0e881..866a18a6 100644 --- a/NetworkLock.h +++ b/NetworkLock.h @@ -73,14 +73,16 @@ class NetworkLock : public MqttReceiver bool _firstTunerStatePublish = true; unsigned long _lastMaintenanceTs = 0; - bool _haEnabled= false; + bool _haEnabled = false; bool _reconnected = false; String _keypadCommandName = ""; String _keypadCommandCode = ""; uint _keypadCommandId = 0; int _keypadCommandEnabled = 1; - uint8_t _queryCommands = 0; + uint8_t _queryCommands = 0; + uint32_t authId = 0; + char authName[33]; char* _buffer; size_t _bufferSize; diff --git a/NetworkOpener.cpp b/NetworkOpener.cpp index 71c6788a..f5e1e60d 100644 --- a/NetworkOpener.cpp +++ b/NetworkOpener.cpp @@ -272,6 +272,8 @@ void NetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& keyTurn } json["battery_critical"] = critical; + json["auth_id"] = authId; + json["auth_name"] = authName; serializeJson(json, _buffer, _bufferSize); publishString(mqtt_topic_lock_json, _buffer); @@ -314,8 +316,6 @@ void NetworkOpener::publishAuthorizationInfo(const std::listpublishKeyTurnerState(_keyTurnerState, _lastKeyTurnerState); updateGpioOutputs(); @@ -333,11 +339,6 @@ void NukiWrapper::updateKeyTurnerState() lockstateToString(_keyTurnerState.lockState, lockStateStr); Log->println(lockStateStr); - if(_publishAuthData) - { - updateAuthData(); - } - postponeBleWatchdog(); } From fc767ddfab55398d3ade81f8707c865dffdfab92 Mon Sep 17 00:00:00 2001 From: iranl Date: Sat, 17 Feb 2024 12:00:47 +0100 Subject: [PATCH 5/5] MQTT JSON Keypad --- MqttTopics.h | 1 + Network.cpp | 52 +++++++++++++++++++++--------------------- NetworkLock.cpp | 57 ++++++++++++++++++++++++++++++----------------- NetworkOpener.cpp | 44 +++++++++++++++++++++++++----------- WebCfgServer.cpp | 2 +- 5 files changed, 95 insertions(+), 61 deletions(-) diff --git a/MqttTopics.h b/MqttTopics.h index eaa1bcd3..2ae01871 100644 --- a/MqttTopics.h +++ b/MqttTopics.h @@ -52,6 +52,7 @@ #define mqtt_topic_keypad_command_code "/keypad/command/code" #define mqtt_topic_keypad_command_enabled "/keypad/command/enabled" #define mqtt_topic_keypad_command_result "/keypad/command/commandResult" +#define mqtt_topic_keypad_json "/keypad/json" #define mqtt_topic_presence "/presence/devices" diff --git a/Network.cpp b/Network.cpp index 5ea17813..428c05a1 100644 --- a/Network.cpp +++ b/Network.cpp @@ -357,13 +357,13 @@ bool Network::update() } _lastMaintenanceTs = ts; } - + if(_preferences->getBool(preference_check_updates)) { if(_lastUpdateCheckTs == 0 || (ts - _lastUpdateCheckTs) > 86400000) { _lastUpdateCheckTs = ts; - + https.useHTTP10(true); https.begin(GITHUB_LATEST_RELEASE_API_URL); @@ -373,16 +373,16 @@ bool Network::update() DynamicJsonDocument doc(6144); DeserializationError jsonError = deserializeJson(doc, https.getStream()); - if (!jsonError) { + if (!jsonError) { _latestVersion = doc["tag_name"]; publishString(_maintenancePathPrefix, mqtt_topic_info_nuki_hub_latest, _latestVersion); - } + } } https.end(); } } - + for(const auto& gpioTs : _gpioTs) { uint8_t pin = gpioTs.first; @@ -750,7 +750,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n json["dev"]["mf"] = "Nuki"; json["dev"]["mdl"] = deviceType; json["dev"]["name"] = name; - + String cuUrl = _preferences->getString(preference_mqtt_hass_cu_url); if (cuUrl != "") @@ -761,7 +761,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n { json["dev"]["cu"] = "http://" + _device->localIP(); } - + json["~"] = baseTopic; json["name"] = nullptr; json["unique_id"] = String(uidString) + "_lock"; @@ -945,11 +945,11 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "", { { "enabled_by_default", "true" }, {"ic", "mdi:counter"}}); - + if(_preferences->getBool(preference_check_updates)) { // NUKI Hub latest - publishHassTopic("sensor", + publishHassTopic("sensor", "nuki_hub_latest", uidString, "_nuki_hub_latest", @@ -964,13 +964,13 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "", { { "enabled_by_default", "true" }, {"ic", "mdi:counter"}}); - + // NUKI Hub update char latest_version_topic[250]; _lockPath.toCharArray(latest_version_topic,_lockPath.length() + 1); strcat(latest_version_topic, mqtt_topic_info_nuki_hub_latest); - publishHassTopic("update", + publishHassTopic("update", "nuki_hub_update", uidString, "_nuki_hub_update", @@ -986,7 +986,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n { { "enabled_by_default", "true" }, { "entity_picture", "https://raw.githubusercontent.com/technyon/nuki_hub/master/icon/favicon-32x32.png" }, { "release_url", GITHUB_LATEST_RELEASE_URL }, - { "latest_version_topic", latest_version_topic }}); + { "latest_version_topic", latest_version_topic }}); } else { @@ -1050,7 +1050,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n { "pl_off", "0" }, { "state_on", "1" }, { "state_off", "0" }}); - + // Unlatch publishHassTopic("button", "unlatch", @@ -1066,8 +1066,8 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "", String("~") + mqtt_topic_lock_action, { { "enabled_by_default", "false" }, - { "pl_prs", "unlatch" }}); - + { "pl_prs", "unlatch" }}); + } } @@ -1362,7 +1362,7 @@ String Network::createHassTopicPath(const String& mqttDeviceType, const String& path.concat("/"); path.concat(mqttDeviceName); path.concat("/config"); - + return path; } @@ -1387,18 +1387,18 @@ void Network::removeHASSConfig(char* uidString) removeHassTopic("lock", "smartlock", uidString); removeHassTopic("binary_sensor", "battery_low", uidString); removeHassTopic("binary_sensor", "keypad_battery_low", uidString); - removeHassTopic("sensor", "battery_voltage", uidString); + removeHassTopic("sensor", "battery_voltage", uidString); removeHassTopic("sensor", "trigger", uidString); removeHassTopic("binary_sensor", "mqtt_connected", uidString); - removeHassTopic("switch", "reset", uidString); + removeHassTopic("switch", "reset", uidString); removeHassTopic("sensor", "firmware_version", uidString); removeHassTopic("sensor", "hardware_version", uidString); removeHassTopic("sensor", "nuki_hub_version", uidString); removeHassTopic("sensor", "nuki_hub_latest", uidString); removeHassTopic("update", "nuki_hub_update", uidString); removeHassTopic("sensor", "nuki_hub_ip", uidString); - removeHassTopic("switch", "led_enabled", uidString); - removeHassTopic("switch", "button_enabled", uidString); + removeHassTopic("switch", "led_enabled", uidString); + removeHassTopic("switch", "button_enabled", uidString); removeHassTopic("button", "unlatch", uidString); removeHassTopic("button", "lockngo", uidString); removeHassTopic("button", "lockngounlatch", uidString); @@ -1406,7 +1406,7 @@ void Network::removeHASSConfig(char* uidString) removeHassTopic("binary_sensor", "door_sensor", uidString); removeHassTopic("binary_sensor", "ring", uidString); removeHassTopic("number", "led_brightness", uidString); - removeHassTopic("sensor", "sound_level", uidString); + removeHassTopic("sensor", "sound_level", uidString); removeHassTopic("number", "sound_level", uidString); removeHassTopic("sensor", "last_action_authorization", uidString); removeHassTopic("sensor", "keypad_status", uidString); @@ -1451,17 +1451,17 @@ DynamicJsonDocument Network::createHassJson(const String& uidString, { json["dev_cla"] = deviceClass; } - + if(stateTopic != "") { json["stat_t"] = stateTopic; } - + if(stateClass != "") { json["stat_cla"] = stateClass; } - + if(entityCat != "") { json["ent_cat"] = entityCat; @@ -1471,7 +1471,7 @@ DynamicJsonDocument Network::createHassJson(const String& uidString, { json["cmd_t"] = commandTopic; } - + json["avty"]["t"] = _lockPath + mqtt_topic_mqtt_connection_state; for(const auto& entry : additionalEntries) @@ -1489,7 +1489,7 @@ DynamicJsonDocument Network::createHassJson(const String& uidString, json[entry.first] = entry.second; } } - + return json; } diff --git a/NetworkLock.cpp b/NetworkLock.cpp index c87ed302..01c877b7 100644 --- a/NetworkLock.cpp +++ b/NetworkLock.cpp @@ -268,40 +268,35 @@ void NetworkLock::publishKeyTurnerState(const NukiLock::KeyTurnerState& keyTurne json["door_sensor_state"] = str; - bool critical = (keyTurnerState.criticalBatteryState & 0b00000001) > 0; - bool charging = (keyTurnerState.criticalBatteryState & 0b00000010) > 0; - uint8_t level = (keyTurnerState.criticalBatteryState & 0b11111100) >> 1; - if(_firstTunerStatePublish || keyTurnerState.criticalBatteryState != lastKeyTurnerState.criticalBatteryState) { + bool critical = (keyTurnerState.criticalBatteryState & 0b00000001) > 0; publishBool(mqtt_topic_battery_critical, critical); - publishBool(mqtt_topic_battery_charging, charging); - publishInt(mqtt_topic_battery_level, level); - } - - json["battery_critical"] = critical; - json["battery_charging"] = charging; - json["battery_level"] = level; - bool keypadBatteryCritical; + bool charging = (keyTurnerState.criticalBatteryState & 0b00000010) > 0; + publishBool(mqtt_topic_battery_charging, charging); - if ((keyTurnerState.accessoryBatteryState & (1 << 7)) != 0) { - keypadBatteryCritical = ((keyTurnerState.accessoryBatteryState & (1 << 6)) != 0); - } - else - { - keypadBatteryCritical = false; + uint8_t level = (keyTurnerState.criticalBatteryState & 0b11111100) >> 1; + publishInt(mqtt_topic_battery_level, level); } if(_firstTunerStatePublish || keyTurnerState.accessoryBatteryState != lastKeyTurnerState.accessoryBatteryState) { - publishBool(mqtt_topic_battery_keypad_critical, keypadBatteryCritical); + if ((keyTurnerState.accessoryBatteryState & (1 << 7)) != 0) { + publishBool(mqtt_topic_battery_keypad_critical, (keyTurnerState.accessoryBatteryState & (1 << 6)) != 0); + } + else + { + publishBool(mqtt_topic_battery_keypad_critical, false); + } } - json["keypad_battery_critical"] = keypadBatteryCritical; json["auth_id"] = authId; json["auth_name"] = authName; + serializeJson(json, _buffer, _bufferSize); + publishString(mqtt_topic_lock_json, _buffer); + _firstTunerStatePublish = false; } @@ -484,15 +479,35 @@ void NetworkLock::publishBleAddress(const std::string &address) void NetworkLock::publishKeypad(const std::list& entries, uint maxKeypadCodeCount) { uint index = 0; + + DynamicJsonDocument json(_bufferSize); + for(const auto& entry : entries) { String basePath = mqtt_topic_keypad; basePath.concat("/code_"); basePath.concat(std::to_string(index).c_str()); publishKeypadEntry(basePath, entry); - + + auto jsonEntry = json.add(); + + jsonEntry["id"] = entry.codeId; + jsonEntry["enabled"] = entry.enabled; + jsonEntry["name"] = entry.name; + jsonEntry["createdYear"] = entry.dateCreatedYear; + jsonEntry["createdMonth"] = entry.dateCreatedMonth; + jsonEntry["createdDay"] = entry.dateCreatedDay; + jsonEntry["createdHour"] = entry.dateCreatedHour; + jsonEntry["createdMin"] = entry.dateCreatedMin; + jsonEntry["createdSec"] = entry.dateCreatedSec; + jsonEntry["lockCount"] = entry.lockCount; + ++index; } + + serializeJson(json, _buffer, _bufferSize); + publishString(mqtt_topic_keypad_json, _buffer); + while(index < maxKeypadCodeCount) { NukiLock::KeypadEntry entry; diff --git a/NetworkOpener.cpp b/NetworkOpener.cpp index f5e1e60d..e24751a7 100644 --- a/NetworkOpener.cpp +++ b/NetworkOpener.cpp @@ -202,14 +202,14 @@ void NetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& keyTurn _currentLockState = keyTurnerState.lockState; char str[50]; - memset(&str, 0, sizeof(str)); + memset(&str, 0, sizeof(str)); DynamicJsonDocument json(_bufferSize); lockstateToString(keyTurnerState.lockState, str); if((_firstTunerStatePublish || keyTurnerState.lockState != lastKeyTurnerState.lockState || keyTurnerState.nukiState != lastKeyTurnerState.nukiState) && keyTurnerState.lockState != NukiOpener::LockState::Undefined) - { + { if(keyTurnerState.nukiState == NukiOpener::State::ContinuousMode) { publishString(mqtt_topic_lock_state, "ContinuousMode"); @@ -224,14 +224,14 @@ void NetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& keyTurn publishBinaryState(keyTurnerState); } } - + json["lock_state"] = str; - + if(keyTurnerState.nukiState == NukiOpener::State::ContinuousMode) { - json["continuous_mode"] = true; + json["continuous_mode"] = 1; } else { - json["continuous_mode"] = false; + json["continuous_mode"] = 0; } memset(&str, 0, sizeof(str)); @@ -241,7 +241,7 @@ void NetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& keyTurn { publishString(mqtt_topic_lock_trigger, str); } - + json["trigger"] = str; memset(&str, 0, sizeof(str)); @@ -251,7 +251,7 @@ void NetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& keyTurn { publishString(mqtt_topic_lock_completionStatus, str); } - + json["lock_completion_status"] = str; memset(&str, 0, sizeof(str)); @@ -261,19 +261,17 @@ void NetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& keyTurn { publishString(mqtt_topic_lock_door_sensor_state, str); } - - json["door_sensor_state"] = str; - bool critical = (keyTurnerState.criticalBatteryState & 0b00000001) > 0; + json["door_sensor_state"] = str; if(_firstTunerStatePublish || keyTurnerState.criticalBatteryState != lastKeyTurnerState.criticalBatteryState) { + bool critical = (keyTurnerState.criticalBatteryState & 0b00000001) > 0; publishBool(mqtt_topic_battery_critical, critical); } - json["battery_critical"] = critical; json["auth_id"] = authId; - json["auth_name"] = authName; + json["auth_name"] = authName; serializeJson(json, _buffer, _bufferSize); publishString(mqtt_topic_lock_json, _buffer); @@ -538,15 +536,35 @@ void NetworkOpener::removeHASSConfig(char* uidString) void NetworkOpener::publishKeypad(const std::list& entries, uint maxKeypadCodeCount) { uint index = 0; + + DynamicJsonDocument json(_bufferSize); + for(const auto& entry : entries) { String basePath = mqtt_topic_keypad; basePath.concat("/code_"); basePath.concat(std::to_string(index).c_str()); publishKeypadEntry(basePath, entry); + + auto jsonEntry = json.add(); + + jsonEntry["id"] = entry.codeId; + jsonEntry["enabled"] = entry.enabled; + jsonEntry["name"] = entry.name; + jsonEntry["createdYear"] = entry.dateCreatedYear; + jsonEntry["createdMonth"] = entry.dateCreatedMonth; + jsonEntry["createdDay"] = entry.dateCreatedDay; + jsonEntry["createdHour"] = entry.dateCreatedHour; + jsonEntry["createdMin"] = entry.dateCreatedMin; + jsonEntry["createdSec"] = entry.dateCreatedSec; + jsonEntry["lockCount"] = entry.lockCount; ++index; } + + serializeJson(json, _buffer, _bufferSize); + publishString(mqtt_topic_keypad_json, _buffer); + while(index < maxKeypadCodeCount) { NukiLock::KeypadEntry entry; diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index daacb29a..4103f84f 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -1368,4 +1368,4 @@ String WebCfgServer::getPreselectionForGpio(const uint8_t &pin) } return String((int8_t)PinRole::Disabled); -} +} \ No newline at end of file