mirror of
https://github.com/MrUnknownDE/OpenIris-ESPIDF.git
synced 2026-04-19 22:43:45 +02:00
Add skeletal implementation of the rest of the commands
This commit is contained in:
@@ -10,8 +10,19 @@ std::unique_ptr<Command> CommandManager::createCommand(CommandType type)
|
||||
return std::make_unique<setWiFiCommand>(projectConfig);
|
||||
case CommandType::UPDATE_WIFI:
|
||||
return std::make_unique<updateWifiCommand>(projectConfig);
|
||||
case CommandType::UPDATE_AP_WIFI:
|
||||
return std::make_unique<updateAPWiFiCommand>(projectConfig);
|
||||
case CommandType::DELETE_NETWORK:
|
||||
return std::make_unique<deleteWifiCommand>(projectConfig);
|
||||
case CommandType::SET_MDNS:
|
||||
return std::make_unique<setMDNSCommand>(projectConfig);
|
||||
// updating the mnds name is essentially the same operation
|
||||
case CommandType::UPDATE_MDNS:
|
||||
return std::make_unique<setMDNSCommand>(projectConfig);
|
||||
case CommandType::UPDATE_CAMERA:
|
||||
return std::make_unique<updateCameraCommand>(projectConfig);
|
||||
case CommandType::SAVE_CONFIG:
|
||||
return std::make_unique<saveConfigCommand>(projectConfig);
|
||||
default:
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -18,7 +18,13 @@ enum CommandType
|
||||
SET_WIFI,
|
||||
UPDATE_WIFI,
|
||||
DELETE_NETWORK,
|
||||
UPDATE_AP_WIFI,
|
||||
UPDATE_MDNS,
|
||||
SET_MDNS,
|
||||
UPDATE_CAMERA,
|
||||
SAVE_CONFIG,
|
||||
RESET_CONFIG,
|
||||
RESTART_DEVICE,
|
||||
};
|
||||
|
||||
std::unordered_map<std::string, CommandType> commandTypeMap = {
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
#ifndef COMMAND_SCHEMA_HPP
|
||||
#define COMMAND_SCHEMA_HPP
|
||||
class BasePayload
|
||||
|
||||
// this generally should be merged with ProjectConfig definitions
|
||||
// and moved into a separate component
|
||||
// and used as more or less models
|
||||
struct BasePayload
|
||||
{
|
||||
};
|
||||
|
||||
class WifiPayload : public BasePayload
|
||||
struct WifiPayload : BasePayload
|
||||
{
|
||||
public:
|
||||
std::string networkName;
|
||||
std::string ssid;
|
||||
std::string password;
|
||||
@@ -14,9 +17,8 @@ public:
|
||||
uint8_t power;
|
||||
};
|
||||
|
||||
class UpdateWifiPayload : public BasePayload
|
||||
struct UpdateWifiPayload : BasePayload
|
||||
{
|
||||
public:
|
||||
std::string networkName;
|
||||
std::optional<std::string> ssid;
|
||||
std::optional<std::string> password;
|
||||
@@ -24,10 +26,33 @@ public:
|
||||
std::optional<uint8_t> power;
|
||||
};
|
||||
|
||||
class deleteNetworkPayload : public BasePayload
|
||||
struct deleteNetworkPayload : BasePayload
|
||||
{
|
||||
public:
|
||||
std::string networkName;
|
||||
};
|
||||
|
||||
// implement
|
||||
struct UpdateAPWiFiPayload : BasePayload
|
||||
{
|
||||
std::string ssid;
|
||||
std::string password;
|
||||
uint8_t channel;
|
||||
};
|
||||
|
||||
struct MDNSPayload : public BasePayload
|
||||
{
|
||||
std::string hostname;
|
||||
};
|
||||
|
||||
// todo implement
|
||||
struct UpdateCameraConfigPayload : BasePayload
|
||||
{
|
||||
uint8_t vflip;
|
||||
uint8_t href;
|
||||
uint8_t framesize;
|
||||
uint8_t quality;
|
||||
uint8_t brightness;
|
||||
// todo add more options here
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -66,8 +66,7 @@ CommandResult setWiFiCommand::execute(std::string &jsonPayload)
|
||||
wifiConfig.password,
|
||||
wifiConfig.channel,
|
||||
wifiConfig.power,
|
||||
false,
|
||||
false);
|
||||
true);
|
||||
|
||||
return CommandResult::getSuccessResult("Config updated");
|
||||
}
|
||||
@@ -168,9 +167,79 @@ CommandResult updateWifiCommand::execute(std::string &jsonPayload)
|
||||
updatedConfig.password.has_value() ? updatedConfig.password.value() : networkToUpdate->password,
|
||||
updatedConfig.channel.has_value() ? updatedConfig.channel.value() : networkToUpdate->channel,
|
||||
updatedConfig.power.has_value() ? updatedConfig.power.value() : networkToUpdate->power,
|
||||
false,
|
||||
false);
|
||||
}
|
||||
else
|
||||
return CommandResult::getErrorResult("Requested network does not exist");
|
||||
}
|
||||
|
||||
std::optional<UpdateAPWiFiPayload> updateAPWiFiCommand::parsePayload(std::string &jsonPayload)
|
||||
{
|
||||
UpdateAPWiFiPayload payload;
|
||||
cJSON *parsedJson = cJSON_Parse(jsonPayload.c_str());
|
||||
|
||||
// todo implement parsing
|
||||
|
||||
cJSON_Delete(parsedJson);
|
||||
return payload;
|
||||
}
|
||||
|
||||
CommandResult updateAPWiFiCommand::execute(std::string &jsonPayload)
|
||||
{
|
||||
auto payload = parsePayload(jsonPayload);
|
||||
// todo implement updating
|
||||
return CommandResult::getSuccessResult("Config updated");
|
||||
}
|
||||
|
||||
std::optional<MDNSPayload> setMDNSCommand::parsePayload(std::string &jsonPayload)
|
||||
{
|
||||
MDNSPayload payload;
|
||||
cJSON *parsedJson = cJSON_Parse(jsonPayload.c_str());
|
||||
if (parsedJson == nullptr)
|
||||
return std::nullopt;
|
||||
|
||||
cJSON *hostnameObject = cJSON_GetObjectItem(parsedJson, "hostname");
|
||||
|
||||
if (hostnameObject == nullptr)
|
||||
{
|
||||
cJSON_Delete(parsedJson);
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
payload.hostname = std::string(hostnameObject->valuestring);
|
||||
cJSON_Delete(parsedJson);
|
||||
return payload;
|
||||
}
|
||||
|
||||
CommandResult setMDNSCommand::execute(std::string &jsonPayload)
|
||||
{
|
||||
auto payload = parsePayload(jsonPayload);
|
||||
if (!payload.has_value())
|
||||
return CommandResult::getErrorResult("Invalid payload");
|
||||
|
||||
projectConfig.setMDNSConfig(payload.value().hostname, false);
|
||||
|
||||
return CommandResult::getSuccessResult("Config updated");
|
||||
}
|
||||
|
||||
std::optional<UpdateCameraConfigPayload> updateCameraCommand::parsePayload(std::string &jsonPayload)
|
||||
{
|
||||
UpdateCameraConfigPayload payload;
|
||||
cJSON *parsedJson = cJSON_Parse(jsonPayload.c_str());
|
||||
|
||||
cJSON_Delete(parsedJson);
|
||||
return payload;
|
||||
}
|
||||
|
||||
CommandResult updateCameraCommand::execute(std::string &jsonPayload)
|
||||
{
|
||||
auto payload = parsePayload(jsonPayload);
|
||||
// todo implement updating
|
||||
return CommandResult::getSuccessResult("Config updated");
|
||||
}
|
||||
|
||||
CommandResult saveConfigCommand::execute(std::string &jsonPayload)
|
||||
{
|
||||
projectConfig.save();
|
||||
return CommandResult::getSuccessResult("Config saved");
|
||||
}
|
||||
@@ -52,4 +52,45 @@ public:
|
||||
std::optional<UpdateWifiPayload> parsePayload(std::string &jsonPayload);
|
||||
};
|
||||
|
||||
class updateAPWiFiCommand : public Command
|
||||
{
|
||||
ProjectConfig &projectConfig;
|
||||
|
||||
public:
|
||||
updateAPWiFiCommand(ProjectConfig &projectConfig) : projectConfig(projectConfig) {};
|
||||
CommandResult execute(std::string &jsonPayload) override;
|
||||
std::optional<UpdateAPWiFiPayload> parsePayload(std::string &jsonPayload);
|
||||
};
|
||||
|
||||
class setMDNSCommand : public Command
|
||||
{
|
||||
ProjectConfig &projectConfig;
|
||||
|
||||
public:
|
||||
setMDNSCommand(ProjectConfig &projectConfig) : projectConfig(projectConfig) {};
|
||||
CommandResult execute(std::string &jsonPayload) override;
|
||||
std::optional<MDNSPayload> parsePayload(std::string &jsonPayload);
|
||||
};
|
||||
|
||||
class updateCameraCommand : public Command
|
||||
{
|
||||
ProjectConfig &projectConfig;
|
||||
|
||||
public:
|
||||
updateCameraCommand(ProjectConfig &projectConfig) : projectConfig(projectConfig) {};
|
||||
CommandResult execute(std::string &jsonPayload) override;
|
||||
std::optional<UpdateCameraConfigPayload> parsePayload(std::string &jsonPayload);
|
||||
};
|
||||
|
||||
class saveConfigCommand : public Command
|
||||
{
|
||||
ProjectConfig &projectConfig;
|
||||
saveConfigCommand(ProjectConfig &projectConfig) : projectConfig(projectConfig) {};
|
||||
CommandResult execute(std::string &jsonPayload) override;
|
||||
};
|
||||
|
||||
// mostlikely missing commands
|
||||
// reset config
|
||||
// restart device
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user