mirror of
https://github.com/MrUnknownDE/OpenIris-ESPIDF.git
synced 2026-04-14 04:03:44 +02:00
Modernize the code a bit, clean up most compilation warnings
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#define _PROJECT_CONFIG_MODELS_HPP_
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <helpers.hpp>
|
||||
#include "sdkconfig.h"
|
||||
@@ -29,19 +30,18 @@ struct DeviceConfig_t : BaseConfigModel
|
||||
|
||||
void load()
|
||||
{
|
||||
this->OTALogin = this->pref->getString("OTALogin", "openiris").c_str();
|
||||
this->OTAPassword = this->pref->getString("OTAPassword", "openiris").c_str();
|
||||
this->OTALogin = this->pref->getString("OTALogin", "openiris");
|
||||
this->OTAPassword = this->pref->getString("OTAPassword", "openiris");
|
||||
this->OTAPort = this->pref->getInt("OTAPort", 3232);
|
||||
};
|
||||
|
||||
void save()
|
||||
{
|
||||
void save() const {
|
||||
this->pref->putString("OTALogin", this->OTALogin.c_str());
|
||||
this->pref->putString("OTAPassword", this->OTAPassword.c_str());
|
||||
this->pref->putInt("OTAPort", this->OTAPort);
|
||||
};
|
||||
|
||||
std::string toRepresentation()
|
||||
std::string toRepresentation() const
|
||||
{
|
||||
return Helpers::format_string(
|
||||
"\"device_config\": {\"OTALogin\": \"%s\", \"OTAPassword\": \"%s\", "
|
||||
@@ -50,7 +50,7 @@ struct DeviceConfig_t : BaseConfigModel
|
||||
};
|
||||
};
|
||||
|
||||
struct MDNSConfig_t : public BaseConfigModel
|
||||
struct MDNSConfig_t : BaseConfigModel
|
||||
{
|
||||
MDNSConfig_t(Preferences *pref) : BaseConfigModel(pref) {}
|
||||
|
||||
@@ -67,11 +67,10 @@ struct MDNSConfig_t : public BaseConfigModel
|
||||
default_hostname = "openiristracker";
|
||||
}
|
||||
|
||||
this->hostname = this->pref->getString("hostname", default_hostname).c_str();
|
||||
this->hostname = this->pref->getString("hostname", default_hostname);
|
||||
};
|
||||
|
||||
void save()
|
||||
{
|
||||
void save() const {
|
||||
this->pref->putString("hostname", this->hostname.c_str());
|
||||
};
|
||||
|
||||
@@ -102,8 +101,7 @@ struct CameraConfig_t : BaseConfigModel
|
||||
this->brightness = this->pref->getInt("brightness", 2);
|
||||
};
|
||||
|
||||
void save()
|
||||
{
|
||||
void save() const {
|
||||
this->pref->putInt("vflip", this->vflip);
|
||||
this->pref->putInt("href", this->href);
|
||||
this->pref->putInt("framesize", this->framesize);
|
||||
@@ -132,12 +130,12 @@ struct WiFiConfig_t : BaseConfigModel
|
||||
|
||||
WiFiConfig_t(
|
||||
Preferences *pref,
|
||||
uint8_t index,
|
||||
const std::string &name,
|
||||
const std::string &ssid,
|
||||
const std::string &password,
|
||||
uint8_t channel,
|
||||
uint8_t power)
|
||||
const uint8_t index,
|
||||
std::string name,
|
||||
std::string ssid,
|
||||
std::string password,
|
||||
const uint8_t channel,
|
||||
const uint8_t power)
|
||||
: BaseConfigModel(pref),
|
||||
index(index),
|
||||
name(std::move(name)),
|
||||
@@ -153,23 +151,22 @@ struct WiFiConfig_t : BaseConfigModel
|
||||
uint8_t channel;
|
||||
uint8_t power;
|
||||
|
||||
void load(uint8_t index)
|
||||
void load(const uint8_t index)
|
||||
{
|
||||
this->index = index;
|
||||
char buffer[2];
|
||||
|
||||
std::string iter_str = Helpers::itoa(index, buffer, 10);
|
||||
this->name = this->pref->getString(("name" + iter_str).c_str(), "").c_str();
|
||||
this->ssid = this->pref->getString(("ssid" + iter_str).c_str(), "").c_str();
|
||||
this->password = this->pref->getString(("password" + iter_str).c_str(), "").c_str();
|
||||
auto const iter_str = std::string(Helpers::itoa(index, buffer, 10));
|
||||
this->name = this->pref->getString(("name" + iter_str).c_str(), "");
|
||||
this->ssid = this->pref->getString(("ssid" + iter_str).c_str(), "");
|
||||
this->password = this->pref->getString(("password" + iter_str).c_str(), "");
|
||||
this->channel = this->pref->getUInt(("channel" + iter_str).c_str());
|
||||
this->power = this->pref->getUInt(("power" + iter_str).c_str());
|
||||
};
|
||||
|
||||
void save()
|
||||
{
|
||||
void save() const {
|
||||
char buffer[2];
|
||||
std::string iter_str = Helpers::itoa(this->index, buffer, 10);
|
||||
auto const iter_str = std::string(Helpers::itoa(this->index, buffer, 10));
|
||||
|
||||
this->pref->putString(("name" + iter_str).c_str(), this->name.c_str());
|
||||
this->pref->putString(("ssid" + iter_str).c_str(), this->ssid.c_str());
|
||||
@@ -197,12 +194,11 @@ struct AP_WiFiConfig_t : BaseConfigModel
|
||||
|
||||
void load()
|
||||
{
|
||||
this->ssid = this->pref->getString("apSSID", CONFIG_AP_WIFI_SSID).c_str();
|
||||
this->password = this->pref->getString("apPassword", CONFIG_AP_WIFI_PASSWORD).c_str();
|
||||
this->ssid = this->pref->getString("apSSID", CONFIG_AP_WIFI_SSID);
|
||||
this->password = this->pref->getString("apPassword", CONFIG_AP_WIFI_PASSWORD);
|
||||
};
|
||||
|
||||
void save()
|
||||
{
|
||||
void save() const {
|
||||
this->pref->putString("apSSID", this->ssid.c_str());
|
||||
this->pref->putString("apPass", this->password.c_str());
|
||||
this->pref->putUInt("apChannel", this->channel);
|
||||
@@ -228,8 +224,7 @@ struct WiFiTxPower_t : BaseConfigModel
|
||||
this->power = this->pref->getUInt("txpower", 52);
|
||||
};
|
||||
|
||||
void save()
|
||||
{
|
||||
void save() const {
|
||||
this->pref->putUInt("txpower", this->power);
|
||||
};
|
||||
|
||||
@@ -255,19 +250,19 @@ public:
|
||||
std::vector<WiFiConfig_t> networks,
|
||||
AP_WiFiConfig_t ap_network,
|
||||
MDNSConfig_t mdns,
|
||||
WiFiTxPower_t txpower) : device(device),
|
||||
WiFiTxPower_t txpower) : device(std::move(device)),
|
||||
camera(camera),
|
||||
networks(networks),
|
||||
ap_network(ap_network),
|
||||
mdns(mdns),
|
||||
networks(std::move(networks)),
|
||||
ap_network(std::move(ap_network)),
|
||||
mdns(std::move(mdns)),
|
||||
txpower(txpower) {}
|
||||
|
||||
std::string toRepresentation()
|
||||
{
|
||||
std::string WifiConfigRepresentation = "";
|
||||
std::string WifiConfigRepresentation;
|
||||
|
||||
// we need a valid json representation, so we can't have a dangling comma at the end
|
||||
if (this->networks.size() > 0)
|
||||
if (!this->networks.empty())
|
||||
{
|
||||
if (this->networks.size() > 1)
|
||||
{
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
#include "ProjectConfig.hpp"
|
||||
|
||||
static auto CONFIGURATION_TAG = "[CONFIGURATION]";
|
||||
|
||||
int getNetworkCount(Preferences *pref)
|
||||
{
|
||||
return pref->getInt("networkcount", 0);
|
||||
}
|
||||
|
||||
void saveNetworkCount(Preferences *pref, int count)
|
||||
void saveNetworkCount(Preferences *pref, const int count)
|
||||
{
|
||||
pref->putInt("networkcount", count);
|
||||
}
|
||||
@@ -19,10 +21,9 @@ ProjectConfig::ProjectConfig(Preferences *pref) : pref(pref),
|
||||
MDNSConfig_t(pref),
|
||||
WiFiTxPower_t(pref)) {}
|
||||
|
||||
ProjectConfig::~ProjectConfig() {}
|
||||
ProjectConfig::~ProjectConfig() = default;
|
||||
|
||||
void ProjectConfig::save()
|
||||
{
|
||||
void ProjectConfig::save() const {
|
||||
ESP_LOGD(CONFIGURATION_TAG, "Saving project config");
|
||||
this->config.device.save();
|
||||
this->config.camera.save();
|
||||
@@ -30,7 +31,7 @@ void ProjectConfig::save()
|
||||
this->config.txpower.save();
|
||||
this->config.ap_network.save();
|
||||
|
||||
auto networks_count = this->config.networks.size();
|
||||
auto const networks_count = static_cast<int>(this->config.networks.size());
|
||||
for (int i = 0; i < networks_count; i++)
|
||||
{
|
||||
this->config.networks[i].save();
|
||||
@@ -54,7 +55,7 @@ void ProjectConfig::load()
|
||||
return;
|
||||
}
|
||||
|
||||
bool success = this->pref->begin("openiris");
|
||||
const bool success = this->pref->begin("openiris");
|
||||
|
||||
ESP_LOGI(CONFIGURATION_TAG, "Config name: openiris");
|
||||
ESP_LOGI(CONFIGURATION_TAG, "Config loaded: %s", success ? "true" : "false");
|
||||
@@ -65,7 +66,7 @@ void ProjectConfig::load()
|
||||
this->config.txpower.load();
|
||||
this->config.ap_network.load();
|
||||
|
||||
auto networks_count = getNetworkCount(this->pref);
|
||||
const auto networks_count = getNetworkCount(this->pref);
|
||||
ESP_LOGD(CONFIGURATION_TAG, "Loading networks: %d", networks_count);
|
||||
for (int i = 0; i < getNetworkCount(this->pref); i++)
|
||||
{
|
||||
@@ -90,7 +91,7 @@ bool ProjectConfig::reset()
|
||||
//**********************************************************************************************************************
|
||||
void ProjectConfig::setDeviceConfig(const std::string &OTALogin,
|
||||
const std::string &OTAPassword,
|
||||
int OTAPort)
|
||||
const int OTAPort)
|
||||
{
|
||||
ESP_LOGD(CONFIGURATION_TAG, "Updating device config");
|
||||
this->config.device.OTALogin.assign(OTALogin);
|
||||
@@ -104,11 +105,11 @@ void ProjectConfig::setMDNSConfig(const std::string &hostname)
|
||||
this->config.mdns.hostname.assign(hostname);
|
||||
}
|
||||
|
||||
void ProjectConfig::setCameraConfig(uint8_t vflip,
|
||||
uint8_t framesize,
|
||||
uint8_t href,
|
||||
uint8_t quality,
|
||||
uint8_t brightness)
|
||||
void ProjectConfig::setCameraConfig(const uint8_t vflip,
|
||||
const uint8_t framesize,
|
||||
const uint8_t href,
|
||||
const uint8_t quality,
|
||||
const uint8_t brightness)
|
||||
{
|
||||
ESP_LOGD(CONFIGURATION_TAG, "Updating camera config");
|
||||
this->config.camera.vflip = vflip;
|
||||
@@ -126,13 +127,11 @@ void ProjectConfig::setWifiConfig(const std::string &networkName,
|
||||
uint8_t channel,
|
||||
uint8_t power)
|
||||
{
|
||||
size_t size = this->config.networks.size();
|
||||
const auto size = this->config.networks.size();
|
||||
|
||||
auto it = std::find_if(
|
||||
this->config.networks.begin(),
|
||||
this->config.networks.end(),
|
||||
[&](WiFiConfig_t &network)
|
||||
{ return network.name == networkName; });
|
||||
const auto it = std::ranges::find_if(this->config.networks,
|
||||
[&](const WiFiConfig_t &network)
|
||||
{ return network.name == networkName; });
|
||||
|
||||
if (it != this->config.networks.end())
|
||||
{
|
||||
@@ -151,7 +150,7 @@ void ProjectConfig::setWifiConfig(const std::string &networkName,
|
||||
if (size == 0)
|
||||
{
|
||||
ESP_LOGI(CONFIGURATION_TAG, "No networks, We're adding a new network");
|
||||
this->config.networks.emplace_back(this->pref, (uint8_t)0, networkName, ssid, password, channel,
|
||||
this->config.networks.emplace_back(this->pref, static_cast<uint8_t>(0), networkName, ssid, password, channel,
|
||||
power);
|
||||
return;
|
||||
}
|
||||
@@ -175,17 +174,14 @@ void ProjectConfig::setWifiConfig(const std::string &networkName,
|
||||
|
||||
void ProjectConfig::deleteWifiConfig(const std::string &networkName)
|
||||
{
|
||||
size_t size = this->config.networks.size();
|
||||
if (size == 0)
|
||||
if (const auto size = this->config.networks.size(); size == 0)
|
||||
{
|
||||
ESP_LOGI(CONFIGURATION_TAG, "No networks, nothing to delete");
|
||||
}
|
||||
|
||||
auto it = std::find_if(
|
||||
this->config.networks.begin(),
|
||||
this->config.networks.end(),
|
||||
[&](WiFiConfig_t &network)
|
||||
{ return network.name == networkName; });
|
||||
const auto it = std::ranges::find_if(this->config.networks,
|
||||
[&](const WiFiConfig_t &network)
|
||||
{ return network.name == networkName; });
|
||||
|
||||
if (it != this->config.networks.end())
|
||||
{
|
||||
@@ -203,7 +199,7 @@ void ProjectConfig::setWiFiTxPower(uint8_t power)
|
||||
|
||||
void ProjectConfig::setAPWifiConfig(const std::string &ssid,
|
||||
const std::string &password,
|
||||
uint8_t channel)
|
||||
const uint8_t channel)
|
||||
{
|
||||
this->config.ap_network.ssid.assign(ssid);
|
||||
this->config.ap_network.password.assign(password);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
#ifndef _PROJECT_CONFIG_HPP_
|
||||
#define _PROJECT_CONFIG_HPP_
|
||||
#ifndef PROJECT_CONFIG_HPP
|
||||
#define PROJECT_CONFIG_HPP
|
||||
#include "esp_log.h"
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
@@ -9,7 +9,6 @@
|
||||
#include "Models.hpp"
|
||||
#include <Preferences.hpp>
|
||||
|
||||
static const char *CONFIGURATION_TAG = "[CONFIGURATION]";
|
||||
|
||||
int getNetworkCount(Preferences *pref);
|
||||
|
||||
@@ -18,11 +17,11 @@ void saveNetworkCount(Preferences *pref, int count);
|
||||
class ProjectConfig
|
||||
{
|
||||
public:
|
||||
ProjectConfig(Preferences *pref);
|
||||
explicit ProjectConfig(Preferences *pref);
|
||||
virtual ~ProjectConfig();
|
||||
|
||||
void load();
|
||||
void save();
|
||||
void save() const;
|
||||
|
||||
void wifiConfigSave();
|
||||
void cameraConfigSave();
|
||||
|
||||
Reference in New Issue
Block a user