Added check for required redraw

This commit is contained in:
CommanderRedYT
2021-12-11 01:28:57 +01:00
parent 169d5434da
commit bd9fdeac7b
5 changed files with 95 additions and 4 deletions

View File

@ -1,10 +1,18 @@
#include "webserver.h"
#include "sdkconfig.h"
#ifdef FEATURE_WEBSERVER
namespace {
constexpr const char * const TAG = "BOBBYWEB";
} // namespace
namespace bobbywebserver {
bool forceRefresh{false};
bool lastScreenWasMenu{};
int8_t lastSelectIndex{};
std::vector<std::pair<std::string, const espgui::MenuItemIcon*>> menuBuf{};
}
httpd_handle_t httpdHandle;
void initWebserver()
@ -18,6 +26,7 @@ void initWebserver()
httpd_config_t httpConfig HTTPD_DEFAULT_CONFIG();
httpConfig.core_id = 1;
httpConfig.max_uri_handlers = 14;
httpConfig.stack_size = 8192;
const auto result = httpd_start(&httpdHandle, &httpConfig);
ESP_LOG_LEVEL_LOCAL((result == ESP_OK ? ESP_LOG_INFO : ESP_LOG_ERROR), TAG, "httpd_start(): %s", esp_err_to_name(result));
@ -68,9 +77,57 @@ esp_err_t webserver_reboot_handler(httpd_req_t *req)
CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::Ok, "text/plain", "REBOOT called...")
}
bool menuDisplayChanged()
{
using namespace bobbywebserver;
if (auto currentDisplay = static_cast<const espgui::Display *>(espgui::currentDisplay.get()))
{
lastScreenWasMenu = true;
if (const auto *menuDisplay = currentDisplay->asMenuDisplay())
{
if (menuBuf.size() != menuDisplay->menuItemCount())
{
menuBuf.resize(menuDisplay->menuItemCount());
auto iterator = std::begin(menuBuf);
menuDisplay->runForEveryMenuItem([&,selectedIndex=menuDisplay->selectedIndex()](const espgui::MenuItem &menuItem){
*(iterator++) = std::make_pair(menuItem.text(), menuItem.icon());
});
lastSelectIndex = menuDisplay->selectedIndex();
return true;
}
bool _return{false};
auto iterator = std::begin(menuBuf);
menuDisplay->runForEveryMenuItem([&,selectedIndex=menuDisplay->selectedIndex()](const espgui::MenuItem &menuItem){
if (menuItem.text() != iterator->first || menuItem.icon() != iterator->second)
{
*iterator = std::make_pair(menuItem.text(), menuItem.icon());
_return = true;
}
iterator++;
});
if (menuDisplay->selectedIndex() != lastSelectIndex)
_return = true;
lastSelectIndex = menuDisplay->selectedIndex();
return _return;
}
return false;
}
else
{
if (lastScreenWasMenu)
{
lastScreenWasMenu = false;
menuBuf.clear();
return true;
}
return false;
}
}
esp_err_t webserver_status_handler(httpd_req_t *req)
{
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
#ifdef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_FUNKTIONIERT
espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil<espcpputils::ticks>(5s).count()};
if (!helper.locked())
{
@ -78,6 +135,7 @@ esp_err_t webserver_status_handler(httpd_req_t *req)
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 wants_json_query;
if (auto result = esphttpdutils::webserver_get_query(req))
@ -85,6 +143,7 @@ esp_err_t webserver_status_handler(httpd_req_t *req)
else
{
ESP_LOGE(TAG, "%.*s", result.error().size(), result.error().data());
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", result.error());
}
@ -92,10 +151,19 @@ esp_err_t webserver_status_handler(httpd_req_t *req)
const auto key_result = httpd_query_key_value(wants_json_query.data(), "json", tmpBuf, 256);
if (key_result == ESP_OK && (tmpBuf == stringSettings.webserver_password || stringSettings.webserver_password.empty()))
{
CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::Ok, "text/plain", "Ok.");
if (!menuDisplayChanged())
{
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::Ok, "text/plain", "Ok.");
}
else
{
return webserver_root_handler(req);
}
}
else
{
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::Unauthorized, "text/plain", "");
}
}

View File

@ -31,6 +31,14 @@ extern httpd_handle_t httpdHandle;
void initWebserver();
void handleWebserver();
bool MenuDisplayChanged();
esp_err_t webserver_reboot_handler(httpd_req_t *req);
esp_err_t webserver_status_handler(httpd_req_t *req);
namespace bobbywebserver {
extern bool forceRefresh;
extern bool lastScreenWasMenu;
extern int8_t lastSelectIndex;
extern std::vector<std::pair<std::string, const espgui::MenuItemIcon*>> menuBuf;
}
#endif

