Begin commands cleanup

This commit is contained in:
Lorow 2024-08-25 01:16:43 +02:00
parent 04af0160c4
commit a085b6d885
5 changed files with 107 additions and 84 deletions

View File

@ -0,0 +1,19 @@
#include "BaseCommand.hpp"
CommandResult PingCommand::execute() {
return CommandResult::getSuccessResult("pong");
}
CommandResult ToggleStreamCommand::validate() {
if (!data.containsKey("state"))
return CommandResult::getErrorResult("Missing state field");
return CommandResult::getSuccessResult("");
}
CommandResult ToggleStreamCommand::execute() {
this->streamServer.toggleTCPStream(data["state"].as<bool>());
return CommandResult::getSuccessResult("TCP Stream state set to:" +
data["state"].as<std::string>());
}

View File

@ -1,9 +1,11 @@
#ifndef COMMAND_HPP
#define COMMAND_HPP
#include <ArduinoJson.h>
#include <optional>
#include <string>
#include <variant>
#include "data/config/project_config.hpp"
#include "network/stream/streamServer.hpp"
@ -56,54 +58,6 @@ class PingCommand : public ICommand {
CommandResult execute() override;
};
class SetWiFiCommand : public ICommand {
ProjectConfig& projectConfig;
JsonVariant data;
public:
SetWiFiCommand(ProjectConfig& projectConfig, JsonVariant data)
: projectConfig(projectConfig), data(data) {}
CommandResult validate() override;
CommandResult execute() override;
};
class SetMDNSCommand : public ICommand {
ProjectConfig& projectConfig;
JsonVariant data;
public:
SetMDNSCommand(ProjectConfig& projectConfig, JsonVariant data)
: projectConfig(projectConfig), data(data) {}
CommandResult validate() override;
CommandResult execute() override;
};
class SaveConfigCommand : public ICommand {
ProjectConfig& projectConfig;
public:
SaveConfigCommand(ProjectConfig& projectConfig)
: projectConfig(projectConfig) {}
CommandResult validate() override {
return CommandResult::getSuccessResult("");
};
CommandResult execute() override;
};
class SetFPSCommand : public ICommand {
ProjectConfig& projectConfig;
JsonVariant data;
public:
SetFPSCommand(ProjectConfig& projectConfig, JsonVariant data)
: projectConfig(projectConfig), data(data) {}
CommandResult validate() override;
CommandResult execute() override;
};
class ToggleStreamCommand : public ICommand {
StreamServer& streamServer;
JsonVariant data;

View File

@ -2,18 +2,18 @@
#ifndef TASK_MANAGER_HPP
#define TASK_MANAGER_HPP
#include <ArduinoJson.h>
#include <optional>
#include <string>
#include <unordered_map>
#include <variant>
#include <iostream>
#include <iterator>
#include <memory>
#include <optional>
#include <sstream>
#include <string>
#include <unordered_map>
#include <variant>
#include <vector>
#include "data/CommandManager/Command.hpp"
#include "data/CommandManager/BaseCommand.hpp"
#include "data/config/project_config.hpp"
#include "network/stream/streamServer.hpp"
@ -41,10 +41,10 @@ const std::unordered_map<std::string, CommandType> commandMap = {
class CommandManager {
private:
ProjectConfig& projectConfig;
StreamServer& streamServer;
ProjectConfig &projectConfig;
StreamServer &streamServer;
std::string join_strings(std::vector<std::string> const& strings,
std::string join_strings(std::vector<std::string> const &strings,
std::string delim) {
std::stringstream ss;
std::copy(strings.begin(), strings.end(),
@ -52,21 +52,21 @@ class CommandManager {
return ss.str();
}
bool hasDataField(JsonVariant& command);
bool hasDataField(JsonVariant &command);
std::unique_ptr<ICommand> createCommand(CommandType commandType,
JsonVariant& data);
JsonVariant &data);
std::variant<std::unique_ptr<ICommand>, CommandResult>
createCommandFromJsonVariant(JsonVariant& command);
createCommandFromJsonVariant(JsonVariant &command);
CommandType getCommandType(JsonVariant& command);
CommandType getCommandType(JsonVariant &command);
// // TODO rewrite the API
// // TODO add FPS/ Freq / cropping to the API
// // TODO rewrite camera handler to be simpler and easier to change
public:
CommandManager(ProjectConfig& projectConfig, StreamServer& streamServer)
CommandManager(ProjectConfig &projectConfig, StreamServer &streamServer)
: projectConfig(projectConfig), streamServer(streamServer) {};
CommandResult handleSingleCommand(CommandsPayload commandsPayload);

View File

@ -1,8 +1,4 @@
#include "Command.hpp"
CommandResult PingCommand::execute() {
return CommandResult::getSuccessResult("pong");
}
#include "ConfigCommands.hpp"
CommandResult SetWiFiCommand::validate() {
if (!data.containsKey("ssid"))
@ -31,11 +27,20 @@ CommandResult SetMDNSCommand::validate() {
}
CommandResult SetMDNSCommand::execute() {
projectConfig.setMDNSConfig(data["hostname"], "openiristracker", false);
projectConfig.setMDNSConfig(data, false);
return CommandResult::getSuccessResult("MDNS set to:" +
data["hostname"].as<std::string>());
}
CommandResult SetDeviceConfigCommand::validate() {
return CommandResult::getSuccessResult("");
}
CommandResult SetDeviceConfigCommand::execute() {
projectConfig.setDeviceConfig(data, false);
return CommandResult::getSuccessResult("Device config updated.");
}
CommandResult SaveConfigCommand::execute() {
projectConfig.save();
return CommandResult::getSuccessResult("CONFIG SAVED");
@ -49,23 +54,7 @@ CommandResult SetFPSCommand::validate() {
}
CommandResult SetFPSCommand::execute() {
// handle FPS here, poc of the interface:
// auto defaultCameraSettings = projectConfig.getDefaultCameraSettings();
// projectConfig.setCamera(..defaultCameraSettings, data["fps"]);
projectConfig.setCameraConfig("fps", data["fps"]);
return CommandResult::getSuccessResult("FPS set to:" +
data["fps"].as<std::string>());
}
CommandResult ToggleStreamCommand::validate() {
if (!data.containsKey("state"))
return CommandResult::getErrorResult("Missing state field");
return CommandResult::getSuccessResult("");
}
CommandResult ToggleStreamCommand::execute() {
this->streamServer.toggleTCPStream(data["state"].as<bool>());
return CommandResult::getSuccessResult("TCP Stream state set to:" +
data["state"].as<std::string>());
}

View File

@ -0,0 +1,61 @@
#include "data/CommandManager/BaseCommand.hpp"
// todo: make use of the update
class SetWiFiCommand : public ICommand {
ProjectConfig& projectConfig;
JsonVariant data;
public:
SetWiFiCommand(ProjectConfig& projectConfig, JsonVariant data)
: projectConfig(projectConfig), data(data) {}
CommandResult validate() override;
CommandResult execute() override;
};
class SetMDNSCommand : public ICommand {
ProjectConfig& projectConfig;
JsonVariant data;
public:
SetMDNSCommand(ProjectConfig& projectConfig, JsonVariant data)
: projectConfig(projectConfig), data(data) {}
CommandResult validate() override;
CommandResult execute() override;
};
class SetDeviceConfigCommand : public ICommand {
ProjectConfig& projectConfig;
JsonVariant data;
public:
SetDeviceConfigCommand(ProjectConfig& projectConfig, JsonVariant data)
: projectConfig(projectConfig), data(data) {}
CommandResult validate() override;
CommandResult execute() override;
};
class SaveConfigCommand : public ICommand {
ProjectConfig& projectConfig;
public:
SaveConfigCommand(ProjectConfig& projectConfig)
: projectConfig(projectConfig) {}
CommandResult validate() override {
return CommandResult::getSuccessResult("");
};
CommandResult execute() override;
};
class SetFPSCommand : public ICommand {
ProjectConfig& projectConfig;
JsonVariant data;
public:
SetFPSCommand(ProjectConfig& projectConfig, JsonVariant data)
: projectConfig(projectConfig), data(data) {}
CommandResult validate() override;
CommandResult execute() override;
};