From 9540898ced042e350328b60ba5551cca37b6a939 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Wed, 29 Dec 2021 18:53:59 +0100 Subject: [PATCH] Lots of improvements with newsettings --- main/displays/menus/commanddebugmenu.h | 5 +- main/displays/menus/feedbackdebugmenu.h | 5 +- main/displays/menus/motorfeedbackdebugmenu.h | 9 +- main/displays/menus/motorstatedebugmenu.h | 9 +- main/dpad6wire.h | 3 +- main/newsettings.h | 8 +- main/webserver_newsettings.cpp | 201 ++++++++++++++++--- 7 files changed, 200 insertions(+), 40 deletions(-) diff --git a/main/displays/menus/commanddebugmenu.h b/main/displays/menus/commanddebugmenu.h index 14fbc6e..c8e4685 100644 --- a/main/displays/menus/commanddebugmenu.h +++ b/main/displays/menus/commanddebugmenu.h @@ -2,6 +2,7 @@ // local includes #include "displays/bobbymenudisplay.h" +#include "debugmenu.h" #include "utils.h" #include "menuitem.h" #include "actions/dummyaction.h" @@ -30,7 +31,7 @@ public: } }; -class FrontCommandDebugMenu : public CommandDebugMenu {}; -class BackCommandDebugMenu : public CommandDebugMenu {}; +using FrontCommandDebugMenu = CommandDebugMenu; +using BackCommandDebugMenu = CommandDebugMenu; } // namespace diff --git a/main/displays/menus/feedbackdebugmenu.h b/main/displays/menus/feedbackdebugmenu.h index 4f6d60c..3fd6ee4 100644 --- a/main/displays/menus/feedbackdebugmenu.h +++ b/main/displays/menus/feedbackdebugmenu.h @@ -2,6 +2,7 @@ // local includes #include "displays/bobbymenudisplay.h" +#include "debugmenu.h" #include "utils.h" #include "menuitem.h" #include "actions/dummyaction.h" @@ -33,7 +34,7 @@ public: } }; -class FrontFeedbackDebugMenu : public FeedbackDebugMenu {}; -class BackFeedbackDebugMenu : public FeedbackDebugMenu {}; +using FrontFeedbackDebugMenu = FeedbackDebugMenu; +using BackFeedbackDebugMenu = FeedbackDebugMenu; } // namespace diff --git a/main/displays/menus/motorfeedbackdebugmenu.h b/main/displays/menus/motorfeedbackdebugmenu.h index 80375c3..bd4a3f8 100644 --- a/main/displays/menus/motorfeedbackdebugmenu.h +++ b/main/displays/menus/motorfeedbackdebugmenu.h @@ -2,6 +2,7 @@ // local includes #include "displays/bobbymenudisplay.h" +#include "debugmenu.h" #include "utils.h" #include "menuitem.h" #include "actions/dummyaction.h" @@ -40,7 +41,7 @@ public: } }; -class FrontLeftMotorFeedbackDebugMenu : public MotorFeedbackDebugMenu {}; -class FrontRightMotorFeedbackDebugMenu : public MotorFeedbackDebugMenu {}; -class BackLeftMotorFeedbackDebugMenu : public MotorFeedbackDebugMenu {}; -class BackRightMotorFeedbackDebugMenu : public MotorFeedbackDebugMenu {}; +using FrontLeftMotorFeedbackDebugMenu = MotorFeedbackDebugMenu; +using FrontRightMotorFeedbackDebugMenu = MotorFeedbackDebugMenu; +using BackLeftMotorFeedbackDebugMenu = MotorFeedbackDebugMenu; +using BackRightMotorFeedbackDebugMenu = MotorFeedbackDebugMenu; diff --git a/main/displays/menus/motorstatedebugmenu.h b/main/displays/menus/motorstatedebugmenu.h index bfa01da..c231bc0 100644 --- a/main/displays/menus/motorstatedebugmenu.h +++ b/main/displays/menus/motorstatedebugmenu.h @@ -8,6 +8,7 @@ // local includes #include "displays/bobbymenudisplay.h" +#include "debugmenu.h" #include "utils.h" #include "texts.h" #include "debugtexthelpers.h" @@ -37,7 +38,7 @@ public: } }; -class FrontLeftMotorStateDebugMenu : public MotorStateDebugMenu {}; -class FrontRightMotorStateDebugMenu : public MotorStateDebugMenu {}; -class BackLeftMotorStateDebugMenu : public MotorStateDebugMenu {}; -class BackRightMotorStateDebugMenu : public MotorStateDebugMenu {}; +using FrontLeftMotorStateDebugMenu = MotorStateDebugMenu; +using FrontRightMotorStateDebugMenu = MotorStateDebugMenu; +using BackLeftMotorStateDebugMenu = MotorStateDebugMenu; +using BackRightMotorStateDebugMenu = MotorStateDebugMenu; diff --git a/main/dpad6wire.h b/main/dpad6wire.h index b353811..643095b 100644 --- a/main/dpad6wire.h +++ b/main/dpad6wire.h @@ -13,6 +13,7 @@ #include "buttons.h" #include "types.h" #include "globals.h" +#include "newsettings.h" namespace { namespace dpad6wire { @@ -135,7 +136,7 @@ void update() const auto now = espchrono::millis_clock::now(); - const std::chrono::milliseconds dpadDebounce{settings.boardcomputerHardware.dpadDebounce}; + const std::chrono::milliseconds dpadDebounce{configs.dpadDebounce.value}; if (lastState.up != newState.up && now - debounceUp > dpadDebounce) { diff --git a/main/newsettings.h b/main/newsettings.h index f5f4a92..f4cbcee 100644 --- a/main/newsettings.h +++ b/main/newsettings.h @@ -84,7 +84,7 @@ class ConfigContainer public: // default allowReset constraints nvsName - ConfigWrapper> baseMacAddressOverride{std::nullopt, NoReset, {}, "baseMacAddrOver" }; + ConfigWrapper> baseMacAddressOverride{std::nullopt, DoReset, {}, "baseMacAddrOver" }; ConfigWrapper hostname {defaultHostname, DoReset, StringMinMaxSize<4, 32>, "hostname" }; ConfigWrapper wifiStaEnabled {true, DoReset, {}, "wifiStaEnabled" }; std::array wifi_configs { @@ -108,9 +108,9 @@ public: ConfigWrapper wifiApAuthmode{WIFI_AUTH_WPA2_PSK, DoReset, {}, "wifiApAuthmode" }; ConfigWrapper timeServerEnabled {false, DoReset, {}, "timeServerEnabl" }; - ConfigWrapper timeServer {"europe.pool.ntp.org", NoReset, StringMaxSize<64>, "timeServer" }; - ConfigWrapper timeSyncMode {SNTP_SYNC_MODE_IMMED, NoReset, {}, "timeSyncMode" }; - ConfigWrapper timeSyncInterval{espchrono::milliseconds32{CONFIG_LWIP_SNTP_UPDATE_DELAY}, NoReset, MinTimeSyncInterval, "timeSyncInterva" }; + ConfigWrapper timeServer {"europe.pool.ntp.org", DoReset, StringMaxSize<64>, "timeServer" }; + ConfigWrapper timeSyncMode {SNTP_SYNC_MODE_IMMED, DoReset, {}, "timeSyncMode" }; + ConfigWrapper timeSyncInterval{espchrono::milliseconds32{CONFIG_LWIP_SNTP_UPDATE_DELAY}, DoReset, MinTimeSyncInterval, "timeSyncInterva" }; ConfigWrapper timezoneOffset{espchrono::minutes32{60}, DoReset, {}, "timezoneOffset" }; // MinMaxValue ConfigWrappertimeDst{espchrono::DayLightSavingMode::EuropeanSummerTime, DoReset, {}, "time_dst" }; diff --git a/main/webserver_newsettings.cpp b/main/webserver_newsettings.cpp index 2af1af2..ab8a85c 100644 --- a/main/webserver_newsettings.cpp +++ b/main/webserver_newsettings.cpp @@ -17,6 +17,7 @@ #include #include #include +#include // local includes #include "newsettings.h" @@ -29,13 +30,26 @@ using esphttpdutils::HtmlTag; namespace { constexpr const char * const TAG = "BOBBYWEB"; +template +struct is_duration : std::false_type {}; + +template +struct is_duration> : std::true_type {}; + +template +inline constexpr bool is_duration_v = is_duration<_Tp>::value; + template typename std::enable_if< - !std::is_same::value && - !std::is_integral::value && - !std::is_same::value && - !std::is_same::value && - !std::is_same::value + !std::is_same_v && + !std::is_integral_v && + !is_duration_v && + !std::is_same_v && + !std::is_same_v && + !std::is_same_v && + !std::is_same_v> && + !std::is_same_v && + !std::is_same_v , bool>::type showInputForSetting(std::string_view key, T value, std::string &body) { @@ -46,7 +60,7 @@ showInputForSetting(std::string_view key, T value, std::string &body) template typename std::enable_if< - std::is_same::value + std::is_same_v , bool>::type showInputForSetting(std::string_view key, T value, std::string &body) { @@ -60,8 +74,8 @@ showInputForSetting(std::string_view key, T value, std::string &body) template typename std::enable_if< - !std::is_same::value && - std::is_integral::value + !std::is_same_v && + std::is_integral_v , bool>::type showInputForSetting(std::string_view key, T value, std::string &body) { @@ -75,7 +89,19 @@ showInputForSetting(std::string_view key, T value, std::string &body) template typename std::enable_if< - std::is_same::value + is_duration_v +, bool>::type +showInputForSetting(std::string_view key, T value, std::string &body) +{ + body += fmt::format("", + esphttpdutils::htmlentities(key), + value.count()); + return true; +} + +template +typename std::enable_if< + std::is_same_v , bool>::type showInputForSetting(std::string_view key, T value, std::string &body) { @@ -87,7 +113,7 @@ showInputForSetting(std::string_view key, T value, std::string &body) template typename std::enable_if< - std::is_same::value + std::is_same_v , bool>::type showInputForSetting(std::string_view key, T value, std::string &body) { @@ -99,20 +125,79 @@ showInputForSetting(std::string_view key, T value, std::string &body) template typename std::enable_if< - std::is_same::value + std::is_same_v , bool>::type showInputForSetting(std::string_view key, T value, std::string &body) { - body += fmt::format("", + body += fmt::format("", esphttpdutils::htmlentities(key), esphttpdutils::htmlentities(wifi_stack::toString(value))); return true; } + +template +typename std::enable_if< + std::is_same_v> +, bool>::type +showInputForSetting(std::string_view key, T value, std::string &body) +{ + body += fmt::format(" ?", + esphttpdutils::htmlentities(key), + value ? esphttpdutils::htmlentities(wifi_stack::toString(*value)) : std::string{}); + return true; +} + +template +typename std::enable_if< + std::is_same_v +, bool>::type +showInputForSetting(std::string_view key, T value, std::string &body) +{ + HtmlTag select{"select", fmt::format("name=\"{}\"", esphttpdutils::htmlentities(key)), body}; + +#define HANDLE_ENUM_KEY(x) \ + { \ + HtmlTag option{"option", fmt::format("value=\"{}\"{}", std::to_underlying(x), value == x ? " selected" : ""), body}; \ + body += esphttpdutils::htmlentities(#x); \ + } + HANDLE_ENUM_KEY(WIFI_AUTH_OPEN) + HANDLE_ENUM_KEY(WIFI_AUTH_WEP) + HANDLE_ENUM_KEY(WIFI_AUTH_WPA_PSK) + HANDLE_ENUM_KEY(WIFI_AUTH_WPA2_PSK) + HANDLE_ENUM_KEY(WIFI_AUTH_WPA_WPA2_PSK) + HANDLE_ENUM_KEY(WIFI_AUTH_WPA2_ENTERPRISE) + HANDLE_ENUM_KEY(WIFI_AUTH_WPA3_PSK) + HANDLE_ENUM_KEY(WIFI_AUTH_WPA2_WPA3_PSK) + HANDLE_ENUM_KEY(WIFI_AUTH_WAPI_PSK) + HANDLE_ENUM_KEY(WIFI_AUTH_MAX) +#undef HANDLE_ENUM_KEY + return true; +} + +template +typename std::enable_if< + std::is_same_v + , bool>::type +showInputForSetting(std::string_view key, T value, std::string &body) +{ + HtmlTag select{"select", fmt::format("name=\"{}\"", esphttpdutils::htmlentities(key)), body}; + +#define HANDLE_ENUM_KEY(x) \ + { \ + HtmlTag option{"option", fmt::format("value=\"{}\"{}", std::to_underlying(x), value == x ? " selected" : ""), body}; \ + body += esphttpdutils::htmlentities(#x); \ + } + HANDLE_ENUM_KEY(SNTP_SYNC_MODE_IMMED) + HANDLE_ENUM_KEY(SNTP_SYNC_MODE_SMOOTH) +#undef HANDLE_ENUM_KEY + return true; +} } // namespace esp_err_t webserver_newSettings_handler(httpd_req_t *req) { #ifndef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_KORREKT_ARBEITET + ESP_LOGI(TAG, "trying to lock..."); espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil(5s).count()}; if (!helper.locked()) { @@ -238,11 +323,14 @@ esp_err_t webserver_newSettings_handler(httpd_req_t *req) namespace { template typename std::enable_if< - !std::is_same::value && - !std::is_integral::value && - !std::is_same::value && - !std::is_same::value && - !std::is_same::value + !std::is_same_v && + !std::is_integral_v && + !std::is_same_v && + !std::is_same_v && + !std::is_same_v && + !std::is_same_v> && + !std::is_same_v && + !std::is_same_v , bool>::type saveSetting(ConfigWrapper &config, std::string_view newValue, std::string &body) { @@ -252,7 +340,7 @@ saveSetting(ConfigWrapper &config, std::string_view newValue, std::string &bo template typename std::enable_if< - std::is_same::value + std::is_same_v , bool>::type saveSetting(ConfigWrapper &config, std::string_view newValue, std::string &body) { @@ -291,8 +379,8 @@ saveSetting(ConfigWrapper &config, std::string_view newValue, std::string &bo template typename std::enable_if< - !std::is_same::value && - std::is_integral::value + !std::is_same_v && + std::is_integral_v , bool>::type saveSetting(ConfigWrapper &config, std::string_view newValue, std::string &body) { @@ -318,7 +406,7 @@ saveSetting(ConfigWrapper &config, std::string_view newValue, std::string &bo template typename std::enable_if< - std::is_same::value + std::is_same_v , bool>::type saveSetting(ConfigWrapper &config, std::string_view newValue, std::string &body) { @@ -336,7 +424,7 @@ saveSetting(ConfigWrapper &config, std::string_view newValue, std::string &bo template typename std::enable_if< - std::is_same::value + std::is_same_v , bool>::type saveSetting(ConfigWrapper &config, std::string_view newValue, std::string &body) { @@ -362,7 +450,7 @@ saveSetting(ConfigWrapper &config, std::string_view newValue, std::string &bo template typename std::enable_if< - std::is_same::value + std::is_same_v , bool>::type saveSetting(ConfigWrapper &config, std::string_view newValue, std::string &body) { @@ -385,6 +473,72 @@ saveSetting(ConfigWrapper &config, std::string_view newValue, std::string &bo return false; } } + +template +typename std::enable_if< + std::is_same_v> +, bool>::type +saveSetting(ConfigWrapper &config, std::string_view newValue, std::string &body) +{ + if (newValue.empty()) + { + if (const auto result = configs.write_config(config, std::nullopt); result) + { + body += "applied"; + return true; + } + else + { + body += result.error(); + return false; + } + } + else if (const auto parsed = wifi_stack::fromString(newValue); parsed) + { + if (const auto result = configs.write_config(config, *parsed); result) + { + body += "applied"; + return true; + } + else + { + body += result.error(); + return false; + } + } + else + { + body += parsed.error(); + return false; + } +} + +template +typename std::enable_if< + std::is_same_v || + std::is_same_v +, bool>::type +saveSetting(ConfigWrapper &config, std::string_view newValue, std::string &body) +{ + if (auto parsed = cpputils::fromString>(newValue)) + { + if (const auto result = configs.write_config(config, T(*parsed)); result) + { + body += "applied"; + return true; + } + else + { + body += result.error(); + return false; + } + } + else + { + body += fmt::format("could not parse {}", newValue); + return false; + } +} } // namespace esp_err_t webserver_saveNewSettings_handler(httpd_req_t *req) @@ -432,6 +586,7 @@ esp_err_t webserver_saveNewSettings_handler(httpd_req_t *req) esphttpdutils::urldecode(valueBuf, valueBufEncoded); body += nvsName; + body += ": "; if (!saveSetting(config, valueBuf, body)) success = false; body += '\n';