View File

@ -9,6 +9,7 @@ constexpr const char * const TAG = "BOBBYWEB";
esp_err_t webserver_root_handler(httpd_req_t *req)
{
#ifdef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_FUNKTIONIERT
espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil<espcpputils::ticks>(5s).count()};
if (!helper.locked())
{
@ -16,6 +17,7 @@ esp_err_t webserver_root_handler(httpd_req_t *req)
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 body;
@ -103,12 +105,12 @@ esp_err_t webserver_root_handler(httpd_req_t *req)
}
else
{
body += "\"err\":\"Screen not implemented yet.\",";
body += "\"err\":\"Screen not implemented yet.\"";
}
}
else
{
body += "\"err\":\"Currently no screen instantiated.\",";
body += "\"err\":\"Currently no screen instantiated.\"";
}
body += "}";
@ -119,6 +121,7 @@ esp_err_t webserver_root_handler(httpd_req_t *req)
size_t lastEckig = body.rfind("],");
if (std::string::npos != lastEckig)
body = body.erase(lastEckig+1, 1);
}
else if (key_result != ESP_ERR_NOT_FOUND && tmpBuf != stringSettings.webserver_password)
{
@ -217,6 +220,7 @@ esp_err_t webserver_root_handler(httpd_req_t *req)
esp_err_t webserver_triggerButton_handler(httpd_req_t *req)
{
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
#ifdef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_FUNKTIONIERT
espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil<espcpputils::ticks>(5s).count()};
if (!helper.locked())
{
@ -224,6 +228,7 @@ esp_err_t webserver_triggerButton_handler(httpd_req_t *req)
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))
@ -334,6 +339,7 @@ esp_err_t webserver_triggerButton_handler(httpd_req_t *req)
esp_err_t webserver_triggerItem_handler(httpd_req_t *req)
{
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
#ifdef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_FUNKTIONIERT
espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil<espcpputils::ticks>(5s).count()};
if (!helper.locked())
{
@ -341,6 +347,7 @@ esp_err_t webserver_triggerItem_handler(httpd_req_t *req)
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))
@ -421,6 +428,7 @@ esp_err_t webserver_triggerItem_handler(httpd_req_t *req)
esp_err_t webserver_setValue_handler(httpd_req_t *req)
{
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
#ifdef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_FUNKTIONIERT
espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil<espcpputils::ticks>(5s).count()};
if (!helper.locked())
{
@ -428,6 +436,7 @@ esp_err_t webserver_setValue_handler(httpd_req_t *req)
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))

View File

@ -114,6 +114,7 @@ showInputForSetting(std::string_view key, T value, JsonObject &body)
esp_err_t webserver_dump_nvs_handler(httpd_req_t *req)
{
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
#ifdef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_FUNKTIONIERT
espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil<espcpputils::ticks>(5s).count()};
if (!helper.locked())
{
@ -121,6 +122,7 @@ esp_err_t webserver_dump_nvs_handler(httpd_req_t *req)
ESP_LOGE(TAG, "%.*s", msg.size(), msg.data());
CALL_AND_EXIT(esphttpdutils::webserver_resp_send, req, esphttpdutils::ResponseStatus::BadRequest, "text/plain", msg);
}
#endif
DynamicJsonDocument doc(6144);
const auto profile = settingsPersister.currentlyOpenProfileIndex();

View File

@ -13,6 +13,7 @@ constexpr const char * const TAG = "BOBBYWEB";
esp_err_t webserver_ota_percentage_handler(httpd_req_t *req)
{
#ifdef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_FUNKTIONIERT
espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil<espcpputils::ticks>(5s).count()};
if (!helper.locked())
{
@ -20,6 +21,7 @@ esp_err_t webserver_ota_percentage_handler(httpd_req_t *req)
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 body;
@ -70,6 +72,7 @@ esp_err_t webserver_ota_percentage_handler(httpd_req_t *req)
esp_err_t webserver_ota_handler(httpd_req_t *req)
{
#ifdef FEATURE_IS_MIR_EGAL_OB_DER_WEBSERVER_FUNKTIONIERT
espcpputils::LockHelper helper{webserver_lock->handle, std::chrono::ceil<espcpputils::ticks>(5s).count()};
if (!helper.locked())
{
@ -77,6 +80,7 @@ esp_err_t webserver_ota_handler(httpd_req_t *req)
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 body;