From 4d025729b099f323aed2538aa12bc17a512c7f20 Mon Sep 17 00:00:00 2001 From: iranl Date: Tue, 28 May 2024 22:54:51 +0200 Subject: [PATCH 1/2] Add option to remove some non-JSON MQTT topics --- README.md | 1 + platformio.ini | 8 +- src/Network.cpp | 20 ++++- src/Network.h | 1 + src/NetworkLock.cpp | 179 ++++++++++++++++++++++++++------------ src/NetworkOpener.cpp | 171 ++++++++++++++++++++++++------------ src/NukiOpenerWrapper.cpp | 8 +- src/NukiWrapper.cpp | 28 +++--- src/PreferencesKeys.h | 5 +- src/WebCfgServer.cpp | 6 ++ 10 files changed, 288 insertions(+), 139 deletions(-) diff --git a/README.md b/README.md index aa629539..55361dc3 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,7 @@ In a browser navigate to the IP address assigned to the ESP32. - Restart on disconnect: Enable to restart the Nuki Hub after 60 seconds without a connection to a network. - Enable MQTT logging: Enable to fill the maintenance/log MQTT topic with debug log information. - Check for Firmware Updates every 24h: Enable to allow the Nuki Hub to check the latest release of the Nuki Hub firmware on boot and every 24 hours. Requires the Nuki Hub to be able to connect to github.com. The latest version will be published to MQTT and will be visible on the main page of the Web Configurator. +- Disable some extraneous non-JSON topics: Enable to not publish non-JSON keypad and config MQTT topics. #### IP Address assignment diff --git a/platformio.ini b/platformio.ini index 2fc67335..e8101ad5 100644 --- a/platformio.ini +++ b/platformio.ini @@ -66,7 +66,7 @@ board = esp32dev build_flags = ${env.build_flags} -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG - -DCONFIG_NIMBLE_CPP_LOG_LEVEL=4 + -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DDEBUG_NUKIHUB -DDEBUG_SENSE_NUKI -DDEBUG_NUKI_COMMAND @@ -80,7 +80,7 @@ board = esp32-s3-devkitc-1 build_flags = ${env.build_flags} -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG - -DCONFIG_NIMBLE_CPP_LOG_LEVEL=4 + -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DDEBUG_NUKIHUB -DDEBUG_SENSE_NUKI -DDEBUG_NUKI_COMMAND @@ -94,7 +94,7 @@ board = esp32-c3-devkitc-02 build_flags = ${env.build_flags} -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG - -DCONFIG_NIMBLE_CPP_LOG_LEVEL=4 + -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DDEBUG_NUKIHUB -DDEBUG_SENSE_NUKI -DDEBUG_NUKI_COMMAND @@ -110,7 +110,7 @@ build_flags = ${env.build_flags} -DFRAMEWORK_ARDUINO_SOLO1 -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG - -DCONFIG_NIMBLE_CPP_LOG_LEVEL=4 + -DCONFIG_NIMBLE_CPP_LOG_LEVEL=0 -DDEBUG_NUKIHUB -DDEBUG_SENSE_NUKI -DDEBUG_NUKI_COMMAND diff --git a/src/Network.cpp b/src/Network.cpp index 55c13311..5359cfda 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -264,13 +264,13 @@ void Network::initialize() bool Network::update() { unsigned long ts = millis(); - + if(ts > 120000 && ts < 125000 && _preferences->getInt(preference_bootloop_counter, 0) > 0) { _preferences->putInt(preference_bootloop_counter, 0); Log->println(F("Bootloop counter reset")); } - + _device->update(); if(!_mqttEnabled) @@ -935,7 +935,7 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "", { { (char*)"en", (char*)"true" }, {(char*)"ic", (char*)"mdi:counter"}}); - + // Nuki Hub build publishHassTopic("sensor", "nuki_hub_build", @@ -2298,7 +2298,7 @@ void Network::publishHASSConfigAdditionalOpenerEntities(char *deviceType, const "", {{(char*)"pl_on", (char*)"ring"}, {(char*)"pl_off", (char*)"standby"}}); - + JsonDocument json; json = createHassJson(uidString, "_ring_event", "Ring", name, baseTopic, String("~") + mqtt_topic_lock_ring, deviceType, "doorbell", "", "", "", {{(char*)"val_tpl", (char*)"{ \"event_type\": \"{{ value }}\" }"}}); json["event_types"][0] = "ring"; @@ -3152,6 +3152,18 @@ void Network::removeHassTopic(const String& mqttDeviceType, const String& mqttDe } } +void Network::removeTopic(const String& mqttPath, const String& mqttTopic) +{ + String path = mqttPath; + path.concat(mqttTopic); + _device->mqttPublish(path.c_str(), MQTT_QOS_LEVEL, true, ""); + + #ifdef DEBUG_NUKIHUB + Log->print(F("Removing MQTT topic: ")); + Log->println(path.c_str()); + #endif +} + void Network::removeHASSConfig(char* uidString) { diff --git a/src/Network.h b/src/Network.h index 2518ef4d..3f99a3bb 100644 --- a/src/Network.h +++ b/src/Network.h @@ -55,6 +55,7 @@ class Network void publishHASSWifiRssiConfig(char* deviceType, const char* baseTopic, char* name, char* uidString); void removeHASSConfig(char* uidString); void removeHASSConfigTopic(char* deviceType, char* name, char* uidString); + void removeTopic(const String& mqttPath, const String& mqttTopic); void batteryTypeToString(const Nuki::BatteryType battype, char* str); void advertisingModeToString(const Nuki::AdvertisingMode advmode, char* str); void timeZoneIdToString(const Nuki::TimeZoneId timeZoneId, char* str); diff --git a/src/NetworkLock.cpp b/src/NetworkLock.cpp index 2b439245..b7ed93b9 100644 --- a/src/NetworkLock.cpp +++ b/src/NetworkLock.cpp @@ -63,20 +63,41 @@ void NetworkLock::initialize() _network->subscribe(_mqttPath, mqtt_topic_query_lockstate); _network->subscribe(_mqttPath, mqtt_topic_query_battery); + if(_preferences->getBool(preference_disable_non_json, false)) + { + _network->removeTopic(_mqttPath, mqtt_topic_keypad_command_action); + _network->removeTopic(_mqttPath, mqtt_topic_keypad_command_id); + _network->removeTopic(_mqttPath, mqtt_topic_keypad_command_name); + _network->removeTopic(_mqttPath, mqtt_topic_keypad_command_code); + _network->removeTopic(_mqttPath, mqtt_topic_keypad_command_enabled); + _network->removeTopic(_mqttPath, mqtt_topic_keypad_command_result); + _network->removeTopic(_mqttPath, mqtt_topic_config_button_enabled); + _network->removeTopic(_mqttPath, mqtt_topic_config_led_enabled); + _network->removeTopic(_mqttPath, mqtt_topic_config_led_brightness); + _network->removeTopic(_mqttPath, mqtt_topic_config_auto_unlock); + _network->removeTopic(_mqttPath, mqtt_topic_config_auto_lock); + _network->removeTopic(_mqttPath, mqtt_topic_config_single_lock); + //_network->removeTopic(_mqttPath, mqtt_topic_presence); + } + if(_preferences->getBool(preference_keypad_control_enabled)) { - _network->subscribe(_mqttPath, mqtt_topic_keypad_command_action); - _network->subscribe(_mqttPath, mqtt_topic_keypad_command_id); - _network->subscribe(_mqttPath, mqtt_topic_keypad_command_name); - _network->subscribe(_mqttPath, mqtt_topic_keypad_command_code); - _network->subscribe(_mqttPath, mqtt_topic_keypad_command_enabled); + if(!_preferences->getBool(preference_disable_non_json, false)) + { + _network->subscribe(_mqttPath, mqtt_topic_keypad_command_action); + _network->subscribe(_mqttPath, mqtt_topic_keypad_command_id); + _network->subscribe(_mqttPath, mqtt_topic_keypad_command_name); + _network->subscribe(_mqttPath, mqtt_topic_keypad_command_code); + _network->subscribe(_mqttPath, mqtt_topic_keypad_command_enabled); + _network->initTopic(_mqttPath, mqtt_topic_keypad_command_action, "--"); + _network->initTopic(_mqttPath, mqtt_topic_keypad_command_id, "0"); + _network->initTopic(_mqttPath, mqtt_topic_keypad_command_name, "--"); + _network->initTopic(_mqttPath, mqtt_topic_keypad_command_code, "000000"); + _network->initTopic(_mqttPath, mqtt_topic_keypad_command_enabled, "1"); + } + _network->subscribe(_mqttPath, mqtt_topic_query_keypad); _network->subscribe(_mqttPath, mqtt_topic_keypad_json_action); - _network->initTopic(_mqttPath, mqtt_topic_keypad_command_action, "--"); - _network->initTopic(_mqttPath, mqtt_topic_keypad_command_id, "0"); - _network->initTopic(_mqttPath, mqtt_topic_keypad_command_name, "--"); - _network->initTopic(_mqttPath, mqtt_topic_keypad_command_code, "000000"); - _network->initTopic(_mqttPath, mqtt_topic_keypad_command_enabled, "1"); _network->initTopic(_mqttPath, mqtt_topic_query_keypad, "0"); _network->initTopic(_mqttPath, mqtt_topic_keypad_json_action, "--"); } @@ -104,8 +125,7 @@ void NetworkLock::onMqttDataReceived(const char* topic, byte* payload, const uns delay(200); restartEsp(RestartReason::RequestedViaMqtt); } - - if(comparePrefixedPath(topic, mqtt_topic_webserver_action)) + else if(comparePrefixedPath(topic, mqtt_topic_webserver_action)) { if(strcmp(value, "") == 0 || strcmp(value, "--") == 0) return; @@ -164,46 +184,50 @@ void NetworkLock::onMqttDataReceived(const char* topic, byte* payload, const uns } } - if(comparePrefixedPath(topic, mqtt_topic_keypad_command_action)) + if(!_preferences->getBool(preference_disable_non_json, false)) { - if(_keypadCommandReceivedReceivedCallback != nullptr) + if(comparePrefixedPath(topic, mqtt_topic_keypad_command_action)) { - if(strcmp(value, "--") == 0) return; + if(_keypadCommandReceivedReceivedCallback != nullptr) + { + if(strcmp(value, "--") == 0) return; - _keypadCommandReceivedReceivedCallback(value, _keypadCommandId, _keypadCommandName, _keypadCommandCode, _keypadCommandEnabled); + _keypadCommandReceivedReceivedCallback(value, _keypadCommandId, _keypadCommandName, _keypadCommandCode, _keypadCommandEnabled); - _keypadCommandId = 0; - _keypadCommandName = "--"; - _keypadCommandCode = "000000"; - _keypadCommandEnabled = 1; + _keypadCommandId = 0; + _keypadCommandName = "--"; + _keypadCommandCode = "000000"; + _keypadCommandEnabled = 1; - if(strcmp(value, "--") != 0) - { - publishString(mqtt_topic_keypad_command_action, "--"); + if(strcmp(value, "--") != 0) + { + publishString(mqtt_topic_keypad_command_action, "--"); + } + publishInt(mqtt_topic_keypad_command_id, _keypadCommandId); + publishString(mqtt_topic_keypad_command_name, _keypadCommandName); + publishString(mqtt_topic_keypad_command_code, _keypadCommandCode); + publishInt(mqtt_topic_keypad_command_enabled, _keypadCommandEnabled); } - publishInt(mqtt_topic_keypad_command_id, _keypadCommandId); - publishString(mqtt_topic_keypad_command_name, _keypadCommandName); - publishString(mqtt_topic_keypad_command_code, _keypadCommandCode); - publishInt(mqtt_topic_keypad_command_enabled, _keypadCommandEnabled); + } + else if(comparePrefixedPath(topic, mqtt_topic_keypad_command_id)) + { + _keypadCommandId = atoi(value); + } + else if(comparePrefixedPath(topic, mqtt_topic_keypad_command_name)) + { + _keypadCommandName = value; + } + else if(comparePrefixedPath(topic, mqtt_topic_keypad_command_code)) + { + _keypadCommandCode = value; + } + else if(comparePrefixedPath(topic, mqtt_topic_keypad_command_enabled)) + { + _keypadCommandEnabled = atoi(value); } } - else if(comparePrefixedPath(topic, mqtt_topic_keypad_command_id)) - { - _keypadCommandId = atoi(value); - } - else if(comparePrefixedPath(topic, mqtt_topic_keypad_command_name)) - { - _keypadCommandName = value; - } - else if(comparePrefixedPath(topic, mqtt_topic_keypad_command_code)) - { - _keypadCommandCode = value; - } - else if(comparePrefixedPath(topic, mqtt_topic_keypad_command_enabled)) - { - _keypadCommandEnabled = atoi(value); - } - else if(comparePrefixedPath(topic, mqtt_topic_query_config) && strcmp(value, "1") == 0) + + if(comparePrefixedPath(topic, mqtt_topic_query_config) && strcmp(value, "1") == 0) { _queryCommands = _queryCommands | QUERY_COMMAND_CONFIG; publishString(mqtt_topic_query_config, "0"); @@ -583,10 +607,15 @@ void NetworkLock::publishConfig(const NukiLock::Config &config) serializeJson(json, _buffer, _bufferSize); publishString(mqtt_topic_config_basic_json, _buffer); - publishBool(mqtt_topic_config_button_enabled, config.buttonEnabled == 1); - publishBool(mqtt_topic_config_led_enabled, config.ledEnabled == 1); - publishInt(mqtt_topic_config_led_brightness, config.ledBrightness); - publishBool(mqtt_topic_config_single_lock, config.singleLock == 1); + + if(!_preferences->getBool(preference_disable_non_json, false)) + { + publishBool(mqtt_topic_config_button_enabled, config.buttonEnabled == 1); + publishBool(mqtt_topic_config_led_enabled, config.ledEnabled == 1); + publishInt(mqtt_topic_config_led_brightness, config.ledBrightness); + publishBool(mqtt_topic_config_single_lock, config.singleLock == 1); + } + publishString(mqtt_topic_info_firmware_version, std::to_string(config.firmwareVersion[0]) + "." + std::to_string(config.firmwareVersion[1]) + "." + std::to_string(config.firmwareVersion[2])); publishString(mqtt_topic_info_hardware_version, std::to_string(config.hardwareRevision[0]) + "." + std::to_string(config.hardwareRevision[1])); } @@ -633,8 +662,12 @@ void NetworkLock::publishAdvancedConfig(const NukiLock::AdvancedConfig &config) serializeJson(json, _buffer, _bufferSize); publishString(mqtt_topic_config_advanced_json, _buffer); - publishBool(mqtt_topic_config_auto_unlock, config.autoUnLockDisabled == 0); - publishBool(mqtt_topic_config_auto_lock, config.autoLockEnabled == 1); + + if(!_preferences->getBool(preference_disable_non_json, false)) + { + publishBool(mqtt_topic_config_auto_unlock, config.autoUnLockDisabled == 0); + publishBool(mqtt_topic_config_auto_lock, config.autoLockEnabled == 1); + } } void NetworkLock::publishRssi(const int& rssi) @@ -746,16 +779,42 @@ void NetworkLock::publishKeypad(const std::list& entries, serializeJson(json, _buffer, _bufferSize); publishString(mqtt_topic_keypad_json, _buffer); - while(index < maxKeypadCodeCount) + if(!_preferences->getBool(preference_disable_non_json, false)) { - NukiLock::KeypadEntry entry; - memset(&entry, 0, sizeof(entry)); - String basePath = mqtt_topic_keypad; - basePath.concat("/code_"); - basePath.concat(std::to_string(index).c_str()); - publishKeypadEntry(basePath, entry); + while(index < maxKeypadCodeCount) + { + NukiLock::KeypadEntry entry; + memset(&entry, 0, sizeof(entry)); + String basePath = mqtt_topic_keypad; + basePath.concat("/code_"); + basePath.concat(std::to_string(index).c_str()); + publishKeypadEntry(basePath, entry); + + ++index; + } + } + else if (maxKeypadCodeCount > 0) + { + for(int i=0; iremoveTopic(codeTopic, "id"); + _network->removeTopic(codeTopic, "enabled"); + _network->removeTopic(codeTopic, "name"); + _network->removeTopic(codeTopic, "createdYear"); + _network->removeTopic(codeTopic, "createdMonth"); + _network->removeTopic(codeTopic, "createdDay"); + _network->removeTopic(codeTopic, "createdHour"); + _network->removeTopic(codeTopic, "createdMin"); + _network->removeTopic(codeTopic, "createdSec"); + _network->removeTopic(codeTopic, "lockCount"); + } - ++index; + _preferences->putUInt(preference_lock_max_keypad_code_count, 0); } } @@ -838,6 +897,7 @@ void NetworkLock::publishConfigCommandResult(const char* result) void NetworkLock::publishKeypadCommandResult(const char* result) { + if(_preferences->getBool(preference_disable_non_json, false)) return; publishString(mqtt_topic_keypad_command_result, result); } @@ -868,6 +928,7 @@ void NetworkLock::setConfigUpdateReceivedCallback(void (*configUpdateReceivedCal void NetworkLock::setKeypadCommandReceivedCallback(void (*keypadCommandReceivedReceivedCallback)(const char* command, const uint& id, const String& name, const String& code, const int& enabled)) { + if(_preferences->getBool(preference_disable_non_json, false)) return; _keypadCommandReceivedReceivedCallback = keypadCommandReceivedReceivedCallback; } @@ -994,6 +1055,8 @@ bool NetworkLock::publishString(const char *topic, const char *value) void NetworkLock::publishKeypadEntry(const String topic, NukiLock::KeypadEntry entry) { + if(_preferences->getBool(preference_disable_non_json, false)) return; + char codeName[sizeof(entry.name) + 1]; memset(codeName, 0, sizeof(codeName)); memcpy(codeName, entry.name, sizeof(entry.name)); diff --git a/src/NetworkOpener.cpp b/src/NetworkOpener.cpp index deb46421..86251271 100644 --- a/src/NetworkOpener.cpp +++ b/src/NetworkOpener.cpp @@ -50,20 +50,38 @@ void NetworkOpener::initialize() _network->subscribe(_mqttPath, mqtt_topic_query_lockstate); _network->subscribe(_mqttPath, mqtt_topic_query_battery); + if(_preferences->getBool(preference_disable_non_json, false)) + { + _network->removeTopic(_mqttPath, mqtt_topic_keypad_command_action); + _network->removeTopic(_mqttPath, mqtt_topic_keypad_command_id); + _network->removeTopic(_mqttPath, mqtt_topic_keypad_command_name); + _network->removeTopic(_mqttPath, mqtt_topic_keypad_command_code); + _network->removeTopic(_mqttPath, mqtt_topic_keypad_command_enabled); + _network->removeTopic(_mqttPath, mqtt_topic_config_button_enabled); + _network->removeTopic(_mqttPath, mqtt_topic_config_led_enabled); + _network->removeTopic(_mqttPath, mqtt_topic_config_sound_level); + _network->removeTopic(_mqttPath, mqtt_topic_keypad_command_result); + //_network->removeTopic(_mqttPath, mqtt_topic_presence); + } + if(_preferences->getBool(preference_keypad_control_enabled)) { - _network->subscribe(_mqttPath, mqtt_topic_keypad_command_action); - _network->subscribe(_mqttPath, mqtt_topic_keypad_command_id); - _network->subscribe(_mqttPath, mqtt_topic_keypad_command_name); - _network->subscribe(_mqttPath, mqtt_topic_keypad_command_code); - _network->subscribe(_mqttPath, mqtt_topic_keypad_command_enabled); + if(!_preferences->getBool(preference_disable_non_json, false)) + { + _network->subscribe(_mqttPath, mqtt_topic_keypad_command_action); + _network->subscribe(_mqttPath, mqtt_topic_keypad_command_id); + _network->subscribe(_mqttPath, mqtt_topic_keypad_command_name); + _network->subscribe(_mqttPath, mqtt_topic_keypad_command_code); + _network->subscribe(_mqttPath, mqtt_topic_keypad_command_enabled); + _network->initTopic(_mqttPath, mqtt_topic_keypad_command_action, "--"); + _network->initTopic(_mqttPath, mqtt_topic_keypad_command_id, "0"); + _network->initTopic(_mqttPath, mqtt_topic_keypad_command_name, "--"); + _network->initTopic(_mqttPath, mqtt_topic_keypad_command_code, "000000"); + _network->initTopic(_mqttPath, mqtt_topic_keypad_command_enabled, "1"); + } + _network->subscribe(_mqttPath, mqtt_topic_query_keypad); _network->subscribe(_mqttPath, mqtt_topic_keypad_json_action); - _network->initTopic(_mqttPath, mqtt_topic_keypad_command_action, "--"); - _network->initTopic(_mqttPath, mqtt_topic_keypad_command_id, "0"); - _network->initTopic(_mqttPath, mqtt_topic_keypad_command_name, "--"); - _network->initTopic(_mqttPath, mqtt_topic_keypad_command_code, "000000"); - _network->initTopic(_mqttPath, mqtt_topic_keypad_command_enabled, "1"); _network->initTopic(_mqttPath, mqtt_topic_query_keypad, "0"); _network->initTopic(_mqttPath, mqtt_topic_keypad_json_action, "--"); } @@ -127,46 +145,50 @@ void NetworkOpener::onMqttDataReceived(const char* topic, byte* payload, const u } } - if(comparePrefixedPath(topic, mqtt_topic_keypad_command_action)) + if(!_preferences->getBool(preference_disable_non_json, false)) { - if(_keypadCommandReceivedReceivedCallback != nullptr) + if(comparePrefixedPath(topic, mqtt_topic_keypad_command_action)) { - if(strcmp(value, "--") == 0) return; + if(_keypadCommandReceivedReceivedCallback != nullptr) + { + if(strcmp(value, "--") == 0) return; - _keypadCommandReceivedReceivedCallback(value, _keypadCommandId, _keypadCommandName, _keypadCommandCode, _keypadCommandEnabled); + _keypadCommandReceivedReceivedCallback(value, _keypadCommandId, _keypadCommandName, _keypadCommandCode, _keypadCommandEnabled); - _keypadCommandId = 0; - _keypadCommandName = "--"; - _keypadCommandCode = "000000"; - _keypadCommandEnabled = 1; + _keypadCommandId = 0; + _keypadCommandName = "--"; + _keypadCommandCode = "000000"; + _keypadCommandEnabled = 1; - if(strcmp(value, "--") != 0) - { - publishString(mqtt_topic_keypad_command_action, "--"); + if(strcmp(value, "--") != 0) + { + publishString(mqtt_topic_keypad_command_action, "--"); + } + publishInt(mqtt_topic_keypad_command_id, _keypadCommandId); + publishString(mqtt_topic_keypad_command_name, _keypadCommandName); + publishString(mqtt_topic_keypad_command_code, _keypadCommandCode); + publishInt(mqtt_topic_keypad_command_enabled, _keypadCommandEnabled); } - publishInt(mqtt_topic_keypad_command_id, _keypadCommandId); - publishString(mqtt_topic_keypad_command_name, _keypadCommandName); - publishString(mqtt_topic_keypad_command_code, _keypadCommandCode); - publishInt(mqtt_topic_keypad_command_enabled, _keypadCommandEnabled); + } + else if(comparePrefixedPath(topic, mqtt_topic_keypad_command_id)) + { + _keypadCommandId = atoi(value); + } + else if(comparePrefixedPath(topic, mqtt_topic_keypad_command_name)) + { + _keypadCommandName = value; + } + else if(comparePrefixedPath(topic, mqtt_topic_keypad_command_code)) + { + _keypadCommandCode = value; + } + else if(comparePrefixedPath(topic, mqtt_topic_keypad_command_enabled)) + { + _keypadCommandEnabled = atoi(value); } } - else if(comparePrefixedPath(topic, mqtt_topic_keypad_command_id)) - { - _keypadCommandId = atoi(value); - } - else if(comparePrefixedPath(topic, mqtt_topic_keypad_command_name)) - { - _keypadCommandName = value; - } - else if(comparePrefixedPath(topic, mqtt_topic_keypad_command_code)) - { - _keypadCommandCode = value; - } - else if(comparePrefixedPath(topic, mqtt_topic_keypad_command_enabled)) - { - _keypadCommandEnabled = atoi(value); - } - else if(comparePrefixedPath(topic, mqtt_topic_query_config) && strcmp(value, "1") == 0) + + if(comparePrefixedPath(topic, mqtt_topic_query_config) && strcmp(value, "1") == 0) { _queryCommands = _queryCommands | QUERY_COMMAND_CONFIG; publishString(mqtt_topic_query_config, "0"); @@ -494,7 +516,7 @@ void NetworkOpener::publishAuthorizationInfo(const std::listgetBool(preference_disable_non_json, false)) + { + publishBool(mqtt_topic_config_button_enabled, config.buttonEnabled == 1); + publishBool(mqtt_topic_config_led_enabled, config.ledFlashEnabled == 1); + } + publishString(mqtt_topic_info_firmware_version, std::to_string(config.firmwareVersion[0]) + "." + std::to_string(config.firmwareVersion[1]) + "." + std::to_string(config.firmwareVersion[2])); publishString(mqtt_topic_info_hardware_version, std::to_string(config.hardwareRevision[0]) + "." + std::to_string(config.hardwareRevision[1])); } @@ -624,7 +651,11 @@ void NetworkOpener::publishAdvancedConfig(const NukiOpener::AdvancedConfig &conf serializeJson(json, _buffer, _bufferSize); publishString(mqtt_topic_config_advanced_json, _buffer); - publishUInt(mqtt_topic_config_sound_level, config.soundLevel); + + if(!_preferences->getBool(preference_disable_non_json, false)) + { + publishUInt(mqtt_topic_config_sound_level, config.soundLevel); + } } void NetworkOpener::publishRssi(const int &rssi) @@ -668,7 +699,7 @@ void NetworkOpener::publishKeypad(const std::list& entrie basePath.concat("/code_"); basePath.concat(std::to_string(index).c_str()); publishKeypadEntry(basePath, entry); - + auto jsonEntry = json.add(); jsonEntry["codeId"] = entry.codeId; @@ -750,16 +781,42 @@ void NetworkOpener::publishKeypad(const std::list& entrie serializeJson(json, _buffer, _bufferSize); publishString(mqtt_topic_keypad_json, _buffer); - while(index < maxKeypadCodeCount) + if(!_preferences->getBool(preference_disable_non_json, false)) { - NukiLock::KeypadEntry entry; - memset(&entry, 0, sizeof(entry)); - String basePath = mqtt_topic_keypad; - basePath.concat("/code_"); - basePath.concat(std::to_string(index).c_str()); - publishKeypadEntry(basePath, entry); + while(index < maxKeypadCodeCount) + { + NukiLock::KeypadEntry entry; + memset(&entry, 0, sizeof(entry)); + String basePath = mqtt_topic_keypad; + basePath.concat("/code_"); + basePath.concat(std::to_string(index).c_str()); + publishKeypadEntry(basePath, entry); + + ++index; + } + } + else if (maxKeypadCodeCount > 0) + { + for(int i=0; iremoveTopic(codeTopic, "id"); + _network->removeTopic(codeTopic, "enabled"); + _network->removeTopic(codeTopic, "name"); + _network->removeTopic(codeTopic, "createdYear"); + _network->removeTopic(codeTopic, "createdMonth"); + _network->removeTopic(codeTopic, "createdDay"); + _network->removeTopic(codeTopic, "createdHour"); + _network->removeTopic(codeTopic, "createdMin"); + _network->removeTopic(codeTopic, "createdSec"); + _network->removeTopic(codeTopic, "lockCount"); + } - ++index; + _preferences->putUInt(preference_lock_max_keypad_code_count, 0); } } @@ -842,6 +899,7 @@ void NetworkOpener::publishConfigCommandResult(const char* result) void NetworkOpener::publishKeypadCommandResult(const char* result) { + if(_preferences->getBool(preference_disable_non_json, false)) return; publishString(mqtt_topic_keypad_command_result, result); } @@ -872,6 +930,7 @@ void NetworkOpener::setConfigUpdateReceivedCallback(void (*configUpdateReceivedC void NetworkOpener::setKeypadCommandReceivedCallback(void (*keypadCommandReceivedReceivedCallback)(const char* command, const uint& id, const String& name, const String& code, const int& enabled)) { + if(_preferences->getBool(preference_disable_non_json, false)) return; _keypadCommandReceivedReceivedCallback = keypadCommandReceivedReceivedCallback; } @@ -928,6 +987,8 @@ void NetworkOpener::publishString(const char* topic, const char* value) void NetworkOpener::publishKeypadEntry(const String topic, NukiLock::KeypadEntry entry) { + if(_preferences->getBool(preference_disable_non_json, false)) return; + char codeName[sizeof(entry.name) + 1]; memset(codeName, 0, sizeof(codeName)); memcpy(codeName, entry.name, sizeof(entry.name)); diff --git a/src/NukiOpenerWrapper.cpp b/src/NukiOpenerWrapper.cpp index 648108ef..c3f9015d 100644 --- a/src/NukiOpenerWrapper.cpp +++ b/src/NukiOpenerWrapper.cpp @@ -32,7 +32,7 @@ NukiOpenerWrapper::NukiOpenerWrapper(const std::string& deviceName, NukiDeviceId network->setLockActionReceivedCallback(nukiOpenerInst->onLockActionReceivedCallback); network->setConfigUpdateReceivedCallback(nukiOpenerInst->onConfigUpdateReceivedCallback); - network->setKeypadCommandReceivedCallback(nukiOpenerInst->onKeypadCommandReceivedCallback); + if(_preferences->getBool(preference_disable_non_json, false)) network->setKeypadCommandReceivedCallback(nukiOpenerInst->onKeypadCommandReceivedCallback); network->setKeypadJsonCommandReceivedCallback(nukiOpenerInst->onKeypadJsonCommandReceivedCallback); _gpio->addCallback(NukiOpenerWrapper::gpioActionCallback); @@ -516,7 +516,7 @@ void NukiOpenerWrapper::updateAuthData(bool retrieved) { log.resize(_preferences->getInt(preference_authlog_max_entries, 3)); } - + if(log.size() > 0) { _network->publishAuthorizationInfo(log, true); @@ -567,7 +567,7 @@ void NukiOpenerWrapper::updateKeypad(bool retrieved) } uint keypadCount = entries.size(); - if(keypadCount > _maxKeypadCodeCount) + if(keypadCount > _maxKeypadCodeCount && !_preferences->getBool(preference_disable_non_json, false)) { _maxKeypadCodeCount = keypadCount; _preferences->putUInt(preference_lock_max_keypad_code_count, _maxKeypadCodeCount); @@ -1354,6 +1354,8 @@ void NukiOpenerWrapper::gpioActionCallback(const GpioAction &action, const int& void NukiOpenerWrapper::onKeypadCommandReceived(const char *command, const uint &id, const String &name, const String &code, const int& enabled) { + if(_preferences->getBool(preference_disable_non_json, false)) return; + if(!_preferences->getBool(preference_keypad_control_enabled)) { _network->publishKeypadCommandResult("KeypadControlDisabled"); diff --git a/src/NukiWrapper.cpp b/src/NukiWrapper.cpp index e9236800..7d497733 100644 --- a/src/NukiWrapper.cpp +++ b/src/NukiWrapper.cpp @@ -32,7 +32,7 @@ NukiWrapper::NukiWrapper(const std::string& deviceName, NukiDeviceId* deviceId, network->setLockActionReceivedCallback(nukiInst->onLockActionReceivedCallback); network->setConfigUpdateReceivedCallback(nukiInst->onConfigUpdateReceivedCallback); - network->setKeypadCommandReceivedCallback(nukiInst->onKeypadCommandReceivedCallback); + if(_preferences->getBool(preference_disable_non_json, false)) network->setKeypadCommandReceivedCallback(nukiInst->onKeypadCommandReceivedCallback); network->setKeypadJsonCommandReceivedCallback(nukiInst->onKeypadJsonCommandReceivedCallback); network->setTimeControlCommandReceivedCallback(nukiInst->onTimeControlCommandReceivedCallback); @@ -202,7 +202,7 @@ void NukiWrapper::update() { _waitKeypadUpdateTs = 0; updateKeypad(true); - } + } if(_waitTimeControlUpdateTs != 0 && ts > _waitTimeControlUpdateTs) { _waitTimeControlUpdateTs = 0; @@ -492,12 +492,12 @@ void NukiWrapper::updateAuthData(bool retrieved) std::list log; _nukiLock.getLogEntries(&log); - + if(log.size() > _preferences->getInt(preference_authlog_max_entries, 3)) { log.resize(_preferences->getInt(preference_authlog_max_entries, 3)); } - + if(log.size() > 0) { _network->publishAuthorizationInfo(log, true); @@ -508,7 +508,7 @@ void NukiWrapper::updateAuthData(bool retrieved) { std::list log; _nukiLock.getLogEntries(&log); - + if(log.size() > _preferences->getInt(preference_authlog_max_entries, MAX_AUTHLOG)) { log.resize(_preferences->getInt(preference_authlog_max_entries, MAX_AUTHLOG)); @@ -522,14 +522,14 @@ void NukiWrapper::updateAuthData(bool retrieved) _network->publishAuthorizationInfo(log, false); } } - + postponeBleWatchdog(); } void NukiWrapper::updateKeypad(bool retrieved) { if(!_preferences->getBool(preference_keypad_info_enabled)) return; - + if(!retrieved) { Log->print(F("Querying lock keypad: ")); @@ -549,14 +549,14 @@ void NukiWrapper::updateKeypad(bool retrieved) Log->println(entries.size()); entries.sort([](const NukiLock::KeypadEntry& a, const NukiLock::KeypadEntry& b) { return a.codeId < b.codeId; }); - + if(entries.size() > _preferences->getInt(preference_keypad_max_entries, MAX_KEYPAD)) { entries.resize(_preferences->getInt(preference_keypad_max_entries, MAX_KEYPAD)); } uint keypadCount = entries.size(); - if(keypadCount > _maxKeypadCodeCount) + if(keypadCount > _maxKeypadCodeCount && !_preferences->getBool(preference_disable_non_json, false)) { _maxKeypadCodeCount = keypadCount; _preferences->putUInt(preference_lock_max_keypad_code_count, _maxKeypadCodeCount); @@ -601,12 +601,12 @@ void NukiWrapper::updateTimeControl(bool retrieved) Log->println(timeControlEntries.size()); timeControlEntries.sort([](const NukiLock::TimeControlEntry& a, const NukiLock::TimeControlEntry& b) { return a.entryId < b.entryId; }); - + if(timeControlEntries.size() > _preferences->getInt(preference_timecontrol_max_entries, MAX_TIMECONTROL)) { timeControlEntries.resize(_preferences->getInt(preference_timecontrol_max_entries, MAX_TIMECONTROL)); } - + _network->publishTimeControl(timeControlEntries); _timeControlIds.clear(); @@ -1348,6 +1348,8 @@ void NukiWrapper::gpioActionCallback(const GpioAction &action, const int& pin) void NukiWrapper::onKeypadCommandReceived(const char *command, const uint &id, const String &name, const String &code, const int& enabled) { + if(_preferences->getBool(preference_disable_non_json, false)) return; + if(!_preferences->getBool(preference_keypad_control_enabled)) { _network->publishKeypadCommandResult("KeypadControlDisabled"); @@ -1740,7 +1742,7 @@ void NukiWrapper::onKeypadJsonCommandReceived(const char *value) entry.codeId = codeId; size_t nameLen = strlen(name); memcpy(&entry.name, name, nameLen > 20 ? 20 : nameLen); - + if(code) entry.code = code; else { @@ -1851,7 +1853,7 @@ void NukiWrapper::onTimeControlCommandReceived(const char *value) const char *time = json["time"].as(); const char *lockAct = json["lockAction"].as(); NukiLock::LockAction timeControlLockAction; - + if(lockAct) { timeControlLockAction = nukiInst->lockActionToEnum(lockAct); diff --git a/src/PreferencesKeys.h b/src/PreferencesKeys.h index b3c77a45..6e4b7b2a 100644 --- a/src/PreferencesKeys.h +++ b/src/PreferencesKeys.h @@ -82,6 +82,7 @@ #define preference_bootloop_counter (char*)"btlpcounter" #define preference_enable_bootloop_reset (char*)"enabtlprst" #define preference_buffer_size (char*)"buffsize" +#define preference_disable_non_json (char*)"disnonjson" class DebugPreferences { @@ -99,7 +100,7 @@ class DebugPreferences preference_keypad_info_enabled, preference_acl, preference_timecontrol_control_enabled, preference_timecontrol_info_enabled, preference_conf_lock_basic_acl, preference_conf_lock_advanced_acl, preference_conf_opener_basic_acl, preference_conf_opener_advanced_acl, 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, + preference_cred_password, preference_publish_authdata, preference_publish_debug_info, preference_presence_detection_timeout, preference_disable_non_json, preference_has_mac_saved, preference_has_mac_byte_0, preference_has_mac_byte_1, preference_has_mac_byte_2, preference_latest_version, preference_task_size_network, preference_task_size_nuki, preference_task_size_pd, preference_authlog_max_entries, preference_keypad_max_entries, preference_timecontrol_max_entries }; @@ -115,7 +116,7 @@ class DebugPreferences preference_started_before, preference_mqtt_log_enabled, preference_check_updates, preference_lock_enabled, preference_opener_enabled, preference_opener_continuous_mode, preference_enable_bootloop_reset, preference_webserver_enabled, preference_find_best_rssi, preference_restart_on_disconnect, preference_keypad_control_enabled, preference_keypad_info_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 + preference_publish_authdata, preference_has_mac_saved, preference_publish_debug_info, preference_network_wifi_fallback_disabled, preference_disable_non_json }; const bool isRedacted(const char* key) const diff --git a/src/WebCfgServer.cpp b/src/WebCfgServer.cpp index 3932b435..33c9b8a6 100644 --- a/src/WebCfgServer.cpp +++ b/src/WebCfgServer.cpp @@ -413,6 +413,11 @@ bool WebCfgServer::processArgs(String& message) _preferences->putBool(preference_check_updates, (value == "1")); configChanged = true; } + else if(key == "DISNONJSON") + { + _preferences->putBool(preference_disable_non_json, (value == "1")); + configChanged = true; + } else if(key == "DHCPENA") { _preferences->putBool(preference_ip_dhcp_enabled, (value == "1")); @@ -1319,6 +1324,7 @@ void WebCfgServer::buildMqttConfigHtml(String &response) printCheckBox(response, "RSTDISC", "Restart on disconnect", _preferences->getBool(preference_restart_on_disconnect), ""); printCheckBox(response, "MQTTLOG", "Enable MQTT logging", _preferences->getBool(preference_mqtt_log_enabled), ""); printCheckBox(response, "CHECKUPDATE", "Check for Firmware Updates every 24h", _preferences->getBool(preference_check_updates), ""); + printCheckBox(response, "DISNONJSON", "Disable some extraneous non-JSON topics", _preferences->getBool(preference_disable_non_json), ""); response.concat(""); response.concat("* If no encryption is configured for the MQTT broker, leave empty. Only supported for Wi-Fi connections.

