mirror of
https://github.com/MrUnknownDE/OpenIris-ESPIDF.git
synced 2026-04-19 14:33:45 +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/config_commands.cpp"
|
||||
"CommandManager/commands/mdns_commands.cpp"
|
||||
"CommandManager/commands/device_commands.cpp"
|
||||
INCLUDE_DIRS
|
||||
"CommandManager"
|
||||
"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);
|
||||
case CommandType::UPDATE_CAMERA:
|
||||
return std::make_unique<updateCameraCommand>(projectConfig);
|
||||
case CommandType::RESTART_CAMERA:
|
||||
return std::make_unique<restartCameraCommand>(cameraManager);
|
||||
case CommandType::GET_CONFIG:
|
||||
return std::make_unique<getConfigCommand>(projectConfig);
|
||||
case CommandType::SAVE_CONFIG:
|
||||
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:
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define COMMANDMANAGER_HPP
|
||||
|
||||
#include <ProjectConfig.hpp>
|
||||
#include <CameraManager.hpp>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <optional>
|
||||
@@ -14,6 +15,7 @@
|
||||
#include "commands/config_commands.hpp"
|
||||
#include "commands/mdns_commands.hpp"
|
||||
#include "commands/wifi_commands.hpp"
|
||||
#include "commands/device_commands.hpp"
|
||||
#include <cJSON.h>
|
||||
|
||||
// mostlikely missing commands
|
||||
@@ -31,6 +33,7 @@ enum CommandType
|
||||
UPDATE_MDNS,
|
||||
SET_MDNS,
|
||||
UPDATE_CAMERA,
|
||||
RESTART_CAMERA,
|
||||
SAVE_CONFIG,
|
||||
GET_CONFIG,
|
||||
RESET_CONFIG,
|
||||
@@ -41,9 +44,10 @@ class CommandManager
|
||||
{
|
||||
private:
|
||||
std::shared_ptr<ProjectConfig> projectConfig;
|
||||
std::shared_ptr<CameraManager> cameraManager;
|
||||
|
||||
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);
|
||||
|
||||
CommandResult executeFromJson(std::string_view json);
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
#ifndef 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
|
||||
{
|
||||
};
|
||||
@@ -54,4 +51,13 @@ struct UpdateCameraConfigPayload : BasePayload
|
||||
// TODO add more options here
|
||||
};
|
||||
|
||||
struct ResetConfigPayload : BasePayload
|
||||
{
|
||||
std::string section;
|
||||
};
|
||||
|
||||
struct RestartCameraPayload : BasePayload
|
||||
{
|
||||
bool mode;
|
||||
};
|
||||
#endif
|
||||
@@ -49,3 +49,37 @@ CommandResult updateCameraCommand::execute(std::string_view jsonPayload)
|
||||
|
||||
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 <CameraManager.hpp>
|
||||
|
||||
class updateCameraCommand : public Command
|
||||
{
|
||||
@@ -10,4 +13,15 @@ public:
|
||||
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
|
||||
@@ -10,4 +10,50 @@ CommandResult getConfigCommand::execute(std::string_view jsonPayload)
|
||||
{
|
||||
auto configRepresentation = projectConfig->getTrackerConfig().toRepresentation();
|
||||
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) {};
|
||||
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;
|
||||
};
|
||||
Reference in New Issue
Block a user