✨ Adds native http wrapper to replace psychic
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
#pragma once
|
||||
|
||||
#include <esp_http_server.h>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
using HttpGetHandler = std::function<esp_err_t(httpd_req_t*)>;
|
||||
using HttpPostHandler = std::function<esp_err_t(httpd_req_t*, JsonVariant&)>;
|
||||
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)>;
|
||||
|
||||
struct HttpRoute {
|
||||
std::string uri;
|
||||
httpd_method_t method;
|
||||
HttpGetHandler getHandler;
|
||||
HttpPostHandler postHandler;
|
||||
bool isWebsocket;
|
||||
};
|
||||
|
||||
class NativeServer {
|
||||
public:
|
||||
NativeServer();
|
||||
~NativeServer();
|
||||
|
||||
void config(size_t maxUriHandlers, size_t stackSize, size_t maxUploadSize);
|
||||
esp_err_t listen(uint16_t port);
|
||||
void stop();
|
||||
|
||||
void on(const char* uri, httpd_method_t method, HttpGetHandler handler);
|
||||
void on(const char* uri, httpd_method_t method, HttpPostHandler handler);
|
||||
|
||||
void onWsFrame(WsFrameHandler handler);
|
||||
void onWsOpen(WsOpenHandler handler);
|
||||
void onWsClose(WsCloseHandler handler);
|
||||
void registerWebsocket(const char* uri);
|
||||
|
||||
esp_err_t wsSend(int sockfd, const uint8_t* data, size_t len);
|
||||
esp_err_t wsSendAll(const uint8_t* data, size_t len);
|
||||
void addWsClient(int sockfd);
|
||||
void removeWsClient(int sockfd);
|
||||
std::vector<int>& getWsClients();
|
||||
|
||||
void addDefaultHeader(const char* key, const char* value);
|
||||
|
||||
httpd_handle_t getHandle() { return server_; }
|
||||
|
||||
static esp_err_t sendJson(httpd_req_t* req, int status, const char* json);
|
||||
static esp_err_t sendJson(httpd_req_t* req, int status, JsonDocument& doc);
|
||||
static esp_err_t sendError(httpd_req_t* req, int status, const char* message);
|
||||
static esp_err_t sendOk(httpd_req_t* req);
|
||||
|
||||
private:
|
||||
httpd_handle_t server_ = nullptr;
|
||||
httpd_config_t config_;
|
||||
std::vector<HttpRoute> routes_;
|
||||
std::map<std::string, std::string> defaultHeaders_;
|
||||
std::vector<int> wsClients_;
|
||||
SemaphoreHandle_t wsMutex_;
|
||||
|
||||
WsFrameHandler wsFrameHandler_;
|
||||
WsOpenHandler wsOpenHandler_;
|
||||
WsCloseHandler wsCloseHandler_;
|
||||
|
||||
static esp_err_t httpHandler(httpd_req_t* req);
|
||||
static esp_err_t wsHandler(httpd_req_t* req);
|
||||
|
||||
void applyDefaultHeaders(httpd_req_t* req);
|
||||
esp_err_t registerRoute(const HttpRoute& route);
|
||||
};
|
||||
|
||||
extern NativeServer nativeServer;
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <communication/native_server.h>
|
||||
#include <communication/comm_base.hpp>
|
||||
|
||||
class NativeWebsocket : public CommAdapterBase {
|
||||
public:
|
||||
NativeWebsocket(NativeServer& server, const char* route = "/api/ws");
|
||||
|
||||
void begin() override;
|
||||
|
||||
private:
|
||||
NativeServer& server_;
|
||||
const char* route_;
|
||||
|
||||
void onWsOpen(httpd_req_t* req);
|
||||
void onWsClose(int sockfd);
|
||||
esp_err_t onFrame(httpd_req_t* req, httpd_ws_frame_t* frame);
|
||||
|
||||
void send(const uint8_t* data, size_t len, int cid = -1) override;
|
||||
};
|
||||
Reference in New Issue
Block a user