mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-06 15:59:52 +03:00
optimized
This commit is contained in:
@@ -14,51 +14,19 @@
|
||||
using namespace std::placeholders; // for `_1` etc
|
||||
|
||||
template <class T>
|
||||
class HttpGetEndpoint {
|
||||
public:
|
||||
HttpGetEndpoint(JsonStateReader<T> stateReader,
|
||||
StatefulService<T> * statefulService,
|
||||
PsychicHttpServer * server,
|
||||
const char * servicePath,
|
||||
SecurityManager * securityManager,
|
||||
AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN,
|
||||
size_t bufferSize = DEFAULT_BUFFER_SIZE)
|
||||
: _stateReader(stateReader)
|
||||
, _statefulService(statefulService)
|
||||
, _bufferSize(bufferSize)
|
||||
, _server(server)
|
||||
, _servicePath(servicePath)
|
||||
, _securityManager(securityManager)
|
||||
, _authenticationPredicate(authenticationPredicate) {
|
||||
}
|
||||
|
||||
class HttpEndpoint {
|
||||
protected:
|
||||
JsonStateReader<T> _stateReader;
|
||||
JsonStateUpdater<T> _stateUpdater;
|
||||
StatefulService<T> * _statefulService;
|
||||
size_t _bufferSize;
|
||||
PsychicHttpServer * _server;
|
||||
const char * _servicePath;
|
||||
SecurityManager * _securityManager;
|
||||
AuthenticationPredicate _authenticationPredicate;
|
||||
PsychicHttpServer * _server;
|
||||
const char * _servicePath;
|
||||
|
||||
void registerURI() {
|
||||
_server->on(_servicePath,
|
||||
HTTP_GET,
|
||||
_securityManager->wrapRequest(
|
||||
[this](PsychicRequest * request) {
|
||||
PsychicJsonResponse response = PsychicJsonResponse(request, false, _bufferSize);
|
||||
JsonObject jsonObject = response.getRoot();
|
||||
_statefulService->read(jsonObject, _stateReader);
|
||||
return response.send();
|
||||
},
|
||||
_authenticationPredicate));
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class HttpPostEndpoint {
|
||||
public:
|
||||
HttpPostEndpoint(JsonStateReader<T> stateReader,
|
||||
HttpEndpoint(JsonStateReader<T> stateReader,
|
||||
JsonStateUpdater<T> stateUpdater,
|
||||
StatefulService<T> * statefulService,
|
||||
PsychicHttpServer * server,
|
||||
@@ -76,18 +44,19 @@ class HttpPostEndpoint {
|
||||
, _bufferSize(bufferSize) {
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
JsonStateReader<T> _stateReader;
|
||||
JsonStateUpdater<T> _stateUpdater;
|
||||
StatefulService<T> * _statefulService;
|
||||
size_t _bufferSize;
|
||||
SecurityManager * _securityManager;
|
||||
AuthenticationPredicate _authenticationPredicate;
|
||||
PsychicHttpServer * _server;
|
||||
const char * _servicePath;
|
||||
|
||||
// register the web server on() endpoints
|
||||
void registerURI() {
|
||||
_server->on(_servicePath,
|
||||
HTTP_GET,
|
||||
_securityManager->wrapRequest(
|
||||
[this](PsychicRequest * request) {
|
||||
PsychicJsonResponse response = PsychicJsonResponse(request, false, _bufferSize);
|
||||
JsonObject jsonObject = response.getRoot();
|
||||
_statefulService->read(jsonObject, _stateReader);
|
||||
return response.send();
|
||||
},
|
||||
_authenticationPredicate));
|
||||
|
||||
_server->on(_servicePath,
|
||||
HTTP_POST,
|
||||
_securityManager->wrapCallback(
|
||||
@@ -102,10 +71,8 @@ class HttpPostEndpoint {
|
||||
if (outcome == StateUpdateResult::ERROR) {
|
||||
return request->reply(400);
|
||||
} else if ((outcome == StateUpdateResult::CHANGED) || (outcome == StateUpdateResult::CHANGED_RESTART)) {
|
||||
// TODO see if this works as intended. Before the stat was updated on an onDisconnect
|
||||
// request->onDisconnect([this]() { _statefulService->callUpdateHandlers(HTTP_ENDPOINT_ORIGIN_ID); });
|
||||
// TODO add support for https
|
||||
_statefulService->callUpdateHandlers(HTTP_ENDPOINT_ORIGIN_ID); // persist the changes to the FS
|
||||
// persist the changes to the FS
|
||||
_statefulService->callUpdateHandlers(HTTP_ENDPOINT_ORIGIN_ID);
|
||||
}
|
||||
|
||||
PsychicJsonResponse response = PsychicJsonResponse(request, false, _bufferSize);
|
||||
@@ -114,34 +81,13 @@ class HttpPostEndpoint {
|
||||
_statefulService->read(jsonObject, _stateReader);
|
||||
|
||||
if (outcome == StateUpdateResult::CHANGED_RESTART) {
|
||||
return request->reply(205); // reboot required
|
||||
response.setCode(205); // reboot required
|
||||
}
|
||||
|
||||
return response.send();
|
||||
},
|
||||
_authenticationPredicate));
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class HttpEndpoint : public HttpGetEndpoint<T>, public HttpPostEndpoint<T> {
|
||||
public:
|
||||
HttpEndpoint(JsonStateReader<T> stateReader,
|
||||
JsonStateUpdater<T> stateUpdater,
|
||||
StatefulService<T> * statefulService,
|
||||
PsychicHttpServer * server,
|
||||
const char * servicePath,
|
||||
SecurityManager * securityManager,
|
||||
AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN,
|
||||
size_t bufferSize = DEFAULT_BUFFER_SIZE)
|
||||
: HttpGetEndpoint<T>(stateReader, statefulService, server, servicePath, securityManager, authenticationPredicate, bufferSize)
|
||||
, HttpPostEndpoint<T>(stateReader, stateUpdater, statefulService, server, servicePath, securityManager, authenticationPredicate, bufferSize) {
|
||||
}
|
||||
|
||||
// register the web server on() endpoints
|
||||
void registerURI() {
|
||||
HttpGetEndpoint<T>::registerURI();
|
||||
HttpPostEndpoint<T>::registerURI();
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -102,9 +102,6 @@ class HttpPostEndpoint {
|
||||
if (outcome == StateUpdateResult::ERROR) {
|
||||
return request->reply(400);
|
||||
} else if ((outcome == StateUpdateResult::CHANGED) || (outcome == StateUpdateResult::CHANGED_RESTART)) {
|
||||
// TODO see if this works as intended. Before the stat was updated on an onDisconnect
|
||||
// request->onDisconnect([this]() { _statefulService->callUpdateHandlers(HTTP_ENDPOINT_ORIGIN_ID); });
|
||||
// TODO add support for https
|
||||
_statefulService->callUpdateHandlers(HTTP_ENDPOINT_ORIGIN_ID); // persist the changes to the FS
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user