mirror of
https://github.com/MrUnknownDE/OpenIris-ESPIDF.git
synced 2026-04-21 15:33:44 +02:00
Fix up commands parsing, implement proper json parsing
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
idf_component_register(SRCS "CommandManager/CommandManager.cpp"
|
idf_component_register(SRCS "CommandManager/CommandManager.cpp"
|
||||||
INCLUDE_DIRS "CommandManager"
|
INCLUDE_DIRS "CommandManager"
|
||||||
REQUIRES ProjectConfig
|
REQUIRES ProjectConfig cJSON
|
||||||
)
|
)
|
||||||
@@ -6,16 +6,52 @@ std::unique_ptr<Command> CommandManager::createCommand(CommandType type)
|
|||||||
{
|
{
|
||||||
case CommandType::PING:
|
case CommandType::PING:
|
||||||
return std::make_unique<PingCommand>();
|
return std::make_unique<PingCommand>();
|
||||||
break;
|
case CommandType::SET_WIFI:
|
||||||
case CommandType::UPDATE_WIFI:
|
return std::make_unique<setWiFiCommand>(projectConfig);
|
||||||
return std::make_unique<UpdateWifiCommand>(projectConfig);
|
|
||||||
default:
|
default:
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CommandResult CommandManager::executeFromJson(const std::string *json)
|
CommandResult CommandManager::executeFromJson(std::string *json)
|
||||||
{
|
{
|
||||||
// parse it, get a type of command, grab a command from the map based on type
|
cJSON *parsedJson = cJSON_Parse(json->c_str());
|
||||||
// parse the payload json, call execute on the command and return the result
|
if (parsedJson == nullptr)
|
||||||
|
return CommandResult::getErrorResult("Invalid JSON");
|
||||||
|
|
||||||
|
const cJSON *commandData = nullptr;
|
||||||
|
const cJSON *commands = cJSON_GetObjectItem(parsedJson, "commands");
|
||||||
|
cJSON_ArrayForEach(commandData, commands)
|
||||||
|
{
|
||||||
|
const cJSON *commandTypeString = cJSON_GetObjectItem(commandData, "command");
|
||||||
|
const cJSON *commandPayload = cJSON_GetObjectItem(commandData, "data");
|
||||||
|
|
||||||
|
auto commandType = commandTypeMap.at(std::string(commandTypeString->valuestring));
|
||||||
|
auto command = createCommand(commandType);
|
||||||
|
|
||||||
|
if (command == nullptr)
|
||||||
|
{
|
||||||
|
cJSON_Delete(parsedJson);
|
||||||
|
return CommandResult::getErrorResult("Unknown command");
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string commandPayloadString = std::string(cJSON_Print(commandPayload));
|
||||||
|
cJSON_Delete(parsedJson);
|
||||||
|
return command->execute(commandPayloadString);
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON_Delete(parsedJson);
|
||||||
|
return CommandResult::getErrorResult("Commands missing");
|
||||||
|
}
|
||||||
|
|
||||||
|
CommandResult CommandManager::executeFromType(CommandType type, std::string *json)
|
||||||
|
{
|
||||||
|
auto command = createCommand(type);
|
||||||
|
|
||||||
|
if (command == nullptr)
|
||||||
|
{
|
||||||
|
return CommandResult::getErrorResult("Unknown command");
|
||||||
|
}
|
||||||
|
|
||||||
|
return command->execute(*json);
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
|
#ifndef COMMANDMANAGER_HPP
|
||||||
|
#define COMMANDMANAGER_HPP
|
||||||
|
|
||||||
#include <ProjectConfig.hpp>
|
#include <ProjectConfig.hpp>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -6,8 +9,10 @@
|
|||||||
#include "CommandResult.hpp"
|
#include "CommandResult.hpp"
|
||||||
#include "CommandSchema.hpp"
|
#include "CommandSchema.hpp"
|
||||||
#include "Commands.hpp"
|
#include "Commands.hpp"
|
||||||
|
#include <cJSON.h>
|
||||||
|
|
||||||
const enum CommandType {
|
enum CommandType
|
||||||
|
{
|
||||||
None,
|
None,
|
||||||
PING,
|
PING,
|
||||||
SET_WIFI,
|
SET_WIFI,
|
||||||
@@ -32,5 +37,8 @@ public:
|
|||||||
CommandManager(ProjectConfig &projectConfig) : projectConfig(projectConfig) {};
|
CommandManager(ProjectConfig &projectConfig) : projectConfig(projectConfig) {};
|
||||||
std::unique_ptr<Command> createCommand(CommandType type);
|
std::unique_ptr<Command> createCommand(CommandType type);
|
||||||
|
|
||||||
CommandResult executeFromJson(const std::string *json);
|
CommandResult executeFromJson(std::string *json);
|
||||||
|
CommandResult executeFromType(CommandType type, std::string *json);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
|
#ifndef COMMAND_RESULT
|
||||||
|
#define COMMAND_RESULT
|
||||||
|
|
||||||
class CommandResult
|
class CommandResult
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
@@ -37,3 +40,5 @@ public:
|
|||||||
std::string getSuccessMessage() const { return successMessage.value(); };
|
std::string getSuccessMessage() const { return successMessage.value(); };
|
||||||
std::string getErrorMessage() const { return errorMessage.value(); }
|
std::string getErrorMessage() const { return errorMessage.value(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#ifndef COMMAND_SCHEMA_HPP
|
||||||
|
#define COMMAND_SCHEMA_HPP
|
||||||
class BasePayload
|
class BasePayload
|
||||||
{
|
{
|
||||||
};
|
};
|
||||||
@@ -24,3 +26,5 @@ class deleteNetworkPayload : public BasePayload
|
|||||||
{
|
{
|
||||||
std::optional<std::string> networkName;
|
std::optional<std::string> networkName;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1 +1,16 @@
|
|||||||
#include "Commands.hpp"
|
#include "Commands.hpp"
|
||||||
|
|
||||||
|
CommandResult PingCommand::execute(std::string &jsonPayload)
|
||||||
|
{
|
||||||
|
return CommandResult::getSuccessResult("pong");
|
||||||
|
}
|
||||||
|
|
||||||
|
WifiPayload setConfigCommand::parsePayload(std::string &jsonPayload)
|
||||||
|
{
|
||||||
|
return WifiPayload();
|
||||||
|
}
|
||||||
|
|
||||||
|
CommandResult setConfigCommand::execute(std::string &jsonPayload)
|
||||||
|
{
|
||||||
|
return CommandResult::getSuccessResult("pong");
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#ifndef COMMANDS_HPP
|
||||||
|
#define COMMANDS_HPP
|
||||||
#include <ProjectConfig.hpp>
|
#include <ProjectConfig.hpp>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -8,31 +10,25 @@
|
|||||||
|
|
||||||
class Command
|
class Command
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
virtual ~Command() = default;
|
virtual ~Command() = default;
|
||||||
virtual CommandResult execute(const std::shared_ptr<BasePayload> &payload) = 0;
|
virtual CommandResult execute(std::string &jsonPayload) = 0;
|
||||||
virtual std::shared_ptr<BasePayload> parsePayload(const std::string *json) = 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class PingCommand : public Command
|
class PingCommand : public Command
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CommandResult execute(const std::shared_ptr<BasePayload> &payload);
|
CommandResult execute(std::string &jsonPayload) override;
|
||||||
std::shared_ptr<BasePayload> parsePayload(const std::string *json);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class setConfigCommand : public Command
|
class setWiFiCommand : 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;
|
ProjectConfig &projectConfig;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
UpdateWifiCommand(ProjectConfig &projectConfig) : projectConfig(projectConfig) {};
|
setWiFiCommand(ProjectConfig &projectConfig) : projectConfig(projectConfig) {};
|
||||||
CommandResult execute(const std::shared_ptr<BasePayload> &payload);
|
CommandResult execute(std::string &jsonPayload) override;
|
||||||
|
WifiPayload parsePayload(std::string &jsonPayload);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -84,4 +84,4 @@ public:
|
|||||||
size_t freeEntries();
|
size_t freeEntries();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif PREFERENCES_HPP
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user