mirror of
https://github.com/MrUnknownDE/OpenIris-ESPIDF.git
synced 2026-04-23 08:13:44 +02:00
Add RESTART_CAMERA, RESET_CONFIG, RESTART_DEVICE commands and implement related endpoints
This commit is contained in:
@@ -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