Refactors feature service

This commit is contained in:
Rune Harlyk
2024-11-08 17:03:31 +01:00
parent 4fff03ce54
commit a7eec4f7f2
8 changed files with 46 additions and 125 deletions
+3 -4
View File
@@ -16,8 +16,7 @@
#include <ESP32SvelteKit.h>
ESP32SvelteKit::ESP32SvelteKit(PsychicHttpServer *server, unsigned int numberEndpoints)
: _server(server),
_numberEndpoints(numberEndpoints),
: _numberEndpoints(numberEndpoints),
#if FT_ENABLED(USE_UPLOAD_FIRMWARE)
_uploadFirmwareService(server),
#endif
@@ -34,7 +33,7 @@ ESP32SvelteKit::ESP32SvelteKit(PsychicHttpServer *server, unsigned int numberEnd
#if FT_ENABLED(USE_MOTION)
_motionService(_server, &_servoController),
#endif
_featureService(server) {
_server(server) {
}
void ESP32SvelteKit::begin() {
@@ -131,6 +130,7 @@ void ESP32SvelteKit::setupServer() {
// MISC
_server->on("/api/ws/events", socket.getHandler());
_server->on("/api/features", feature_service::getFeatures);
#ifdef EMBED_WWW
ESP_LOGV("ESP32SvelteKit", "Registering routes from PROGMEM static resources");
@@ -193,7 +193,6 @@ void ESP32SvelteKit::setupMDNS() {
void ESP32SvelteKit::startServices() {
_apService.begin();
_featureService.begin();
#if FT_ENABLED(USE_UPLOAD_FIRMWARE)
_uploadFirmwareService.begin();
+1 -4
View File
@@ -27,7 +27,7 @@
#include <ESPmDNS.h>
#include <LEDService.h>
#include <event_socket.h>
#include <FeaturesService.h>
#include <features.h>
#include <MotionService.h>
#include <ntp_service.h>
#include <camera_service.h>
@@ -76,8 +76,6 @@ class ESP32SvelteKit {
BatteryService *getBatteryService() { return &_batteryService; }
#endif
FeaturesService *getFeatureService() { return &_featureService; }
#if FT_ENABLED(USE_MOTION)
MotionService *getMotionService() { return &_motionService; }
#endif
@@ -102,7 +100,6 @@ class ESP32SvelteKit {
private:
PsychicHttpServer *_server;
unsigned int _numberEndpoints;
FeaturesService _featureService;
WiFiService _wifiService;
APService _apService;
EventSocket _socket;
@@ -1,57 +0,0 @@
/**
* ESP32 SvelteKit
*
* A simple, secure and extensible framework for IoT projects for ESP32 platforms
* with responsive Sveltekit front-end built with TailwindCSS and DaisyUI.
* https://github.com/theelims/ESP32-sveltekit
*
* Copyright (C) 2018 - 2023 rjwats
* Copyright (C) 2023 theelims
* Copyright (C) 2024 runeharlyk
*
* All Rights Reserved. This software may be modified and distributed under
* the terms of the LGPL v3 license. See the LICENSE file for details.
**/
#include <FeaturesService.h>
FeaturesService::FeaturesService(PsychicHttpServer *server) : _server(server) {}
void FeaturesService::begin() {
_server->on(FEATURES_SERVICE_PATH, HTTP_GET, [&](PsychicRequest *request) {
PsychicJsonResponse response = PsychicJsonResponse(request, false);
JsonObject root = response.getRoot();
root["ntp"] = USE_NTP;
root["upload_firmware"] = USE_UPLOAD_FIRMWARE;
root["download_firmware"] = USE_DOWNLOAD_FIRMWARE;
root["sleep"] = USE_SLEEP;
root["battery"] = USE_BATTERY;
root["analytics"] = USE_ANALYTICS;
root["camera"] = USE_CAMERA;
root["imu"] = USE_IMU;
root["mag"] = USE_MAG;
root["bmp"] = USE_BMP;
root["sonar"] = USE_USS;
root["firmware_version"] = APP_VERSION;
root["firmware_name"] = APP_NAME;
root["firmware_built_target"] = BUILD_TARGET;
// Iterate over user features
for (auto &element : userFeatures) {
root[element.feature.c_str()] = element.enabled;
}
return response.send();
});
ESP_LOGV("FeaturesService", "Registered GET endpoint: %s", FEATURES_SERVICE_PATH);
}
void FeaturesService::addFeature(String feature, bool enabled) {
UserFeature newFeature;
newFeature.feature = feature;
newFeature.enabled = enabled;
userFeatures.push_back(newFeature);
}
@@ -1,45 +0,0 @@
#ifndef FeaturesService_h
#define FeaturesService_h
/**
* ESP32 SvelteKit
*
* A simple, secure and extensible framework for IoT projects for ESP32 platforms
* with responsive Sveltekit front-end built with TailwindCSS and DaisyUI.
* https://github.com/theelims/ESP32-sveltekit
*
* Copyright (C) 2018 - 2023 rjwats
* Copyright (C) 2023 theelims
*
* All Rights Reserved. This software may be modified and distributed under
* the terms of the LGPL v3 license. See the LICENSE file for details.
**/
#include <Features.h>
#include <WiFi.h>
#include <ArduinoJson.h>
#include <PsychicHttp.h>
#include <vector>
#define FEATURES_SERVICE_PATH "/api/features"
typedef struct {
String feature;
bool enabled;
} UserFeature;
class FeaturesService {
public:
FeaturesService(PsychicHttpServer *server);
void begin();
void addFeature(String feature, bool enabled);
private:
PsychicHttpServer *_server;
std::vector<UserFeature> userFeatures;
};
#endif
+1 -1
View File
@@ -8,7 +8,7 @@
#include <MathUtils.h>
#include <Timing.h>
#include <filesystem.h>
#include <Features.h>
#include <features.h>
#include <settings/peripherals_settings.h>
#include <stateful_service_endpoint.h>
+1 -1
View File
@@ -6,7 +6,7 @@
#include <task_manager.h>
#include <WiFi.h>
#include <async_worker.h>
#include <Features.h>
#include <features.h>
namespace Camera {
+29
View File
@@ -0,0 +1,29 @@
#include <features.h>
namespace feature_service {
void features(JsonObject &root) {
root["ntp"] = USE_NTP;
root["upload_firmware"] = USE_UPLOAD_FIRMWARE;
root["download_firmware"] = USE_DOWNLOAD_FIRMWARE;
root["sleep"] = USE_SLEEP;
root["battery"] = USE_BATTERY;
root["analytics"] = USE_ANALYTICS;
root["camera"] = USE_CAMERA;
root["imu"] = USE_IMU;
root["mag"] = USE_MAG;
root["bmp"] = USE_BMP;
root["sonar"] = USE_USS;
root["firmware_version"] = APP_VERSION;
root["firmware_name"] = APP_NAME;
root["firmware_built_target"] = BUILD_TARGET;
}
esp_err_t getFeatures(PsychicRequest *request) {
PsychicJsonResponse response = PsychicJsonResponse(request, false);
JsonObject root = response.getRoot();
features(root);
return response.send();
}
} // namespace feature_service
@@ -1,19 +1,9 @@
#ifndef Features_h
#define Features_h
/**
* ESP32 SvelteKit
*
* A simple, secure and extensible framework for IoT projects for ESP32 platforms
* with responsive Sveltekit front-end built with TailwindCSS and DaisyUI.
* https://github.com/theelims/ESP32-sveltekit
*
* Copyright (C) 2018 - 2023 rjwats
* Copyright (C) 2023 theelims
*
* All Rights Reserved. This software may be modified and distributed under
* the terms of the LGPL v3 license. See the LICENSE file for details.
**/
#include <WiFi.h>
#include <ArduinoJson.h>
#include <PsychicHttp.h>
#define FT_ENABLED(feature) feature
@@ -77,4 +67,12 @@
#define USE_GPS 0
#endif
namespace feature_service {
void features(JsonObject &root);
esp_err_t getFeatures(PsychicRequest *request);
} // namespace feature_service
#endif