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
+1 -1
View File
@@ -10,5 +10,5 @@ idf_component_register(
INCLUDE_DIRS INCLUDE_DIRS
"CommandManager" "CommandManager"
"CommandManager/commands" "CommandManager/commands"
REQUIRES ProjectConfig cJSON CameraManager REQUIRES ProjectConfig cJSON CameraManager OpenIrisTasks
) )
@@ -4,6 +4,7 @@ std::unordered_map<std::string, CommandType> commandTypeMap = {
{"ping", CommandType::PING}, {"ping", CommandType::PING},
{"set_wifi", CommandType::SET_WIFI}, {"set_wifi", CommandType::SET_WIFI},
{"update_wifi", CommandType::UPDATE_WIFI}, {"update_wifi", CommandType::UPDATE_WIFI},
{"set_streaming_mode", CommandType::SET_STREAMING_MODE},
{"update_device", CommandType::UPDATE_DEVICE}, {"update_device", CommandType::UPDATE_DEVICE},
{"delete_network", CommandType::DELETE_NETWORK}, {"delete_network", CommandType::DELETE_NETWORK},
{"update_ap_wifi", CommandType::UPDATE_AP_WIFI}, {"update_ap_wifi", CommandType::UPDATE_AP_WIFI},
@@ -22,6 +23,8 @@ std::function<CommandResult()> CommandManager::createCommand(CommandType type, s
{ {
case CommandType::PING: case CommandType::PING:
return { PingCommand }; return { PingCommand };
case CommandType::SET_STREAMING_MODE:
return [this, json] {return setDeviceModeCommand(this->registry, json); };
case CommandType::SET_WIFI: case CommandType::SET_WIFI:
return [this, json] { return setWiFiCommand(this->registry, json); }; return [this, json] { return setWiFiCommand(this->registry, json); };
case CommandType::UPDATE_WIFI: case CommandType::UPDATE_WIFI:
@@ -25,6 +25,7 @@ enum class CommandType
PING, PING,
SET_WIFI, SET_WIFI,
UPDATE_DEVICE, UPDATE_DEVICE,
SET_STREAMING_MODE,
UPDATE_WIFI, UPDATE_WIFI,
DELETE_NETWORK, DELETE_NETWORK,
UPDATE_AP_WIFI, UPDATE_AP_WIFI,
@@ -40,7 +41,6 @@ enum class CommandType
class CommandManager class CommandManager
{ {
private:
std::shared_ptr<DependencyRegistry> registry; std::shared_ptr<DependencyRegistry> registry;
public: public:
@@ -1,9 +1,7 @@
#ifndef COMMAND_SCHEMA_HPP #ifndef COMMAND_SCHEMA_HPP
#define COMMAND_SCHEMA_HPP #define COMMAND_SCHEMA_HPP
struct BasePayload struct BasePayload {};
{
};
struct WifiPayload : BasePayload struct WifiPayload : BasePayload
{ {
@@ -28,7 +26,6 @@ struct deleteNetworkPayload : BasePayload
std::string networkName; std::string networkName;
}; };
// implement
struct UpdateAPWiFiPayload : BasePayload struct UpdateAPWiFiPayload : BasePayload
{ {
std::optional<std::string> ssid; std::optional<std::string> ssid;
@@ -36,7 +33,7 @@ struct UpdateAPWiFiPayload : BasePayload
std::optional<uint8_t> channel; std::optional<uint8_t> channel;
}; };
struct MDNSPayload : public BasePayload struct MDNSPayload : BasePayload
{ {
std::string hostname; std::string hostname;
}; };
@@ -29,6 +29,7 @@ CommandResult saveConfigCommand(std::shared_ptr<DependencyRegistry> registry)
projectConfig->save(); projectConfig->save();
return CommandResult::getSuccessResult("Config saved"); return CommandResult::getSuccessResult("Config saved");
} }
CommandResult getConfigCommand(std::shared_ptr<DependencyRegistry> registry) CommandResult getConfigCommand(std::shared_ptr<DependencyRegistry> registry)
{ {
std::shared_ptr<ProjectConfig> projectConfig = registry->resolve<ProjectConfig>(DependencyType::project_config); std::shared_ptr<ProjectConfig> projectConfig = registry->resolve<ProjectConfig>(DependencyType::project_config);
@@ -1,8 +1,33 @@
#include "device_commands.hpp" #include "device_commands.hpp"
CommandResult restartDeviceCommand() #include <cJSON.h>
{ #include <ProjectConfig.hpp>
// todo implement this: https://github.com/EyeTrackVR/OpenIris/blob/master/ESP/lib/src/tasks/tasks.cpp
// OpenIrisTasks::ScheduleRestart(2000); // 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"); return CommandResult::getSuccessResult("Device restarted");
} }
@@ -1,3 +1,7 @@
#include "CommandResult.hpp" #include "CommandResult.hpp"
#include "OpenIrisTasks.hpp"
#include "DependencyRegistry.hpp"
CommandResult setDeviceModeCommand(std::shared_ptr<DependencyRegistry> registry, std::string_view jsonPayload);
CommandResult restartDeviceCommand(); CommandResult restartDeviceCommand();
@@ -1,6 +1,6 @@
#pragma once #pragma once
#ifndef _PROJECT_CONFIG_MODELS_HPP_ #ifndef PROJECT_CONFIG_MODELS_HPP
#define _PROJECT_CONFIG_MODELS_HPP_ #define PROJECT_CONFIG_MODELS_HPP
#include <string> #include <string>
#include <utility> #include <utility>
@@ -20,6 +20,26 @@ struct BaseConfigModel
Preferences *pref; Preferences *pref;
}; };
enum class StreamingMode {
AUTO,
UVC,
WIFI,
};
struct DeviceMode_t : BaseConfigModel {
StreamingMode mode;
explicit DeviceMode_t( Preferences *pref) : BaseConfigModel(pref), mode(StreamingMode::AUTO){}
void load() {
this->mode = static_cast<StreamingMode>(this->pref->getInt("mode", 0));
}
void save() const {
this->pref->putInt("mode", static_cast<int>(this->mode));
}
};
struct DeviceConfig_t : BaseConfigModel struct DeviceConfig_t : BaseConfigModel
{ {
DeviceConfig_t(Preferences *pref) : BaseConfigModel(pref) {} DeviceConfig_t(Preferences *pref) : BaseConfigModel(pref) {}
@@ -238,6 +258,7 @@ class TrackerConfig_t
{ {
public: public:
DeviceConfig_t device; DeviceConfig_t device;
DeviceMode_t device_mode;
CameraConfig_t camera; CameraConfig_t camera;
std::vector<WiFiConfig_t> networks; std::vector<WiFiConfig_t> networks;
AP_WiFiConfig_t ap_network; AP_WiFiConfig_t ap_network;
@@ -246,16 +267,18 @@ public:
TrackerConfig_t( TrackerConfig_t(
DeviceConfig_t device, DeviceConfig_t device,
DeviceMode_t device_mode,
CameraConfig_t camera, CameraConfig_t camera,
std::vector<WiFiConfig_t> networks, std::vector<WiFiConfig_t> networks,
AP_WiFiConfig_t ap_network, AP_WiFiConfig_t ap_network,
MDNSConfig_t mdns, MDNSConfig_t mdns,
WiFiTxPower_t txpower) : device(std::move(device)), WiFiTxPower_t txpower) : device(std::move(device)),
camera(camera), device_mode(std::move(device_mode)),
camera(std::move(camera)),
networks(std::move(networks)), networks(std::move(networks)),
ap_network(std::move(ap_network)), ap_network(std::move(ap_network)),
mdns(std::move(mdns)), mdns(std::move(mdns)),
txpower(txpower) {} txpower(std::move(txpower)) {}
std::string toRepresentation() std::string toRepresentation()
{ {
@@ -15,6 +15,7 @@ void saveNetworkCount(Preferences *pref, const int count)
ProjectConfig::ProjectConfig(Preferences *pref) : pref(pref), ProjectConfig::ProjectConfig(Preferences *pref) : pref(pref),
_already_loaded(false), _already_loaded(false),
config(DeviceConfig_t(pref), config(DeviceConfig_t(pref),
DeviceMode_t(pref),
CameraConfig_t(pref), CameraConfig_t(pref),
std::vector<WiFiConfig_t>{}, std::vector<WiFiConfig_t>{},
AP_WiFiConfig_t(pref), AP_WiFiConfig_t(pref),
@@ -26,6 +27,7 @@ ProjectConfig::~ProjectConfig() = default;
void ProjectConfig::save() const { void ProjectConfig::save() const {
ESP_LOGD(CONFIGURATION_TAG, "Saving project config"); ESP_LOGD(CONFIGURATION_TAG, "Saving project config");
this->config.device.save(); this->config.device.save();
this->config.device_mode.save();
this->config.camera.save(); this->config.camera.save();
this->config.mdns.save(); this->config.mdns.save();
this->config.txpower.save(); this->config.txpower.save();
@@ -61,6 +63,7 @@ void ProjectConfig::load()
ESP_LOGI(CONFIGURATION_TAG, "Config loaded: %s", success ? "true" : "false"); ESP_LOGI(CONFIGURATION_TAG, "Config loaded: %s", success ? "true" : "false");
this->config.device.load(); this->config.device.load();
this->config.device_mode.load();
this->config.camera.load(); this->config.camera.load();
this->config.mdns.load(); this->config.mdns.load();
this->config.txpower.load(); this->config.txpower.load();
@@ -207,6 +210,10 @@ void ProjectConfig::setAPWifiConfig(const std::string &ssid,
ESP_LOGD(CONFIGURATION_TAG, "Updating access point config"); ESP_LOGD(CONFIGURATION_TAG, "Updating access point config");
} }
void ProjectConfig::setDeviceMode(const StreamingMode deviceMode) {
this->config.device_mode.mode = deviceMode;
}
//********************************************************************************************************************** //**********************************************************************************************************************
//* //*
//! Get Methods //! Get Methods
@@ -241,3 +248,7 @@ TrackerConfig_t &ProjectConfig::getTrackerConfig()
{ {
return this->config; return this->config;
} }
DeviceMode_t &ProjectConfig::getDeviceMode() {
return this->config.device_mode;
}
@@ -31,6 +31,7 @@ public:
bool reset(); bool reset();
DeviceConfig_t &getDeviceConfig(); DeviceConfig_t &getDeviceConfig();
DeviceMode_t &getDeviceMode();
CameraConfig_t &getCameraConfig(); CameraConfig_t &getCameraConfig();
std::vector<WiFiConfig_t> &getWifiConfigs(); std::vector<WiFiConfig_t> &getWifiConfigs();
AP_WiFiConfig_t &getAPWifiConfig(); AP_WiFiConfig_t &getAPWifiConfig();
@@ -59,6 +60,7 @@ public:
const std::string &password, const std::string &password,
uint8_t channel); uint8_t channel);
void setWiFiTxPower(uint8_t power); void setWiFiTxPower(uint8_t power);
void setDeviceMode(StreamingMode deviceMode);
private: private:
Preferences *pref; Preferences *pref;