Updates stateful service

This commit is contained in:
Rune Harlyk
2024-11-08 16:56:06 +01:00
parent 9be13d1df5
commit 4fff03ce54
7 changed files with 43 additions and 46 deletions
@@ -5,10 +5,11 @@
namespace Camera { namespace Camera {
#include <CameraService.h> #include <camera_service.h>
#include <EventEndpoint.h> #include <EventEndpoint.h>
#include <FSPersistence.h> #include <FSPersistence.h>
#include <HttpEndpoint.h> #include <HttpEndpoint.h>
#include <stateful_service_endpoint.h>
#include <JsonUtils.h> #include <JsonUtils.h>
#include <PsychicHttp.h> #include <PsychicHttp.h>
#include <SettingValue.h> #include <SettingValue.h>
@@ -17,20 +18,17 @@ namespace Camera {
#include <filesystem.h> #include <filesystem.h>
#define EVENT_CAMERA_SETTINGS "CameraSettings" #define EVENT_CAMERA_SETTINGS "CameraSettings"
#define CAMERA_SETTINGS_PATH "/api/camera/settings"
class CameraSettingsService : public StatefulService<CameraSettings> { class CameraSettingsService : public StatefulService<CameraSettings> {
public: public:
CameraSettingsService(PsychicHttpServer *server, FS *fs) CameraSettingsService()
: _server(server), : endpoint(CameraSettings::read, CameraSettings::update, this),
_httpEndpoint(CameraSettings::read, CameraSettings::update, this, server, CAMERA_SETTINGS_PATH),
_eventEndpoint(CameraSettings::read, CameraSettings::update, this, EVENT_CAMERA_SETTINGS), _eventEndpoint(CameraSettings::read, CameraSettings::update, this, EVENT_CAMERA_SETTINGS),
_fsPersistence(CameraSettings::read, CameraSettings::update, this, fs, CAMERA_SETTINGS_FILE) { _fsPersistence(CameraSettings::read, CameraSettings::update, this, &ESPFS, CAMERA_SETTINGS_FILE) {
addUpdateHandler([&](const String &originId) { updateCamera(); }, false); addUpdateHandler([&](const String &originId) { updateCamera(); }, false);
} }
void begin() { void begin() {
_httpEndpoint.begin();
_eventEndpoint.begin(); _eventEndpoint.begin();
_fsPersistence.readFromFS(); _fsPersistence.readFromFS();
sensor_t *s = safe_sensor_get(); sensor_t *s = safe_sensor_get();
@@ -96,9 +94,9 @@ class CameraSettingsService : public StatefulService<CameraSettings> {
safe_sensor_return(); safe_sensor_return();
} }
StatefulHttpEndpoint<CameraSettings> endpoint;
private: private:
PsychicHttpServer *_server;
HttpEndpoint<CameraSettings> _httpEndpoint;
EventEndpoint<CameraSettings> _eventEndpoint; EventEndpoint<CameraSettings> _eventEndpoint;
FSPersistence<CameraSettings> _fsPersistence; FSPersistence<CameraSettings> _fsPersistence;
}; };
+20 -7
View File
@@ -18,7 +18,6 @@
ESP32SvelteKit::ESP32SvelteKit(PsychicHttpServer *server, unsigned int numberEndpoints) ESP32SvelteKit::ESP32SvelteKit(PsychicHttpServer *server, unsigned int numberEndpoints)
: _server(server), : _server(server),
_numberEndpoints(numberEndpoints), _numberEndpoints(numberEndpoints),
_featureService(server),
#if FT_ENABLED(USE_UPLOAD_FIRMWARE) #if FT_ENABLED(USE_UPLOAD_FIRMWARE)
_uploadFirmwareService(server), _uploadFirmwareService(server),
#endif #endif
@@ -30,16 +29,12 @@ ESP32SvelteKit::ESP32SvelteKit(PsychicHttpServer *server, unsigned int numberEnd
#endif #endif
#if FT_ENABLED(USE_BATTERY) #if FT_ENABLED(USE_BATTERY)
_batteryService(&_peripherals), _batteryService(&_peripherals),
#endif
#if FT_ENABLED(USE_CAMERA)
_cameraService(server),
_cameraSettingsService(server, &ESPFS),
#endif #endif
_servoController(server, &ESPFS, &_peripherals), _servoController(server, &ESPFS, &_peripherals),
#if FT_ENABLED(USE_MOTION) #if FT_ENABLED(USE_MOTION)
_motionService(_server, &_servoController), _motionService(_server, &_servoController),
#endif #endif
_peripherals(server, &ESPFS) { _featureService(server) {
} }
void ESP32SvelteKit::begin() { void ESP32SvelteKit::begin() {
@@ -96,6 +91,17 @@ void ESP32SvelteKit::setupServer() {
}); });
#endif #endif
// Camera
_server->on("/api/camera/still", HTTP_GET,
[this](PsychicRequest *request) { return _cameraService.cameraStill(request); });
_server->on("/api/camera/stream", HTTP_GET,
[this](PsychicRequest *request) { return _cameraService.cameraStream(request); });
_server->on("/api/camera/settings", HTTP_GET,
[this](PsychicRequest *request) { return _cameraSettingsService.endpoint.getState(request); });
_server->on("/api/camera/settings", HTTP_POST, [this](PsychicRequest *request, JsonVariant &json) {
return _cameraSettingsService.endpoint.handleStateUpdate(request, json);
});
// SYSTEM // SYSTEM
_server->on("/api/system/reset", HTTP_POST, system_service::handleReset); _server->on("/api/system/reset", HTTP_POST, system_service::handleReset);
_server->on("/api/system/restart", HTTP_POST, system_service::handleRestart); _server->on("/api/system/restart", HTTP_POST, system_service::handleRestart);
@@ -109,13 +115,20 @@ void ESP32SvelteKit::setupServer() {
_server->on("/api/files/upload/*", HTTP_POST, FileSystem::uploadHandler); _server->on("/api/files/upload/*", HTTP_POST, FileSystem::uploadHandler);
_server->on("/api/files/edit", HTTP_POST, FileSystem::handleEdit); _server->on("/api/files/edit", HTTP_POST, FileSystem::handleEdit);
// servo // SERVO
_server->on("/api/servo/config", HTTP_GET, _server->on("/api/servo/config", HTTP_GET,
[this](PsychicRequest *request) { return _servoController.endpoint.getState(request); }); [this](PsychicRequest *request) { return _servoController.endpoint.getState(request); });
_server->on("/api/servo/config", HTTP_POST, [this](PsychicRequest *request, JsonVariant &json) { _server->on("/api/servo/config", HTTP_POST, [this](PsychicRequest *request, JsonVariant &json) {
return _servoController.endpoint.handleStateUpdate(request, json); return _servoController.endpoint.handleStateUpdate(request, json);
}); });
// PERIPHERALS
_server->on("/api/peripheral/settings", HTTP_GET,
[this](PsychicRequest *request) { return _peripherals.endpoint.getState(request); });
_server->on("/api/peripheral/settings", HTTP_POST, [this](PsychicRequest *request, JsonVariant &json) {
return _peripherals.endpoint.handleStateUpdate(request, json);
});
// MISC // MISC
_server->on("/api/ws/events", socket.getHandler()); _server->on("/api/ws/events", socket.getHandler());
+1 -1
View File
@@ -30,7 +30,7 @@
#include <FeaturesService.h> #include <FeaturesService.h>
#include <MotionService.h> #include <MotionService.h>
#include <ntp_service.h> #include <ntp_service.h>
#include <CameraService.h> #include <camera_service.h>
#include <CameraSettingsService.h> #include <CameraSettingsService.h>
#include <PsychicHttp.h> #include <PsychicHttp.h>
#include <task_manager.h> #include <task_manager.h>
+6 -9
View File
@@ -10,6 +10,7 @@
#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 <list> #include <list>
#include <SPI.h> #include <SPI.h>
@@ -24,7 +25,6 @@
#include <NewPing.h> #include <NewPing.h>
#define EVENT_CONFIGURATION_SETTINGS "peripheralSettings" #define EVENT_CONFIGURATION_SETTINGS "peripheralSettings"
#define CONFIGURATION_SETTINGS_PATH "/api/peripheral/settings"
#define EVENT_I2C_SCAN "i2cScan" #define EVENT_I2C_SCAN "i2cScan"
@@ -58,10 +58,8 @@
class Peripherals : public StatefulService<PeripheralsConfiguration> { class Peripherals : public StatefulService<PeripheralsConfiguration> {
public: public:
Peripherals(PsychicHttpServer *server, FS *fs) Peripherals()
: _server(server), : endpoint(PeripheralsConfiguration::read, PeripheralsConfiguration::update, this),
_httpEndpoint(PeripheralsConfiguration::read, PeripheralsConfiguration::update, this, server,
CONFIGURATION_SETTINGS_PATH),
_eventEndpoint(PeripheralsConfiguration::read, PeripheralsConfiguration::update, this, _eventEndpoint(PeripheralsConfiguration::read, PeripheralsConfiguration::update, this,
EVENT_CONFIGURATION_SETTINGS), EVENT_CONFIGURATION_SETTINGS),
#if FT_ENABLED(USE_MAG) #if FT_ENABLED(USE_MAG)
@@ -70,14 +68,13 @@ class Peripherals : public StatefulService<PeripheralsConfiguration> {
#if FT_ENABLED(USE_BMP) #if FT_ENABLED(USE_BMP)
_bmp(10085), _bmp(10085),
#endif #endif
_fsPersistence(PeripheralsConfiguration::read, PeripheralsConfiguration::update, this, fs, _fsPersistence(PeripheralsConfiguration::read, PeripheralsConfiguration::update, this, &ESPFS,
DEVICE_CONFIG_FILE) { DEVICE_CONFIG_FILE) {
_accessMutex = xSemaphoreCreateMutex(); _accessMutex = xSemaphoreCreateMutex();
addUpdateHandler([&](const String &originId) { updatePins(); }, false); addUpdateHandler([&](const String &originId) { updatePins(); }, false);
}; };
void begin() { void begin() {
_httpEndpoint.begin();
_eventEndpoint.begin(); _eventEndpoint.begin();
_fsPersistence.readFromFS(); _fsPersistence.readFromFS();
@@ -346,6 +343,8 @@ class Peripherals : public StatefulService<PeripheralsConfiguration> {
return temperature; return temperature;
} }
StatefulHttpEndpoint<PeripheralsConfiguration> endpoint;
protected: protected:
void updateImu() { void updateImu() {
doc.clear(); doc.clear();
@@ -399,8 +398,6 @@ class Peripherals : public StatefulService<PeripheralsConfiguration> {
float rightDistance() { return _right_distance; } float rightDistance() { return _right_distance; }
private: private:
PsychicHttpServer *_server;
HttpEndpoint<PeripheralsConfiguration> _httpEndpoint;
EventEndpoint<PeripheralsConfiguration> _eventEndpoint; EventEndpoint<PeripheralsConfiguration> _eventEndpoint;
FSPersistence<PeripheralsConfiguration> _fsPersistence; FSPersistence<PeripheralsConfiguration> _fsPersistence;
@@ -1,4 +1,4 @@
#include <CameraService.h> #include <camera_service.h>
namespace Camera { namespace Camera {
@@ -29,17 +29,9 @@ sensor_t *safe_sensor_get() {
void safe_sensor_return() { xSemaphoreGive(cameraMutex); } void safe_sensor_return() { xSemaphoreGive(cameraMutex); }
CameraService::CameraService(PsychicHttpServer *server) : _server(server) {} CameraService::CameraService() {}
void CameraService::begin() {
InitializeCamera();
_server->on(STILL_SERVICE_PATH, HTTP_GET, [this](PsychicRequest *request) { return cameraStill(request); });
_server->on(STREAM_SERVICE_PATH, HTTP_GET, [this](PsychicRequest *request) { return cameraStream(request); });
ESP_LOGV(TAG, "Registered GET endpoint: %s", STILL_SERVICE_PATH); esp_err_t CameraService::begin() {
ESP_LOGV(TAG, "Registered GET endpoint: %s", STREAM_SERVICE_PATH);
}
esp_err_t CameraService::InitializeCamera() {
camera_config_t camera_config; camera_config_t camera_config;
camera_config.ledc_channel = LEDC_CHANNEL_0; camera_config.ledc_channel = LEDC_CHANNEL_0;
camera_config.ledc_timer = LEDC_TIMER_0; camera_config.ledc_timer = LEDC_TIMER_0;
@@ -13,12 +13,9 @@ namespace Camera {
#include <esp_camera.h> #include <esp_camera.h>
#if USE_CAMERA #if USE_CAMERA
#include <CameraPins.h> #include <camera_pins.h>
#endif #endif
#define STREAM_SERVICE_PATH "/api/camera/stream"
#define STILL_SERVICE_PATH "/api/camera/still"
#define PART_BOUNDARY "frame" #define PART_BOUNDARY "frame"
camera_fb_t *safe_camera_fb_get(); camera_fb_t *safe_camera_fb_get();
@@ -27,15 +24,15 @@ void safe_sensor_return();
class CameraService { class CameraService {
public: public:
CameraService(PsychicHttpServer *server); CameraService();
void begin(); esp_err_t begin();
esp_err_t cameraStill(PsychicRequest *request);
esp_err_t cameraStream(PsychicRequest *request);
private: private:
PsychicHttpServer *_server; PsychicHttpServer *_server;
esp_err_t cameraStill(PsychicRequest *request);
esp_err_t cameraStream(PsychicRequest *request);
esp_err_t InitializeCamera();
}; };
} // namespace Camera } // namespace Camera