optimize endpoints, so re-use where possible

This commit is contained in:
proddy
2024-09-13 10:03:11 +02:00
parent 63a3152b91
commit 191edffe3c
23 changed files with 192 additions and 428 deletions

View File

@@ -28,13 +28,6 @@ WebCustomizationService::WebCustomizationService(AsyncWebServer * server, FS * f
server->on(EMSESP_DEVICE_ENTITIES_PATH,
HTTP_GET,
securityManager->wrapRequest([this](AsyncWebServerRequest * request) { device_entities(request); }, AuthenticationPredicates::IS_AUTHENTICATED));
server->on(EMSESP_DEVICES_SERVICE_PATH,
HTTP_GET,
securityManager->wrapRequest([this](AsyncWebServerRequest * request) { devices(request); }, AuthenticationPredicates::IS_AUTHENTICATED));
server->on(EMSESP_GET_CUSTOMIZATIONS_PATH,
HTTP_GET,
securityManager->wrapRequest([this](AsyncWebServerRequest * request) { getCustomizations(request); }, AuthenticationPredicates::IS_ADMIN));
// POST
server->on(EMSESP_RESET_CUSTOMIZATION_SERVICE_PATH,
@@ -166,29 +159,6 @@ void WebCustomizationService::reset_customization(AsyncWebServerRequest * reques
#endif
}
// send back a list of devices used in the customization web page
void WebCustomizationService::devices(AsyncWebServerRequest * request) {
auto * response = new AsyncJsonResponse(false);
JsonObject root = response->getRoot();
// list is already sorted by device type
// controller is ignored since it doesn't have any associated entities
JsonArray devices = root["devices"].to<JsonArray>();
for (const auto & emsdevice : EMSESP::emsdevices) {
if (emsdevice->has_entities()) {
JsonObject obj = devices.add<JsonObject>();
obj["i"] = emsdevice->unique_id(); // its unique id
obj["s"] = emsdevice->name(); // custom name
obj["t"] = emsdevice->device_type(); // internal device type ID
obj["tn"] = std::string(emsdevice->device_type_2_device_name_translated()); // translated device type name
obj["url"] = emsdevice->device_type_name(); // non-translated, lower-case, used for API URL
}
}
response->setLength();
request->send(response);
}
// send back list of device entities
void WebCustomizationService::device_entities(AsyncWebServerRequest * request) {
uint8_t id;
@@ -425,17 +395,4 @@ void WebCustomizationService::test() {
}
#endif
// return all customizations in a json object
void WebCustomizationService::getCustomizations(AsyncWebServerRequest * request) {
auto * response = new AsyncJsonResponse(false);
JsonObject root = response->getRoot();
root["type"] = "customizations";
System::extractSettings(EMSESP_CUSTOMIZATION_FILE, "Customizations", root);
response->setLength();
request->send(response);
}
} // namespace emsesp