Add command to set device mode between auto/uvc/wifi, add config to represent streaming mode, implement restart task, implement restart device command

This commit is contained in:
Lorow
2025-05-21 23:21:56 +02:00
parent b7bae7fb3e
commit cd2791ba6f
10 changed files with 83 additions and 17 deletions

View File

@@ -10,5 +10,5 @@ idf_component_register(
INCLUDE_DIRS
"CommandManager"
"CommandManager/commands"
REQUIRES ProjectConfig cJSON CameraManager
REQUIRES ProjectConfig cJSON CameraManager OpenIrisTasks
)

View File

@@ -4,6 +4,7 @@ std::unordered_map<std::string, CommandType> commandTypeMap = {
{"ping", CommandType::PING},
{"set_wifi", CommandType::SET_WIFI},
{"update_wifi", CommandType::UPDATE_WIFI},
{"set_streaming_mode", CommandType::SET_STREAMING_MODE},
{"update_device", CommandType::UPDATE_DEVICE},
{"delete_network", CommandType::DELETE_NETWORK},
{"update_ap_wifi", CommandType::UPDATE_AP_WIFI},
@@ -22,6 +23,8 @@ std::function<CommandResult()> CommandManager::createCommand(CommandType type, s
{
case CommandType::PING:
return { PingCommand };
case CommandType::SET_STREAMING_MODE:
return [this, json] {return setDeviceModeCommand(this->registry, json); };
case CommandType::SET_WIFI:
return [this, json] { return setWiFiCommand(this->registry, json); };
case CommandType::UPDATE_WIFI:

View File

@@ -25,6 +25,7 @@ enum class CommandType
PING,
SET_WIFI,
UPDATE_DEVICE,
SET_STREAMING_MODE,
UPDATE_WIFI,
DELETE_NETWORK,
UPDATE_AP_WIFI,
@@ -40,7 +41,6 @@ enum class CommandType
class CommandManager
{
private:
std::shared_ptr<DependencyRegistry> registry;
public:

View File

@@ -1,9 +1,7 @@
#ifndef COMMAND_SCHEMA_HPP
#define COMMAND_SCHEMA_HPP
struct BasePayload
{
};
struct BasePayload {};
struct WifiPayload : BasePayload
{
@@ -28,7 +26,6 @@ struct deleteNetworkPayload : BasePayload
std::string networkName;
};
// implement
struct UpdateAPWiFiPayload : BasePayload
{
std::optional<std::string> ssid;
@@ -36,7 +33,7 @@ struct UpdateAPWiFiPayload : BasePayload
std::optional<uint8_t> channel;
};
struct MDNSPayload : public BasePayload
struct MDNSPayload : BasePayload
{
std::string hostname;
};

View File

@@ -29,6 +29,7 @@ CommandResult saveConfigCommand(std::shared_ptr<DependencyRegistry> registry)
projectConfig->save();
return CommandResult::getSuccessResult("Config saved");
}
CommandResult getConfigCommand(std::shared_ptr<DependencyRegistry> registry)
{
std::shared_ptr<ProjectConfig> projectConfig = registry->resolve<ProjectConfig>(DependencyType::project_config);

View File

@@ -1,8 +1,33 @@
#include "device_commands.hpp"
CommandResult restartDeviceCommand()
{
// todo implement this: https://github.com/EyeTrackVR/OpenIris/blob/master/ESP/lib/src/tasks/tasks.cpp
// OpenIrisTasks::ScheduleRestart(2000);
return CommandResult::getSuccessResult("Device restarted");
}
#include <cJSON.h>
#include <ProjectConfig.hpp>
// Implementation inspired by SummerSigh work, initial PR opened in openiris repo, adapted to this rewrite
CommandResult setDeviceModeCommand(std::shared_ptr<DependencyRegistry> registry, std::string_view jsonPayload) {
const auto parsedJson = cJSON_Parse(jsonPayload.data());
if (parsedJson == nullptr) {
return CommandResult::getErrorResult("Invalid payload");
}
const auto modeObject = cJSON_GetObjectItem(parsedJson, "mode");
if (modeObject == nullptr) {
return CommandResult::getErrorResult("Invalid payload - missing mode");
}
const auto mode = modeObject->valueint;
if (mode < 0 || mode > 2) {
return CommandResult::getErrorResult("Invalid payload - unsupported mode");
}
const auto projectConfig = registry->resolve<ProjectConfig>(DependencyType::project_config);
projectConfig->setDeviceMode(static_cast<StreamingMode>(mode));
return CommandResult::getSuccessResult("Device mode set");
}
CommandResult restartDeviceCommand() {
OpenIrisTasks::ScheduleRestart(2000);
return CommandResult::getSuccessResult("Device restarted");
}

View File

@@ -1,3 +1,7 @@
#include "CommandResult.hpp"
#include "OpenIrisTasks.hpp"
#include "DependencyRegistry.hpp"
CommandResult setDeviceModeCommand(std::shared_ptr<DependencyRegistry> registry, std::string_view jsonPayload);
CommandResult restartDeviceCommand();