From 6643fd7bc46d89ba19e8569bd0dcc8fc319ad5be Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Sat, 13 Nov 2021 12:46:24 +0100 Subject: [PATCH] Added nvs dump (needs some help for profile dumping; 'std::string html;' probably gets to large, otherwise works) --- main/CMakeLists.txt | 2 + main/webserver.h | 8 + main/webserver_displaycontrol.h | 3 +- main/webserver_dumpnvs.cpp | 0 main/webserver_dumpnvs.h | 258 ++++++++++++++++++++++++++++++++ main/webserver_ota.h | 3 +- main/webserver_settings.h | 3 +- main/webserver_stringsettings.h | 3 +- 8 files changed, 276 insertions(+), 4 deletions(-) create mode 100644 main/webserver_dumpnvs.cpp create mode 100644 main/webserver_dumpnvs.h diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 1d64070..ebac81c 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -55,6 +55,7 @@ set(headers webserver_ota.h webserver_settings.h webserver_stringsettings.h + webserver_dumpnvs.h wifi_bobbycar.h wifitexthelpers.h accessors/globalaccessors.h @@ -238,6 +239,7 @@ set(sources webserver_ota.cpp webserver_settings.cpp webserver_stringsettings.cpp + webserver_dumpnvs.cpp wifi_bobbycar.cpp wifitexthelpers.cpp accessors/globalaccessors.cpp diff --git a/main/webserver.h b/main/webserver.h index 16e4d27..d1328ee 100644 --- a/main/webserver.h +++ b/main/webserver.h @@ -22,6 +22,10 @@ #endif #include "webserver_settings.h" #include "webserver_stringsettings.h" +#ifdef OLD_NVS +#include "webserver_dumpnvs.h" +using namespace dump_nvs_handler; +#endif #ifdef FEATURE_WEBSERVER namespace { @@ -30,6 +34,7 @@ httpd_handle_t httpdHandle; void initWebserver(); void handleWebserver(); esp_err_t webserver_reboot_handler(httpd_req_t *req); + } namespace { @@ -63,6 +68,9 @@ void initWebserver() httpd_uri_t { .uri = "/saveSettings", .method = HTTP_GET, .handler = webserver_saveSettings_handler, .user_ctx = NULL }, httpd_uri_t { .uri = "/stringSettings", .method = HTTP_GET, .handler = webserver_stringSettings_handler, .user_ctx = NULL }, httpd_uri_t { .uri = "/saveStringSettings", .method = HTTP_GET, .handler = webserver_saveStringSettings_handler, .user_ctx = NULL }, +#ifdef OLD_NVS + // httpd_uri_t { .uri = "/dumpnvs", .method = HTTP_GET, .handler = webserver_dump_nvs_handler, .user_ctx = NULL }, +#endif }) { const auto result = httpd_register_uri_handler(httpdHandle, &uri); diff --git a/main/webserver_displaycontrol.h b/main/webserver_displaycontrol.h index ed4b153..3721d7d 100644 --- a/main/webserver_displaycontrol.h +++ b/main/webserver_displaycontrol.h @@ -77,7 +77,8 @@ esp_err_t webserver_root_handler(httpd_req_t *req) #endif "Settings - " - "String Settings"; + "String Settings - " + "Dump NVS"; } { diff --git a/main/webserver_dumpnvs.cpp b/main/webserver_dumpnvs.cpp new file mode 100644 index 0000000..e69de29 diff --git a/main/webserver_dumpnvs.h b/main/webserver_dumpnvs.h new file mode 100644 index 0000000..6d6643c --- /dev/null +++ b/main/webserver_dumpnvs.h @@ -0,0 +1,258 @@ +#pragma once + +// esp-idf includes +#ifdef FEATURE_WEBSERVER +#include +#endif +#include + +// 3rdparty lib includes +#include +#include +#include +#include +#include +#include + +// local includes +#include "globals.h" +#include "webserver_lock.h" +#include "settingsutils.h" + +#ifdef FEATURE_WEBSERVER +namespace dump_nvs_handler { +esp_err_t webserver_dump_nvs_handler(httpd_req_t *req); +} // namespace + +using esphttpdutils::HtmlTag; + +namespace dump_nvs_handler { + +template +typename std::enable_if< + !std::is_same::value && + !std::is_integral::value && + !std::is_same>::value +, bool>::type +showInputForSetting(std::string_view key, T value, std::string &body) +{ + HtmlTag spanTag{"span", "style=\"color: red;\"", body}; + body += "Unsupported config type"; + return false; +} + +template +typename std::enable_if< + std::is_same::value +, bool>::type +showInputForSetting(std::string_view key, T value, std::string &body) +{ + body += fmt::format("" + "", + esphttpdutils::htmlentities(key), + value ? "checked " : "", + esphttpdutils::htmlentities(key)); + return true; +} + +template +typename std::enable_if< + !std::is_same::value && + std::is_integral::value +, bool>::type +showInputForSetting(std::string_view key, T value, std::string &body) +{ + body += fmt::format("", + esphttpdutils::htmlentities(key), + value, + std::numeric_limits::min(), + std::numeric_limits::max()); + return true; +} + +template +typename std::enable_if< + std::is_same>::value +, bool>::type +showInputForSetting(std::string_view key, T value, std::string &body) +{ + body += fmt::format("", + esphttpdutils::htmlentities(key), + value[0], + value[1], + value[2], + value[3]); + return true; +} + +esp_err_t webserver_dump_nvs_handler(httpd_req_t *req) +{ + espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil(5s).count()}; + if (!helper.locked()) + { + constexpr const std::string_view msg = "could not lock webserver_lock"; + ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); + CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg); + } + + std::string body; + const auto profile = settingsPersister.currentlyOpenProfileIndex(); + const auto switchBackProfile = profile ? int(*profile) : 0; + + { + HtmlTag htmlTag{"html", body}; + + { + HtmlTag headTag{"head", body}; + + { + HtmlTag titleTag{"title", body}; + body += "NVS dump"; + } + + body += ""; + + HtmlTag styleTag{"style", "type=\"text/css\"", body}; + body += + ".form-table {" + "display: table;" + "border-collapse: separate;" + "border-spacing: 10px 0;" + "}" + + ".form-table .form-table-row {" + "display: table-row;" + "}" + + ".form-table .form-table-row .form-table-cell {" + "display: table-cell;" + "}"; + } + + { + HtmlTag bodyTag{"body", body}; + + { + HtmlTag h1Tag{"h1", body}; + body += "NVS dump"; + } + + { + HtmlTag pTag{"p", body}; + body += "Display control - " +#ifdef FEATURE_OTA + "Update - " +#endif + "Settings - " + "String Settings - " + "Dump NVS"; + } + + HtmlTag divTag{"div", "class=\"form-table\"", body}; + // Common setting + { + HtmlTag h1Tag{"h1", body}; + body += "stringSettings"; + } + stringSettings.executeForEveryCommonSetting([&](std::string_view key, const auto &value){ + HtmlTag divTag{"div", "class=\"form-table-row\"", body}; + + { + HtmlTag divTag{"div", "class=\"form-table\"", body}; + HtmlTag bTag{"b", body}; + body += esphttpdutils::htmlentities(key); + } + + { + HtmlTag divTag{"div", "class=\"form-table-cell\"", body}; + body += fmt::format("", + esphttpdutils::htmlentities(key), + esphttpdutils::htmlentities(value)); + } + }); + + { + HtmlTag h1Tag{"h1", body}; + body += "settings"; + } + settings.executeForEveryCommonSetting([&](std::string_view key, const auto &value){ + HtmlTag divTag{"div", "class=\"form-table-row\"", body}; + + { + HtmlTag divTag{"div", "class=\"form-table\"", body}; + HtmlTag bTag{"b", body}; + body += esphttpdutils::htmlentities(key); + } + + { + HtmlTag divTag{"div", "class=\"form-table-cell\"", body}; + showInputForSetting(key, value, body); + } + }); + + // ToDo: Do something that it does not crash (std::string probabbly gets to big) + // After that, change "profile_num < 1" to "profile_num < 4", uncomment the "switchProfile(profile_num);" and reenable the url in webserver.h + + // Profile settings + for (uint8_t profile_num = 0; profile_num < 1; profile_num++) { + +#ifdef SIMPLIFIED_TRIGGER_TRIGGERONPRESET + if (profile_num == SIMPLIFIED_TRIGGER_TRIGGERONPRESET) { + continue; + } +#endif + // switchProfile(profile_num); + + const auto cur_profile = settingsPersister.currentlyOpenProfileIndex(); + const auto cur_profile_int = profile ? int(*cur_profile) : 0; + { + HtmlTag h1Tag{"h1", body}; + body += fmt::format("stringSettings (Profile {})", cur_profile_int); + } + stringSettings.executeForEveryProfileSetting([&](const char *key, auto &value){ + HtmlTag divTag{"div", "class=\"form-table-row\"", body}; + + { + HtmlTag divTag{"div", "class=\"form-table\"", body}; + HtmlTag bTag{"b", body}; + body += esphttpdutils::htmlentities(key); + } + + { + HtmlTag divTag{"div", "class=\"form-table-cell\"", body}; + body += fmt::format("", + esphttpdutils::htmlentities(key), + esphttpdutils::htmlentities(value)); + } + }); + + { + HtmlTag h1Tag{"h1", body}; + body += fmt::format("settings (Profile {})", cur_profile_int); + } + settings.executeForEveryProfileSetting([&](const char *key, auto &value){ + HtmlTag divTag{"div", "class=\"form-table-row\"", body}; + + { + HtmlTag divTag{"div", "class=\"form-table\"", body}; + HtmlTag bTag{"b", body}; + body += esphttpdutils::htmlentities(key); + } + + { + HtmlTag divTag{"div", "class=\"form-table-cell\"", body}; + showInputForSetting(key, value, body); + } + }); + } + } + } + + switchProfile(switchBackProfile); + + CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::Ok, "text/html", body) +} +} // namespace + +#endif + diff --git a/main/webserver_ota.h b/main/webserver_ota.h index 9e19ff5..5ea061e 100644 --- a/main/webserver_ota.h +++ b/main/webserver_ota.h @@ -70,7 +70,8 @@ esp_err_t webserver_ota_handler(httpd_req_t *req) body += "Display control - " "Update - " "Settings - " - "String Settings"; + "String Settings - " + "Dump NVS"; } if (const esp_app_desc_t *app_desc = esp_ota_get_app_description()) diff --git a/main/webserver_settings.h b/main/webserver_settings.h index 98e5da3..1fa9f24 100644 --- a/main/webserver_settings.h +++ b/main/webserver_settings.h @@ -145,7 +145,8 @@ esp_err_t webserver_settings_handler(httpd_req_t *req) "Update - " #endif "Settings - " - "String Settings"; + "String Settings - " + "Dump NVS"; } HtmlTag divTag{"div", "class=\"form-table\"", body}; diff --git a/main/webserver_stringsettings.h b/main/webserver_stringsettings.h index b27a4eb..5c45351 100644 --- a/main/webserver_stringsettings.h +++ b/main/webserver_stringsettings.h @@ -84,7 +84,8 @@ esp_err_t webserver_stringSettings_handler(httpd_req_t *req) "Update - " #endif "Settings - " - "String Settings"; + "String Settings - " + "Dump NVS"; } HtmlTag divTag{"div", "class=\"form-table\"", body};