mirror of
https://github.com/MrUnknownDE/OpenIris-ESPIDF.git
synced 2026-05-05 13:36:04 +02:00
Implement some commands
This commit is contained in:
@@ -8,6 +8,8 @@ std::unique_ptr<Command> CommandManager::createCommand(CommandType type)
|
|||||||
return std::make_unique<PingCommand>();
|
return std::make_unique<PingCommand>();
|
||||||
case CommandType::SET_WIFI:
|
case CommandType::SET_WIFI:
|
||||||
return std::make_unique<setWiFiCommand>(projectConfig);
|
return std::make_unique<setWiFiCommand>(projectConfig);
|
||||||
|
case CommandType::DELETE_NETWORK:
|
||||||
|
return std::make_unique<deleteWifiCommand>(projectConfig);
|
||||||
default:
|
default:
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ class BasePayload
|
|||||||
|
|
||||||
class WifiPayload : public BasePayload
|
class WifiPayload : public BasePayload
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
std::string networkName;
|
std::string networkName;
|
||||||
std::string ssid;
|
std::string ssid;
|
||||||
std::string password;
|
std::string password;
|
||||||
@@ -15,6 +16,7 @@ class WifiPayload : public BasePayload
|
|||||||
|
|
||||||
class UpdateWifiPayload : public BasePayload
|
class UpdateWifiPayload : public BasePayload
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
std::optional<std::string> networkName;
|
std::optional<std::string> networkName;
|
||||||
std::optional<std::string> ssid;
|
std::optional<std::string> ssid;
|
||||||
std::optional<std::string> password;
|
std::optional<std::string> password;
|
||||||
@@ -24,7 +26,8 @@ class UpdateWifiPayload : public BasePayload
|
|||||||
|
|
||||||
class deleteNetworkPayload : public BasePayload
|
class deleteNetworkPayload : public BasePayload
|
||||||
{
|
{
|
||||||
std::optional<std::string> networkName;
|
public:
|
||||||
|
std::string networkName;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -5,12 +5,100 @@ CommandResult PingCommand::execute(std::string &jsonPayload)
|
|||||||
return CommandResult::getSuccessResult("pong");
|
return CommandResult::getSuccessResult("pong");
|
||||||
}
|
}
|
||||||
|
|
||||||
WifiPayload setConfigCommand::parsePayload(std::string &jsonPayload)
|
std::optional<WifiPayload> setWiFiCommand::parsePayload(std::string &jsonPayload)
|
||||||
{
|
{
|
||||||
return WifiPayload();
|
WifiPayload payload;
|
||||||
|
cJSON *parsedJson = cJSON_Parse(jsonPayload.c_str());
|
||||||
|
|
||||||
|
if (parsedJson == nullptr)
|
||||||
|
return std::nullopt;
|
||||||
|
|
||||||
|
cJSON *networkName = cJSON_GetObjectItem(parsedJson, "name");
|
||||||
|
cJSON *ssidPtr = cJSON_GetObjectItem(parsedJson, "ssid");
|
||||||
|
cJSON *passwordPtr = cJSON_GetObjectItem(parsedJson, "password");
|
||||||
|
cJSON *channelPtr = cJSON_GetObjectItem(parsedJson, "channel");
|
||||||
|
cJSON *powerPtr = cJSON_GetObjectItem(parsedJson, "power");
|
||||||
|
|
||||||
|
if (ssidPtr == nullptr)
|
||||||
|
{
|
||||||
|
// without an SSID we can't do anything
|
||||||
|
cJSON_Delete(parsedJson);
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (networkName == nullptr)
|
||||||
|
payload.networkName = "main";
|
||||||
|
else
|
||||||
|
payload.networkName = std::string(networkName->valuestring);
|
||||||
|
|
||||||
|
payload.ssid = std::string(ssidPtr->valuestring);
|
||||||
|
|
||||||
|
if (passwordPtr == nullptr)
|
||||||
|
payload.password = "";
|
||||||
|
else
|
||||||
|
payload.password = std::string(passwordPtr->valuestring);
|
||||||
|
|
||||||
|
if (channelPtr == nullptr)
|
||||||
|
payload.channel = 0;
|
||||||
|
else
|
||||||
|
payload.channel = channelPtr->valueint;
|
||||||
|
|
||||||
|
if (powerPtr == nullptr)
|
||||||
|
payload.power = 0;
|
||||||
|
else
|
||||||
|
payload.power = powerPtr->valueint;
|
||||||
|
|
||||||
|
cJSON_Delete(parsedJson);
|
||||||
|
return payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
CommandResult setConfigCommand::execute(std::string &jsonPayload)
|
CommandResult setWiFiCommand::execute(std::string &jsonPayload)
|
||||||
{
|
{
|
||||||
return CommandResult::getSuccessResult("pong");
|
auto payload = parsePayload(jsonPayload);
|
||||||
|
if (!payload.has_value())
|
||||||
|
{
|
||||||
|
return CommandResult::getErrorResult("Invalid payload");
|
||||||
|
}
|
||||||
|
auto wifiConfig = payload.value();
|
||||||
|
projectConfig.setWifiConfig(
|
||||||
|
wifiConfig.networkName,
|
||||||
|
wifiConfig.ssid,
|
||||||
|
wifiConfig.password,
|
||||||
|
wifiConfig.channel,
|
||||||
|
wifiConfig.power,
|
||||||
|
false,
|
||||||
|
false);
|
||||||
|
|
||||||
|
return CommandResult::getSuccessResult("Config updated");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<deleteNetworkPayload> deleteWifiCommand::parsePayload(std::string &jsonPayload)
|
||||||
|
{
|
||||||
|
deleteNetworkPayload payload;
|
||||||
|
cJSON *parsedJson = cJSON_Parse(jsonPayload.c_str());
|
||||||
|
|
||||||
|
if (parsedJson == nullptr)
|
||||||
|
return std::nullopt;
|
||||||
|
|
||||||
|
cJSON *networkName = cJSON_GetObjectItem(parsedJson, "name");
|
||||||
|
if (networkName == nullptr)
|
||||||
|
{
|
||||||
|
cJSON_Delete(parsedJson);
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
payload.networkName = std::string(networkName->valuestring);
|
||||||
|
|
||||||
|
cJSON_Delete(parsedJson);
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
CommandResult deleteWifiCommand::execute(std::string &jsonPayload)
|
||||||
|
{
|
||||||
|
auto payload = parsePayload(jsonPayload);
|
||||||
|
if (!payload.has_value())
|
||||||
|
return CommandResult::getErrorResult("Invalid payload");
|
||||||
|
|
||||||
|
projectConfig.deleteWifiConfig(payload.value().networkName, false);
|
||||||
|
return CommandResult::getSuccessResult("Config updated");
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#include <cJSON.h>
|
||||||
#include "CommandResult.hpp"
|
#include "CommandResult.hpp"
|
||||||
#include "CommandSchema.hpp"
|
#include "CommandSchema.hpp"
|
||||||
|
|
||||||
@@ -28,7 +29,17 @@ class setWiFiCommand : public Command
|
|||||||
public:
|
public:
|
||||||
setWiFiCommand(ProjectConfig &projectConfig) : projectConfig(projectConfig) {};
|
setWiFiCommand(ProjectConfig &projectConfig) : projectConfig(projectConfig) {};
|
||||||
CommandResult execute(std::string &jsonPayload) override;
|
CommandResult execute(std::string &jsonPayload) override;
|
||||||
WifiPayload parsePayload(std::string &jsonPayload);
|
std::optional<WifiPayload> parsePayload(std::string &jsonPayload);
|
||||||
|
};
|
||||||
|
|
||||||
|
class deleteWifiCommand : public Command
|
||||||
|
{
|
||||||
|
ProjectConfig &projectConfig;
|
||||||
|
|
||||||
|
public:
|
||||||
|
deleteWifiCommand(ProjectConfig &projectConfig) : projectConfig(projectConfig) {};
|
||||||
|
CommandResult execute(std::string &jsonPayload) override;
|
||||||
|
std::optional<deleteNetworkPayload> parsePayload(std::string &jsonPayload);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -131,6 +131,9 @@ public:
|
|||||||
uint8_t power,
|
uint8_t power,
|
||||||
bool adhoc,
|
bool adhoc,
|
||||||
bool shouldNotify);
|
bool shouldNotify);
|
||||||
|
|
||||||
|
void deleteWifiConfig(const std::string &networkName, bool shouldNotify);
|
||||||
|
|
||||||
void setAPWifiConfig(const std::string &ssid,
|
void setAPWifiConfig(const std::string &ssid,
|
||||||
const std::string &password,
|
const std::string &password,
|
||||||
uint8_t channel,
|
uint8_t channel,
|
||||||
@@ -138,8 +141,6 @@ public:
|
|||||||
bool shouldNotify);
|
bool shouldNotify);
|
||||||
void setWiFiTxPower(uint8_t power, bool shouldNotify);
|
void setWiFiTxPower(uint8_t power, bool shouldNotify);
|
||||||
|
|
||||||
void deleteWifiConfig(const std::string &networkName, bool shouldNotify);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TrackerConfig_t config;
|
TrackerConfig_t config;
|
||||||
std::string _name;
|
std::string _name;
|
||||||
|
|||||||
Reference in New Issue
Block a user