From 2094595e78f5658aa34a23120fa6b3d108036dc4 Mon Sep 17 00:00:00 2001 From: technyon Date: Sun, 5 Feb 2023 12:11:10 +0100 Subject: [PATCH] add /info endpoint to publish preference content --- NukiOpenerWrapper.cpp | 5 ++ NukiOpenerWrapper.h | 1 + NukiWrapper.cpp | 5 ++ NukiWrapper.h | 1 + PreferencesKeys.h | 111 ++++++++++++++++++++++++++++++++++++++++++ WebCfgServer.cpp | 26 +++++++++- WebCfgServer.h | 1 + 7 files changed, 149 insertions(+), 1 deletion(-) diff --git a/NukiOpenerWrapper.cpp b/NukiOpenerWrapper.cpp index 7ba2685..aa82aa7 100644 --- a/NukiOpenerWrapper.cpp +++ b/NukiOpenerWrapper.cpp @@ -205,6 +205,11 @@ void NukiOpenerWrapper::update() memcpy(&_lastKeyTurnerState, &_keyTurnerState, sizeof(NukiOpener::OpenerState)); } +bool NukiOpenerWrapper::isPinSet() +{ + return _nukiOpener.getSecurityPincode() != 0; +} + void NukiOpenerWrapper::setPin(const uint16_t pin) { _nukiOpener.saveSecurityPincode(pin); diff --git a/NukiOpenerWrapper.h b/NukiOpenerWrapper.h index 152d9cf..8290624 100644 --- a/NukiOpenerWrapper.h +++ b/NukiOpenerWrapper.h @@ -15,6 +15,7 @@ public: void initialize(); void update(); + bool isPinSet(); void setPin(const uint16_t pin); void unpair(); diff --git a/NukiWrapper.cpp b/NukiWrapper.cpp index 7c72a76..805bf13 100644 --- a/NukiWrapper.cpp +++ b/NukiWrapper.cpp @@ -251,6 +251,11 @@ void NukiWrapper::unlatch() _nextLockAction = NukiLock::LockAction::Unlatch; } +bool NukiWrapper::isPinSet() +{ + return _nukiLock.getSecurityPincode() != 0; +} + void NukiWrapper::setPin(const uint16_t pin) { _nukiLock.saveSecurityPincode(pin); diff --git a/NukiWrapper.h b/NukiWrapper.h index 6a6e758..6b03b30 100644 --- a/NukiWrapper.h +++ b/NukiWrapper.h @@ -19,6 +19,7 @@ public: void unlock(); void unlatch(); + bool isPinSet(); void setPin(const uint16_t pin); void unpair(); diff --git a/PreferencesKeys.h b/PreferencesKeys.h index 806bf44..5a4b3bd 100644 --- a/PreferencesKeys.h +++ b/PreferencesKeys.h @@ -1,5 +1,7 @@ #pragma once +#include + #define preference_started_before "run" #define preference_deviceId "deviceId" #define preference_mqtt_broker "mqttbroker" @@ -41,5 +43,114 @@ #define preference_has_mac_byte_1 "macb1" #define preference_has_mac_byte_2 "macb2" +class DebugPreferences +{ +private: + std::vector _keys = + { + preference_started_before, preference_deviceId, preference_mqtt_broker, preference_mqtt_broker_port, preference_mqtt_user, preference_mqtt_password, preference_mqtt_log_enabled, preference_lock_enabled, preference_mqtt_lock_path, preference_opener_enabled, preference_mqtt_opener_path, preference_max_keypad_code_count, preference_mqtt_ca, preference_mqtt_crt, preference_mqtt_key, preference_mqtt_hass_discovery, preference_network_hardware, preference_network_hardware_gpio, preference_rssi_publish_interval, preference_hostname, preference_network_timeout, preference_restart_on_disconnect, preference_restart_timer, preference_restart_ble_beacon_lost, preference_query_interval_lockstate, preference_query_interval_battery, preference_query_interval_keypad, preference_keypad_control_enabled, preference_register_as_app, preference_command_nr_of_retries, preference_command_retry_delay, preference_cred_user, preference_cred_password, preference_publish_authdata, preference_gpio_locking_enabled, preference_presence_detection_timeout, preference_has_mac_saved, preference_has_mac_byte_0, preference_has_mac_byte_1, preference_has_mac_byte_2, + }; + std::vector _redact = + { + preference_mqtt_user, preference_mqtt_password, + preference_mqtt_ca, preference_mqtt_crt, preference_mqtt_key, + preference_cred_user, preference_cred_password, + }; + std::vector _boolPrefs = + { + preference_started_before, preference_mqtt_log_enabled, preference_lock_enabled, preference_opener_enabled, + preference_restart_on_disconnect, preference_keypad_control_enabled, preference_register_as_app, + preference_publish_authdata, preference_gpio_locking_enabled, preference_has_mac_saved, + }; + const bool isRedacted(const char* key) const + { + return std::find(_redact.begin(), _redact.end(), key) != _redact.end(); + } + const String redact(const String s) const + { + return s == "" ? "" : "***"; + } + const String redact(const int i) const + { + return i == 0 ? "" : "***"; + } + const String redact(const uint i) const + { + return i == 0 ? "" : "***"; + } + const void appendPreferenceInt(Preferences *preferences, String& s, const char* description, const char* key) + { + s.concat(description); + s.concat(": "); + s.concat(isRedacted(key) ? redact(preferences->getInt(key)) : String(preferences->getInt(key))); + s.concat("\n"); + } + const void appendPreferenceUInt(Preferences *preferences, String& s, const char* description, const char* key) + { + s.concat(description); + s.concat(": "); + s.concat(isRedacted(key) ? redact(preferences->getUInt(key)) : String(preferences->getUInt(key))); + s.concat("\n"); + } + const void appendPreferenceBool(Preferences *preferences, String& s, const char* description, const char* key) + { + s.concat(description); + s.concat(": "); + s.concat(preferences->getBool(key) ? "true" : "false"); + s.concat("\n"); + } + const void appendPreferenceString(Preferences *preferences, String& s, const char* description, const char* key) + { + s.concat(description); + s.concat(": "); + s.concat(isRedacted(key) ? redact(preferences->getString(key)) : preferences->getString(key)); + s.concat("\n"); + } + + const void appendPreference(Preferences *preferences, String& s, const char* key) + { + if(std::find(_boolPrefs.begin(), _boolPrefs.end(), key) != _boolPrefs.end()) + { + appendPreferenceBool(preferences, s, key, key); + return; + } + + switch(preferences->getType(key)) + { + case PT_I8: + case PT_I16: + case PT_I32: + case PT_I64: + appendPreferenceInt(preferences, s, key, key); + break; + case PT_U8: + case PT_U16: + case PT_U32: + case PT_U64: + appendPreferenceUInt(preferences, s, key, key); + break; + case PT_STR: + appendPreferenceString(preferences, s, key, key); + break; + default: + appendPreferenceString(preferences, s, key, key); + break; + } + } + +public: + const String preferencesToString(Preferences *preferences) + { + String s = ""; + + for(const auto& key : _keys) + { + appendPreference(preferences, s, key); + } + + return s; + } + +}; diff --git a/WebCfgServer.cpp b/WebCfgServer.cpp index c6ba8d1..7b6ad63 100644 --- a/WebCfgServer.cpp +++ b/WebCfgServer.cpp @@ -159,6 +159,14 @@ void WebCfgServer::initialize() handleOtaUpload(); }); + _server.on("/info", [&]() { + if (_hasCredentials && !_server.authenticate(_credUser, _credPassword)) { + return _server.requestAuthentication(); + } + String response = ""; + buildInfoHtml(response); + _server.send(200, "text/html", response); + }); _server.begin(); @@ -698,6 +706,22 @@ void WebCfgServer::buildConfigureWifiHtml(String &response) response.concat(""); } +void WebCfgServer::buildInfoHtml(String &response) +{ + DebugPreferences debugPreferences; + + buildHtmlHeader(response); + response.concat("

Info

");
+    response.concat(debugPreferences.preferencesToString(_preferences));
+
+    response.concat("Lock PIN set: ");
+    response.concat(_nuki->isPinSet() ? "Yes\n" : "No\n");
+    response.concat("Opener PIN set: ");
+    response.concat(_nukiOpener->isPinSet() ? "Yes\n" : "No\n");
+
+    response.concat("
"); +} + void WebCfgServer::processUnpair(bool opener) { String response = ""; @@ -981,4 +1005,4 @@ const std::vector> WebCfgServer::getNetworkGpioOptions } return options; -} \ No newline at end of file +} diff --git a/WebCfgServer.h b/WebCfgServer.h index cf1be2b..4736519 100644 --- a/WebCfgServer.h +++ b/WebCfgServer.h @@ -37,6 +37,7 @@ private: void buildNukiConfigHtml(String& response); void buildConfirmHtml(String& response, const String &message, uint32_t redirectDelay = 5); void buildConfigureWifiHtml(String& response); + void buildInfoHtml(String& response); void sendCss(); void sendFavicon(); void processUnpair(bool opener);