Add skeletal implementation of the rest of the commands

This commit is contained in:
Lorow
2024-11-12 00:09:23 +01:00
parent 2e51a9ae42
commit ef94fd7648
7 changed files with 170 additions and 37 deletions
@@ -10,8 +10,19 @@ std::unique_ptr<Command> CommandManager::createCommand(CommandType type)
return std::make_unique<setWiFiCommand>(projectConfig); return std::make_unique<setWiFiCommand>(projectConfig);
case CommandType::UPDATE_WIFI: case CommandType::UPDATE_WIFI:
return std::make_unique<updateWifiCommand>(projectConfig); return std::make_unique<updateWifiCommand>(projectConfig);
case CommandType::UPDATE_AP_WIFI:
return std::make_unique<updateAPWiFiCommand>(projectConfig);
case CommandType::DELETE_NETWORK: case CommandType::DELETE_NETWORK:
return std::make_unique<deleteWifiCommand>(projectConfig); 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: default:
return nullptr; return nullptr;
} }
@@ -18,7 +18,13 @@ enum CommandType
SET_WIFI, SET_WIFI,
UPDATE_WIFI, UPDATE_WIFI,
DELETE_NETWORK, DELETE_NETWORK,
UPDATE_AP_WIFI,
UPDATE_MDNS,
SET_MDNS,
UPDATE_CAMERA,
SAVE_CONFIG, SAVE_CONFIG,
RESET_CONFIG,
RESTART_DEVICE,
}; };
std::unordered_map<std::string, CommandType> commandTypeMap = { std::unordered_map<std::string, CommandType> commandTypeMap = {
@@ -1,12 +1,15 @@
#ifndef COMMAND_SCHEMA_HPP #ifndef COMMAND_SCHEMA_HPP
#define 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 networkName;
std::string ssid; std::string ssid;
std::string password; std::string password;
@@ -14,9 +17,8 @@ public:
uint8_t power; uint8_t power;
}; };
class UpdateWifiPayload : public BasePayload struct UpdateWifiPayload : BasePayload
{ {
public:
std::string networkName; std::string networkName;
std::optional<std::string> ssid; std::optional<std::string> ssid;
std::optional<std::string> password; std::optional<std::string> password;
@@ -24,10 +26,33 @@ public:
std::optional<uint8_t> power; std::optional<uint8_t> power;
}; };
class deleteNetworkPayload : public BasePayload struct deleteNetworkPayload : BasePayload
{ {
public:
std::string networkName; 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 #endif
@@ -66,8 +66,7 @@ CommandResult setWiFiCommand::execute(std::string &jsonPayload)
wifiConfig.password, wifiConfig.password,
wifiConfig.channel, wifiConfig.channel,
wifiConfig.power, wifiConfig.power,
false, true);
false);
return CommandResult::getSuccessResult("Config updated"); 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.password.has_value() ? updatedConfig.password.value() : networkToUpdate->password,
updatedConfig.channel.has_value() ? updatedConfig.channel.value() : networkToUpdate->channel, updatedConfig.channel.has_value() ? updatedConfig.channel.value() : networkToUpdate->channel,
updatedConfig.power.has_value() ? updatedConfig.power.value() : networkToUpdate->power, updatedConfig.power.has_value() ? updatedConfig.power.value() : networkToUpdate->power,
false,
false); false);
} }
else else
return CommandResult::getErrorResult("Requested network does not exist"); 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); 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 #endif
@@ -129,7 +129,6 @@ void ProjectConfig::mdnsConfigSave()
{ {
/* Device Config */ /* Device Config */
putString("hostname", this->config.mdns.hostname.c_str()); putString("hostname", this->config.mdns.hostname.c_str());
putString("service", this->config.mdns.service.c_str());
} }
void ProjectConfig::wifiTxPowerConfigSave() void ProjectConfig::wifiTxPowerConfigSave()
@@ -173,7 +172,6 @@ void ProjectConfig::load()
/* MDNS Config */ /* MDNS Config */
this->config.mdns.hostname = getString("hostname", _mdnsName.c_str()).c_str(); this->config.mdns.hostname = getString("hostname", _mdnsName.c_str()).c_str();
this->config.mdns.service = getString("service").c_str();
/* Wifi TX Power Config */ /* Wifi TX Power Config */
// 11dBm is the default value // 11dBm is the default value
@@ -247,12 +245,10 @@ void ProjectConfig::setDeviceConfig(const std::string &OTALogin,
} }
void ProjectConfig::setMDNSConfig(const std::string &hostname, void ProjectConfig::setMDNSConfig(const std::string &hostname,
const std::string &service,
bool shouldNotify) bool shouldNotify)
{ {
ESP_LOGD(CONFIGURATION_TAG, "Updating MDNS config"); ESP_LOGD(CONFIGURATION_TAG, "Updating MDNS config");
this->config.mdns.hostname.assign(hostname); this->config.mdns.hostname.assign(hostname);
this->config.mdns.service.assign(service);
// TODO turn this on // TODO turn this on
// if (shouldNotify) // if (shouldNotify)
@@ -284,12 +280,8 @@ void ProjectConfig::setWifiConfig(const std::string &networkName,
const std::string &password, const std::string &password,
uint8_t channel, uint8_t channel,
uint8_t power, uint8_t power,
bool adhoc,
bool shouldNotify) bool shouldNotify)
{ {
// we store the ADHOC flag as false because the networks we store in the
// config are the ones we want the esp to connect to, rather than host as AP,
// and here we're just updating them
size_t size = this->config.networks.size(); size_t size = this->config.networks.size();
for (auto it = this->config.networks.begin(); for (auto it = this->config.networks.begin();
@@ -305,7 +297,6 @@ void ProjectConfig::setWifiConfig(const std::string &networkName,
it->password = password; it->password = password;
it->channel = channel; it->channel = channel;
it->power = power; it->power = power;
it->adhoc = false;
if (shouldNotify) if (shouldNotify)
{ {
@@ -398,14 +389,11 @@ void ProjectConfig::setWiFiTxPower(uint8_t power, bool shouldNotify)
void ProjectConfig::setAPWifiConfig(const std::string &ssid, void ProjectConfig::setAPWifiConfig(const std::string &ssid,
const std::string &password, const std::string &password,
uint8_t channel, uint8_t channel,
bool adhoc,
bool shouldNotify) bool shouldNotify)
{ {
this->config.ap_network.ssid.assign(ssid); this->config.ap_network.ssid.assign(ssid);
this->config.ap_network.password.assign(password); this->config.ap_network.password.assign(password);
this->config.ap_network.channel = channel; this->config.ap_network.channel = channel;
this->config.ap_network.adhoc = adhoc;
ESP_LOGD(CONFIGURATION_TAG, "Updating access point config"); ESP_LOGD(CONFIGURATION_TAG, "Updating access point config");
if (shouldNotify) if (shouldNotify)
{ {
@@ -437,8 +425,8 @@ std::string ProjectConfig::DeviceConfig_t::toRepresentation()
std::string ProjectConfig::MDNSConfig_t::toRepresentation() std::string ProjectConfig::MDNSConfig_t::toRepresentation()
{ {
std::string json = Helpers::format_string( std::string json = Helpers::format_string(
"\"mdns_config\": {\"hostname\": \"%s\", \"service\": \"%s\"}", "\"mdns_config\": {\"hostname\": \"%s\"}",
this->hostname.c_str(), this->service.c_str()); this->hostname.c_str());
return json; return json;
} }
@@ -457,9 +445,9 @@ std::string ProjectConfig::WiFiConfig_t::toRepresentation()
std::string json = Helpers::format_string( std::string json = Helpers::format_string(
"{\"name\": \"%s\", \"ssid\": \"%s\", \"password\": \"%s\", " "{\"name\": \"%s\", \"ssid\": \"%s\", \"password\": \"%s\", "
"\"channel\": " "\"channel\": "
"%u, \"power\": %u,\"adhoc\": %s}", "%u, \"power\": %u}",
this->name.c_str(), this->ssid.c_str(), this->password.c_str(), this->name.c_str(), this->ssid.c_str(), this->password.c_str(),
this->channel, this->power, this->adhoc ? "true" : "false"); this->channel, this->power);
return json; return json;
} }
@@ -467,9 +455,8 @@ std::string ProjectConfig::AP_WiFiConfig_t::toRepresentation()
{ {
std::string json = Helpers::format_string( std::string json = Helpers::format_string(
"\"ap_wifi_config\": {\"ssid\": \"%s\", \"password\": \"%s\", " "\"ap_wifi_config\": {\"ssid\": \"%s\", \"password\": \"%s\", "
"\"channel\": %u, \"adhoc\": %s}", "\"channel\": %u}",
this->ssid.c_str(), this->password.c_str(), this->channel, this->ssid.c_str(), this->password.c_str(), this->channel);
this->adhoc ? "true" : "false");
return json; return json;
} }
@@ -39,7 +39,6 @@ public:
struct MDNSConfig_t struct MDNSConfig_t
{ {
std::string hostname; std::string hostname;
std::string service;
std::string toRepresentation(); std::string toRepresentation();
}; };
@@ -67,14 +66,12 @@ public:
ssid(std::move(ssid)), ssid(std::move(ssid)),
password(std::move(password)), password(std::move(password)),
channel(channel), channel(channel),
power(power), power(power) {}
adhoc(adhoc) {}
std::string name; std::string name;
std::string ssid; std::string ssid;
std::string password; std::string password;
uint8_t channel; uint8_t channel;
uint8_t power; uint8_t power;
bool adhoc;
std::string toRepresentation(); std::string toRepresentation();
}; };
@@ -84,7 +81,7 @@ public:
std::string ssid; std::string ssid;
std::string password; std::string password;
uint8_t channel; uint8_t channel;
bool adhoc;
std::string toRepresentation(); std::string toRepresentation();
}; };
@@ -116,7 +113,6 @@ public:
int OTAPort, int OTAPort,
bool shouldNotify); bool shouldNotify);
void setMDNSConfig(const std::string &hostname, void setMDNSConfig(const std::string &hostname,
const std::string &service,
bool shouldNotify); bool shouldNotify);
void setCameraConfig(uint8_t vflip, void setCameraConfig(uint8_t vflip,
uint8_t framesize, uint8_t framesize,
@@ -129,7 +125,6 @@ public:
const std::string &password, const std::string &password,
uint8_t channel, uint8_t channel,
uint8_t power, uint8_t power,
bool adhoc,
bool shouldNotify); bool shouldNotify);
void deleteWifiConfig(const std::string &networkName, bool shouldNotify); void deleteWifiConfig(const std::string &networkName, bool shouldNotify);
@@ -137,7 +132,6 @@ public:
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,
bool adhoc,
bool shouldNotify); bool shouldNotify);
void setWiFiTxPower(uint8_t power, bool shouldNotify); void setWiFiTxPower(uint8_t power, bool shouldNotify);