✨ Refactors feature service
This commit is contained in:
@@ -16,8 +16,7 @@
|
|||||||
#include <ESP32SvelteKit.h>
|
#include <ESP32SvelteKit.h>
|
||||||
|
|
||||||
ESP32SvelteKit::ESP32SvelteKit(PsychicHttpServer *server, unsigned int numberEndpoints)
|
ESP32SvelteKit::ESP32SvelteKit(PsychicHttpServer *server, unsigned int numberEndpoints)
|
||||||
: _server(server),
|
: _numberEndpoints(numberEndpoints),
|
||||||
_numberEndpoints(numberEndpoints),
|
|
||||||
#if FT_ENABLED(USE_UPLOAD_FIRMWARE)
|
#if FT_ENABLED(USE_UPLOAD_FIRMWARE)
|
||||||
_uploadFirmwareService(server),
|
_uploadFirmwareService(server),
|
||||||
#endif
|
#endif
|
||||||
@@ -34,7 +33,7 @@ ESP32SvelteKit::ESP32SvelteKit(PsychicHttpServer *server, unsigned int numberEnd
|
|||||||
#if FT_ENABLED(USE_MOTION)
|
#if FT_ENABLED(USE_MOTION)
|
||||||
_motionService(_server, &_servoController),
|
_motionService(_server, &_servoController),
|
||||||
#endif
|
#endif
|
||||||
_featureService(server) {
|
_server(server) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void ESP32SvelteKit::begin() {
|
void ESP32SvelteKit::begin() {
|
||||||
@@ -131,6 +130,7 @@ void ESP32SvelteKit::setupServer() {
|
|||||||
|
|
||||||
// MISC
|
// MISC
|
||||||
_server->on("/api/ws/events", socket.getHandler());
|
_server->on("/api/ws/events", socket.getHandler());
|
||||||
|
_server->on("/api/features", feature_service::getFeatures);
|
||||||
|
|
||||||
#ifdef EMBED_WWW
|
#ifdef EMBED_WWW
|
||||||
ESP_LOGV("ESP32SvelteKit", "Registering routes from PROGMEM static resources");
|
ESP_LOGV("ESP32SvelteKit", "Registering routes from PROGMEM static resources");
|
||||||
@@ -193,7 +193,6 @@ void ESP32SvelteKit::setupMDNS() {
|
|||||||
|
|
||||||
void ESP32SvelteKit::startServices() {
|
void ESP32SvelteKit::startServices() {
|
||||||
_apService.begin();
|
_apService.begin();
|
||||||
_featureService.begin();
|
|
||||||
|
|
||||||
#if FT_ENABLED(USE_UPLOAD_FIRMWARE)
|
#if FT_ENABLED(USE_UPLOAD_FIRMWARE)
|
||||||
_uploadFirmwareService.begin();
|
_uploadFirmwareService.begin();
|
||||||
|
|||||||
@@ -27,7 +27,7 @@
|
|||||||
#include <ESPmDNS.h>
|
#include <ESPmDNS.h>
|
||||||
#include <LEDService.h>
|
#include <LEDService.h>
|
||||||
#include <event_socket.h>
|
#include <event_socket.h>
|
||||||
#include <FeaturesService.h>
|
#include <features.h>
|
||||||
#include <MotionService.h>
|
#include <MotionService.h>
|
||||||
#include <ntp_service.h>
|
#include <ntp_service.h>
|
||||||
#include <camera_service.h>
|
#include <camera_service.h>
|
||||||
@@ -76,8 +76,6 @@ class ESP32SvelteKit {
|
|||||||
BatteryService *getBatteryService() { return &_batteryService; }
|
BatteryService *getBatteryService() { return &_batteryService; }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
FeaturesService *getFeatureService() { return &_featureService; }
|
|
||||||
|
|
||||||
#if FT_ENABLED(USE_MOTION)
|
#if FT_ENABLED(USE_MOTION)
|
||||||
MotionService *getMotionService() { return &_motionService; }
|
MotionService *getMotionService() { return &_motionService; }
|
||||||
#endif
|
#endif
|
||||||
@@ -102,7 +100,6 @@ class ESP32SvelteKit {
|
|||||||
private:
|
private:
|
||||||
PsychicHttpServer *_server;
|
PsychicHttpServer *_server;
|
||||||
unsigned int _numberEndpoints;
|
unsigned int _numberEndpoints;
|
||||||
FeaturesService _featureService;
|
|
||||||
WiFiService _wifiService;
|
WiFiService _wifiService;
|
||||||
APService _apService;
|
APService _apService;
|
||||||
EventSocket _socket;
|
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
|
|
||||||
@@ -8,7 +8,7 @@
|
|||||||
#include <MathUtils.h>
|
#include <MathUtils.h>
|
||||||
#include <Timing.h>
|
#include <Timing.h>
|
||||||
#include <filesystem.h>
|
#include <filesystem.h>
|
||||||
#include <Features.h>
|
#include <features.h>
|
||||||
#include <settings/peripherals_settings.h>
|
#include <settings/peripherals_settings.h>
|
||||||
#include <stateful_service_endpoint.h>
|
#include <stateful_service_endpoint.h>
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
#include <task_manager.h>
|
#include <task_manager.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <async_worker.h>
|
#include <async_worker.h>
|
||||||
#include <Features.h>
|
#include <features.h>
|
||||||
|
|
||||||
namespace Camera {
|
namespace Camera {
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
#ifndef Features_h
|
||||||
#define Features_h
|
#define Features_h
|
||||||
|
|
||||||
/**
|
#include <WiFi.h>
|
||||||
* ESP32 SvelteKit
|
#include <ArduinoJson.h>
|
||||||
*
|
#include <PsychicHttp.h>
|
||||||
* 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.
|
|
||||||
**/
|
|
||||||
|
|
||||||
#define FT_ENABLED(feature) feature
|
#define FT_ENABLED(feature) feature
|
||||||
|
|
||||||
@@ -77,4 +67,12 @@
|
|||||||
#define USE_GPS 0
|
#define USE_GPS 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
namespace feature_service {
|
||||||
|
|
||||||
|
void features(JsonObject &root);
|
||||||
|
|
||||||
|
esp_err_t getFeatures(PsychicRequest *request);
|
||||||
|
|
||||||
|
} // namespace feature_service
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user