Implement rudimentary command manager architecture

This commit is contained in:
Lorow
2024-11-06 00:10:01 +01:00
parent a5276f4d1d
commit 89f8d23421
8 changed files with 166 additions and 2 deletions

View File

@@ -0,0 +1,4 @@
idf_component_register(SRCS "CommandManager/CommandManager.cpp"
INCLUDE_DIRS "CommandManager"
REQUIRES ProjectConfig
)

View File

@@ -0,0 +1,21 @@
#include "CommandManager.hpp"
std::unique_ptr<Command> CommandManager::createCommand(CommandType type)
{
switch (type)
{
case CommandType::PING:
return std::make_unique<PingCommand>();
break;
case CommandType::UPDATE_WIFI:
return std::make_unique<UpdateWifiCommand>(projectConfig);
default:
return nullptr;
}
}
CommandResult CommandManager::executeFromJson(const std::string *json)
{
// parse it, get a type of command, grab a command from the map based on type
// parse the payload json, call execute on the command and return the result
}

View File

@@ -0,0 +1,36 @@
#include <ProjectConfig.hpp>
#include <memory>
#include <string>
#include <optional>
#include <unordered_map>
#include "CommandResult.hpp"
#include "CommandSchema.hpp"
#include "Commands.hpp"
const enum CommandType {
None,
PING,
SET_WIFI,
UPDATE_WIFI,
DELETE_NETWORK,
SAVE_CONFIG,
};
std::unordered_map<std::string, CommandType> commandTypeMap = {
{"ping", CommandType::PING},
{"set_wifi", CommandType::SET_WIFI},
{"update_wifi", CommandType::UPDATE_WIFI},
{"delete_network", CommandType::DELETE_NETWORK},
{"save_config", CommandType::SAVE_CONFIG},
};
class CommandManager
{
ProjectConfig &projectConfig;
public:
CommandManager(ProjectConfig &projectConfig) : projectConfig(projectConfig) {};
std::unique_ptr<Command> createCommand(CommandType type);
CommandResult executeFromJson(const std::string *json);
};

View File

@@ -0,0 +1,39 @@
class CommandResult
{
private:
std::optional<std::string> successMessage;
std::optional<std::string> errorMessage;
public:
CommandResult(std::optional<std::string> success_message,
std::optional<std::string> error_message)
{
if (success_message.has_value())
{
this->successMessage =
"{\"message\":\"" + success_message.value() + "\"}";
}
else
this->successMessage = std::nullopt;
if (error_message.has_value())
this->errorMessage = "{\"error\":\"" + error_message.value() + "\"}";
else
this->errorMessage = std::nullopt;
}
bool isSuccess() const { return successMessage.has_value(); }
static CommandResult getSuccessResult(std::string message)
{
return CommandResult(message, std::nullopt);
}
static CommandResult getErrorResult(std::string message)
{
return CommandResult(std::nullopt, message);
}
std::string getSuccessMessage() const { return successMessage.value(); };
std::string getErrorMessage() const { return errorMessage.value(); }
};

View File

@@ -0,0 +1,26 @@
class BasePayload
{
};
class WifiPayload : public BasePayload
{
std::string networkName;
std::string ssid;
std::string password;
uint8_t channel;
uint8_t power;
};
class UpdateWifiPayload : public BasePayload
{
std::optional<std::string> networkName;
std::optional<std::string> ssid;
std::optional<std::string> password;
std::optional<uint8_t> channel;
std::optional<uint8_t> power;
};
class deleteNetworkPayload : public BasePayload
{
std::optional<std::string> networkName;
};

View File

@@ -0,0 +1 @@
#include "Commands.hpp"

View File

@@ -0,0 +1,38 @@
#include <ProjectConfig.hpp>
#include <memory>
#include <string>
#include <optional>
#include <unordered_map>
#include "CommandResult.hpp"
#include "CommandSchema.hpp"
class Command
{
virtual ~Command() = default;
virtual CommandResult execute(const std::shared_ptr<BasePayload> &payload) = 0;
virtual std::shared_ptr<BasePayload> parsePayload(const std::string *json) = 0;
};
class PingCommand : public Command
{
public:
CommandResult execute(const std::shared_ptr<BasePayload> &payload);
std::shared_ptr<BasePayload> parsePayload(const std::string *json);
};
class setConfigCommand : public Command
{
public:
CommandResult execute(const std::shared_ptr<BasePayload> &payload);
std::shared_ptr<BasePayload> parsePayload(const std::string *json);
};
class UpdateWifiCommand : public Command
{
private:
ProjectConfig &projectConfig;
public:
UpdateWifiCommand(ProjectConfig &projectConfig) : projectConfig(projectConfig) {};
CommandResult execute(const std::shared_ptr<BasePayload> &payload);
};

View File

@@ -1,5 +1,4 @@
idf_component_register(SRCS "RestAPI/RestAPI.cpp"
INCLUDE_DIRS "RestAPI"
# REQUIRES CommandManager mongoose
REQUIRES mongoose
REQUIRES CommandManager mongoose
)