mirror of
https://github.com/MrUnknownDE/OpenIris-ESPIDF.git
synced 2026-05-04 21:26:04 +02:00
Add basic rest_api handler
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
idf_component_register(SRCS "RestAPI/RestAPI.cpp"
|
||||||
|
INCLUDE_DIRS "RestAPI"
|
||||||
|
# REQUIRES CommandManager mongoose
|
||||||
|
REQUIRES mongoose
|
||||||
|
)
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
#include "RestAPI.hpp"
|
||||||
|
|
||||||
|
RestAPI::RestAPI(std::string url)
|
||||||
|
{
|
||||||
|
this->url = url;
|
||||||
|
// updates
|
||||||
|
routes.emplace("/api/update/wifi/", &RestAPI::handle_wifi);
|
||||||
|
routes.emplace("/api/update/device/", &RestAPI::handle_update_device);
|
||||||
|
routes.emplace("/api/update/camera/", &RestAPI::handle_update_camera);
|
||||||
|
|
||||||
|
// post will reset it
|
||||||
|
// resets
|
||||||
|
routes.emplace("/api/reset/config/", &RestAPI::handle_reset_config);
|
||||||
|
routes.emplace("/api/reset/wifi/", &RestAPI::handle_reset_wifi_config);
|
||||||
|
routes.emplace("/api/reset/txpower/", &RestAPI::handle_reset_txpower_config);
|
||||||
|
routes.emplace("/api/reset/camera/", &RestAPI::handle_reset_camera_config);
|
||||||
|
|
||||||
|
// gets
|
||||||
|
routes.emplace("/api/get/config/", &RestAPI::handle_get_config);
|
||||||
|
routes.emplace("/api/reboot/", &RestAPI::handle_reboot);
|
||||||
|
routes.emplace("/api/reboot/", &RestAPI::handle_reboot);
|
||||||
|
|
||||||
|
// reboots
|
||||||
|
routes.emplace("/api/reboot/device/", &RestAPI::handle_reboot);
|
||||||
|
routes.emplace("/api/reboot/camera/", &RestAPI::handle_camera_reboot);
|
||||||
|
|
||||||
|
// heartbeat
|
||||||
|
routes.emplace("/api/ping/", &RestAPI::pong);
|
||||||
|
|
||||||
|
// special
|
||||||
|
routes.emplace("/api/save/", &RestAPI::handle_save);
|
||||||
|
}
|
||||||
|
|
||||||
|
void RestAPI::begin()
|
||||||
|
{
|
||||||
|
mg_log_set(MG_LL_DEBUG);
|
||||||
|
mg_mgr_init(&mgr);
|
||||||
|
// every route is handled through this class, with commands themselves by a command manager
|
||||||
|
// hence we pass a pointer to this in mg_http_listen
|
||||||
|
mg_http_listen(&mgr, this->url.c_str(), (mg_event_handler_t)RestAPIHelpers::event_handler, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void RestAPI::handle_request(struct mg_connection *connection, int event, void *event_data)
|
||||||
|
{
|
||||||
|
if (event == MG_EV_HTTP_MSG)
|
||||||
|
{
|
||||||
|
struct mg_http_message *message = (struct mg_http_message *)event_data;
|
||||||
|
std::string uri = std::string(message->uri.buf, message->uri.len);
|
||||||
|
|
||||||
|
bool found = false;
|
||||||
|
for (auto it = this->routes.begin(); it != this->routes.end(); it++)
|
||||||
|
{
|
||||||
|
if (it->first == uri)
|
||||||
|
{
|
||||||
|
found = true;
|
||||||
|
RequestContext *context = new RequestContext{
|
||||||
|
.connection = connection,
|
||||||
|
};
|
||||||
|
auto method = it->second;
|
||||||
|
(*this.*method)(context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!found)
|
||||||
|
{
|
||||||
|
mg_http_reply(connection, 404, "", "Wrong URL");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void RestAPIHelpers::event_handler(struct mg_connection *connection, int event, void *event_data)
|
||||||
|
{
|
||||||
|
RestAPI *rest_api_handler = static_cast<RestAPI *>(connection->fn_data);
|
||||||
|
rest_api_handler->handle_request(connection, event, event_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void RestAPI::poll()
|
||||||
|
{
|
||||||
|
mg_mgr_poll(&mgr, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
void RestAPI::handle_reboot(RequestContext *context)
|
||||||
|
{
|
||||||
|
mg_http_reply(context->connection, 200, "", "Yes sir");
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
#include <string>
|
||||||
|
#include <memory>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <mongoose.h>
|
||||||
|
|
||||||
|
#include "esp_log.h"
|
||||||
|
|
||||||
|
struct RequestContext
|
||||||
|
{
|
||||||
|
mg_connection *connection;
|
||||||
|
};
|
||||||
|
|
||||||
|
class RestAPI
|
||||||
|
{
|
||||||
|
using route_handler = void (RestAPI::*)(RequestContext *);
|
||||||
|
typedef std::unordered_map<std::string, route_handler> route_map;
|
||||||
|
std::string url;
|
||||||
|
route_map routes;
|
||||||
|
|
||||||
|
struct mg_mgr mgr;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void handle_reboot(RequestContext *context);
|
||||||
|
|
||||||
|
public:
|
||||||
|
// this will also need command manager
|
||||||
|
RestAPI(std::string url);
|
||||||
|
void begin();
|
||||||
|
void handle_request(struct mg_connection *connection, int event, void *event_data);
|
||||||
|
void poll();
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace RestAPIHelpers
|
||||||
|
{
|
||||||
|
void event_handler(struct mg_connection *connection, int event, void *event_data);
|
||||||
|
};
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
#include "esp_http_server.h"
|
#include "esp_http_server.h"
|
||||||
#include "esp_timer.h"
|
#include "esp_timer.h"
|
||||||
#include <StateManager.hpp>
|
#include <StateManager.hpp>
|
||||||
#include <WebSocketLogger.hpp >
|
#include <WebSocketLogger.hpp>
|
||||||
#include <Helpers.hpp>
|
#include <Helpers.hpp>
|
||||||
|
|
||||||
extern WebSocketLogger webSocketLogger;
|
extern WebSocketLogger webSocketLogger;
|
||||||
|
|||||||
+6
-4
@@ -1,6 +1,6 @@
|
|||||||
dependencies:
|
dependencies:
|
||||||
espressif/cmake_utilities:
|
espressif/cmake_utilities:
|
||||||
component_hash: 351350613ceafba240b761b4ea991e0f231ac7a9f59a9ee901f751bddc0bb18f
|
component_hash: 7e20811092150b668a710cfbf43164721b23bac766811f4e68036450a3265fba
|
||||||
dependencies:
|
dependencies:
|
||||||
- name: idf
|
- name: idf
|
||||||
require: private
|
require: private
|
||||||
@@ -8,18 +8,19 @@ dependencies:
|
|||||||
source:
|
source:
|
||||||
registry_url: https://components.espressif.com/
|
registry_url: https://components.espressif.com/
|
||||||
type: service
|
type: service
|
||||||
version: 0.5.3
|
version: 1.0.0
|
||||||
espressif/esp32-camera:
|
espressif/esp32-camera:
|
||||||
component_hash: 154da98781f3ee98f6dbbdd3aaec97497051fb1a570c300fc0fd3a946874a02e
|
component_hash: a82de4ee0b383bd34695935385c6a6c720129084580c2bbb55dce76eb2a3788f
|
||||||
dependencies: []
|
dependencies: []
|
||||||
source:
|
source:
|
||||||
registry_url: https://components.espressif.com/
|
registry_url: https://components.espressif.com/
|
||||||
type: service
|
type: service
|
||||||
version: 2.0.12
|
version: 2.0.13
|
||||||
espressif/led_strip:
|
espressif/led_strip:
|
||||||
component_hash: 28c6509a727ef74925b372ed404772aeedf11cce10b78c3f69b3c66799095e2d
|
component_hash: 28c6509a727ef74925b372ed404772aeedf11cce10b78c3f69b3c66799095e2d
|
||||||
dependencies:
|
dependencies:
|
||||||
- name: idf
|
- name: idf
|
||||||
|
registry_url: https://components.espressif.com
|
||||||
require: private
|
require: private
|
||||||
version: '>=4.4'
|
version: '>=4.4'
|
||||||
source:
|
source:
|
||||||
@@ -62,6 +63,7 @@ dependencies:
|
|||||||
require: private
|
require: private
|
||||||
version: '>=0.15.0~10'
|
version: '>=0.15.0~10'
|
||||||
- name: idf
|
- name: idf
|
||||||
|
registry_url: https://components.espressif.com
|
||||||
require: private
|
require: private
|
||||||
version: '>=5.0'
|
version: '>=5.0'
|
||||||
source:
|
source:
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
#include <CameraManager.hpp>
|
#include <CameraManager.hpp>
|
||||||
#include <WebSocketLogger.hpp>
|
#include <WebSocketLogger.hpp>
|
||||||
#include <StreamServer.hpp>
|
#include <StreamServer.hpp>
|
||||||
|
#include <RestAPI.hpp>
|
||||||
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
|
||||||
@@ -35,6 +36,7 @@ WiFiManager wifiManager(deviceConfig);
|
|||||||
MDNSManager mdnsManager(deviceConfig);
|
MDNSManager mdnsManager(deviceConfig);
|
||||||
CameraManager cameraHandler(deviceConfig);
|
CameraManager cameraHandler(deviceConfig);
|
||||||
StreamServer streamServer(80);
|
StreamServer streamServer(80);
|
||||||
|
RestAPI restAPI("http://0.0.0.0:81");
|
||||||
|
|
||||||
#ifdef CONFIG_WIRED_MODE
|
#ifdef CONFIG_WIRED_MODE
|
||||||
UVCStreamManager uvcStream;
|
UVCStreamManager uvcStream;
|
||||||
@@ -97,6 +99,7 @@ extern "C" void app_main(void)
|
|||||||
deviceConfig.load();
|
deviceConfig.load();
|
||||||
wifiManager.Begin();
|
wifiManager.Begin();
|
||||||
mdnsManager.start();
|
mdnsManager.start();
|
||||||
|
restAPI.begin();
|
||||||
cameraHandler.setupCamera();
|
cameraHandler.setupCamera();
|
||||||
streamServer.startStreamServer();
|
streamServer.startStreamServer();
|
||||||
|
|
||||||
@@ -107,6 +110,7 @@ extern "C" void app_main(void)
|
|||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
ledManager.handleLED();
|
ledManager.handleLED();
|
||||||
|
restAPI.poll();
|
||||||
vTaskDelay(CONFIG_BLINK_PERIOD / portTICK_PERIOD_MS);
|
vTaskDelay(CONFIG_BLINK_PERIOD / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user