implemented getters setters for newconfig

This commit is contained in:
2021-12-28 23:06:58 +01:00
parent 138412e2ae
commit 5cdf650627
2 changed files with 295 additions and 210 deletions

View File

@@ -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,