mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-08 08:49:52 +03:00
optimize endpoints, so re-use where possible
This commit is contained in:
@@ -1128,8 +1128,6 @@ bool System::check_restore() {
|
||||
bool reboot_required = false;
|
||||
|
||||
#ifndef EMSESP_STANDALONE
|
||||
// see if we have a temp file, if so try and read it
|
||||
// TODO find a nicer way to see if a file exists without reporting an error, like using lfs_stat. exists() uses open so same problem.
|
||||
File new_file = LittleFS.open(TEMP_FILENAME_PATH);
|
||||
if (new_file) {
|
||||
JsonDocument jsonDocument;
|
||||
|
||||
@@ -29,9 +29,6 @@ WebCustomEntityService::WebCustomEntityService(AsyncWebServer * server, FS * fs,
|
||||
securityManager,
|
||||
AuthenticationPredicates::IS_AUTHENTICATED)
|
||||
, _fsPersistence(WebCustomEntity::read, WebCustomEntity::update, this, fs, EMSESP_CUSTOMENTITY_FILE) {
|
||||
server->on(EMSESP_GET_ENTITIES_PATH,
|
||||
HTTP_GET,
|
||||
securityManager->wrapRequest([this](AsyncWebServerRequest * request) { getEntities(request); }, AuthenticationPredicates::IS_ADMIN));
|
||||
}
|
||||
|
||||
// load the settings when the service starts
|
||||
@@ -711,17 +708,4 @@ void WebCustomEntityService::test() {
|
||||
}
|
||||
#endif
|
||||
|
||||
// return entities as a json object
|
||||
void WebCustomEntityService::getEntities(AsyncWebServerRequest * request) {
|
||||
auto * response = new AsyncJsonResponse(false);
|
||||
JsonObject root = response->getRoot();
|
||||
|
||||
root["type"] = "entities";
|
||||
|
||||
System::extractSettings(EMSESP_CUSTOMENTITY_FILE, "Entities", root);
|
||||
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
@@ -22,7 +22,6 @@
|
||||
|
||||
#define EMSESP_CUSTOMENTITY_FILE "/config/emsespEntity.json"
|
||||
#define EMSESP_CUSTOMENTITY_SERVICE_PATH "/rest/customEntities" // GET and POST
|
||||
#define EMSESP_GET_ENTITIES_PATH "/rest/getEntities"
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -22,9 +22,7 @@
|
||||
#define EMSESP_CUSTOMIZATION_FILE "/config/emsespCustomization.json"
|
||||
|
||||
// GET
|
||||
#define EMSESP_DEVICES_SERVICE_PATH "/rest/devices"
|
||||
#define EMSESP_DEVICE_ENTITIES_PATH "/rest/deviceEntities"
|
||||
#define EMSESP_GET_CUSTOMIZATIONS_PATH "/rest/getCustomizations"
|
||||
|
||||
// POST
|
||||
#define EMSESP_CUSTOMIZATION_ENTITIES_PATH "/rest/customizationEntities"
|
||||
@@ -98,9 +96,7 @@ class WebCustomizationService : public StatefulService<WebCustomization> {
|
||||
FSPersistence<WebCustomization> _fsPersistence;
|
||||
|
||||
// GET
|
||||
void devices(AsyncWebServerRequest * request);
|
||||
void device_entities(AsyncWebServerRequest * request);
|
||||
void getCustomizations(AsyncWebServerRequest * request);
|
||||
|
||||
// POST
|
||||
void customization_entities(AsyncWebServerRequest * request, JsonVariant json);
|
||||
|
||||
@@ -66,6 +66,7 @@ void WebDataService::core_data(AsyncWebServerRequest * request) {
|
||||
obj["p"] = emsdevice->product_id(); // productid
|
||||
obj["v"] = emsdevice->version(); // version
|
||||
obj["e"] = emsdevice->count_entities(); // number of entities
|
||||
obj["url"] = emsdevice->device_type_name(); // non-translated, lower-case, used for API URL in customization page
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,9 +25,6 @@ namespace emsesp {
|
||||
WebSchedulerService::WebSchedulerService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager)
|
||||
: _httpEndpoint(WebScheduler::read, WebScheduler::update, this, server, EMSESP_SCHEDULER_SERVICE_PATH, securityManager, AuthenticationPredicates::IS_AUTHENTICATED)
|
||||
, _fsPersistence(WebScheduler::read, WebScheduler::update, this, fs, EMSESP_SCHEDULER_FILE) {
|
||||
server->on(EMSESP_GET_SCHEDULE_PATH,
|
||||
HTTP_GET,
|
||||
securityManager->wrapRequest([this](AsyncWebServerRequest * request) { getSchedule(request); }, AuthenticationPredicates::IS_ADMIN));
|
||||
}
|
||||
|
||||
// load the settings when the service starts
|
||||
@@ -613,17 +610,4 @@ void WebSchedulerService::test() {
|
||||
}
|
||||
#endif
|
||||
|
||||
// return schedule entries in a json object
|
||||
void WebSchedulerService::getSchedule(AsyncWebServerRequest * request) {
|
||||
auto * response = new AsyncJsonResponse(false);
|
||||
JsonObject root = response->getRoot();
|
||||
|
||||
root["type"] = "schedule";
|
||||
|
||||
System::extractSettings(EMSESP_SCHEDULER_FILE, "Schedule", root);
|
||||
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
|
||||
#define EMSESP_SCHEDULER_FILE "/config/emsespScheduler.json"
|
||||
#define EMSESP_SCHEDULER_SERVICE_PATH "/rest/schedule" // GET and POST
|
||||
#define EMSESP_GET_SCHEDULE_PATH "/rest/getSchedule"
|
||||
|
||||
// bit flags for the schedule items. Matches those in interface/src/app/main/SchedulerDialog.tsx
|
||||
// 0-127 (0->0x7F) is day schedule
|
||||
@@ -91,8 +90,6 @@ class WebSchedulerService : public StatefulService<WebScheduler> {
|
||||
HttpEndpoint<WebScheduler> _httpEndpoint;
|
||||
FSPersistence<WebScheduler> _fsPersistence;
|
||||
|
||||
void getSchedule(AsyncWebServerRequest * request);
|
||||
|
||||
std::list<ScheduleItem> * scheduleItems_; // pointer to the list of schedule events
|
||||
bool ha_registered_ = false;
|
||||
std::deque<ScheduleItem *> cmd_changed_;
|
||||
|
||||
@@ -28,9 +28,6 @@ WebSettingsService::WebSettingsService(AsyncWebServer * server, FS * fs, Securit
|
||||
server->on(EMSESP_BOARD_PROFILE_SERVICE_PATH,
|
||||
HTTP_GET,
|
||||
securityManager->wrapRequest([this](AsyncWebServerRequest * request) { board_profile(request); }, AuthenticationPredicates::IS_AUTHENTICATED));
|
||||
server->on(EMSESP_GET_SETTINGS_PATH,
|
||||
HTTP_GET,
|
||||
securityManager->wrapRequest([this](AsyncWebServerRequest * request) { getSettings(request); }, AuthenticationPredicates::IS_ADMIN));
|
||||
addUpdateHandler([this] { onUpdate(); }, false);
|
||||
}
|
||||
|
||||
@@ -463,25 +460,4 @@ void WebSettingsService::board_profile(AsyncWebServerRequest * request) {
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
// returns json with all system settings
|
||||
void WebSettingsService::getSettings(AsyncWebServerRequest * request) {
|
||||
auto * response = new AsyncJsonResponse(false);
|
||||
JsonObject root = response->getRoot();
|
||||
|
||||
root["type"] = "settings";
|
||||
|
||||
JsonObject node = root["System"].to<JsonObject>();
|
||||
node["version"] = EMSESP_APP_VERSION;
|
||||
|
||||
System::extractSettings(NETWORK_SETTINGS_FILE, "Network", root);
|
||||
System::extractSettings(AP_SETTINGS_FILE, "AP", root);
|
||||
System::extractSettings(MQTT_SETTINGS_FILE, "MQTT", root);
|
||||
System::extractSettings(NTP_SETTINGS_FILE, "NTP", root);
|
||||
System::extractSettings(SECURITY_SETTINGS_FILE, "Security", root);
|
||||
System::extractSettings(EMSESP_SETTINGS_FILE, "Settings", root);
|
||||
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
|
||||
#define EMSESP_SETTINGS_SERVICE_PATH "/rest/settings"
|
||||
#define EMSESP_BOARD_PROFILE_SERVICE_PATH "/rest/boardProfile"
|
||||
#define EMSESP_GET_SETTINGS_PATH "/rest/getSettings"
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
@@ -136,7 +135,6 @@ class WebSettingsService : public StatefulService<WebSettings> {
|
||||
FSPersistence<WebSettings> _fsPersistence;
|
||||
|
||||
void board_profile(AsyncWebServerRequest * request);
|
||||
void getSettings(AsyncWebServerRequest * request);
|
||||
|
||||
void onUpdate();
|
||||
};
|
||||
|
||||
@@ -26,13 +26,16 @@ namespace emsesp {
|
||||
|
||||
WebStatusService::WebStatusService(AsyncWebServer * server, SecurityManager * securityManager) {
|
||||
// GET
|
||||
server->on(EMSESP_HARDWARE_STATUS_SERVICE_PATH, HTTP_GET, [this](AsyncWebServerRequest * request) { hardwareStatus(request); });
|
||||
server->on(EMSESP_SYSTEM_STATUS_SERVICE_PATH, HTTP_GET, [this](AsyncWebServerRequest * request) { systemStatus(request); });
|
||||
server->on(EMSESP_EXPORT_DATA_SERVICE_PATH, HTTP_GET, [this](AsyncWebServerRequest * request) { exportData(request); });
|
||||
|
||||
// POST
|
||||
server->on(EMSESP_CHECK_UPGRADE_PATH, [this](AsyncWebServerRequest * request, JsonVariant json) { checkUpgrade(request, json); });
|
||||
}
|
||||
|
||||
// /rest/systemStatus
|
||||
// This contains both system & hardware Status to avoid having multiple costly endpoints
|
||||
// This is also used for polling during the RestartMonitor to see if EMS-ESP is alive
|
||||
void WebStatusService::systemStatus(AsyncWebServerRequest * request) {
|
||||
EMSESP::system_.refreshHeapMem(); // refresh free heap and max alloc heap
|
||||
|
||||
@@ -40,7 +43,12 @@ void WebStatusService::systemStatus(AsyncWebServerRequest * request) {
|
||||
JsonObject root = response->getRoot();
|
||||
|
||||
root["emsesp_version"] = EMSESP_APP_VERSION;
|
||||
root["status"] = EMSESP::bus_status(); // 0, 1 or 2
|
||||
|
||||
//
|
||||
// System Status
|
||||
//
|
||||
root["emsesp_version"] = EMSESP_APP_VERSION;
|
||||
root["bus_status"] = EMSESP::bus_status(); // 0, 1 or 2
|
||||
root["bus_uptime"] = EMSbus::bus_uptime();
|
||||
root["num_devices"] = EMSESP::count_devices();
|
||||
root["num_sensors"] = EMSESP::temperaturesensor_.no_sensors();
|
||||
@@ -74,20 +82,6 @@ void WebStatusService::systemStatus(AsyncWebServerRequest * request) {
|
||||
#endif
|
||||
}
|
||||
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
// /rest/hardwareStatus
|
||||
// This is also used for polling during the RestartMonitor to see if EMS-ESP is alive
|
||||
void WebStatusService::hardwareStatus(AsyncWebServerRequest * request) {
|
||||
EMSESP::system_.refreshHeapMem(); // refresh free heap and max alloc heap
|
||||
|
||||
auto * response = new AsyncJsonResponse(false);
|
||||
JsonObject root = response->getRoot();
|
||||
|
||||
root["emsesp_version"] = EMSESP_APP_VERSION;
|
||||
|
||||
#ifdef EMSESP_DEBUG
|
||||
#ifdef EMSESP_TEST
|
||||
root["build_flags"] = "DEBUG,TEST";
|
||||
@@ -98,15 +92,16 @@ void WebStatusService::hardwareStatus(AsyncWebServerRequest * request) {
|
||||
root["build_flags"] = "TEST";
|
||||
#endif
|
||||
|
||||
//
|
||||
// Hardware Status
|
||||
//
|
||||
root["esp_platform"] = EMSESP_PLATFORM;
|
||||
|
||||
#ifndef EMSESP_STANDALONE
|
||||
root["cpu_type"] = ESP.getChipModel();
|
||||
root["cpu_rev"] = ESP.getChipRevision();
|
||||
root["cpu_cores"] = ESP.getChipCores();
|
||||
root["cpu_freq_mhz"] = ESP.getCpuFreqMHz();
|
||||
root["max_alloc_heap"] = EMSESP::system_.getMaxAllocMem();
|
||||
root["free_heap"] = EMSESP::system_.getHeapMem();
|
||||
root["arduino_version"] = ARDUINO_VERSION;
|
||||
root["sdk_version"] = ESP.getSdkVersion();
|
||||
root["partition"] = esp_ota_get_running_partition()->label; // active partition
|
||||
@@ -170,4 +165,36 @@ void WebStatusService::checkUpgrade(AsyncWebServerRequest * request, JsonVariant
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
// returns data for a specific feature/settings as a json object
|
||||
void WebStatusService::exportData(AsyncWebServerRequest * request) {
|
||||
auto * response = new AsyncJsonResponse();
|
||||
JsonObject root = response->getRoot();
|
||||
|
||||
String type = request->getParam("type")->value();
|
||||
|
||||
if (type == "settings") {
|
||||
JsonObject node = root["System"].to<JsonObject>();
|
||||
node["version"] = EMSESP_APP_VERSION;
|
||||
System::extractSettings(NETWORK_SETTINGS_FILE, "Network", root);
|
||||
System::extractSettings(AP_SETTINGS_FILE, "AP", root);
|
||||
System::extractSettings(MQTT_SETTINGS_FILE, "MQTT", root);
|
||||
System::extractSettings(NTP_SETTINGS_FILE, "NTP", root);
|
||||
System::extractSettings(SECURITY_SETTINGS_FILE, "Security", root);
|
||||
System::extractSettings(EMSESP_SETTINGS_FILE, "Settings", root);
|
||||
} else if (type == "schedule") {
|
||||
System::extractSettings(EMSESP_SCHEDULER_FILE, "Schedule", root);
|
||||
} else if (type == "customizations") {
|
||||
System::extractSettings(EMSESP_CUSTOMIZATION_FILE, "Customizations", root);
|
||||
} else if (type == "entities") {
|
||||
System::extractSettings(EMSESP_CUSTOMENTITY_FILE, "Entities", root);
|
||||
} else {
|
||||
request->send(400);
|
||||
return;
|
||||
}
|
||||
|
||||
root["type"] = type;
|
||||
response->setLength();
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#ifndef WebStatusService_h
|
||||
#define WebStatusService_h
|
||||
|
||||
#define EMSESP_HARDWARE_STATUS_SERVICE_PATH "/rest/hardwareStatus"
|
||||
#define EMSESP_SYSTEM_STATUS_SERVICE_PATH "/rest/systemStatus"
|
||||
#define EMSESP_CHECK_UPGRADE_PATH "/rest/checkUpgrade"
|
||||
#define EMSESP_EXPORT_DATA_SERVICE_PATH "/rest/exportData"
|
||||
|
||||
#include <semver200.h>
|
||||
#include <semver200.h> // for version checking
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
@@ -15,8 +15,8 @@ class WebStatusService {
|
||||
|
||||
private:
|
||||
void systemStatus(AsyncWebServerRequest * request);
|
||||
void hardwareStatus(AsyncWebServerRequest * request);
|
||||
void checkUpgrade(AsyncWebServerRequest * request, JsonVariant json);
|
||||
void exportData(AsyncWebServerRequest * request);
|
||||
};
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
Reference in New Issue
Block a user