🎨 Renames webserver and websocket
This commit is contained in:
+4
-4
@@ -21,10 +21,10 @@ struct HttpRoute {
|
|||||||
bool isWebsocket;
|
bool isWebsocket;
|
||||||
};
|
};
|
||||||
|
|
||||||
class NativeServer {
|
class WebServer {
|
||||||
public:
|
public:
|
||||||
NativeServer();
|
WebServer();
|
||||||
~NativeServer();
|
~WebServer();
|
||||||
|
|
||||||
void config(size_t maxUriHandlers, size_t stackSize);
|
void config(size_t maxUriHandlers, size_t stackSize);
|
||||||
esp_err_t listen(uint16_t port);
|
esp_err_t listen(uint16_t port);
|
||||||
@@ -72,4 +72,4 @@ class NativeServer {
|
|||||||
esp_err_t registerRoute(const HttpRoute& route);
|
esp_err_t registerRoute(const HttpRoute& route);
|
||||||
};
|
};
|
||||||
|
|
||||||
extern NativeServer nativeServer;
|
extern WebServer server;
|
||||||
+4
-4
@@ -1,17 +1,17 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <communication/native_server.h>
|
#include <communication/webserver.h>
|
||||||
#include <communication/comm_base.hpp>
|
#include <communication/comm_base.hpp>
|
||||||
|
|
||||||
class NativeWebsocket : public CommAdapterBase {
|
class Websocket : public CommAdapterBase {
|
||||||
public:
|
public:
|
||||||
NativeWebsocket(NativeServer& server, const char* route = "/api/ws");
|
Websocket(WebServer& server, const char* route = "/api/ws");
|
||||||
|
|
||||||
void begin() override;
|
void begin() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
NativeServer& server_;
|
WebServer& server_;
|
||||||
const char* route_;
|
const char* route_;
|
||||||
|
|
||||||
void onWsOpen(httpd_req_t* req);
|
void onWsOpen(httpd_req_t* req);
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
#include <esp_http_server.h>
|
#include <esp_http_server.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <template/stateful_service.h>
|
#include <template/stateful_service.h>
|
||||||
#include <communication/native_server.h>
|
#include <communication/webserver.h>
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
@@ -27,7 +27,7 @@ class StatefulHttpEndpoint {
|
|||||||
StateUpdateResult outcome = _statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
|
StateUpdateResult outcome = _statefulService->updateWithoutPropagation(jsonObject, _stateUpdater);
|
||||||
|
|
||||||
if (outcome == StateUpdateResult::ERROR) {
|
if (outcome == StateUpdateResult::ERROR) {
|
||||||
return NativeServer::sendError(request, 400, "Invalid state");
|
return WebServer::sendError(request, 400, "Invalid state");
|
||||||
} else if ((outcome == StateUpdateResult::CHANGED)) {
|
} else if ((outcome == StateUpdateResult::CHANGED)) {
|
||||||
_statefulService->callUpdateHandlers(HTTP_ENDPOINT_ORIGIN_ID);
|
_statefulService->callUpdateHandlers(HTTP_ENDPOINT_ORIGIN_ID);
|
||||||
}
|
}
|
||||||
@@ -35,13 +35,13 @@ class StatefulHttpEndpoint {
|
|||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
JsonVariant root = doc.to<JsonVariant>();
|
JsonVariant root = doc.to<JsonVariant>();
|
||||||
_statefulService->read(root, _stateReader);
|
_statefulService->read(root, _stateReader);
|
||||||
return NativeServer::sendJson(request, 200, doc);
|
return WebServer::sendJson(request, 200, doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t getState(httpd_req_t *request) {
|
esp_err_t getState(httpd_req_t *request) {
|
||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
JsonVariant root = doc.to<JsonVariant>();
|
JsonVariant root = doc.to<JsonVariant>();
|
||||||
_statefulService->read(root, _stateReader);
|
_statefulService->read(root, _stateReader);
|
||||||
return NativeServer::sendJson(request, 200, doc);
|
return WebServer::sendJson(request, 200, doc);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <communication/native_server.h>
|
#include <communication/webserver.h>
|
||||||
#include "WWWData.h"
|
#include "WWWData.h"
|
||||||
|
|
||||||
void mountStaticAssets(NativeServer& s);
|
void mountStaticAssets(WebServer& s);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#include <ap_service.h>
|
#include <ap_service.h>
|
||||||
#include <communication/native_server.h>
|
#include <communication/webserver.h>
|
||||||
|
|
||||||
static const char *TAG = "APService";
|
static const char *TAG = "APService";
|
||||||
|
|
||||||
@@ -17,7 +17,7 @@ esp_err_t APService::getStatus(httpd_req_t *request) {
|
|||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
JsonObject root = doc.to<JsonObject>();
|
JsonObject root = doc.to<JsonObject>();
|
||||||
status(root);
|
status(root);
|
||||||
return NativeServer::sendJson(request, 200, doc);
|
return WebServer::sendJson(request, 200, doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
void APService::status(JsonObject &root) {
|
void APService::status(JsonObject &root) {
|
||||||
|
|||||||
@@ -1,30 +1,30 @@
|
|||||||
#include <communication/native_server.h>
|
#include <communication/webserver.h>
|
||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
static const char* TAG = "NativeServer";
|
static const char* TAG = "WebServer";
|
||||||
|
|
||||||
NativeServer nativeServer;
|
WebServer server;
|
||||||
|
|
||||||
NativeServer::NativeServer() {
|
WebServer::WebServer() {
|
||||||
config_ = HTTPD_DEFAULT_CONFIG();
|
config_ = HTTPD_DEFAULT_CONFIG();
|
||||||
wsMutex_ = xSemaphoreCreateMutex();
|
wsMutex_ = xSemaphoreCreateMutex();
|
||||||
}
|
}
|
||||||
|
|
||||||
NativeServer::~NativeServer() {
|
WebServer::~WebServer() {
|
||||||
stop();
|
stop();
|
||||||
vSemaphoreDelete(wsMutex_);
|
vSemaphoreDelete(wsMutex_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NativeServer::config(size_t maxUriHandlers, size_t stackSize) {
|
void WebServer::config(size_t maxUriHandlers, size_t stackSize) {
|
||||||
config_.max_uri_handlers = maxUriHandlers;
|
config_.max_uri_handlers = maxUriHandlers;
|
||||||
config_.stack_size = stackSize;
|
config_.stack_size = stackSize;
|
||||||
config_.max_resp_headers = 16;
|
config_.max_resp_headers = 16;
|
||||||
config_.lru_purge_enable = true;
|
config_.lru_purge_enable = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t NativeServer::listen(uint16_t port) {
|
esp_err_t WebServer::listen(uint16_t port) {
|
||||||
config_.server_port = port;
|
config_.server_port = port;
|
||||||
config_.ctrl_port = port + 32768;
|
config_.ctrl_port = port + 32768;
|
||||||
|
|
||||||
@@ -38,23 +38,23 @@ esp_err_t NativeServer::listen(uint16_t port) {
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NativeServer::stop() {
|
void WebServer::stop() {
|
||||||
if (server_) {
|
if (server_) {
|
||||||
httpd_stop(server_);
|
httpd_stop(server_);
|
||||||
server_ = nullptr;
|
server_ = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void NativeServer::applyDefaultHeaders(httpd_req_t* req) {
|
void WebServer::applyDefaultHeaders(httpd_req_t* req) {
|
||||||
for (const auto& [key, value] : defaultHeaders_) {
|
for (const auto& [key, value] : defaultHeaders_) {
|
||||||
httpd_resp_set_hdr(req, key.c_str(), value.c_str());
|
httpd_resp_set_hdr(req, key.c_str(), value.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void NativeServer::addDefaultHeader(const char* key, const char* value) { defaultHeaders_[key] = value; }
|
void WebServer::addDefaultHeader(const char* key, const char* value) { defaultHeaders_[key] = value; }
|
||||||
|
|
||||||
esp_err_t NativeServer::httpHandler(httpd_req_t* req) {
|
esp_err_t WebServer::httpHandler(httpd_req_t* req) {
|
||||||
NativeServer* self = static_cast<NativeServer*>(req->user_ctx);
|
WebServer* self = static_cast<WebServer*>(req->user_ctx);
|
||||||
self->applyDefaultHeaders(req);
|
self->applyDefaultHeaders(req);
|
||||||
|
|
||||||
for (const auto& route : self->routes_) {
|
for (const auto& route : self->routes_) {
|
||||||
@@ -120,8 +120,8 @@ esp_err_t NativeServer::httpHandler(httpd_req_t* req) {
|
|||||||
return ESP_FAIL;
|
return ESP_FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t NativeServer::wsHandler(httpd_req_t* req) {
|
esp_err_t WebServer::wsHandler(httpd_req_t* req) {
|
||||||
NativeServer* self = static_cast<NativeServer*>(req->user_ctx);
|
WebServer* self = static_cast<WebServer*>(req->user_ctx);
|
||||||
|
|
||||||
if (req->method == HTTP_GET) {
|
if (req->method == HTTP_GET) {
|
||||||
int sockfd = httpd_req_to_sockfd(req);
|
int sockfd = httpd_req_to_sockfd(req);
|
||||||
@@ -181,7 +181,7 @@ esp_err_t NativeServer::wsHandler(httpd_req_t* req) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NativeServer::on(const char* uri, httpd_method_t method, HttpGetHandler handler) {
|
void WebServer::on(const char* uri, httpd_method_t method, HttpGetHandler handler) {
|
||||||
HttpRoute route;
|
HttpRoute route;
|
||||||
route.uri = uri;
|
route.uri = uri;
|
||||||
route.method = method;
|
route.method = method;
|
||||||
@@ -195,7 +195,7 @@ void NativeServer::on(const char* uri, httpd_method_t method, HttpGetHandler han
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void NativeServer::on(const char* uri, httpd_method_t method, HttpPostHandler handler) {
|
void WebServer::on(const char* uri, httpd_method_t method, HttpPostHandler handler) {
|
||||||
HttpRoute route;
|
HttpRoute route;
|
||||||
route.uri = uri;
|
route.uri = uri;
|
||||||
route.method = method;
|
route.method = method;
|
||||||
@@ -209,7 +209,7 @@ void NativeServer::on(const char* uri, httpd_method_t method, HttpPostHandler ha
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t NativeServer::registerRoute(const HttpRoute& route) {
|
esp_err_t WebServer::registerRoute(const HttpRoute& route) {
|
||||||
httpd_uri_t httpd_route = {.uri = route.uri.c_str(),
|
httpd_uri_t httpd_route = {.uri = route.uri.c_str(),
|
||||||
.method = route.method,
|
.method = route.method,
|
||||||
.handler = route.isWebsocket ? wsHandler : httpHandler,
|
.handler = route.isWebsocket ? wsHandler : httpHandler,
|
||||||
@@ -220,7 +220,7 @@ esp_err_t NativeServer::registerRoute(const HttpRoute& route) {
|
|||||||
return httpd_register_uri_handler(server_, &httpd_route);
|
return httpd_register_uri_handler(server_, &httpd_route);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NativeServer::registerWebsocket(const char* uri) {
|
void WebServer::registerWebsocket(const char* uri) {
|
||||||
HttpRoute route;
|
HttpRoute route;
|
||||||
route.uri = uri;
|
route.uri = uri;
|
||||||
route.method = HTTP_GET;
|
route.method = HTTP_GET;
|
||||||
@@ -234,32 +234,32 @@ void NativeServer::registerWebsocket(const char* uri) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void NativeServer::onWsFrame(WsFrameHandler handler) { wsFrameHandler_ = handler; }
|
void WebServer::onWsFrame(WsFrameHandler handler) { wsFrameHandler_ = handler; }
|
||||||
|
|
||||||
void NativeServer::onWsOpen(WsOpenHandler handler) { wsOpenHandler_ = handler; }
|
void WebServer::onWsOpen(WsOpenHandler handler) { wsOpenHandler_ = handler; }
|
||||||
|
|
||||||
void NativeServer::onWsClose(WsCloseHandler handler) { wsCloseHandler_ = handler; }
|
void WebServer::onWsClose(WsCloseHandler handler) { wsCloseHandler_ = handler; }
|
||||||
|
|
||||||
void NativeServer::addWsClient(int sockfd) {
|
void WebServer::addWsClient(int sockfd) {
|
||||||
xSemaphoreTake(wsMutex_, portMAX_DELAY);
|
xSemaphoreTake(wsMutex_, portMAX_DELAY);
|
||||||
wsClients_.push_back(sockfd);
|
wsClients_.push_back(sockfd);
|
||||||
xSemaphoreGive(wsMutex_);
|
xSemaphoreGive(wsMutex_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NativeServer::removeWsClient(int sockfd) {
|
void WebServer::removeWsClient(int sockfd) {
|
||||||
xSemaphoreTake(wsMutex_, portMAX_DELAY);
|
xSemaphoreTake(wsMutex_, portMAX_DELAY);
|
||||||
wsClients_.erase(std::remove(wsClients_.begin(), wsClients_.end(), sockfd), wsClients_.end());
|
wsClients_.erase(std::remove(wsClients_.begin(), wsClients_.end(), sockfd), wsClients_.end());
|
||||||
xSemaphoreGive(wsMutex_);
|
xSemaphoreGive(wsMutex_);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<int> NativeServer::getWsClients() {
|
std::vector<int> WebServer::getWsClients() {
|
||||||
xSemaphoreTake(wsMutex_, portMAX_DELAY);
|
xSemaphoreTake(wsMutex_, portMAX_DELAY);
|
||||||
std::vector<int> clients = wsClients_;
|
std::vector<int> clients = wsClients_;
|
||||||
xSemaphoreGive(wsMutex_);
|
xSemaphoreGive(wsMutex_);
|
||||||
return clients;
|
return clients;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t NativeServer::wsSend(int sockfd, const uint8_t* data, size_t len) {
|
esp_err_t WebServer::wsSend(int sockfd, const uint8_t* data, size_t len) {
|
||||||
httpd_ws_frame_t frame = {.final = true,
|
httpd_ws_frame_t frame = {.final = true,
|
||||||
.fragmented = false,
|
.fragmented = false,
|
||||||
.type = HTTPD_WS_TYPE_BINARY,
|
.type = HTTPD_WS_TYPE_BINARY,
|
||||||
@@ -268,7 +268,7 @@ esp_err_t NativeServer::wsSend(int sockfd, const uint8_t* data, size_t len) {
|
|||||||
return httpd_ws_send_frame_async(server_, sockfd, &frame);
|
return httpd_ws_send_frame_async(server_, sockfd, &frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t NativeServer::wsSendAll(const uint8_t* data, size_t len) {
|
esp_err_t WebServer::wsSendAll(const uint8_t* data, size_t len) {
|
||||||
xSemaphoreTake(wsMutex_, portMAX_DELAY);
|
xSemaphoreTake(wsMutex_, portMAX_DELAY);
|
||||||
for (int sockfd : wsClients_) {
|
for (int sockfd : wsClients_) {
|
||||||
wsSend(sockfd, data, len);
|
wsSend(sockfd, data, len);
|
||||||
@@ -277,7 +277,7 @@ esp_err_t NativeServer::wsSendAll(const uint8_t* data, size_t len) {
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t NativeServer::sendJson(httpd_req_t* req, int status, const char* json) {
|
esp_err_t WebServer::sendJson(httpd_req_t* req, int status, const char* json) {
|
||||||
httpd_resp_set_status(req, status == 200 ? "200 OK"
|
httpd_resp_set_status(req, status == 200 ? "200 OK"
|
||||||
: status == 400 ? "400 Bad Request"
|
: status == 400 ? "400 Bad Request"
|
||||||
: status == 404 ? "404 Not Found"
|
: status == 404 ? "404 Not Found"
|
||||||
@@ -287,16 +287,16 @@ esp_err_t NativeServer::sendJson(httpd_req_t* req, int status, const char* json)
|
|||||||
return httpd_resp_send(req, json, strlen(json));
|
return httpd_resp_send(req, json, strlen(json));
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t NativeServer::sendJson(httpd_req_t* req, int status, JsonDocument& doc) {
|
esp_err_t WebServer::sendJson(httpd_req_t* req, int status, JsonDocument& doc) {
|
||||||
std::string json;
|
std::string json;
|
||||||
serializeJson(doc, json);
|
serializeJson(doc, json);
|
||||||
return sendJson(req, status, json.c_str());
|
return sendJson(req, status, json.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t NativeServer::sendError(httpd_req_t* req, int status, const char* message) {
|
esp_err_t WebServer::sendError(httpd_req_t* req, int status, const char* message) {
|
||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
doc["error"] = message;
|
doc["error"] = message;
|
||||||
return sendJson(req, status, doc);
|
return sendJson(req, status, doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t NativeServer::sendOk(httpd_req_t* req) { return sendJson(req, 200, "{\"status\":\"ok\"}"); }
|
esp_err_t WebServer::sendOk(httpd_req_t* req) { return sendJson(req, 200, "{\"status\":\"ok\"}"); }
|
||||||
+8
-8
@@ -1,29 +1,29 @@
|
|||||||
#include <communication/native_websocket.h>
|
#include <communication/websocket.h>
|
||||||
#include <esp_log.h>
|
#include <esp_log.h>
|
||||||
|
|
||||||
static const char* TAG = "NativeWebsocket";
|
static const char* TAG = "Websocket";
|
||||||
|
|
||||||
NativeWebsocket::NativeWebsocket(NativeServer& server, const char* route) : server_(server), route_(route) {}
|
Websocket::Websocket(WebServer& server, const char* route) : server_(server), route_(route) {}
|
||||||
|
|
||||||
void NativeWebsocket::begin() {
|
void Websocket::begin() {
|
||||||
server_.onWsOpen([this](httpd_req_t* req) { onWsOpen(req); });
|
server_.onWsOpen([this](httpd_req_t* req) { onWsOpen(req); });
|
||||||
server_.onWsClose([this](int sockfd) { onWsClose(sockfd); });
|
server_.onWsClose([this](int sockfd) { onWsClose(sockfd); });
|
||||||
server_.onWsFrame([this](httpd_req_t* req, httpd_ws_frame_t* frame) { return onFrame(req, frame); });
|
server_.onWsFrame([this](httpd_req_t* req, httpd_ws_frame_t* frame) { return onFrame(req, frame); });
|
||||||
server_.registerWebsocket(route_);
|
server_.registerWebsocket(route_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NativeWebsocket::onWsOpen(httpd_req_t* req) {
|
void Websocket::onWsOpen(httpd_req_t* req) {
|
||||||
int sockfd = httpd_req_to_sockfd(req);
|
int sockfd = httpd_req_to_sockfd(req);
|
||||||
ESP_LOGI(TAG, "Client connected: %d", sockfd);
|
ESP_LOGI(TAG, "Client connected: %d", sockfd);
|
||||||
sendPong(sockfd);
|
sendPong(sockfd);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NativeWebsocket::onWsClose(int sockfd) {
|
void Websocket::onWsClose(int sockfd) {
|
||||||
ESP_LOGI(TAG, "Client disconnected: %d", sockfd);
|
ESP_LOGI(TAG, "Client disconnected: %d", sockfd);
|
||||||
removeClient(sockfd);
|
removeClient(sockfd);
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t NativeWebsocket::onFrame(httpd_req_t* req, httpd_ws_frame_t* frame) {
|
esp_err_t Websocket::onFrame(httpd_req_t* req, httpd_ws_frame_t* frame) {
|
||||||
if (frame->type != HTTPD_WS_TYPE_BINARY) {
|
if (frame->type != HTTPD_WS_TYPE_BINARY) {
|
||||||
ESP_LOGW(TAG, "Expected binary frame, got type %d", frame->type);
|
ESP_LOGW(TAG, "Expected binary frame, got type %d", frame->type);
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
@@ -34,7 +34,7 @@ esp_err_t NativeWebsocket::onFrame(httpd_req_t* req, httpd_ws_frame_t* frame) {
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NativeWebsocket::send(const uint8_t* data, size_t len, int cid) {
|
void Websocket::send(const uint8_t* data, size_t len, int cid) {
|
||||||
if (cid >= 0) {
|
if (cid >= 0) {
|
||||||
esp_err_t err = server_.wsSend(cid, data, len);
|
esp_err_t err = server_.wsSend(cid, data, len);
|
||||||
if (err != ESP_OK) {
|
if (err != ESP_OK) {
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
#include <features.h>
|
#include <features.h>
|
||||||
#include <communication/native_server.h>
|
#include <communication/webserver.h>
|
||||||
|
|
||||||
namespace feature_service {
|
namespace feature_service {
|
||||||
|
|
||||||
@@ -61,7 +61,7 @@ esp_err_t getFeatures(httpd_req_t* request) {
|
|||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
JsonObject root = doc.to<JsonObject>();
|
JsonObject root = doc.to<JsonObject>();
|
||||||
features(root);
|
features(root);
|
||||||
return NativeServer::sendJson(request, 200, doc);
|
return WebServer::sendJson(request, 200, doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace feature_service
|
} // namespace feature_service
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#include <filesystem.h>
|
#include <filesystem.h>
|
||||||
#include <communication/native_server.h>
|
#include <communication/webserver.h>
|
||||||
|
|
||||||
static const char *TAG = "FileService";
|
static const char *TAG = "FileService";
|
||||||
|
|
||||||
@@ -15,11 +15,11 @@ esp_err_t getConfigFile(httpd_req_t *request) {
|
|||||||
const char *uri = request->uri;
|
const char *uri = request->uri;
|
||||||
std::string path = "/config" + std::string(uri).substr(11);
|
std::string path = "/config" + std::string(uri).substr(11);
|
||||||
if (!ESP_FS.exists(path.c_str())) {
|
if (!ESP_FS.exists(path.c_str())) {
|
||||||
return NativeServer::sendError(request, 404, "File not found");
|
return WebServer::sendError(request, 404, "File not found");
|
||||||
}
|
}
|
||||||
File file = ESP_FS.open(path.c_str(), "r");
|
File file = ESP_FS.open(path.c_str(), "r");
|
||||||
if (!file) {
|
if (!file) {
|
||||||
return NativeServer::sendError(request, 500, "Failed to open file");
|
return WebServer::sendError(request, 500, "Failed to open file");
|
||||||
}
|
}
|
||||||
String content = file.readString();
|
String content = file.readString();
|
||||||
file.close();
|
file.close();
|
||||||
@@ -31,10 +31,9 @@ esp_err_t handleDelete(httpd_req_t *request, JsonVariant &json) {
|
|||||||
if (json.is<JsonObject>()) {
|
if (json.is<JsonObject>()) {
|
||||||
const char *filename = json["file"].as<const char *>();
|
const char *filename = json["file"].as<const char *>();
|
||||||
ESP_LOGI(TAG, "Deleting file: %s", filename);
|
ESP_LOGI(TAG, "Deleting file: %s", filename);
|
||||||
return deleteFile(filename) ? NativeServer::sendOk(request)
|
return deleteFile(filename) ? WebServer::sendOk(request) : WebServer::sendError(request, 500, "Delete failed");
|
||||||
: NativeServer::sendError(request, 500, "Delete failed");
|
|
||||||
}
|
}
|
||||||
return NativeServer::sendError(request, 400, "Invalid request");
|
return WebServer::sendError(request, 400, "Invalid request");
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t handleEdit(httpd_req_t *request, JsonVariant &json) {
|
esp_err_t handleEdit(httpd_req_t *request, JsonVariant &json) {
|
||||||
@@ -42,10 +41,10 @@ esp_err_t handleEdit(httpd_req_t *request, JsonVariant &json) {
|
|||||||
const char *filename = json["file"].as<const char *>();
|
const char *filename = json["file"].as<const char *>();
|
||||||
const char *content = json["content"].as<const char *>();
|
const char *content = json["content"].as<const char *>();
|
||||||
ESP_LOGI(TAG, "Editing file: %s", filename);
|
ESP_LOGI(TAG, "Editing file: %s", filename);
|
||||||
return editFile(filename, content) ? NativeServer::sendOk(request)
|
return editFile(filename, content) ? WebServer::sendOk(request)
|
||||||
: NativeServer::sendError(request, 500, "Edit failed");
|
: WebServer::sendError(request, 500, "Edit failed");
|
||||||
}
|
}
|
||||||
return NativeServer::sendError(request, 400, "Invalid request");
|
return WebServer::sendError(request, 400, "Invalid request");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool deleteFile(const char *filename) { return ESP_FS.remove(filename); }
|
bool deleteFile(const char *filename) { return ESP_FS.remove(filename); }
|
||||||
@@ -92,7 +91,7 @@ bool editFile(const char *filename, const char *content) {
|
|||||||
esp_err_t mkdir(httpd_req_t *request, JsonVariant &json) {
|
esp_err_t mkdir(httpd_req_t *request, JsonVariant &json) {
|
||||||
const char *path = json["path"].as<const char *>();
|
const char *path = json["path"].as<const char *>();
|
||||||
ESP_LOGI(TAG, "Creating directory: %s", path);
|
ESP_LOGI(TAG, "Creating directory: %s", path);
|
||||||
return ESP_FS.mkdir(path) ? NativeServer::sendOk(request) : NativeServer::sendError(request, 500, "mkdir failed");
|
return ESP_FS.mkdir(path) ? WebServer::sendOk(request) : WebServer::sendError(request, 500, "mkdir failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace FileSystem
|
} // namespace FileSystem
|
||||||
|
|||||||
+40
-44
@@ -10,8 +10,8 @@
|
|||||||
#include <peripherals/servo_controller.h>
|
#include <peripherals/servo_controller.h>
|
||||||
#include <peripherals/led_service.h>
|
#include <peripherals/led_service.h>
|
||||||
#include <peripherals/camera_service.h>
|
#include <peripherals/camera_service.h>
|
||||||
#include <communication/native_server.h>
|
#include <communication/webserver.h>
|
||||||
#include <communication/native_websocket.h>
|
#include <communication/websocket.h>
|
||||||
#include <features.h>
|
#include <features.h>
|
||||||
#include <motion.h>
|
#include <motion.h>
|
||||||
#include <wifi_service.h>
|
#include <wifi_service.h>
|
||||||
@@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
#include <www_mount.hpp>
|
#include <www_mount.hpp>
|
||||||
|
|
||||||
NativeWebsocket socket {nativeServer, "/api/ws"};
|
Websocket socket {server, "/api/ws"};
|
||||||
|
|
||||||
Peripherals peripherals;
|
Peripherals peripherals;
|
||||||
ServoController servoController;
|
ServoController servoController;
|
||||||
@@ -40,87 +40,83 @@ WiFiService wifiService;
|
|||||||
APService apService;
|
APService apService;
|
||||||
|
|
||||||
void setupServer() {
|
void setupServer() {
|
||||||
nativeServer.config(50 + WWW_ASSETS_COUNT, 32768);
|
server.config(50 + WWW_ASSETS_COUNT, 32768);
|
||||||
nativeServer.listen(80);
|
server.listen(80);
|
||||||
|
|
||||||
nativeServer.on("/api/system/reset", HTTP_POST,
|
server.on("/api/system/reset", HTTP_POST,
|
||||||
[&](httpd_req_t *request, JsonVariant &json) { return system_service::handleReset(request); });
|
[&](httpd_req_t *request, JsonVariant &json) { return system_service::handleReset(request); });
|
||||||
nativeServer.on("/api/system/restart", HTTP_POST,
|
server.on("/api/system/restart", HTTP_POST,
|
||||||
[&](httpd_req_t *request, JsonVariant &json) { return system_service::handleRestart(request); });
|
[&](httpd_req_t *request, JsonVariant &json) { return system_service::handleRestart(request); });
|
||||||
nativeServer.on("/api/system/sleep", HTTP_POST,
|
server.on("/api/system/sleep", HTTP_POST,
|
||||||
[&](httpd_req_t *request, JsonVariant &json) { return system_service::handleSleep(request); });
|
[&](httpd_req_t *request, JsonVariant &json) { return system_service::handleSleep(request); });
|
||||||
#if USE_CAMERA
|
#if USE_CAMERA
|
||||||
nativeServer.on("/api/camera/still", HTTP_GET,
|
server.on("/api/camera/still", HTTP_GET, [&](httpd_req_t *request) { return cameraService.cameraStill(request); });
|
||||||
[&](httpd_req_t *request) { return cameraService.cameraStill(request); });
|
server.on("/api/camera/stream", HTTP_GET,
|
||||||
nativeServer.on("/api/camera/stream", HTTP_GET,
|
|
||||||
[&](httpd_req_t *request) { return cameraService.cameraStream(request); });
|
[&](httpd_req_t *request) { return cameraService.cameraStream(request); });
|
||||||
nativeServer.on("/api/camera/settings", HTTP_GET,
|
server.on("/api/camera/settings", HTTP_GET,
|
||||||
[&](httpd_req_t *request) { return cameraService.endpoint.getState(request); });
|
[&](httpd_req_t *request) { return cameraService.endpoint.getState(request); });
|
||||||
nativeServer.on("/api/camera/settings", HTTP_POST, [&](httpd_req_t *request, JsonVariant &json) {
|
server.on("/api/camera/settings", HTTP_POST, [&](httpd_req_t *request, JsonVariant &json) {
|
||||||
return cameraService.endpoint.handleStateUpdate(request, json);
|
return cameraService.endpoint.handleStateUpdate(request, json);
|
||||||
});
|
});
|
||||||
#endif
|
#endif
|
||||||
nativeServer.on("/api/servo/config", HTTP_GET,
|
server.on("/api/servo/config", HTTP_GET,
|
||||||
[&](httpd_req_t *request) { return servoController.endpoint.getState(request); });
|
[&](httpd_req_t *request) { return servoController.endpoint.getState(request); });
|
||||||
nativeServer.on("/api/servo/config", HTTP_POST, [&](httpd_req_t *request, JsonVariant &json) {
|
server.on("/api/servo/config", HTTP_POST, [&](httpd_req_t *request, JsonVariant &json) {
|
||||||
return servoController.endpoint.handleStateUpdate(request, json);
|
return servoController.endpoint.handleStateUpdate(request, json);
|
||||||
});
|
});
|
||||||
|
|
||||||
nativeServer.on("/api/wifi/sta/settings", HTTP_GET,
|
server.on("/api/wifi/sta/settings", HTTP_GET,
|
||||||
[&](httpd_req_t *request) { return wifiService.endpoint.getState(request); });
|
[&](httpd_req_t *request) { return wifiService.endpoint.getState(request); });
|
||||||
nativeServer.on("/api/wifi/sta/settings", HTTP_POST, [&](httpd_req_t *request, JsonVariant &json) {
|
server.on("/api/wifi/sta/settings", HTTP_POST, [&](httpd_req_t *request, JsonVariant &json) {
|
||||||
return wifiService.endpoint.handleStateUpdate(request, json);
|
return wifiService.endpoint.handleStateUpdate(request, json);
|
||||||
});
|
});
|
||||||
nativeServer.on("/api/wifi/scan", HTTP_GET, [&](httpd_req_t *request) { return wifiService.handleScan(request); });
|
server.on("/api/wifi/scan", HTTP_GET, [&](httpd_req_t *request) { return wifiService.handleScan(request); });
|
||||||
nativeServer.on("/api/wifi/networks", HTTP_GET,
|
server.on("/api/wifi/networks", HTTP_GET, [&](httpd_req_t *request) { return wifiService.getNetworks(request); });
|
||||||
[&](httpd_req_t *request) { return wifiService.getNetworks(request); });
|
server.on("/api/wifi/sta/status", HTTP_GET,
|
||||||
nativeServer.on("/api/wifi/sta/status", HTTP_GET,
|
|
||||||
[&](httpd_req_t *request) { return wifiService.getNetworkStatus(request); });
|
[&](httpd_req_t *request) { return wifiService.getNetworkStatus(request); });
|
||||||
|
|
||||||
nativeServer.on("/api/ap/status", HTTP_GET, [&](httpd_req_t *request) { return apService.getStatus(request); });
|
server.on("/api/ap/status", HTTP_GET, [&](httpd_req_t *request) { return apService.getStatus(request); });
|
||||||
nativeServer.on("/api/ap/settings", HTTP_GET,
|
server.on("/api/ap/settings", HTTP_GET, [&](httpd_req_t *request) { return apService.endpoint.getState(request); });
|
||||||
[&](httpd_req_t *request) { return apService.endpoint.getState(request); });
|
server.on("/api/ap/settings", HTTP_POST, [&](httpd_req_t *request, JsonVariant &json) {
|
||||||
nativeServer.on("/api/ap/settings", HTTP_POST, [&](httpd_req_t *request, JsonVariant &json) {
|
|
||||||
return apService.endpoint.handleStateUpdate(request, json);
|
return apService.endpoint.handleStateUpdate(request, json);
|
||||||
});
|
});
|
||||||
|
|
||||||
nativeServer.on("/api/peripherals", HTTP_GET,
|
server.on("/api/peripherals", HTTP_GET,
|
||||||
[&](httpd_req_t *request) { return peripherals.endpoint.getState(request); });
|
[&](httpd_req_t *request) { return peripherals.endpoint.getState(request); });
|
||||||
nativeServer.on("/api/peripherals", HTTP_POST, [&](httpd_req_t *request, JsonVariant &json) {
|
server.on("/api/peripherals", HTTP_POST, [&](httpd_req_t *request, JsonVariant &json) {
|
||||||
return peripherals.endpoint.handleStateUpdate(request, json);
|
return peripherals.endpoint.handleStateUpdate(request, json);
|
||||||
});
|
});
|
||||||
|
|
||||||
#if FT_ENABLED(USE_MDNS)
|
#if FT_ENABLED(USE_MDNS)
|
||||||
nativeServer.on("/api/mdns", HTTP_GET,
|
server.on("/api/mdns", HTTP_GET, [&](httpd_req_t *request) { return mdnsService.endpoint.getState(request); });
|
||||||
[&](httpd_req_t *request) { return mdnsService.endpoint.getState(request); });
|
server.on("/api/mdns", HTTP_POST, [&](httpd_req_t *request, JsonVariant &json) {
|
||||||
nativeServer.on("/api/mdns", HTTP_POST, [&](httpd_req_t *request, JsonVariant &json) {
|
|
||||||
return mdnsService.endpoint.handleStateUpdate(request, json);
|
return mdnsService.endpoint.handleStateUpdate(request, json);
|
||||||
});
|
});
|
||||||
nativeServer.on("/api/mdns/status", HTTP_GET, [&](httpd_req_t *request) { return mdnsService.getStatus(request); });
|
server.on("/api/mdns/status", HTTP_GET, [&](httpd_req_t *request) { return mdnsService.getStatus(request); });
|
||||||
nativeServer.on("/api/mdns/query", HTTP_POST,
|
server.on("/api/mdns/query", HTTP_POST,
|
||||||
[&](httpd_req_t *request, JsonVariant &json) { return mdnsService.queryServices(request, json); });
|
[&](httpd_req_t *request, JsonVariant &json) { return mdnsService.queryServices(request, json); });
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
nativeServer.on("/api/config/*", HTTP_GET, [](httpd_req_t *request) { return FileSystem::getConfigFile(request); });
|
server.on("/api/config/*", HTTP_GET, [](httpd_req_t *request) { return FileSystem::getConfigFile(request); });
|
||||||
nativeServer.on("/api/files", HTTP_GET, [&](httpd_req_t *request) { return FileSystem::getFiles(request); });
|
server.on("/api/files", HTTP_GET, [&](httpd_req_t *request) { return FileSystem::getFiles(request); });
|
||||||
nativeServer.on("/api/files/delete", HTTP_POST,
|
server.on("/api/files/delete", HTTP_POST,
|
||||||
[&](httpd_req_t *request, JsonVariant &json) { return FileSystem::handleDelete(request, json); });
|
[&](httpd_req_t *request, JsonVariant &json) { return FileSystem::handleDelete(request, json); });
|
||||||
nativeServer.on("/api/files/edit", HTTP_POST,
|
server.on("/api/files/edit", HTTP_POST,
|
||||||
[&](httpd_req_t *request, JsonVariant &json) { return FileSystem::handleEdit(request, json); });
|
[&](httpd_req_t *request, JsonVariant &json) { return FileSystem::handleEdit(request, json); });
|
||||||
nativeServer.on("/api/files/mkdir", HTTP_POST,
|
server.on("/api/files/mkdir", HTTP_POST,
|
||||||
[&](httpd_req_t *request, JsonVariant &json) { return FileSystem::mkdir(request, json); });
|
[&](httpd_req_t *request, JsonVariant &json) { return FileSystem::mkdir(request, json); });
|
||||||
#if EMBED_WEBAPP
|
#if EMBED_WEBAPP
|
||||||
mountStaticAssets(nativeServer);
|
mountStaticAssets(server);
|
||||||
#endif
|
#endif
|
||||||
nativeServer.on("/*", HTTP_OPTIONS, [](httpd_req_t *request) {
|
server.on("/*", HTTP_OPTIONS, [](httpd_req_t *request) {
|
||||||
httpd_resp_set_status(request, "200 OK");
|
httpd_resp_set_status(request, "200 OK");
|
||||||
return httpd_resp_send(request, nullptr, 0);
|
return httpd_resp_send(request, nullptr, 0);
|
||||||
});
|
});
|
||||||
nativeServer.addDefaultHeader("Server", APP_NAME);
|
server.addDefaultHeader("Server", APP_NAME);
|
||||||
nativeServer.addDefaultHeader("Access-Control-Allow-Origin", "*");
|
server.addDefaultHeader("Access-Control-Allow-Origin", "*");
|
||||||
nativeServer.addDefaultHeader("Access-Control-Allow-Headers", "Accept, Content-Type, Authorization");
|
server.addDefaultHeader("Access-Control-Allow-Headers", "Accept, Content-Type, Authorization");
|
||||||
nativeServer.addDefaultHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
server.addDefaultHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
||||||
nativeServer.addDefaultHeader("Access-Control-Max-Age", "86400");
|
server.addDefaultHeader("Access-Control-Max-Age", "86400");
|
||||||
}
|
}
|
||||||
|
|
||||||
void setupEventSocket() {
|
void setupEventSocket() {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#include <mdns_service.h>
|
#include <mdns_service.h>
|
||||||
#include <communication/native_server.h>
|
#include <communication/webserver.h>
|
||||||
|
|
||||||
static const char *TAG = "MDNSService";
|
static const char *TAG = "MDNSService";
|
||||||
|
|
||||||
@@ -69,7 +69,7 @@ esp_err_t MDNSService::getStatus(httpd_req_t *request) {
|
|||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
JsonVariant root = doc.to<JsonVariant>();
|
JsonVariant root = doc.to<JsonVariant>();
|
||||||
getStatus(root);
|
getStatus(root);
|
||||||
return NativeServer::sendJson(request, 200, doc);
|
return WebServer::sendJson(request, 200, doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MDNSService::getStatus(JsonVariant &root) {
|
void MDNSService::getStatus(JsonVariant &root) {
|
||||||
@@ -97,5 +97,5 @@ esp_err_t MDNSService::queryServices(httpd_req_t *request, JsonVariant &json) {
|
|||||||
serviceObj["port"] = MDNS.port(i);
|
serviceObj["port"] = MDNS.port(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
return NativeServer::sendJson(request, 200, doc);
|
return WebServer::sendJson(request, 200, doc);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#include <peripherals/camera_service.h>
|
#include <peripherals/camera_service.h>
|
||||||
#include <communication/native_server.h>
|
#include <communication/webserver.h>
|
||||||
|
|
||||||
namespace Camera {
|
namespace Camera {
|
||||||
|
|
||||||
@@ -88,7 +88,7 @@ esp_err_t CameraService::cameraStill(httpd_req_t *request) {
|
|||||||
camera_fb_t *fb = safe_camera_fb_get();
|
camera_fb_t *fb = safe_camera_fb_get();
|
||||||
if (!fb) {
|
if (!fb) {
|
||||||
ESP_LOGE(TAG, "Camera capture failed");
|
ESP_LOGE(TAG, "Camera capture failed");
|
||||||
return NativeServer::sendError(request, 500, "Camera capture failed");
|
return WebServer::sendError(request, 500, "Camera capture failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
httpd_resp_set_type(request, "image/jpeg");
|
httpd_resp_set_type(request, "image/jpeg");
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#include "system_service.h"
|
#include "system_service.h"
|
||||||
#include <communication/native_server.h>
|
#include <communication/webserver.h>
|
||||||
|
|
||||||
namespace system_service {
|
namespace system_service {
|
||||||
|
|
||||||
@@ -7,17 +7,17 @@ static const char *TAG = "SystemService";
|
|||||||
|
|
||||||
esp_err_t handleReset(httpd_req_t *request) {
|
esp_err_t handleReset(httpd_req_t *request) {
|
||||||
reset();
|
reset();
|
||||||
return NativeServer::sendOk(request);
|
return WebServer::sendOk(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t handleRestart(httpd_req_t *request) {
|
esp_err_t handleRestart(httpd_req_t *request) {
|
||||||
restart();
|
restart();
|
||||||
return NativeServer::sendOk(request);
|
return WebServer::sendOk(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t handleSleep(httpd_req_t *request) {
|
esp_err_t handleSleep(httpd_req_t *request) {
|
||||||
sleep();
|
sleep();
|
||||||
return NativeServer::sendOk(request);
|
return WebServer::sendOk(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset() {
|
void reset() {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#include <wifi_service.h>
|
#include <wifi_service.h>
|
||||||
#include <communication/native_server.h>
|
#include <communication/webserver.h>
|
||||||
|
|
||||||
WiFiService::WiFiService()
|
WiFiService::WiFiService()
|
||||||
: _persistence(WiFiSettings::read, WiFiSettings::update, this, WIFI_SETTINGS_FILE),
|
: _persistence(WiFiSettings::read, WiFiSettings::update, this, WIFI_SETTINGS_FILE),
|
||||||
@@ -60,7 +60,7 @@ esp_err_t WiFiService::getNetworks(httpd_req_t *request) {
|
|||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
JsonObject root = doc.to<JsonObject>();
|
JsonObject root = doc.to<JsonObject>();
|
||||||
getNetworks(root);
|
getNetworks(root);
|
||||||
return NativeServer::sendJson(request, 200, doc);
|
return WebServer::sendJson(request, 200, doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WiFiService::setupMDNS(const char *hostname) {
|
void WiFiService::setupMDNS(const char *hostname) {
|
||||||
@@ -88,7 +88,7 @@ esp_err_t WiFiService::getNetworkStatus(httpd_req_t *request) {
|
|||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
JsonObject root = doc.to<JsonObject>();
|
JsonObject root = doc.to<JsonObject>();
|
||||||
getNetworkStatus(root);
|
getNetworkStatus(root);
|
||||||
return NativeServer::sendJson(request, 200, doc);
|
return WebServer::sendJson(request, 200, doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WiFiService::getNetworkStatus(JsonObject &root) {
|
void WiFiService::getNetworkStatus(JsonObject &root) {
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ static esp_err_t web_send(httpd_req_t* req, const WebAsset& asset) {
|
|||||||
return httpd_resp_send(req, (const char*)asset.data, asset.len);
|
return httpd_resp_send(req, (const char*)asset.data, asset.len);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mountStaticAssets(NativeServer& server) {
|
void mountStaticAssets(WebServer& server) {
|
||||||
for (size_t i = 0; i < WWW_ASSETS_COUNT; i++) {
|
for (size_t i = 0; i < WWW_ASSETS_COUNT; i++) {
|
||||||
const WebAsset* a = &WWW_ASSETS[i];
|
const WebAsset* a = &WWW_ASSETS[i];
|
||||||
server.on(a->uri, HTTP_GET, [a](httpd_req_t* req) { return web_send(req, *a); });
|
server.on(a->uri, HTTP_GET, [a](httpd_req_t* req) { return web_send(req, *a); });
|
||||||
|
|||||||
Reference in New Issue
Block a user