implemented getters setters for newconfig
This commit is contained in:
@ -80,122 +80,137 @@ public:
|
||||
ConfigWrapper<uint8_t> wifiApChannel {1, DoReset, {}, "wifiApChannel" };
|
||||
ConfigWrapper<wifi_auth_mode_t> wifiApAuthmode{WIFI_AUTH_WPA2_PSK, DoReset, {}, "wifiApAuthmode" };
|
||||
|
||||
#define NEW_SETTINGS(x) \
|
||||
x(baseMacAddressOverride) \
|
||||
x(hostname) \
|
||||
\
|
||||
x(wifiStaEnabled) \
|
||||
x(wifi_configs[0].ssid) \
|
||||
x(wifi_configs[0].key) \
|
||||
x(wifi_configs[0].useStaticIp) \
|
||||
x(wifi_configs[0].staticIp) \
|
||||
x(wifi_configs[0].staticSubnet) \
|
||||
x(wifi_configs[0].staticGateway) \
|
||||
x(wifi_configs[0].useStaticDns) \
|
||||
x(wifi_configs[0].staticDns0) \
|
||||
x(wifi_configs[0].staticDns1) \
|
||||
x(wifi_configs[0].staticDns2) \
|
||||
x(wifi_configs[1].ssid) \
|
||||
x(wifi_configs[1].key) \
|
||||
x(wifi_configs[1].useStaticIp) \
|
||||
x(wifi_configs[1].staticIp) \
|
||||
x(wifi_configs[1].staticSubnet) \
|
||||
x(wifi_configs[1].staticGateway) \
|
||||
x(wifi_configs[1].useStaticDns) \
|
||||
x(wifi_configs[1].staticDns0) \
|
||||
x(wifi_configs[1].staticDns1) \
|
||||
x(wifi_configs[1].staticDns2) \
|
||||
x(wifi_configs[2].ssid) \
|
||||
x(wifi_configs[2].key) \
|
||||
x(wifi_configs[2].useStaticIp) \
|
||||
x(wifi_configs[2].staticIp) \
|
||||
x(wifi_configs[2].staticSubnet) \
|
||||
x(wifi_configs[2].staticGateway) \
|
||||
x(wifi_configs[2].useStaticDns) \
|
||||
x(wifi_configs[2].staticDns0) \
|
||||
x(wifi_configs[2].staticDns1) \
|
||||
x(wifi_configs[2].staticDns2) \
|
||||
x(wifi_configs[3].ssid) \
|
||||
x(wifi_configs[3].key) \
|
||||
x(wifi_configs[3].useStaticIp) \
|
||||
x(wifi_configs[3].staticIp) \
|
||||
x(wifi_configs[3].staticSubnet) \
|
||||
x(wifi_configs[3].staticGateway) \
|
||||
x(wifi_configs[3].useStaticDns) \
|
||||
x(wifi_configs[3].staticDns0) \
|
||||
x(wifi_configs[3].staticDns1) \
|
||||
x(wifi_configs[3].staticDns2) \
|
||||
x(wifi_configs[4].ssid) \
|
||||
x(wifi_configs[4].key) \
|
||||
x(wifi_configs[4].useStaticIp) \
|
||||
x(wifi_configs[4].staticIp) \
|
||||
x(wifi_configs[4].staticSubnet) \
|
||||
x(wifi_configs[4].staticGateway) \
|
||||
x(wifi_configs[4].useStaticDns) \
|
||||
x(wifi_configs[4].staticDns0) \
|
||||
x(wifi_configs[4].staticDns1) \
|
||||
x(wifi_configs[4].staticDns2) \
|
||||
x(wifi_configs[5].ssid) \
|
||||
x(wifi_configs[5].key) \
|
||||
x(wifi_configs[5].useStaticIp) \
|
||||
x(wifi_configs[5].staticIp) \
|
||||
x(wifi_configs[5].staticSubnet) \
|
||||
x(wifi_configs[5].staticGateway) \
|
||||
x(wifi_configs[5].useStaticDns) \
|
||||
x(wifi_configs[5].staticDns0) \
|
||||
x(wifi_configs[5].staticDns1) \
|
||||
x(wifi_configs[5].staticDns2) \
|
||||
x(wifi_configs[6].ssid) \
|
||||
x(wifi_configs[6].key) \
|
||||
x(wifi_configs[6].useStaticIp) \
|
||||
x(wifi_configs[6].staticIp) \
|
||||
x(wifi_configs[6].staticSubnet) \
|
||||
x(wifi_configs[6].staticGateway) \
|
||||
x(wifi_configs[6].useStaticDns) \
|
||||
x(wifi_configs[6].staticDns0) \
|
||||
x(wifi_configs[6].staticDns1) \
|
||||
x(wifi_configs[6].staticDns2) \
|
||||
x(wifi_configs[7].ssid) \
|
||||
x(wifi_configs[7].key) \
|
||||
x(wifi_configs[7].useStaticIp) \
|
||||
x(wifi_configs[7].staticIp) \
|
||||
x(wifi_configs[7].staticSubnet) \
|
||||
x(wifi_configs[7].staticGateway) \
|
||||
x(wifi_configs[7].useStaticDns) \
|
||||
x(wifi_configs[7].staticDns0) \
|
||||
x(wifi_configs[7].staticDns1) \
|
||||
x(wifi_configs[7].staticDns2) \
|
||||
x(wifi_configs[8].ssid) \
|
||||
x(wifi_configs[8].key) \
|
||||
x(wifi_configs[8].useStaticIp) \
|
||||
x(wifi_configs[8].staticIp) \
|
||||
x(wifi_configs[8].staticSubnet) \
|
||||
x(wifi_configs[8].staticGateway) \
|
||||
x(wifi_configs[8].useStaticDns) \
|
||||
x(wifi_configs[8].staticDns0) \
|
||||
x(wifi_configs[8].staticDns1) \
|
||||
x(wifi_configs[8].staticDns2) \
|
||||
x(wifi_configs[9].ssid) \
|
||||
x(wifi_configs[9].key) \
|
||||
x(wifi_configs[9].useStaticIp) \
|
||||
x(wifi_configs[9].staticIp) \
|
||||
x(wifi_configs[9].staticSubnet) \
|
||||
x(wifi_configs[9].staticGateway) \
|
||||
x(wifi_configs[9].useStaticDns) \
|
||||
x(wifi_configs[9].staticDns0) \
|
||||
x(wifi_configs[9].staticDns1) \
|
||||
x(wifi_configs[9].staticDns2) \
|
||||
\
|
||||
x(wifiStaMinRssi) \
|
||||
\
|
||||
x(wifiApEnabled) \
|
||||
x(factoryWifiApName) \
|
||||
x(wifiApName) \
|
||||
x(factoryWifiApKey) \
|
||||
x(wifiApKey) \
|
||||
x(wifiApChannel) \
|
||||
//x(wifiApAuthmode)
|
||||
|
||||
template<typename T>
|
||||
void callForEveryConfig(T &&callback)
|
||||
{
|
||||
#define HELPER(x) callback(x);
|
||||
NEW_SETTINGS(HELPER)
|
||||
#undef HELPER
|
||||
callback(wifiApAuthmode);
|
||||
}
|
||||
|
||||
auto getAllConfigParams()
|
||||
{
|
||||
return cpputils::make_array(
|
||||
std::ref<ConfigWrapperInterface>(baseMacAddressOverride),
|
||||
std::ref<ConfigWrapperInterface>(hostname),
|
||||
|
||||
std::ref<ConfigWrapperInterface>(wifiStaEnabled),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[0].ssid),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[0].key),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[0].useStaticIp),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[0].staticIp),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[0].staticSubnet),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[0].staticGateway),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[0].useStaticDns),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[0].staticDns0),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[0].staticDns1),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[0].staticDns2),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[1].ssid),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[1].key),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[1].useStaticIp),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[1].staticIp),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[1].staticSubnet),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[1].staticGateway),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[1].useStaticDns),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[1].staticDns0),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[1].staticDns1),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[1].staticDns2),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[2].ssid),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[2].key),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[2].useStaticIp),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[2].staticIp),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[2].staticSubnet),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[2].staticGateway),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[2].useStaticDns),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[2].staticDns0),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[2].staticDns1),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[2].staticDns2),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[3].ssid),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[3].key),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[3].useStaticIp),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[3].staticIp),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[3].staticSubnet),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[3].staticGateway),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[3].useStaticDns),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[3].staticDns0),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[3].staticDns1),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[3].staticDns2),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[4].ssid),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[4].key),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[4].useStaticIp),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[4].staticIp),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[4].staticSubnet),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[4].staticGateway),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[4].useStaticDns),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[4].staticDns0),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[4].staticDns1),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[4].staticDns2),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[5].ssid),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[5].key),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[5].useStaticIp),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[5].staticIp),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[5].staticSubnet),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[5].staticGateway),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[5].useStaticDns),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[5].staticDns0),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[5].staticDns1),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[5].staticDns2),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[6].ssid),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[6].key),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[6].useStaticIp),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[6].staticIp),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[6].staticSubnet),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[6].staticGateway),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[6].useStaticDns),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[6].staticDns0),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[6].staticDns1),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[6].staticDns2),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[7].ssid),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[7].key),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[7].useStaticIp),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[7].staticIp),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[7].staticSubnet),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[7].staticGateway),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[7].useStaticDns),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[7].staticDns0),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[7].staticDns1),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[7].staticDns2),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[8].ssid),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[8].key),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[8].useStaticIp),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[8].staticIp),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[8].staticSubnet),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[8].staticGateway),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[8].useStaticDns),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[8].staticDns0),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[8].staticDns1),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[8].staticDns2),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[9].ssid),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[9].key),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[9].useStaticIp),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[9].staticIp),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[9].staticSubnet),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[9].staticGateway),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[9].useStaticDns),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[9].staticDns0),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[9].staticDns1),
|
||||
std::ref<ConfigWrapperInterface>(wifi_configs[9].staticDns2),
|
||||
|
||||
std::ref<ConfigWrapperInterface>(wifiStaMinRssi),
|
||||
|
||||
std::ref<ConfigWrapperInterface>(wifiApEnabled),
|
||||
std::ref<ConfigWrapperInterface>(factoryWifiApName),
|
||||
std::ref<ConfigWrapperInterface>(wifiApName),
|
||||
std::ref<ConfigWrapperInterface>(factoryWifiApKey),
|
||||
std::ref<ConfigWrapperInterface>(wifiApKey),
|
||||
std::ref<ConfigWrapperInterface>(wifiApChannel),
|
||||
#define HELPER(x) std::ref<ConfigWrapperInterface>(x),
|
||||
NEW_SETTINGS(HELPER)
|
||||
#undef HELPER
|
||||
std::ref<ConfigWrapperInterface>(wifiApAuthmode)
|
||||
);
|
||||
}
|
||||
|
@ -32,9 +32,11 @@ constexpr const char * const TAG = "BOBBYWEB";
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!std::is_same<T, bool>::value &&
|
||||
!std::is_integral<T>::value &&
|
||||
!std::is_same<T, std::array<int8_t, 4>>::value
|
||||
, bool>::type
|
||||
!std::is_integral<T>::value &&
|
||||
!std::is_same<T, std::string>::value &&
|
||||
!std::is_same<T, wifi_stack::ip_address_t>::value &&
|
||||
!std::is_same<T, wifi_stack::mac_t>::value
|
||||
, bool>::type
|
||||
showInputForSetting(std::string_view key, T value, std::string &body)
|
||||
{
|
||||
HtmlTag spanTag{"span", "style=\"color: red;\"", body};
|
||||
@ -45,7 +47,7 @@ showInputForSetting(std::string_view key, T value, std::string &body)
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
std::is_same<T, bool>::value
|
||||
, bool>::type
|
||||
, bool>::type
|
||||
showInputForSetting(std::string_view key, T value, std::string &body)
|
||||
{
|
||||
body += fmt::format("<input type=\"checkbox\" name=\"{}\" value=\"true\" {}/>"
|
||||
@ -59,8 +61,8 @@ showInputForSetting(std::string_view key, T value, std::string &body)
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!std::is_same<T, bool>::value &&
|
||||
std::is_integral<T>::value
|
||||
, bool>::type
|
||||
std::is_integral<T>::value
|
||||
, bool>::type
|
||||
showInputForSetting(std::string_view key, T value, std::string &body)
|
||||
{
|
||||
body += fmt::format("<input type=\"number\" name=\"{}\" value=\"{}\" min=\"{}\" max=\"{}\" step=\"1\" />",
|
||||
@ -73,16 +75,37 @@ showInputForSetting(std::string_view key, T value, std::string &body)
|
||||
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
std::is_same<T, std::array<int8_t, 4>>::value
|
||||
, bool>::type
|
||||
std::is_same<T, std::string>::value
|
||||
, bool>::type
|
||||
showInputForSetting(std::string_view key, T value, std::string &body)
|
||||
{
|
||||
body += fmt::format("<input type=\"text\" name=\"{}\" value=\"{}{}{}{}\" pattern=\"[0-9]{{4}}\" />",
|
||||
body += fmt::format("<input type=\"text\" name=\"{}\" value=\"{}\" />",
|
||||
esphttpdutils::htmlentities(key),
|
||||
value[0],
|
||||
value[1],
|
||||
value[2],
|
||||
value[3]);
|
||||
esphttpdutils::htmlentities(value));
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
std::is_same<T, wifi_stack::ip_address_t>::value
|
||||
, bool>::type
|
||||
showInputForSetting(std::string_view key, T value, std::string &body)
|
||||
{
|
||||
body += fmt::format("<input type=\"text\" name=\"{}\" value=\"{}\" pattern=\"[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\" />",
|
||||
esphttpdutils::htmlentities(key),
|
||||
esphttpdutils::htmlentities(wifi_stack::toString(value)));
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
std::is_same<T, wifi_stack::mac_t>::value
|
||||
, bool>::type
|
||||
showInputForSetting(std::string_view key, T value, std::string &body)
|
||||
{
|
||||
body += fmt::format("<input type=\"text\" name=\"{}\" value=\"{}\" pattern=\"[0-9a-fA-F]{2}(?:\\:[0-9a-fA-F]{2}){5}\" />",
|
||||
esphttpdutils::htmlentities(key),
|
||||
esphttpdutils::htmlentities(wifi_stack::toString(value)));
|
||||
return true;
|
||||
}
|
||||
} // namespace
|
||||
@ -153,17 +176,18 @@ esp_err_t webserver_newSettings_handler(httpd_req_t *req)
|
||||
|
||||
HtmlTag divTag{"div", "class=\"form-table\"", body};
|
||||
|
||||
ESP_LOGI(TAG, "before the loop");
|
||||
for (const ConfigWrapperInterface &config : configs.getAllConfigParams())
|
||||
{
|
||||
configs.callForEveryConfig([&](const auto &config){
|
||||
if (body.size() > 2048)
|
||||
{
|
||||
CALL_AND_EXIT_ON_ERROR(httpd_resp_send_chunk, req, body.data(), body.size());
|
||||
if (const auto result = httpd_resp_send_chunk(req, body.data(), body.size()); result != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "httpd_resp_send_chunk() failed with %s", esp_err_to_name(result));
|
||||
//return result;
|
||||
}
|
||||
body.clear();
|
||||
}
|
||||
|
||||
const std::string_view nvsName{config.nvsName()};
|
||||
ESP_LOGI(TAG, "in the loop %.*s %zd", nvsName.size(), nvsName.data(), body.size());
|
||||
|
||||
HtmlTag formTag{"form", "class=\"form-table-row\" action=\"/saveNewSettings\" method=\"GET\"", body};
|
||||
|
||||
@ -175,7 +199,7 @@ esp_err_t webserver_newSettings_handler(httpd_req_t *req)
|
||||
|
||||
{
|
||||
HtmlTag divTag{"div", "class=\"form-table-cell\"", body};
|
||||
showInputForSetting(nvsName, 1, body);
|
||||
showInputForSetting(nvsName, config.value, body);
|
||||
}
|
||||
|
||||
{
|
||||
@ -183,29 +207,7 @@ esp_err_t webserver_newSettings_handler(httpd_req_t *req)
|
||||
HtmlTag buttonTag{"button", "type=\"submit\"", body};
|
||||
body += "Save";
|
||||
}
|
||||
}
|
||||
ESP_LOGI(TAG, "after the loop");
|
||||
|
||||
// settings.executeForEveryCommonSetting([&](std::string_view key, const auto &value){
|
||||
// HtmlTag formTag{"form", "class=\"form-table-row\" action=\"/saveNewSettings\" method=\"GET\"", 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);
|
||||
// }
|
||||
|
||||
// {
|
||||
// HtmlTag divTag{"div", "class=\"form-table-cell\"", body};
|
||||
// HtmlTag buttonTag{"button", "type=\"submit\"", body};
|
||||
// body += "Save";
|
||||
// }
|
||||
// });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -219,10 +221,12 @@ namespace {
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!std::is_same<T, bool>::value &&
|
||||
!std::is_integral<T>::value &&
|
||||
!std::is_same<T, std::array<int8_t, 4>>::value
|
||||
, bool>::type
|
||||
saveSetting(T &value, std::string_view newValue, std::string &body)
|
||||
!std::is_integral<T>::value &&
|
||||
!std::is_same<T, std::string>::value &&
|
||||
!std::is_same<T, wifi_stack::ip_address_t>::value &&
|
||||
!std::is_same<T, wifi_stack::mac_t>::value
|
||||
, bool>::type
|
||||
saveSetting(ConfigWrapper<T> &config, std::string_view newValue, std::string &body)
|
||||
{
|
||||
body += "Unsupported config type";
|
||||
return false;
|
||||
@ -231,20 +235,34 @@ saveSetting(T &value, std::string_view newValue, std::string &body)
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
std::is_same<T, bool>::value
|
||||
, bool>::type
|
||||
saveSetting(T &value, std::string_view newValue, std::string &body)
|
||||
, bool>::type
|
||||
saveSetting(ConfigWrapper<T> &config, std::string_view newValue, std::string &body)
|
||||
{
|
||||
if (newValue == "true")
|
||||
{
|
||||
value = true;
|
||||
body += "applied";
|
||||
return true;
|
||||
if (const auto result = configs.write_config(config, true); result)
|
||||
{
|
||||
body += "applied";
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
body += result.error();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if (newValue == "false")
|
||||
{
|
||||
value = false;
|
||||
body += "applied";
|
||||
return true;
|
||||
if (const auto result = configs.write_config(config, false); result)
|
||||
{
|
||||
body += "applied";
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
body += result.error();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -256,15 +274,22 @@ saveSetting(T &value, std::string_view newValue, std::string &body)
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!std::is_same<T, bool>::value &&
|
||||
std::is_integral<T>::value
|
||||
, bool>::type
|
||||
saveSetting(T &value, std::string_view newValue, std::string &body)
|
||||
std::is_integral<T>::value
|
||||
, bool>::type
|
||||
saveSetting(ConfigWrapper<T> &config, std::string_view newValue, std::string &body)
|
||||
{
|
||||
if (auto parsed = cpputils::fromString<T>(newValue))
|
||||
{
|
||||
value = *parsed;
|
||||
body += "applied";
|
||||
return true;
|
||||
if (const auto result = configs.write_config(config, *parsed); result)
|
||||
{
|
||||
body += "applied";
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
body += result.error();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -275,19 +300,70 @@ saveSetting(T &value, std::string_view newValue, std::string &body)
|
||||
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
std::is_same<T, std::array<int8_t, 4>>::value
|
||||
, bool>::type
|
||||
saveSetting(T &value, std::string_view newValue, std::string &body)
|
||||
std::is_same<T, std::string>::value
|
||||
, bool>::type
|
||||
saveSetting(ConfigWrapper<T> &config, std::string_view newValue, std::string &body)
|
||||
{
|
||||
if (std::array<int8_t, 4> parsed; std::sscanf(newValue.data(), "%1hhi%1hhi%1hhi%1hhi", &parsed[0], &parsed[1], &parsed[2], &parsed[3]) == 4)
|
||||
if (const auto result = configs.write_config(config, std::string{newValue}); result)
|
||||
{
|
||||
value = parsed;
|
||||
body += "applied";
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
body += fmt::format("could not parse {}", newValue);
|
||||
body += result.error();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
std::is_same<T, wifi_stack::ip_address_t>::value
|
||||
, bool>::type
|
||||
saveSetting(ConfigWrapper<T> &config, std::string_view newValue, std::string &body)
|
||||
{
|
||||
if (const auto parsed = wifi_stack::fromString<wifi_stack::ip_address_t>(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 T>
|
||||
typename std::enable_if<
|
||||
std::is_same<T, wifi_stack::mac_t>::value
|
||||
, bool>::type
|
||||
saveSetting(ConfigWrapper<T> &config, std::string_view newValue, std::string &body)
|
||||
{
|
||||
if (const auto parsed = wifi_stack::fromString<wifi_stack::mac_t>(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;
|
||||
}
|
||||
}
|
||||
@ -317,45 +393,39 @@ esp_err_t webserver_saveNewSettings_handler(httpd_req_t *req)
|
||||
std::string body;
|
||||
bool success{true};
|
||||
|
||||
// settings.executeForEveryCommonSetting([&](std::string_view key, auto &value){
|
||||
// char valueBufEncoded[256];
|
||||
// if (const auto result = httpd_query_key_value(query.data(), key.data(), valueBufEncoded, 256); result != ESP_OK)
|
||||
// {
|
||||
// if (result != ESP_ERR_NOT_FOUND)
|
||||
// {
|
||||
// const auto msg = fmt::format("{}: httpd_query_key_value() failed with {}", key, esp_err_to_name(result));
|
||||
// ESP_LOGE(TAG, "%.*s", msg.size(), msg.data());
|
||||
// body += msg;
|
||||
// body += '\n';
|
||||
// success = false;
|
||||
// }
|
||||
// return;
|
||||
// }
|
||||
configs.callForEveryConfig([&](auto &config){
|
||||
const std::string_view nvsName{config.nvsName()};
|
||||
|
||||
// char valueBuf[257];
|
||||
// esphttpdutils::urldecode(valueBuf, valueBufEncoded);
|
||||
char valueBufEncoded[256];
|
||||
if (const auto result = httpd_query_key_value(query.data(), nvsName.data(), valueBufEncoded, 256); result != ESP_OK)
|
||||
{
|
||||
if (result != ESP_ERR_NOT_FOUND)
|
||||
{
|
||||
const auto msg = fmt::format("{}: httpd_query_key_value() failed with {}", nvsName, esp_err_to_name(result));
|
||||
ESP_LOGE(TAG, "%.*s", msg.size(), msg.data());
|
||||
body += msg;
|
||||
body += '\n';
|
||||
success = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// body += key;
|
||||
// if (!saveSetting(value, valueBuf, body))
|
||||
// success = false;
|
||||
// body += '\n';
|
||||
// });
|
||||
char valueBuf[257];
|
||||
esphttpdutils::urldecode(valueBuf, valueBufEncoded);
|
||||
|
||||
body += nvsName;
|
||||
if (!saveSetting(config, valueBuf, body))
|
||||
success = false;
|
||||
body += '\n';
|
||||
});
|
||||
|
||||
if (body.empty())
|
||||
CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::Ok, "text/plain", "nothing changed?!")
|
||||
|
||||
// if (settingsPersister.save(settings))
|
||||
// body += "settings persisted successfully";
|
||||
// else
|
||||
// {
|
||||
// body += "error while persisting settings";
|
||||
// success = false;
|
||||
// }
|
||||
|
||||
if (success)
|
||||
{
|
||||
CALL_AND_EXIT_ON_ERROR(httpd_resp_set_hdr, req, "Location", "/settings")
|
||||
body += "\nOk, continue at /settings";
|
||||
CALL_AND_EXIT_ON_ERROR(httpd_resp_set_hdr, req, "Location", "/newSettings")
|
||||
body += "\nOk, continue at /newSettings";
|
||||
}
|
||||
|
||||
CALL_AND_EXIT(esphttpdutils::webserver_resp_send,
|
||||
|
Reference in New Issue
Block a user