mirror of
https://github.com/MrUnknownDE/OpenIris-ESPIDF.git
synced 2026-04-18 05:53:44 +02:00
Implement rudimentary command manager architecture
This commit is contained in:
4
components/CommandManager/CMakeLists.txt
Normal file
4
components/CommandManager/CMakeLists.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
idf_component_register(SRCS "CommandManager/CommandManager.cpp"
|
||||
INCLUDE_DIRS "CommandManager"
|
||||
REQUIRES ProjectConfig
|
||||
)
|
||||
21
components/CommandManager/CommandManager/CommandManager.cpp
Normal file
21
components/CommandManager/CommandManager/CommandManager.cpp
Normal 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
|
||||
}
|
||||
36
components/CommandManager/CommandManager/CommandManager.hpp
Normal file
36
components/CommandManager/CommandManager/CommandManager.hpp
Normal 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);
|
||||
};
|
||||
39
components/CommandManager/CommandManager/CommandResult.hpp
Normal file
39
components/CommandManager/CommandManager/CommandResult.hpp
Normal 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(); }
|
||||
};
|
||||
26
components/CommandManager/CommandManager/CommandSchema.hpp
Normal file
26
components/CommandManager/CommandManager/CommandSchema.hpp
Normal 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;
|
||||
};
|
||||
1
components/CommandManager/CommandManager/Commands.cpp
Normal file
1
components/CommandManager/CommandManager/Commands.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#include "Commands.hpp"
|
||||
38
components/CommandManager/CommandManager/Commands.hpp
Normal file
38
components/CommandManager/CommandManager/Commands.hpp
Normal 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);
|
||||
};
|
||||
@@ -1,5 +1,4 @@
|
||||
idf_component_register(SRCS "RestAPI/RestAPI.cpp"
|
||||
INCLUDE_DIRS "RestAPI"
|
||||
# REQUIRES CommandManager mongoose
|
||||
REQUIRES mongoose
|
||||
REQUIRES CommandManager mongoose
|
||||
)
|
||||
Reference in New Issue
Block a user