add /info endpoint to publish preference content

This commit is contained in:
technyon
2023-02-05 12:11:10 +01:00
parent 327038119e
commit 2094595e78
7 changed files with 149 additions and 1 deletions

View File

@@ -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);

View File

@@ -15,6 +15,7 @@ public:
void initialize();
void update();
bool isPinSet();
void setPin(const uint16_t pin);
void unpair();

View File

@@ -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);

View File

@@ -19,6 +19,7 @@ public:
void unlock();
void unlatch();
bool isPinSet();
void setPin(const uint16_t pin);
void unpair();

View File

@@ -1,5 +1,7 @@
#pragma once
#include <vector>
#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<char*> _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<char*> _redact =
{
preference_mqtt_user, preference_mqtt_password,
preference_mqtt_ca, preference_mqtt_crt, preference_mqtt_key,
preference_cred_user, preference_cred_password,
};
std::vector<char*> _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;
}
};

View File

@@ -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("</BODY></HTML>");
}
void WebCfgServer::buildInfoHtml(String &response)
{
DebugPreferences debugPreferences;
buildHtmlHeader(response);
response.concat("<h3>Info</h3> <pre>");
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("</pre> </BODY></HTML>");
}
void WebCfgServer::processUnpair(bool opener)
{
String response = "";
@@ -981,4 +1005,4 @@ const std::vector<std::pair<String, String>> WebCfgServer::getNetworkGpioOptions
}
return options;
}
}

View File

@@ -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);