mirror of
https://github.com/MrUnknownDE/OpenIris-ESPIDF.git
synced 2026-05-05 21:36:05 +02:00
Add RESTART_CAMERA, RESET_CONFIG, RESTART_DEVICE commands and implement related endpoints
This commit is contained in:
@@ -6,8 +6,9 @@ idf_component_register(
|
|||||||
"CommandManager/commands/wifi_commands.cpp"
|
"CommandManager/commands/wifi_commands.cpp"
|
||||||
"CommandManager/commands/config_commands.cpp"
|
"CommandManager/commands/config_commands.cpp"
|
||||||
"CommandManager/commands/mdns_commands.cpp"
|
"CommandManager/commands/mdns_commands.cpp"
|
||||||
|
"CommandManager/commands/device_commands.cpp"
|
||||||
INCLUDE_DIRS
|
INCLUDE_DIRS
|
||||||
"CommandManager"
|
"CommandManager"
|
||||||
"CommandManager/commands"
|
"CommandManager/commands"
|
||||||
REQUIRES ProjectConfig cJSON
|
REQUIRES ProjectConfig cJSON CameraManager
|
||||||
)
|
)
|
||||||
@@ -29,10 +29,16 @@ std::unique_ptr<Command> CommandManager::createCommand(CommandType type)
|
|||||||
return std::make_unique<setMDNSCommand>(projectConfig);
|
return std::make_unique<setMDNSCommand>(projectConfig);
|
||||||
case CommandType::UPDATE_CAMERA:
|
case CommandType::UPDATE_CAMERA:
|
||||||
return std::make_unique<updateCameraCommand>(projectConfig);
|
return std::make_unique<updateCameraCommand>(projectConfig);
|
||||||
|
case CommandType::RESTART_CAMERA:
|
||||||
|
return std::make_unique<restartCameraCommand>(cameraManager);
|
||||||
case CommandType::GET_CONFIG:
|
case CommandType::GET_CONFIG:
|
||||||
return std::make_unique<getConfigCommand>(projectConfig);
|
return std::make_unique<getConfigCommand>(projectConfig);
|
||||||
case CommandType::SAVE_CONFIG:
|
case CommandType::SAVE_CONFIG:
|
||||||
return std::make_unique<saveConfigCommand>(projectConfig);
|
return std::make_unique<saveConfigCommand>(projectConfig);
|
||||||
|
case CommandType::RESET_CONFIG:
|
||||||
|
return std::make_unique<resetConfigCommand>(projectConfig);
|
||||||
|
case CommandType::RESTART_DEVICE:
|
||||||
|
return std::make_unique<restartDeviceCommand>();
|
||||||
default:
|
default:
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#define COMMANDMANAGER_HPP
|
#define COMMANDMANAGER_HPP
|
||||||
|
|
||||||
#include <ProjectConfig.hpp>
|
#include <ProjectConfig.hpp>
|
||||||
|
#include <CameraManager.hpp>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
@@ -14,6 +15,7 @@
|
|||||||
#include "commands/config_commands.hpp"
|
#include "commands/config_commands.hpp"
|
||||||
#include "commands/mdns_commands.hpp"
|
#include "commands/mdns_commands.hpp"
|
||||||
#include "commands/wifi_commands.hpp"
|
#include "commands/wifi_commands.hpp"
|
||||||
|
#include "commands/device_commands.hpp"
|
||||||
#include <cJSON.h>
|
#include <cJSON.h>
|
||||||
|
|
||||||
// mostlikely missing commands
|
// mostlikely missing commands
|
||||||
@@ -31,6 +33,7 @@ enum CommandType
|
|||||||
UPDATE_MDNS,
|
UPDATE_MDNS,
|
||||||
SET_MDNS,
|
SET_MDNS,
|
||||||
UPDATE_CAMERA,
|
UPDATE_CAMERA,
|
||||||
|
RESTART_CAMERA,
|
||||||
SAVE_CONFIG,
|
SAVE_CONFIG,
|
||||||
GET_CONFIG,
|
GET_CONFIG,
|
||||||
RESET_CONFIG,
|
RESET_CONFIG,
|
||||||
@@ -41,9 +44,10 @@ class CommandManager
|
|||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<ProjectConfig> projectConfig;
|
std::shared_ptr<ProjectConfig> projectConfig;
|
||||||
|
std::shared_ptr<CameraManager> cameraManager;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CommandManager(std::shared_ptr<ProjectConfig> projectConfig) : projectConfig(projectConfig) {};
|
CommandManager(std::shared_ptr<ProjectConfig> projectConfig, std::shared_ptr<CameraManager> cameraManager) : projectConfig(projectConfig), cameraManager(cameraManager) {};
|
||||||
std::unique_ptr<Command> createCommand(CommandType type);
|
std::unique_ptr<Command> createCommand(CommandType type);
|
||||||
|
|
||||||
CommandResult executeFromJson(std::string_view json);
|
CommandResult executeFromJson(std::string_view json);
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
#ifndef COMMAND_SCHEMA_HPP
|
#ifndef COMMAND_SCHEMA_HPP
|
||||||
#define COMMAND_SCHEMA_HPP
|
#define COMMAND_SCHEMA_HPP
|
||||||
|
|
||||||
// this generally should be merged with ProjectConfig definitions
|
|
||||||
// and moved into a separate component
|
|
||||||
// and used as more or less models
|
|
||||||
struct BasePayload
|
struct BasePayload
|
||||||
{
|
{
|
||||||
};
|
};
|
||||||
@@ -54,4 +51,13 @@ struct UpdateCameraConfigPayload : BasePayload
|
|||||||
// TODO add more options here
|
// TODO add more options here
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct ResetConfigPayload : BasePayload
|
||||||
|
{
|
||||||
|
std::string section;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct RestartCameraPayload : BasePayload
|
||||||
|
{
|
||||||
|
bool mode;
|
||||||
|
};
|
||||||
#endif
|
#endif
|
||||||
@@ -49,3 +49,37 @@ CommandResult updateCameraCommand::execute(std::string_view jsonPayload)
|
|||||||
|
|
||||||
return CommandResult::getSuccessResult("Config updated");
|
return CommandResult::getSuccessResult("Config updated");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<RestartCameraPayload> restartCameraCommand::parsePayload(std::string_view jsonPayload)
|
||||||
|
{
|
||||||
|
RestartCameraPayload payload;
|
||||||
|
cJSON *parsedJson = cJSON_Parse(jsonPayload.data());
|
||||||
|
|
||||||
|
if (parsedJson == nullptr)
|
||||||
|
return std::nullopt;
|
||||||
|
|
||||||
|
cJSON *mode = cJSON_GetObjectItem(parsedJson, "mode");
|
||||||
|
|
||||||
|
if (mode == nullptr)
|
||||||
|
{
|
||||||
|
cJSON_Delete(parsedJson);
|
||||||
|
}
|
||||||
|
|
||||||
|
payload.mode = (bool)mode->valueint;
|
||||||
|
|
||||||
|
cJSON_Delete(parsedJson);
|
||||||
|
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
CommandResult restartCameraCommand::execute(std::string_view jsonPayload)
|
||||||
|
{
|
||||||
|
auto payload = parsePayload(jsonPayload);
|
||||||
|
if (!payload.has_value())
|
||||||
|
{
|
||||||
|
return CommandResult::getErrorResult("Invalid payload");
|
||||||
|
}
|
||||||
|
|
||||||
|
this->cameraManager->resetCamera(payload.value().mode);
|
||||||
|
return CommandResult::getSuccessResult("Camera restarted");
|
||||||
|
}
|
||||||
@@ -1,4 +1,7 @@
|
|||||||
|
#ifndef CAMERA_COMMANDS_HPP
|
||||||
|
#define CAMERA_COMMANDS_HPP
|
||||||
#include "BaseCommand.hpp"
|
#include "BaseCommand.hpp"
|
||||||
|
#include <CameraManager.hpp>
|
||||||
|
|
||||||
class updateCameraCommand : public Command
|
class updateCameraCommand : public Command
|
||||||
{
|
{
|
||||||
@@ -10,4 +13,15 @@ public:
|
|||||||
std::optional<UpdateCameraConfigPayload> parsePayload(std::string_view jsonPayload);
|
std::optional<UpdateCameraConfigPayload> parsePayload(std::string_view jsonPayload);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class restartCameraCommand : public Command
|
||||||
|
{
|
||||||
|
std::shared_ptr<CameraManager> cameraManager;
|
||||||
|
|
||||||
|
public:
|
||||||
|
restartCameraCommand(std::shared_ptr<CameraManager> cameraManager) : cameraManager(cameraManager) {};
|
||||||
|
CommandResult execute(std::string_view jsonPayload) override;
|
||||||
|
std::optional<RestartCameraPayload> parsePayload(std::string_view jsonPayload);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
// add cropping command
|
// add cropping command
|
||||||
@@ -11,3 +11,49 @@ CommandResult getConfigCommand::execute(std::string_view jsonPayload)
|
|||||||
auto configRepresentation = projectConfig->getTrackerConfig().toRepresentation();
|
auto configRepresentation = projectConfig->getTrackerConfig().toRepresentation();
|
||||||
return CommandResult::getSuccessResult(configRepresentation);
|
return CommandResult::getSuccessResult(configRepresentation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<ResetConfigPayload> resetConfigCommand::parsePayload(std::string_view jsonPayload)
|
||||||
|
{
|
||||||
|
ResetConfigPayload payload;
|
||||||
|
cJSON *parsedJson = cJSON_Parse(jsonPayload.data());
|
||||||
|
|
||||||
|
if (parsedJson == nullptr)
|
||||||
|
return std::nullopt;
|
||||||
|
|
||||||
|
cJSON *section = cJSON_GetObjectItem(parsedJson, "section");
|
||||||
|
|
||||||
|
if (section == nullptr)
|
||||||
|
{
|
||||||
|
cJSON_Delete(parsedJson);
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
payload.section = std::string(section->valuestring);
|
||||||
|
|
||||||
|
cJSON_Delete(parsedJson);
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
CommandResult resetConfigCommand::execute(std::string_view jsonPayload)
|
||||||
|
{
|
||||||
|
auto payload = parsePayload(jsonPayload);
|
||||||
|
|
||||||
|
if (!payload.has_value())
|
||||||
|
{
|
||||||
|
return CommandResult::getErrorResult("Invalid payload or missing section");
|
||||||
|
}
|
||||||
|
|
||||||
|
auto sectionPayload = payload.value();
|
||||||
|
|
||||||
|
if (std::find(this->supported_sections.begin(), this->supported_sections.end(), sectionPayload.section) == this->supported_sections.end())
|
||||||
|
{
|
||||||
|
return CommandResult::getErrorResult("Selected section is unsupported");
|
||||||
|
}
|
||||||
|
|
||||||
|
// we cannot match on string, and making a map would be overkill right now, sooo
|
||||||
|
// todo, add more granual control for other sections, like only reset camera, or only reset wifi
|
||||||
|
if (sectionPayload.section == "all")
|
||||||
|
this->projectConfig->reset();
|
||||||
|
|
||||||
|
return CommandResult::getSuccessResult("Config reset");
|
||||||
|
}
|
||||||
@@ -15,3 +15,16 @@ public:
|
|||||||
getConfigCommand(std::shared_ptr<ProjectConfig> projectConfig) : projectConfig(projectConfig) {};
|
getConfigCommand(std::shared_ptr<ProjectConfig> projectConfig) : projectConfig(projectConfig) {};
|
||||||
CommandResult execute(std::string_view jsonPayload) override;
|
CommandResult execute(std::string_view jsonPayload) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class resetConfigCommand : public Command
|
||||||
|
{
|
||||||
|
std::array<std::string, 4> supported_sections = {
|
||||||
|
"all",
|
||||||
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
|
std::shared_ptr<ProjectConfig> projectConfig;
|
||||||
|
resetConfigCommand(std::shared_ptr<ProjectConfig> projectConfig) : projectConfig(projectConfig) {};
|
||||||
|
CommandResult execute(std::string_view jsonPayload) override;
|
||||||
|
std::optional<ResetConfigPayload> parsePayload(std::string_view jsonPayload);
|
||||||
|
};
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#include "device_commands.hpp"
|
||||||
|
|
||||||
|
CommandResult restartDeviceCommand::execute(std::string_view jsonPayload)
|
||||||
|
{
|
||||||
|
// todo implement this: https://github.com/EyeTrackVR/OpenIris/blob/master/ESP/lib/src/tasks/tasks.cpp
|
||||||
|
// OpenIrisTasks::ScheduleRestart(2000);
|
||||||
|
return CommandResult::getSuccessResult("Device restarted");
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#include "BaseCommand.hpp"
|
||||||
|
|
||||||
|
class restartDeviceCommand : public Command
|
||||||
|
{
|
||||||
|
CommandResult execute(std::string_view jsonPayload) override;
|
||||||
|
};
|
||||||
@@ -13,10 +13,6 @@ RestAPI::RestAPI(std::string url, std::shared_ptr<CommandManager> commandManager
|
|||||||
// post will reset it
|
// post will reset it
|
||||||
// resets
|
// resets
|
||||||
routes.emplace("/api/reset/config/", &RestAPI::handle_reset_config);
|
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
|
// gets
|
||||||
routes.emplace("/api/get/config/", &RestAPI::handle_get_config);
|
routes.emplace("/api/get/config/", &RestAPI::handle_get_config);
|
||||||
|
|
||||||
@@ -129,27 +125,21 @@ void RestAPI::handle_get_config(RequestContext *context)
|
|||||||
|
|
||||||
void RestAPI::handle_reset_config(RequestContext *context)
|
void RestAPI::handle_reset_config(RequestContext *context)
|
||||||
{
|
{
|
||||||
mg_http_reply(context->connection, 200, JSON_RESPONSE, "{%m:%m}", MG_ESC("result"), "Config reset");
|
if (context->method != POST_METHOD)
|
||||||
|
{
|
||||||
|
mg_http_reply(context->connection, 401, JSON_RESPONSE, "{%m:%m}", MG_ESC("error"), "Method not allowed");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RestAPI::handle_reset_wifi_config(RequestContext *context)
|
auto result = this->command_manager->executeFromType(CommandType::RESET_CONFIG, "{\"section\": \"all\"}");
|
||||||
{
|
int code = result.isSuccess() ? 200 : 500;
|
||||||
mg_http_reply(context->connection, 200, JSON_RESPONSE, "{%m:%m}", MG_ESC("result"), "WiFi Config reset");
|
mg_http_reply(context->connection, code, JSON_RESPONSE, "{%m:%m}", MG_ESC("result"), result.getResult());
|
||||||
}
|
|
||||||
|
|
||||||
void RestAPI::handle_reset_txpower_config(RequestContext *context)
|
|
||||||
{
|
|
||||||
mg_http_reply(context->connection, 200, JSON_RESPONSE, "{%m:%m}", MG_ESC("result"), "TX Power Config reset");
|
|
||||||
}
|
|
||||||
|
|
||||||
void RestAPI::handle_reset_camera_config(RequestContext *context)
|
|
||||||
{
|
|
||||||
mg_http_reply(context->connection, 200, JSON_RESPONSE, "{%m:%m}", MG_ESC("result"), "Camera Config reset");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// reboots
|
// reboots
|
||||||
void RestAPI::handle_reboot(RequestContext *context)
|
void RestAPI::handle_reboot(RequestContext *context)
|
||||||
{
|
{
|
||||||
|
auto result = this->command_manager->executeFromType(CommandType::RESTART_DEVICE, "");
|
||||||
mg_http_reply(context->connection, 200, JSON_RESPONSE, "{%m:%m}", MG_ESC("result"), "Ok");
|
mg_http_reply(context->connection, 200, JSON_RESPONSE, "{%m:%m}", MG_ESC("result"), "Ok");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,9 +39,6 @@ private:
|
|||||||
|
|
||||||
// resets
|
// resets
|
||||||
void handle_reset_config(RequestContext *context);
|
void handle_reset_config(RequestContext *context);
|
||||||
void handle_reset_wifi_config(RequestContext *context);
|
|
||||||
void handle_reset_txpower_config(RequestContext *context);
|
|
||||||
void handle_reset_camera_config(RequestContext *context);
|
|
||||||
|
|
||||||
// reboots
|
// reboots
|
||||||
void handle_reboot(RequestContext *context);
|
void handle_reboot(RequestContext *context);
|
||||||
|
|||||||
@@ -35,10 +35,10 @@ WebSocketLogger webSocketLogger;
|
|||||||
auto deviceConfig = std::make_shared<ProjectConfig>("openiris", CONFIG_MDNS_HOSTNAME);
|
auto deviceConfig = std::make_shared<ProjectConfig>("openiris", CONFIG_MDNS_HOSTNAME);
|
||||||
WiFiManager wifiManager(deviceConfig);
|
WiFiManager wifiManager(deviceConfig);
|
||||||
MDNSManager mdnsManager(deviceConfig);
|
MDNSManager mdnsManager(deviceConfig);
|
||||||
CameraManager cameraHandler(deviceConfig);
|
auto cameraHandler = std::make_shared<CameraManager>(deviceConfig);
|
||||||
StreamServer streamServer(80);
|
StreamServer streamServer(80);
|
||||||
|
|
||||||
auto commandManager = std::make_shared<CommandManager>(deviceConfig);
|
auto commandManager = std::make_shared<CommandManager>(deviceConfig, cameraHandler);
|
||||||
RestAPI restAPI("http://0.0.0.0:81", commandManager);
|
RestAPI restAPI("http://0.0.0.0:81", commandManager);
|
||||||
|
|
||||||
#ifdef CONFIG_WIRED_MODE
|
#ifdef CONFIG_WIRED_MODE
|
||||||
@@ -103,7 +103,7 @@ extern "C" void app_main(void)
|
|||||||
wifiManager.Begin();
|
wifiManager.Begin();
|
||||||
mdnsManager.start();
|
mdnsManager.start();
|
||||||
restAPI.begin();
|
restAPI.begin();
|
||||||
cameraHandler.setupCamera();
|
cameraHandler->setupCamera();
|
||||||
streamServer.startStreamServer();
|
streamServer.startStreamServer();
|
||||||
|
|
||||||
#ifdef CONFIG_WIRED_MODE
|
#ifdef CONFIG_WIRED_MODE
|
||||||
|
|||||||
Reference in New Issue
Block a user