fix: duplication of wifi network config

- migrate to cpp iterators
- migrate to assign method for strings
This commit is contained in:
ZanzyTHEbar 2023-03-03 15:32:17 +00:00
parent 6d8cb51f9e
commit 651b7b62ba
3 changed files with 48 additions and 32 deletions

View File

@ -267,35 +267,46 @@ void ProjectConfig::setWifiConfig(const std::string& networkName,
// 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();
int index;
// we're allowing to store up to three additional networks
if (size == 0) {
Serial.println("No networks, We're adding a new network");
this->config.networks.emplace_back(networkName, ssid, password, *channel,
*power, false);
if (shouldNotify)
this->notify(ObserverEvent::networksConfigUpdated);
return;
}
int networkToUpdate = -1;
for (int i = 0; i < size; i++) {
if (this->config.networks[i].name == networkName) {
// we've found a preexisting network, let's upate it
networkToUpdate = i;
break;
for (auto it = this->config.networks.begin();
it != this->config.networks.end();) {
if (it->name == networkName) {
log_i("Found network %s, updating it ...", it->name.c_str());
it->name = networkName;
it->ssid = ssid;
it->password = password;
it->channel = *channel;
it->power = *power;
it->adhoc = false;
if (shouldNotify)
this->notify(ObserverEvent::networksConfigUpdated);
return;
} else {
++it;
}
}
if (networkToUpdate >= 0) {
this->config.networks[networkToUpdate].name = networkName;
this->config.networks[networkToUpdate].ssid = ssid;
this->config.networks[networkToUpdate].password = password;
this->config.networks[networkToUpdate].channel = *channel;
this->config.networks[networkToUpdate].power = *power;
this->config.networks[networkToUpdate].adhoc = false;
} else if (size < 3) {
if (size < 3) {
Serial.println("We're adding a new network");
// we don't have that network yet, we can add it as we still have some space
// we're using emplace_back as push_back will create a copy of it, we want
// to avoid that
// we don't have that network yet, we can add it as we still have some
// space we're using emplace_back as push_back will create a copy of it,
// we want to avoid that
this->config.networks.emplace_back(networkName, ssid, password, *channel,
*power, false);
}
@ -311,18 +322,16 @@ void ProjectConfig::deleteWifiConfig(const std::string& networkName,
Serial.println("No networks, nothing to delete");
}
int networkToDelete = -1;
for (int i = 0; i < size; i++) {
if (networkName == this->config.networks[i].name) {
// we've found a preexisting network, let's upate it
networkToDelete = i;
break; // we can break here as we're not allowing duplicate names
}
}
for (auto it = this->config.networks.begin();
it != this->config.networks.end();) {
if (it->name == networkName) {
log_i("Found network %s", it->name.c_str());
it = this->config.networks.erase(it);
log_i("Deleted network %s", networkName.c_str());
if (networkToDelete >= 0) {
this->config.networks.erase(this->config.networks.begin() +
networkToDelete);
} else {
++it;
}
}
if (shouldNotify)

View File

@ -3,6 +3,7 @@
#define PROJECT_CONFIG_HPP
#include <Arduino.h>
#include <Preferences.h>
#include <algorithm>
#include <string>
#include <vector>

View File

@ -78,11 +78,11 @@ void BaseAPI::setWiFi(AsyncWebServerRequest* request) {
for (int i = 0; i < params; i++) {
AsyncWebParameter* param = request->getParam(i);
if (param->name() == "networkName") {
networkName = param->value().c_str();
networkName.assign(param->value().c_str());
} else if (param->name() == "ssid") {
ssid = param->value().c_str();
ssid.assign(param->value().c_str());
} else if (param->name() == "password") {
password = param->value().c_str();
password.assign(param->value().c_str());
} else if (param->name() == "channel") {
channel = (uint8_t)atoi(param->value().c_str());
} else if (param->name() == "power") {
@ -178,7 +178,13 @@ void BaseAPI::setDeviceConfig(AsyncWebServerRequest* request) {
for (int i = 0; i < params; i++) {
AsyncWebParameter* param = request->getParam(i);
if (param->name() == "hostname") {
hostname.assign(param->value().c_str());
std::string result = param->value().c_str();
// Convert to lower case using lambda - to force lower case
std::for_each(result.begin(), result.end(),
[](char& c) { c = ::tolower(c); });
hostname.assign(result);
} else if (param->name() == "service") {
service.assign(param->value().c_str());
} else if (param->name() == "ota_port") {