"); From 960399536d916f68b6ca804580ab8d32f944ef67 Mon Sep 17 00:00:00 2001 From: iranl Date: Wed, 29 May 2024 20:25:00 +0200 Subject: [PATCH 2/2] Add battery JSON --- README.md | 2 ++ src/MqttTopics.h | 2 ++ src/Network.cpp | 20 ++++++++----- src/NetworkLock.cpp | 70 ++++++++++++++++++++++++++++++++----------- src/NetworkOpener.cpp | 37 ++++++++++++++++++++--- 5 files changed, 101 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 55361dc3..8b2ae846 100644 --- a/README.md +++ b/README.md @@ -264,6 +264,8 @@ In a browser navigate to the IP address assigned to the ESP32. - battery/maxTurnCurrent: The highest current of the turn motor during the last lock action (A) (Lock only). - battery/lockDistance: The total distance during the last lock action in centidegrees (Lock only). - battery/keypadCritical: 1 if the battery level of a connected keypad is critical, otherwise 0. +- battery/basicJson: The current battery state (critical, charging, level and keypad critical) of the Nuki Lock/Opener as JSON data. +- battery/advancedJson: : The current battery state (critical, batteryDrain, batteryVoltage, lockAction, startVoltage, lowestVoltage, lockDistance, startTemperature, maxTurnCurrent and batteryResistance) of the Nuki Lock/Opener as JSON data. ### Keypad diff --git a/src/MqttTopics.h b/src/MqttTopics.h index af639a2a..ff2caa3c 100644 --- a/src/MqttTopics.h +++ b/src/MqttTopics.h @@ -48,6 +48,8 @@ #define mqtt_topic_battery_max_turn_current "/battery/maxTurnCurrent" #define mqtt_topic_battery_lock_distance "/battery/lockDistance" #define mqtt_topic_battery_keypad_critical "/battery/keypadCritical" +#define mqtt_topic_battery_basic_json "/battery/basicJson" +#define mqtt_topic_battery_advanced_json "/battery/advancedJson" #define mqtt_topic_keypad "/keypad" #define mqtt_topic_keypad_command_action "/keypad/command/action" diff --git a/src/Network.cpp b/src/Network.cpp index 5359cfda..6ae2b05c 100644 --- a/src/Network.cpp +++ b/src/Network.cpp @@ -806,14 +806,15 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "Battery low", name, baseTopic, - String("~") + mqtt_topic_battery_critical, + String("~") + mqtt_topic_battery_basic_json, deviceType, "battery", "", "diagnostic", "", {{(char*)"pl_on", (char*)"1"}, - {(char*)"pl_off", (char*)"0"}}); + {(char*)"pl_off", (char*)"0"}, + {(char*)"val_tpl", (char*)"{{value_json.critical}}" }}); // Battery voltage publishHassTopic("sensor", @@ -823,13 +824,14 @@ void Network::publishHASSConfig(char* deviceType, const char* baseTopic, char* n "Battery voltage", name, baseTopic, - String("~") + mqtt_topic_battery_voltage, + String("~") + mqtt_topic_battery_advanced_json, deviceType, "voltage", "measurement", "diagnostic", "", - { {(char*)"unit_of_meas", (char*)"V"} }); + { {(char*)"unit_of_meas", (char*)"V"}, + {(char*)"val_tpl", (char*)"{{value_json.level}}" }}); // Trigger publishHassTopic("sensor", @@ -1333,13 +1335,14 @@ void Network::publishHASSConfigAdditionalLockEntities(char *deviceType, const ch "Battery level", name, baseTopic, - String("~") + mqtt_topic_battery_level, + String("~") + mqtt_topic_battery_basic_json, deviceType, "battery", "measurement", "diagnostic", "", - { {(char*)"unit_of_meas", (char*)"%"} }); + { {(char*)"unit_of_meas", (char*)"%"}, + {(char*)"val_tpl", (char*)"{{value_json.level}}" }}); if((int)basicLockConfigAclPrefs[7] == 1) { @@ -3032,14 +3035,15 @@ void Network::publishHASSConfigKeypad(char *deviceType, const char *baseTopic, c "Keypad battery low", name, baseTopic, - String("~") + mqtt_topic_battery_keypad_critical, + String("~") + mqtt_topic_battery_basic_json, deviceType, "battery", "", "diagnostic", "", {{(char*)"pl_on", (char*)"1"}, - {(char*)"pl_off", (char*)"0"}}); + {(char*)"pl_off", (char*)"0"}, + {(char*)"val_tpl", (char*)"{{value_json.keypadCritical}}" }}); // Query Keypad publishHassTopic("button", diff --git a/src/NetworkLock.cpp b/src/NetworkLock.cpp index b7ed93b9..da3d7a68 100644 --- a/src/NetworkLock.cpp +++ b/src/NetworkLock.cpp @@ -77,6 +77,14 @@ void NetworkLock::initialize() _network->removeTopic(_mqttPath, mqtt_topic_config_auto_unlock); _network->removeTopic(_mqttPath, mqtt_topic_config_auto_lock); _network->removeTopic(_mqttPath, mqtt_topic_config_single_lock); + _network->removeTopic(_mqttPath, mqtt_topic_battery_level); + _network->removeTopic(_mqttPath, mqtt_topic_battery_critical); + _network->removeTopic(_mqttPath, mqtt_topic_battery_charging); + _network->removeTopic(_mqttPath, mqtt_topic_battery_voltage); + _network->removeTopic(_mqttPath, mqtt_topic_battery_drain); + _network->removeTopic(_mqttPath, mqtt_topic_battery_max_turn_current); + _network->removeTopic(_mqttPath, mqtt_topic_battery_lock_distance); + _network->removeTopic(_mqttPath, mqtt_topic_battery_keypad_critical); //_network->removeTopic(_mqttPath, mqtt_topic_presence); } @@ -291,6 +299,7 @@ void NetworkLock::publishKeyTurnerState(const NukiLock::KeyTurnerState& keyTurne memset(&str, 0, sizeof(str)); JsonDocument json; + JsonDocument jsonBattery; lockstateToString(keyTurnerState.lockState, str); @@ -358,27 +367,26 @@ void NetworkLock::publishKeyTurnerState(const NukiLock::KeyTurnerState& keyTurne json["door_sensor_state"] = str; - if(_firstTunerStatePublish || keyTurnerState.criticalBatteryState != lastKeyTurnerState.criticalBatteryState) + bool critical = (keyTurnerState.criticalBatteryState & 0b00000001) > 0; + bool charging = (keyTurnerState.criticalBatteryState & 0b00000010) > 0; + uint8_t level = (keyTurnerState.criticalBatteryState & 0b11111100) >> 1; + bool keypadCritical = (keyTurnerState.accessoryBatteryState & (1 << 7)) != 0 ? (keyTurnerState.accessoryBatteryState & (1 << 6)) != 0 : false; + + jsonBattery["critical"] = critical ? "1" : "0"; + jsonBattery["charging"] = charging ? "1" : "0"; + jsonBattery["level"] = level; + jsonBattery["keypadCritical"] = keypadCritical ? "1" : "0"; + + if((_firstTunerStatePublish || keyTurnerState.criticalBatteryState != lastKeyTurnerState.criticalBatteryState) && !_preferences->getBool(preference_disable_non_json, false)) { - 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); } - if(_firstTunerStatePublish || keyTurnerState.accessoryBatteryState != lastKeyTurnerState.accessoryBatteryState) + if((_firstTunerStatePublish || keyTurnerState.accessoryBatteryState != lastKeyTurnerState.accessoryBatteryState) && !_preferences->getBool(preference_disable_non_json, false)) { - 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, keypadCritical); } json["auth_id"] = _authId; @@ -386,6 +394,9 @@ void NetworkLock::publishKeyTurnerState(const NukiLock::KeyTurnerState& keyTurne serializeJson(json, _buffer, _bufferSize); publishString(mqtt_topic_lock_json, _buffer); + + serializeJson(jsonBattery, _buffer, _bufferSize); + publishString(mqtt_topic_battery_basic_json, _buffer); _firstTunerStatePublish = false; } @@ -552,10 +563,33 @@ void NetworkLock::publishLockstateCommandResult(const char *resultStr) void NetworkLock::publishBatteryReport(const NukiLock::BatteryReport& batteryReport) { - publishFloat(mqtt_topic_battery_voltage, (float)batteryReport.batteryVoltage / 1000.0); - publishInt(mqtt_topic_battery_drain, batteryReport.batteryDrain); // milliwatt seconds - publishFloat(mqtt_topic_battery_max_turn_current, (float)batteryReport.maxTurnCurrent / 1000.0); - publishInt(mqtt_topic_battery_lock_distance, batteryReport.lockDistance); // degrees + if(!_preferences->getBool(preference_disable_non_json, false)) + { + publishFloat(mqtt_topic_battery_voltage, (float)batteryReport.batteryVoltage / 1000.0); + publishInt(mqtt_topic_battery_drain, batteryReport.batteryDrain); // milliwatt seconds + publishFloat(mqtt_topic_battery_max_turn_current, (float)batteryReport.maxTurnCurrent / 1000.0); + publishInt(mqtt_topic_battery_lock_distance, batteryReport.lockDistance); // degrees + } + + char str[50]; + memset(&str, 0, sizeof(str)); + + JsonDocument json; + + json["batteryDrain"] = batteryReport.batteryDrain; + json["batteryVoltage"] = (float)batteryReport.batteryVoltage / 1000.0; + json["critical"] = batteryReport.criticalBatteryState; + lockactionToString(batteryReport.lockAction, str); + json["lockAction"] = str; + json["startVoltage"] = (float)batteryReport.startVoltage / 1000.0; + json["lowestVoltage"] = (float)batteryReport.lowestVoltage / 1000.0; + json["lockDistance"] = (float)batteryReport.lockDistance / 1000.0; + json["startTemperature"] = batteryReport.startTemperature; + json["maxTurnCurrent"] = (float)batteryReport.maxTurnCurrent / 1000.0; + json["batteryResistance"] = (float)batteryReport.batteryResistance / 1000.0; + + serializeJson(json, _buffer, _bufferSize); + publishString(mqtt_topic_battery_advanced_json, _buffer); } void NetworkLock::publishConfig(const NukiLock::Config &config) diff --git a/src/NetworkOpener.cpp b/src/NetworkOpener.cpp index 86251271..f06f01db 100644 --- a/src/NetworkOpener.cpp +++ b/src/NetworkOpener.cpp @@ -61,6 +61,11 @@ void NetworkOpener::initialize() _network->removeTopic(_mqttPath, mqtt_topic_config_led_enabled); _network->removeTopic(_mqttPath, mqtt_topic_config_sound_level); _network->removeTopic(_mqttPath, mqtt_topic_keypad_command_result); + _network->removeTopic(_mqttPath, mqtt_topic_battery_level); + _network->removeTopic(_mqttPath, mqtt_topic_battery_critical); + _network->removeTopic(_mqttPath, mqtt_topic_battery_charging); + _network->removeTopic(_mqttPath, mqtt_topic_battery_voltage); + _network->removeTopic(_mqttPath, mqtt_topic_battery_keypad_critical); //_network->removeTopic(_mqttPath, mqtt_topic_presence); } @@ -254,6 +259,7 @@ void NetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& keyTurn memset(&str, 0, sizeof(str)); JsonDocument json; + JsonDocument jsonBattery; lockstateToString(keyTurnerState.lockState, str); @@ -327,9 +333,11 @@ void NetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& keyTurn json["door_sensor_state"] = str; - if(_firstTunerStatePublish || keyTurnerState.criticalBatteryState != lastKeyTurnerState.criticalBatteryState) + bool critical = (keyTurnerState.criticalBatteryState & 0b00000001) > 0; + jsonBattery["critical"] = critical ? "1" : "0"; + + if((_firstTunerStatePublish || keyTurnerState.criticalBatteryState != lastKeyTurnerState.criticalBatteryState) && !_preferences->getBool(preference_disable_non_json, false)) { - bool critical = (keyTurnerState.criticalBatteryState & 0b00000001) > 0; publishBool(mqtt_topic_battery_critical, critical); } @@ -339,6 +347,9 @@ void NetworkOpener::publishKeyTurnerState(const NukiOpener::OpenerState& keyTurn serializeJson(json, _buffer, _bufferSize); publishString(mqtt_topic_lock_json, _buffer); + serializeJson(jsonBattery, _buffer, _bufferSize); + publishString(mqtt_topic_battery_basic_json, _buffer); + _firstTunerStatePublish = false; } @@ -543,7 +554,25 @@ void NetworkOpener::publishLockstateCommandResult(const char *resultStr) void NetworkOpener::publishBatteryReport(const NukiOpener::BatteryReport& batteryReport) { - publishFloat(mqtt_topic_battery_voltage, (float)batteryReport.batteryVoltage / 1000.0); + if(!_preferences->getBool(preference_disable_non_json, false)) + { + publishFloat(mqtt_topic_battery_voltage, (float)batteryReport.batteryVoltage / 1000.0); + } + + char str[50]; + memset(&str, 0, sizeof(str)); + + JsonDocument json; + + json["batteryVoltage"] = (float)batteryReport.batteryVoltage / 1000.0; + json["critical"] = batteryReport.criticalBatteryState; + lockactionToString(batteryReport.lockAction, str); + json["lockAction"] = str; + json["startVoltage"] = (float)batteryReport.startVoltage / 1000.0; + json["lowestVoltage"] = (float)batteryReport.lowestVoltage / 1000.0; + + serializeJson(json, _buffer, _bufferSize); + publishString(mqtt_topic_battery_advanced_json, _buffer); } void NetworkOpener::publishConfig(const NukiOpener::Config &config) @@ -595,7 +624,7 @@ void NetworkOpener::publishConfig(const NukiOpener::Config &config) serializeJson(json, _buffer, _bufferSize); publishString(mqtt_topic_config_basic_json, _buffer); - + if(!_preferences->getBool(preference_disable_non_json, false)) { publishBool(mqtt_topic_config_button_enabled, config.buttonEnabled == 1);