#pragma once #include #include #include #define HTTP_ENDPOINT_ORIGIN_ID "http" #define HTTPS_ENDPOINT_ORIGIN_ID "https" template class StatefulHttpEndpoint { protected: JsonStateReader _stateReader; JsonStateUpdater _stateUpdater; StatefulService *_statefulService; public: StatefulHttpEndpoint(JsonStateReader stateReader, JsonStateUpdater stateUpdater, StatefulService *statefulService) : _stateReader(stateReader), _stateUpdater(stateUpdater), _statefulService(statefulService) {} esp_err_t handleStateUpdate(PsychicRequest *request, JsonVariant &json) { if (!json.is()) { return request->reply(400); } JsonObject jsonObject = json.as(); StateUpdateResult outcome = _statefulService->updateWithoutPropagation(jsonObject, _stateUpdater); if (outcome == StateUpdateResult::ERROR) { return request->reply(400); } else if ((outcome == StateUpdateResult::CHANGED)) { // persist the changes to the FS _statefulService->callUpdateHandlers(HTTP_ENDPOINT_ORIGIN_ID); } PsychicJsonResponse response = PsychicJsonResponse(request, false); jsonObject = response.getRoot(); _statefulService->read(jsonObject, _stateReader); return response.send(); } esp_err_t getState(PsychicRequest *request) { PsychicJsonResponse response = PsychicJsonResponse(request, false); JsonObject jsonObject = response.getRoot(); _statefulService->read(jsonObject, _stateReader); return response.send(); } };