2020-05-14 22:23:45 +00:00
|
|
|
#ifndef HttpEndpoint_h
|
|
|
|
#define HttpEndpoint_h
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
#include <AsyncJson.h>
|
|
|
|
#include <ESPAsyncWebServer.h>
|
|
|
|
|
|
|
|
#include <SecurityManager.h>
|
|
|
|
#include <StatefulService.h>
|
|
|
|
|
|
|
|
#define HTTP_ENDPOINT_ORIGIN_ID "http"
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class HttpGetEndpoint {
|
|
|
|
public:
|
2020-05-29 19:18:43 +00:00
|
|
|
HttpGetEndpoint(JsonStateReader<T> stateReader,
|
2020-05-14 22:23:45 +00:00
|
|
|
StatefulService<T>* statefulService,
|
|
|
|
AsyncWebServer* server,
|
|
|
|
const String& servicePath,
|
|
|
|
SecurityManager* securityManager,
|
2020-05-22 18:26:12 +00:00
|
|
|
AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN,
|
|
|
|
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
|
2020-05-29 19:18:43 +00:00
|
|
|
_stateReader(stateReader), _statefulService(statefulService), _bufferSize(bufferSize) {
|
2020-05-14 22:23:45 +00:00
|
|
|
server->on(servicePath.c_str(),
|
|
|
|
HTTP_GET,
|
|
|
|
securityManager->wrapRequest(std::bind(&HttpGetEndpoint::fetchSettings, this, std::placeholders::_1),
|
|
|
|
authenticationPredicate));
|
|
|
|
}
|
|
|
|
|
2020-05-29 19:18:43 +00:00
|
|
|
HttpGetEndpoint(JsonStateReader<T> stateReader,
|
2020-05-14 22:23:45 +00:00
|
|
|
StatefulService<T>* statefulService,
|
|
|
|
AsyncWebServer* server,
|
2020-05-22 18:26:12 +00:00
|
|
|
const String& servicePath,
|
|
|
|
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
|
2020-05-29 19:18:43 +00:00
|
|
|
_stateReader(stateReader), _statefulService(statefulService), _bufferSize(bufferSize) {
|
2020-05-14 22:23:45 +00:00
|
|
|
server->on(servicePath.c_str(), HTTP_GET, std::bind(&HttpGetEndpoint::fetchSettings, this, std::placeholders::_1));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2020-05-29 19:18:43 +00:00
|
|
|
JsonStateReader<T> _stateReader;
|
2020-05-14 22:23:45 +00:00
|
|
|
StatefulService<T>* _statefulService;
|
2020-05-22 18:26:12 +00:00
|
|
|
size_t _bufferSize;
|
2020-05-14 22:23:45 +00:00
|
|
|
|
|
|
|
void fetchSettings(AsyncWebServerRequest* request) {
|
2020-05-22 18:26:12 +00:00
|
|
|
AsyncJsonResponse* response = new AsyncJsonResponse(false, _bufferSize);
|
2020-05-14 22:23:45 +00:00
|
|
|
JsonObject jsonObject = response->getRoot().to<JsonObject>();
|
2020-05-29 19:18:43 +00:00
|
|
|
_statefulService->read(jsonObject, _stateReader);
|
2020-05-14 22:23:45 +00:00
|
|
|
|
|
|
|
response->setLength();
|
|
|
|
request->send(response);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class HttpPostEndpoint {
|
|
|
|
public:
|
2020-05-29 19:18:43 +00:00
|
|
|
HttpPostEndpoint(JsonStateReader<T> stateReader,
|
|
|
|
JsonStateUpdater<T> stateUpdater,
|
2020-05-14 22:23:45 +00:00
|
|
|
StatefulService<T>* statefulService,
|
|
|
|
AsyncWebServer* server,
|
|
|
|
const String& servicePath,
|
|
|
|
SecurityManager* securityManager,
|
2020-05-22 18:26:12 +00:00
|
|
|
AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN,
|
|
|
|
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
|
2020-05-29 19:18:43 +00:00
|
|
|
_stateReader(stateReader),
|
|
|
|
_stateUpdater(stateUpdater),
|
2020-05-14 22:23:45 +00:00
|
|
|
_statefulService(statefulService),
|
|
|
|
_updateHandler(
|
|
|
|
servicePath,
|
|
|
|
securityManager->wrapCallback(
|
|
|
|
std::bind(&HttpPostEndpoint::updateSettings, this, std::placeholders::_1, std::placeholders::_2),
|
2020-05-22 18:26:12 +00:00
|
|
|
authenticationPredicate),
|
|
|
|
bufferSize),
|
|
|
|
_bufferSize(bufferSize) {
|
2020-05-14 22:23:45 +00:00
|
|
|
_updateHandler.setMethod(HTTP_POST);
|
|
|
|
server->addHandler(&_updateHandler);
|
|
|
|
}
|
|
|
|
|
2020-05-29 19:18:43 +00:00
|
|
|
HttpPostEndpoint(JsonStateReader<T> stateReader,
|
|
|
|
JsonStateUpdater<T> stateUpdater,
|
2020-05-14 22:23:45 +00:00
|
|
|
StatefulService<T>* statefulService,
|
|
|
|
AsyncWebServer* server,
|
2020-05-22 18:26:12 +00:00
|
|
|
const String& servicePath,
|
|
|
|
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
|
2020-05-29 19:18:43 +00:00
|
|
|
_stateReader(stateReader),
|
|
|
|
_stateUpdater(stateUpdater),
|
2020-05-14 22:23:45 +00:00
|
|
|
_statefulService(statefulService),
|
|
|
|
_updateHandler(servicePath,
|
2020-05-22 18:26:12 +00:00
|
|
|
std::bind(&HttpPostEndpoint::updateSettings, this, std::placeholders::_1, std::placeholders::_2),
|
|
|
|
bufferSize),
|
|
|
|
_bufferSize(bufferSize) {
|
2020-05-14 22:23:45 +00:00
|
|
|
_updateHandler.setMethod(HTTP_POST);
|
|
|
|
server->addHandler(&_updateHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2020-05-29 19:18:43 +00:00
|
|
|
JsonStateReader<T> _stateReader;
|
|
|
|
JsonStateUpdater<T> _stateUpdater;
|
2020-05-14 22:23:45 +00:00
|
|
|
StatefulService<T>* _statefulService;
|
|
|
|
AsyncCallbackJsonWebHandler _updateHandler;
|
2020-05-22 18:26:12 +00:00
|
|
|
size_t _bufferSize;
|
2020-05-14 22:23:45 +00:00
|
|
|
|
|
|
|
void updateSettings(AsyncWebServerRequest* request, JsonVariant& json) {
|
2020-05-29 19:18:43 +00:00
|
|
|
if (!json.is<JsonObject>()) {
|
|
|
|
request->send(400);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
JsonObject jsonObject = json.as<JsonObject>();
|
|
|
|
StateUpdateResult outcome = _statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
|
|
|
|
if (outcome == StateUpdateResult::ERROR) {
|
2020-05-14 22:23:45 +00:00
|
|
|
request->send(400);
|
2020-05-29 19:18:43 +00:00
|
|
|
return;
|
2020-05-14 22:23:45 +00:00
|
|
|
}
|
2020-05-29 19:18:43 +00:00
|
|
|
if (outcome == StateUpdateResult::CHANGED) {
|
|
|
|
request->onDisconnect([this]() { _statefulService->callUpdateHandlers(HTTP_ENDPOINT_ORIGIN_ID); });
|
|
|
|
}
|
|
|
|
AsyncJsonResponse* response = new AsyncJsonResponse(false, _bufferSize);
|
|
|
|
jsonObject = response->getRoot().to<JsonObject>();
|
|
|
|
_statefulService->read(jsonObject, _stateReader);
|
|
|
|
response->setLength();
|
|
|
|
request->send(response);
|
2020-05-14 22:23:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class HttpEndpoint : public HttpGetEndpoint<T>, public HttpPostEndpoint<T> {
|
|
|
|
public:
|
2020-05-29 19:18:43 +00:00
|
|
|
HttpEndpoint(JsonStateReader<T> stateReader,
|
|
|
|
JsonStateUpdater<T> stateUpdater,
|
2020-05-14 22:23:45 +00:00
|
|
|
StatefulService<T>* statefulService,
|
|
|
|
AsyncWebServer* server,
|
|
|
|
const String& servicePath,
|
|
|
|
SecurityManager* securityManager,
|
2020-05-22 18:26:12 +00:00
|
|
|
AuthenticationPredicate authenticationPredicate = AuthenticationPredicates::IS_ADMIN,
|
|
|
|
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
|
2020-05-29 19:18:43 +00:00
|
|
|
HttpGetEndpoint<T>(stateReader,
|
2020-05-14 22:23:45 +00:00
|
|
|
statefulService,
|
|
|
|
server,
|
|
|
|
servicePath,
|
|
|
|
securityManager,
|
2020-05-22 18:26:12 +00:00
|
|
|
authenticationPredicate,
|
|
|
|
bufferSize),
|
2020-05-29 19:18:43 +00:00
|
|
|
HttpPostEndpoint<T>(stateReader,
|
|
|
|
stateUpdater,
|
2020-05-14 22:23:45 +00:00
|
|
|
statefulService,
|
|
|
|
server,
|
|
|
|
servicePath,
|
|
|
|
securityManager,
|
2020-05-22 18:26:12 +00:00
|
|
|
authenticationPredicate,
|
|
|
|
bufferSize) {
|
2020-05-14 22:23:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 19:18:43 +00:00
|
|
|
HttpEndpoint(JsonStateReader<T> stateReader,
|
|
|
|
JsonStateUpdater<T> stateUpdater,
|
2020-05-14 22:23:45 +00:00
|
|
|
StatefulService<T>* statefulService,
|
|
|
|
AsyncWebServer* server,
|
2020-05-22 18:26:12 +00:00
|
|
|
const String& servicePath,
|
|
|
|
size_t bufferSize = DEFAULT_BUFFER_SIZE) :
|
2020-05-29 19:18:43 +00:00
|
|
|
HttpGetEndpoint<T>(stateReader, statefulService, server, servicePath, bufferSize),
|
|
|
|
HttpPostEndpoint<T>(stateReader, stateUpdater, statefulService, server, servicePath, bufferSize) {
|
2020-05-14 22:23:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // end HttpEndpoint
|