mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-08 00:39:50 +03:00
asyncwebserver optimizations
This commit is contained in:
@@ -1 +1 @@
|
||||
#define EMSESP_APP_VERSION "3.6.5-dev.16"
|
||||
#define EMSESP_APP_VERSION "3.6.5-dev.17"
|
||||
|
||||
@@ -24,12 +24,11 @@ uint32_t WebAPIService::api_count_ = 0;
|
||||
uint16_t WebAPIService::api_fails_ = 0;
|
||||
|
||||
WebAPIService::WebAPIService(AsyncWebServer * server, SecurityManager * securityManager)
|
||||
: _securityManager(securityManager)
|
||||
, _apiHandler(EMSESP_API_SERVICE_PATH, [this](AsyncWebServerRequest * request, JsonVariant json) { webAPIService_post(request, json); }) { // for POSTs
|
||||
server->on(EMSESP_API_SERVICE_PATH, HTTP_GET, [this](AsyncWebServerRequest * request) { webAPIService_get(request); }); // for GETs
|
||||
server->addHandler(&_apiHandler);
|
||||
: _securityManager(securityManager) {
|
||||
// API
|
||||
server->on(EMSESP_API_SERVICE_PATH, [this](AsyncWebServerRequest * request, JsonVariant json) { webAPIService(request, json); });
|
||||
|
||||
// for settings
|
||||
// settings
|
||||
server->on(GET_SETTINGS_PATH,
|
||||
HTTP_GET,
|
||||
securityManager->wrapRequest([this](AsyncWebServerRequest * request) { getSettings(request); }, AuthenticationPredicates::IS_ADMIN));
|
||||
@@ -47,31 +46,23 @@ WebAPIService::WebAPIService(AsyncWebServer * server, SecurityManager * security
|
||||
securityManager->wrapRequest([this](AsyncWebServerRequest * request) { getEntities(request); }, AuthenticationPredicates::IS_ADMIN));
|
||||
}
|
||||
|
||||
// HTTP GET
|
||||
// GET /{device}
|
||||
// GET /{device}/{entity}
|
||||
void WebAPIService::webAPIService_get(AsyncWebServerRequest * request) {
|
||||
// has no body JSON so create dummy as empty input object
|
||||
JsonDocument input_doc;
|
||||
JsonObject input = input_doc.to<JsonObject>();
|
||||
parse(request, input);
|
||||
}
|
||||
|
||||
// For HTTP POSTS with an optional JSON body
|
||||
// HTTP_POST | HTTP_PUT | HTTP_PATCH
|
||||
// POST /{device}[/{hc|id}][/{name}]
|
||||
void WebAPIService::webAPIService_post(AsyncWebServerRequest * request, JsonVariant json) {
|
||||
// POST|GET /{device}
|
||||
// POST|GET /{device}/{entity}
|
||||
void WebAPIService::webAPIService(AsyncWebServerRequest * request, JsonVariant json) {
|
||||
JsonObject input;
|
||||
// if no body then treat it as a secure GET
|
||||
if (!json.is<JsonObject>()) {
|
||||
webAPIService_get(request);
|
||||
return;
|
||||
if ((request->method() == HTTP_GET) || (!json.is<JsonObject>())) {
|
||||
// HTTP GET
|
||||
JsonDocument input_doc; // has no body JSON so create dummy as empty input object
|
||||
input = input_doc.to<JsonObject>();
|
||||
} else {
|
||||
// HTTP_POST | HTTP_PUT | HTTP_PATCH
|
||||
input = json.as<JsonObject>(); // extract values from the json. these will be used as default values
|
||||
}
|
||||
|
||||
// extract values from the json. these will be used as default values
|
||||
auto && input = json.as<JsonObject>();
|
||||
parse(request, input);
|
||||
}
|
||||
|
||||
|
||||
// parse the URL looking for query or path parameters
|
||||
// reporting back any errors
|
||||
void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject input) {
|
||||
@@ -84,7 +75,7 @@ void WebAPIService::parse(AsyncWebServerRequest * request, JsonObject input) {
|
||||
|
||||
// check for query parameters first, the old style from v2
|
||||
// api?device={device}&cmd={name}&data={value}&id={hc}
|
||||
if (request->url() == "/api") {
|
||||
if (request->url() == EMSESP_API_SERVICE_PATH) {
|
||||
// get the device
|
||||
if (request->hasParam(F_(device))) {
|
||||
input["device"] = request->getParam(F_(device))->value().c_str();
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#define WebAPIService_h
|
||||
|
||||
#define EMSESP_API_SERVICE_PATH "/api"
|
||||
|
||||
#define GET_SETTINGS_PATH "/rest/getSettings"
|
||||
#define GET_CUSTOMIZATIONS_PATH "/rest/getCustomizations"
|
||||
#define GET_SCHEDULE_PATH "/rest/getSchedule"
|
||||
@@ -31,8 +32,7 @@ class WebAPIService {
|
||||
public:
|
||||
WebAPIService(AsyncWebServer * server, SecurityManager * securityManager);
|
||||
|
||||
void webAPIService_post(AsyncWebServerRequest * request, JsonVariant json); // for POSTs
|
||||
void webAPIService_get(AsyncWebServerRequest * request); // for GETs
|
||||
void webAPIService(AsyncWebServerRequest * request, JsonVariant json);
|
||||
|
||||
static uint32_t api_count() {
|
||||
return api_count_;
|
||||
@@ -43,14 +43,12 @@ class WebAPIService {
|
||||
}
|
||||
|
||||
private:
|
||||
SecurityManager * _securityManager;
|
||||
AsyncCallbackJsonWebHandler _apiHandler; // for POSTs
|
||||
SecurityManager * _securityManager;
|
||||
|
||||
static uint32_t api_count_;
|
||||
static uint16_t api_fails_;
|
||||
|
||||
void parse(AsyncWebServerRequest * request, JsonObject input);
|
||||
|
||||
void getSettings(AsyncWebServerRequest * request);
|
||||
void getCustomizations(AsyncWebServerRequest * request);
|
||||
void getSchedule(AsyncWebServerRequest * request);
|
||||
|
||||
@@ -23,10 +23,7 @@ namespace emsesp {
|
||||
bool WebCustomization::_start = true;
|
||||
|
||||
WebCustomizationService::WebCustomizationService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager)
|
||||
: _fsPersistence(WebCustomization::read, WebCustomization::update, this, fs, EMSESP_CUSTOMIZATION_FILE)
|
||||
, _masked_entities_handler(CUSTOMIZATION_ENTITIES_PATH,
|
||||
securityManager->wrapCallback([this](AsyncWebServerRequest * request, JsonVariant json) { customization_entities(request, json); },
|
||||
AuthenticationPredicates::IS_AUTHENTICATED)) {
|
||||
: _fsPersistence(WebCustomization::read, WebCustomization::update, this, fs, EMSESP_CUSTOMIZATION_FILE) {
|
||||
server->on(DEVICE_ENTITIES_PATH,
|
||||
HTTP_GET,
|
||||
securityManager->wrapRequest([this](AsyncWebServerRequest * request) { device_entities(request); }, AuthenticationPredicates::IS_AUTHENTICATED));
|
||||
@@ -39,9 +36,9 @@ WebCustomizationService::WebCustomizationService(AsyncWebServer * server, FS * f
|
||||
HTTP_POST,
|
||||
securityManager->wrapRequest([this](AsyncWebServerRequest * request) { reset_customization(request); }, AuthenticationPredicates::IS_ADMIN));
|
||||
|
||||
_masked_entities_handler.setMethod(HTTP_POST);
|
||||
_masked_entities_handler.setMaxContentLength(2048);
|
||||
server->addHandler(&_masked_entities_handler);
|
||||
server->on(CUSTOMIZATION_ENTITIES_PATH,
|
||||
securityManager->wrapCallback([this](AsyncWebServerRequest * request, JsonVariant json) { customization_entities(request, json); },
|
||||
AuthenticationPredicates::IS_AUTHENTICATED));
|
||||
}
|
||||
|
||||
// this creates the customization file, saving it to the FS
|
||||
|
||||
@@ -101,8 +101,6 @@ class WebCustomizationService : public StatefulService<WebCustomization> {
|
||||
// POST
|
||||
void customization_entities(AsyncWebServerRequest * request, JsonVariant json);
|
||||
void reset_customization(AsyncWebServerRequest * request); // command
|
||||
|
||||
AsyncCallbackJsonWebHandler _masked_entities_handler;
|
||||
};
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
@@ -21,16 +21,18 @@
|
||||
namespace emsesp {
|
||||
|
||||
WebDataService::WebDataService(AsyncWebServer * server, SecurityManager * securityManager)
|
||||
: _write_value_handler(WRITE_DEVICE_VALUE_SERVICE_PATH,
|
||||
securityManager->wrapCallback([this](AsyncWebServerRequest * request, JsonVariant json) { write_device_value(request, json); },
|
||||
AuthenticationPredicates::IS_ADMIN))
|
||||
, _write_temperature_handler(WRITE_TEMPERATURE_SENSOR_SERVICE_PATH,
|
||||
securityManager->wrapCallback([this](AsyncWebServerRequest * request,
|
||||
JsonVariant json) { write_temperature_sensor(request, json); },
|
||||
AuthenticationPredicates::IS_ADMIN))
|
||||
, _write_analog_handler(WRITE_ANALOG_SENSOR_SERVICE_PATH,
|
||||
securityManager->wrapCallback([this](AsyncWebServerRequest * request, JsonVariant json) { write_analog_sensor(request, json); },
|
||||
AuthenticationPredicates::IS_ADMIN)) {
|
||||
|
||||
{
|
||||
// write endpoints
|
||||
server->on(WRITE_DEVICE_VALUE_SERVICE_PATH,
|
||||
securityManager->wrapCallback([this](AsyncWebServerRequest * request, JsonVariant json) { write_device_value(request, json); },
|
||||
AuthenticationPredicates::IS_ADMIN));
|
||||
server->on(WRITE_TEMPERATURE_SENSOR_SERVICE_PATH,
|
||||
securityManager->wrapCallback([this](AsyncWebServerRequest * request, JsonVariant json) { write_temperature_sensor(request, json); },
|
||||
AuthenticationPredicates::IS_ADMIN));
|
||||
server->on(WRITE_ANALOG_SENSOR_SERVICE_PATH,
|
||||
securityManager->wrapCallback([this](AsyncWebServerRequest * request, JsonVariant json) { write_analog_sensor(request, json); },
|
||||
AuthenticationPredicates::IS_ADMIN));
|
||||
// GET's
|
||||
server->on(DEVICE_DATA_SERVICE_PATH,
|
||||
HTTP_GET,
|
||||
@@ -49,19 +51,6 @@ WebDataService::WebDataService(AsyncWebServer * server, SecurityManager * securi
|
||||
server->on(SCAN_DEVICES_SERVICE_PATH,
|
||||
HTTP_POST,
|
||||
securityManager->wrapRequest([this](AsyncWebServerRequest * request) { scan_devices(request); }, AuthenticationPredicates::IS_ADMIN));
|
||||
|
||||
|
||||
_write_value_handler.setMethod(HTTP_POST);
|
||||
_write_value_handler.setMaxContentLength(256);
|
||||
server->addHandler(&_write_value_handler);
|
||||
|
||||
_write_temperature_handler.setMethod(HTTP_POST);
|
||||
_write_temperature_handler.setMaxContentLength(256);
|
||||
server->addHandler(&_write_temperature_handler);
|
||||
|
||||
_write_analog_handler.setMethod(HTTP_POST);
|
||||
_write_analog_handler.setMaxContentLength(256);
|
||||
server->addHandler(&_write_analog_handler);
|
||||
}
|
||||
|
||||
// scan devices service
|
||||
|
||||
@@ -51,8 +51,6 @@ class WebDataService {
|
||||
void write_temperature_sensor(AsyncWebServerRequest * request, JsonVariant json);
|
||||
void write_analog_sensor(AsyncWebServerRequest * request, JsonVariant json);
|
||||
void scan_devices(AsyncWebServerRequest * request); // command
|
||||
|
||||
AsyncCallbackJsonWebHandler _write_value_handler, _write_temperature_handler, _write_analog_handler;
|
||||
};
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
@@ -21,17 +21,16 @@
|
||||
namespace emsesp {
|
||||
|
||||
WebLogService::WebLogService(AsyncWebServer * server, SecurityManager * securityManager)
|
||||
: events_(EVENT_SOURCE_LOG_PATH)
|
||||
, setValues_(LOG_SETTINGS_PATH, [this](AsyncWebServerRequest * request, JsonVariant json) { setValues(request, json); }) {
|
||||
events_.setFilter(securityManager->filterRequest(AuthenticationPredicates::IS_ADMIN));
|
||||
|
||||
: events_(EVENT_SOURCE_LOG_PATH) {
|
||||
// set settings
|
||||
server->on(LOG_SETTINGS_PATH, [this](AsyncWebServerRequest * request, JsonVariant json) { setValues(request, json); });
|
||||
// get settings
|
||||
server->on(LOG_SETTINGS_PATH, HTTP_GET, [this](AsyncWebServerRequest * request) { getValues(request); });
|
||||
|
||||
// for bring back the whole log - is a command, hence a POST
|
||||
server->on(FETCH_LOG_PATH, HTTP_POST, [this](AsyncWebServerRequest * request) { fetchLog(request); });
|
||||
|
||||
server->addHandler(&setValues_);
|
||||
events_.setFilter(securityManager->filterRequest(AuthenticationPredicates::IS_ADMIN));
|
||||
server->addHandler(&events_);
|
||||
}
|
||||
|
||||
|
||||
@@ -66,8 +66,6 @@ class WebLogService : public uuid::log::Handler {
|
||||
|
||||
void setValues(AsyncWebServerRequest * request, JsonVariant json);
|
||||
|
||||
AsyncCallbackJsonWebHandler setValues_; // for POSTs
|
||||
|
||||
uint64_t last_transmit_ = 0; // Last transmit time
|
||||
size_t maximum_log_messages_ = MAX_LOG_MESSAGES; // Maximum number of log messages to buffer before they are output
|
||||
size_t limit_log_messages_ = 1; // dynamic limit
|
||||
|
||||
Reference in New Issue
Block a user