Modernize the code a bit, clean up most compilation warnings

This commit is contained in:
Lorow
2025-05-18 17:33:05 +02:00
parent 0635bbd5c2
commit 4f0ab541cb
26 changed files with 329 additions and 383 deletions

View File

@@ -17,45 +17,42 @@ std::unordered_map<std::string, CommandType> commandTypeMap = {
{"restart_device", CommandType::RESTART_DEVICE},
};
std::function<CommandResult()> CommandManager::createCommand(CommandType type, std::string_view json)
{
typedef std::function<CommandResult()> CommandFunction;
std::function<CommandResult()> CommandManager::createCommand(CommandType type, std::string_view json) const {
switch (type)
{
case CommandType::PING:
return CommandFunction(PingCommand);
return { PingCommand };
case CommandType::SET_WIFI:
return CommandFunction(std::bind(setWiFiCommand, this->registry, json));
return [this, json] { return setWiFiCommand(this->registry, json); };
case CommandType::UPDATE_WIFI:
return CommandFunction(std::bind(updateWiFiCommand, this->registry, json));
return [this, json] { return updateWiFiCommand(this->registry, json); };
case CommandType::UPDATE_AP_WIFI:
return CommandFunction(std::bind(updateAPWiFiCommand, this->registry, json));
return [this, json] { return updateAPWiFiCommand(this->registry, json); };
case CommandType::DELETE_NETWORK:
return CommandFunction(std::bind(deleteWiFiCommand, this->registry, json));
return [this, json] { return deleteWiFiCommand(this->registry, json); };
case CommandType::SET_MDNS:
return CommandFunction(std::bind(setMDNSCommand, this->registry, json));
return [this, json] { return setMDNSCommand(this->registry, json); };
// updating the mnds name is essentially the same operation
case CommandType::UPDATE_MDNS:
return CommandFunction(std::bind(setMDNSCommand, this->registry, json));
return [this, json] { return setMDNSCommand(this->registry, json); };
case CommandType::UPDATE_CAMERA:
return CommandFunction(std::bind(updateCameraCommand, this->registry, json));
return [this, json] { return updateCameraCommand(this->registry, json); };
case CommandType::RESTART_CAMERA:
return CommandFunction(std::bind(restartCameraCommand, this->registry, json));
return [this, json] { return restartCameraCommand(this->registry, json); };
case CommandType::GET_CONFIG:
return CommandFunction(std::bind(getConfigCommand, this->registry));
return [this] { return getConfigCommand(this->registry); };
case CommandType::SAVE_CONFIG:
return CommandFunction(std::bind(saveConfigCommand, this->registry));
return [this] { return saveConfigCommand(this->registry); };
case CommandType::RESET_CONFIG:
return CommandFunction(std::bind(resetConfigCommand, this->registry, json));
return [this, json] { return resetConfigCommand(this->registry, json); };
case CommandType::RESTART_DEVICE:
return CommandFunction(restartDeviceCommand);
return restartDeviceCommand;
default:
return nullptr;
}
}
CommandResult CommandManager::executeFromJson(std::string_view json)
CommandResult CommandManager::executeFromJson(const std::string_view json) const
{
cJSON *parsedJson = cJSON_Parse(json.data());
if (parsedJson == nullptr)
@@ -84,9 +81,9 @@ CommandResult CommandManager::executeFromJson(std::string_view json)
}
const cJSON *commandPayload = cJSON_GetObjectItem(commandData, "data");
auto commandType = commandTypeMap.at(std::string(commandTypeString->valuestring));
const auto commandType = commandTypeMap.at(std::string(commandTypeString->valuestring));
std::string commandPayloadString = "";
std::string commandPayloadString;
if (commandPayload != nullptr)
commandPayloadString = std::string(cJSON_Print(commandPayload));
@@ -100,16 +97,16 @@ CommandResult CommandManager::executeFromJson(std::string_view json)
cJSON_AddItemToArray(responses, response);
}
auto jsonString = cJSON_Print(responseDocument);
const auto jsonString = cJSON_Print(responseDocument);
cJSON_Delete(responseDocument);
cJSON_Delete(parsedJson);
return CommandResult::getSuccessResult(jsonString);
}
CommandResult CommandManager::executeFromType(CommandType type, std::string_view json)
CommandResult CommandManager::executeFromType(const CommandType type, const std::string_view json) const
{
auto command = createCommand(type, json);
const auto command = createCommand(type, json);
if (command == nullptr)
{

View File

@@ -19,7 +19,7 @@
#include "commands/device_commands.hpp"
#include <cJSON.h>
enum CommandType
enum class CommandType
{
None,
PING,
@@ -44,11 +44,11 @@ private:
std::shared_ptr<DependencyRegistry> registry;
public:
CommandManager(std::shared_ptr<DependencyRegistry> DependencyRegistry) : registry(DependencyRegistry) {};
std::function<CommandResult()> createCommand(CommandType type, std::string_view json);
explicit CommandManager(const std::shared_ptr<DependencyRegistry> &DependencyRegistry) : registry(DependencyRegistry) {};
std::function<CommandResult()> createCommand(CommandType type, std::string_view json) const;
CommandResult executeFromJson(std::string_view json);
CommandResult executeFromType(CommandType type, std::string_view json);
CommandResult executeFromJson(std::string_view json) const;
CommandResult executeFromType(CommandType type, std::string_view json) const;
};
#endif

View File

@@ -16,7 +16,7 @@ private:
Status status;
std::string message;
CommandResult(std::string message, Status status)
CommandResult(std::string message, const Status status)
{
this->status = status;
if (status == Status::SUCCESS)
@@ -33,12 +33,12 @@ private:
public:
bool isSuccess() const { return status == Status::SUCCESS; }
static CommandResult getSuccessResult(std::string message)
static CommandResult getSuccessResult(const std::string &message)
{
return CommandResult(message, Status::SUCCESS);
}
static CommandResult getErrorResult(std::string message)
static CommandResult getErrorResult(const std::string &message)
{
return CommandResult(message, Status::FAILURE);
}

View File

@@ -4,7 +4,7 @@
#include <memory>
#include <unordered_map>
enum DependencyType
enum class DependencyType
{
project_config,
camera_manager

View File

@@ -8,11 +8,11 @@ std::optional<WifiPayload> parseSetWiFiCommandPayload(std::string_view jsonPaylo
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");
const cJSON *networkName = cJSON_GetObjectItem(parsedJson, "name");
const cJSON *ssidPtr = cJSON_GetObjectItem(parsedJson, "ssid");
const cJSON *passwordPtr = cJSON_GetObjectItem(parsedJson, "password");
const cJSON *channelPtr = cJSON_GetObjectItem(parsedJson, "channel");
const cJSON *powerPtr = cJSON_GetObjectItem(parsedJson, "power");
if (ssidPtr == nullptr)
{
@@ -50,12 +50,12 @@ std::optional<WifiPayload> parseSetWiFiCommandPayload(std::string_view jsonPaylo
std::optional<deleteNetworkPayload> parseDeleteWifiCommandPayload(std::string_view jsonPayload)
{
deleteNetworkPayload payload;
cJSON *parsedJson = cJSON_Parse(jsonPayload.data());
auto *parsedJson = cJSON_Parse(jsonPayload.data());
if (parsedJson == nullptr)
return std::nullopt;
cJSON *networkName = cJSON_GetObjectItem(parsedJson, "name");
const auto *networkName = cJSON_GetObjectItem(parsedJson, "name");
if (networkName == nullptr)
{
cJSON_Delete(parsedJson);
@@ -76,7 +76,7 @@ std::optional<UpdateWifiPayload> parseUpdateWifiCommandPayload(std::string_view
if (parsedJson == nullptr)
return std::nullopt;
cJSON *networkName = cJSON_GetObjectItem(parsedJson, "name");
const cJSON *networkName = cJSON_GetObjectItem(parsedJson, "name");
if (networkName == nullptr)
{
cJSON_Delete(parsedJson);
@@ -85,10 +85,10 @@ std::optional<UpdateWifiPayload> parseUpdateWifiCommandPayload(std::string_view
payload.networkName = std::string(networkName->valuestring);
cJSON *ssidObject = cJSON_GetObjectItem(parsedJson, "ssid");
cJSON *passwordObject = cJSON_GetObjectItem(parsedJson, "password");
cJSON *channelObject = cJSON_GetObjectItem(parsedJson, "channel");
cJSON *powerObject = cJSON_GetObjectItem(parsedJson, "power");
const auto *ssidObject = cJSON_GetObjectItem(parsedJson, "ssid");
const auto *passwordObject = cJSON_GetObjectItem(parsedJson, "password");
const auto *channelObject = cJSON_GetObjectItem(parsedJson, "channel");
const auto *powerObject = cJSON_GetObjectItem(parsedJson, "power");
if (ssidObject != nullptr && (strcmp(ssidObject->valuestring, "") == 0))
{
@@ -113,7 +113,7 @@ std::optional<UpdateWifiPayload> parseUpdateWifiCommandPayload(std::string_view
return payload;
}
std::optional<UpdateAPWiFiPayload> parseUpdateAPWiFiCommandPayload(std::string_view jsonPayload)
std::optional<UpdateAPWiFiPayload> parseUpdateAPWiFiCommandPayload(const std::string_view jsonPayload)
{
UpdateAPWiFiPayload payload;
cJSON *parsedJson = cJSON_Parse(jsonPayload.data());
@@ -123,9 +123,9 @@ std::optional<UpdateAPWiFiPayload> parseUpdateAPWiFiCommandPayload(std::string_v
return std::nullopt;
}
cJSON *ssidObject = cJSON_GetObjectItem(parsedJson, "ssid");
cJSON *passwordObject = cJSON_GetObjectItem(parsedJson, "password");
cJSON *channelObject = cJSON_GetObjectItem(parsedJson, "channel");
const cJSON *ssidObject = cJSON_GetObjectItem(parsedJson, "ssid");
const cJSON *passwordObject = cJSON_GetObjectItem(parsedJson, "password");
const cJSON *channelObject = cJSON_GetObjectItem(parsedJson, "channel");
if (ssidObject != nullptr)
payload.ssid = std::string(ssidObject->valuestring);
@@ -142,13 +142,13 @@ std::optional<UpdateAPWiFiPayload> parseUpdateAPWiFiCommandPayload(std::string_v
CommandResult setWiFiCommand(std::shared_ptr<DependencyRegistry> registry, std::string_view jsonPayload)
{
auto payload = parseSetWiFiCommandPayload(jsonPayload);
const auto payload = parseSetWiFiCommandPayload(jsonPayload);
if (!payload.has_value())
{
return CommandResult::getErrorResult("Invalid payload");
}
auto wifiConfig = payload.value();
const auto wifiConfig = payload.value();
std::shared_ptr<ProjectConfig> projectConfig = registry->resolve<ProjectConfig>(DependencyType::project_config);
projectConfig->setWifiConfig(
@@ -163,11 +163,11 @@ CommandResult setWiFiCommand(std::shared_ptr<DependencyRegistry> registry, std::
CommandResult deleteWiFiCommand(std::shared_ptr<DependencyRegistry> registry, std::string_view jsonPayload)
{
auto payload = parseDeleteWifiCommandPayload(jsonPayload);
const auto payload = parseDeleteWifiCommandPayload(jsonPayload);
if (!payload.has_value())
return CommandResult::getErrorResult("Invalid payload");
std::shared_ptr<ProjectConfig> projectConfig = registry->resolve<ProjectConfig>(DependencyType::project_config);
auto projectConfig = registry->resolve<ProjectConfig>(DependencyType::project_config);
projectConfig->deleteWifiConfig(payload.value().networkName);
return CommandResult::getSuccessResult("Config updated");
@@ -175,7 +175,7 @@ CommandResult deleteWiFiCommand(std::shared_ptr<DependencyRegistry> registry, st
CommandResult updateWiFiCommand(std::shared_ptr<DependencyRegistry> registry, std::string_view jsonPayload)
{
auto payload = parseUpdateWifiCommandPayload(jsonPayload);
const auto payload = parseUpdateWifiCommandPayload(jsonPayload);
if (!payload.has_value())
{
return CommandResult::getErrorResult("Invalid payload");
@@ -183,13 +183,12 @@ CommandResult updateWiFiCommand(std::shared_ptr<DependencyRegistry> registry, st
auto updatedConfig = payload.value();
std::shared_ptr<ProjectConfig> projectConfig = registry->resolve<ProjectConfig>(DependencyType::project_config);
auto projectConfig = registry->resolve<ProjectConfig>(DependencyType::project_config);
auto storedNetworks = projectConfig->getWifiConfigs();
if (auto networkToUpdate = std::find_if(
storedNetworks.begin(),
storedNetworks.end(),
[&](auto &network)
{ return network.name == updatedConfig.networkName; });
if (const auto networkToUpdate = std::ranges::find_if(
storedNetworks,
[&](auto &network){ return network.name == updatedConfig.networkName; }
);
networkToUpdate != storedNetworks.end())
{
projectConfig->setWifiConfig(
@@ -207,15 +206,15 @@ CommandResult updateWiFiCommand(std::shared_ptr<DependencyRegistry> registry, st
CommandResult updateAPWiFiCommand(std::shared_ptr<DependencyRegistry> registry, std::string_view jsonPayload)
{
auto payload = parseUpdateAPWiFiCommandPayload(jsonPayload);
const auto payload = parseUpdateAPWiFiCommandPayload(jsonPayload);
if (!payload.has_value())
return CommandResult::getErrorResult("Invalid payload");
auto updatedConfig = payload.value();
std::shared_ptr<ProjectConfig> projectConfig = registry->resolve<ProjectConfig>(DependencyType::project_config);
auto previousAPConfig = projectConfig->getAPWifiConfig();
auto projectConfig = registry->resolve<ProjectConfig>(DependencyType::project_config);
const auto previousAPConfig = projectConfig->getAPWifiConfig();
projectConfig->setAPWifiConfig(
updatedConfig.ssid.has_value() ? updatedConfig.ssid.value() : previousAPConfig.ssid,