Redone protobuf receiving to be more dynamic and scalable
This commit is contained in:
@@ -8,10 +8,11 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <pb_encode.h>
|
||||
#include <pb_decode.h>
|
||||
#include <platform_shared/api.pb.h>
|
||||
|
||||
using HttpGetHandler = std::function<esp_err_t(httpd_req_t*)>;
|
||||
using HttpPostHandler = std::function<esp_err_t(httpd_req_t*, JsonVariant&)>;
|
||||
using HttpRawHandler = std::function<esp_err_t(httpd_req_t*)>;
|
||||
using HttpProtoHandler = std::function<esp_err_t(httpd_req_t*, api_Request*)>;
|
||||
using WsFrameHandler = std::function<esp_err_t(httpd_req_t*, httpd_ws_frame_t*)>;
|
||||
using WsOpenHandler = std::function<void(httpd_req_t*)>;
|
||||
using WsCloseHandler = std::function<void(int)>;
|
||||
@@ -21,7 +22,7 @@ struct HttpRoute {
|
||||
httpd_method_t method;
|
||||
HttpGetHandler getHandler;
|
||||
HttpPostHandler postHandler;
|
||||
HttpRawHandler rawHandler; // For proto handlers that don't need JSON parsing
|
||||
HttpProtoHandler protoHandler; // For proto handlers that don't need JSON parsing
|
||||
bool isWebsocket;
|
||||
};
|
||||
|
||||
@@ -36,7 +37,7 @@ class WebServer {
|
||||
|
||||
void on(const char* uri, httpd_method_t method, HttpGetHandler handler);
|
||||
void on(const char* uri, httpd_method_t method, HttpPostHandler handler);
|
||||
void onRaw(const char* uri, httpd_method_t method, HttpRawHandler handler);
|
||||
void onProto(const char* uri, httpd_method_t method, HttpProtoHandler handler);
|
||||
|
||||
void onWsFrame(WsFrameHandler handler);
|
||||
void onWsOpen(WsOpenHandler handler);
|
||||
|
||||
@@ -58,27 +58,22 @@ class StatefulProtoEndpoint {
|
||||
_responseAssigner(responseAssigner) {}
|
||||
|
||||
/**
|
||||
* Handles POST requests: decodes Request, updates state, returns Response
|
||||
* Handles POST requests: extracts payload from pre-decoded Request, updates state, returns Response
|
||||
*/
|
||||
esp_err_t handleStateUpdate(httpd_req_t* request) {
|
||||
api_Request req = api_Request_init_zero;
|
||||
if (!NativeServer::receiveProto(request, req, api_Request_fields)) {
|
||||
return sendErrorResponse(request, 400, "Failed to decode request");
|
||||
}
|
||||
|
||||
esp_err_t handleStateUpdate(httpd_req_t* httpReq, api_Request* protoReq) {
|
||||
ProtoT protoMsg = {};
|
||||
if (!_requestExtractor(req, protoMsg)) {
|
||||
return sendErrorResponse(request, 400, "Invalid request type");
|
||||
if (!_requestExtractor(*protoReq, protoMsg)) {
|
||||
return sendErrorResponse(httpReq, 400, "Invalid request type");
|
||||
}
|
||||
|
||||
StateUpdateResult outcome = _statefulService->update(
|
||||
[this, &protoMsg](T& settings) { return _stateUpdater(protoMsg, settings); }, PROTO_ENDPOINT_ORIGIN_ID);
|
||||
|
||||
if (outcome == StateUpdateResult::ERROR) {
|
||||
return sendErrorResponse(request, 400, "Invalid state");
|
||||
return sendErrorResponse(httpReq, 400, "Invalid state");
|
||||
}
|
||||
|
||||
return sendStateResponse(request, 200);
|
||||
return sendStateResponse(httpReq, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user