Merge pull request #196 from bobbycar-graz/webserver_newsettings_reset

Add reset option to webserver newSettings
This commit is contained in:
2021-12-29 05:25:25 +01:00
committed by GitHub
3 changed files with 87 additions and 5 deletions

View File

@ -59,7 +59,7 @@ void initWebserver()
{
httpd_config_t httpConfig HTTPD_DEFAULT_CONFIG();
httpConfig.core_id = 1;
httpConfig.max_uri_handlers = 16;
httpConfig.max_uri_handlers = 17;
httpConfig.stack_size = 8192;
const auto result = httpd_start(&httpdHandle, &httpConfig);
@ -83,8 +83,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 },
httpd_uri_t { .uri = "/newSettings", .method = HTTP_GET, .handler = webserver_newSettings_handler, .user_ctx = NULL },
httpd_uri_t { .uri = "/saveNewSettings", .method = HTTP_GET, .handler = webserver_saveNewSettings_handler, .user_ctx = NULL },
httpd_uri_t { .uri = "/newSettings", .method = HTTP_GET, .handler = webserver_newSettings_handler, .user_ctx = NULL },
httpd_uri_t { .uri = "/saveNewSettings", .method = HTTP_GET, .handler = webserver_saveNewSettings_handler, .user_ctx = NULL },
httpd_uri_t { .uri = "/resetNewSettings", .method = HTTP_GET, .handler = webserver_resetNewSettings_handler, .user_ctx = NULL },
#ifdef OLD_NVS
httpd_uri_t { .uri = "/dumpnvs", .method = HTTP_GET, .handler = webserver_dump_nvs_handler, .user_ctx = NULL },
#endif

View File

@ -213,8 +213,17 @@ esp_err_t webserver_newSettings_handler(httpd_req_t *req)
if (config.touched())
{
HtmlTag buttonTag{"span", "style=\"color: red;\"", body};
body += "Touched";
{
HtmlTag buttonTag{"span", "style=\"color: red;\"", body};
body += "Touched";
}
body += ' ';
{
HtmlTag buttonTag{"a", fmt::format("href=\"/resetNewSettings?{}=1\"", esphttpdutils::htmlentities(nvsName)), body};
body += "Reset";
}
}
}
});
@ -444,4 +453,75 @@ esp_err_t webserver_saveNewSettings_handler(httpd_req_t *req)
"text/plain",
body)
}
esp_err_t webserver_resetNewSettings_handler(httpd_req_t *req)
{
#ifndef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_KORREKT_ARBEITET
espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil<espcpputils::ticks>(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);
}
#endif
std::string query;
if (auto result = esphttpdutils::webserver_get_query(req))
query = *result;
else
{
ESP_LOGE(TAG, "%.*s", result.error().size(), result.error().data());
CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", result.error());
}
std::string body;
bool success{true};
configs.callForEveryConfig([&](auto &config){
const std::string_view nvsName{config.nvsName()};
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 += nvsName;
body += ' ';
if (const auto result = configs.reset_config(config); result)
body += "reset successful";
else
{
body += result.error();
success = false;
}
body += '\n';
});
if (body.empty())
CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::Ok, "text/plain", "nothing changed?!")
if (success)
{
CALL_AND_EXIT_ON_ERROR(httpd_resp_set_hdr, req, "Location", "/newSettings")
body += "\nOk, continue at /newSettings";
}
CALL_AND_EXIT(esphttpdutils::webserver_resp_send,
req,
success ? esphttpdutils::ResponseStatus::TemporaryRedirect : esphttpdutils::ResponseStatus::BadRequest,
"text/plain",
body)
}
#endif

View File

@ -9,4 +9,5 @@
#ifdef FEATURE_WEBSERVER
esp_err_t webserver_newSettings_handler(httpd_req_t *req);
esp_err_t webserver_saveNewSettings_handler(httpd_req_t *req);
esp_err_t webserver_resetNewSettings_handler(httpd_req_t *req);
#endif