Fix wifi command not updating/adding new networks

This commit is contained in:
lorow 2022-11-23 01:23:51 +01:00
parent 5dc236af06
commit 18ac6ff484
2 changed files with 36 additions and 27 deletions

View File

@ -242,40 +242,45 @@ void ProjectConfig::setCameraConfig(uint8_t *vflip, uint8_t *framesize, uint8_t
void ProjectConfig::setWifiConfig(const std::string &networkName, const std::string &ssid, const std::string &password, uint8_t *channel, bool adhoc, bool shouldNotify)
{
WiFiConfig_t *networkToUpdate = nullptr;
// 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();
if (size > 0)
{
for (int i = 0; i < size; i++)
{
if (strcmp(this->config.networks[i].name.c_str(), networkName.c_str()) == 0)
networkToUpdate = &this->config.networks[i];
//! push_back creates a copy of the object, so we need to use emplace_back
if (networkToUpdate != nullptr)
{
this->config.networks.emplace_back(
networkName,
ssid,
password,
*channel,
false);
}
log_d("Updating wifi config");
}
}
else
{
//! push_back creates a copy of the object, so we need to use emplace_back
// 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,
false);
}
int networkToUpdate = -1;
for (int i = 0; i < size; i++){
if (strcmp(this->config.networks[i].name.c_str(), networkName.c_str()) == 0){
// we've found a preexisting network, let's upate it
networkToUpdate = i;
break;
}
}
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].adhoc = false;
} else 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
this->config.networks.emplace_back(
networkName,
ssid,
password,
*channel,
false);
networkToUpdate = &this->config.networks[0];
}
if (shouldNotify)

View File

@ -69,7 +69,7 @@ void BaseAPI::setWiFi(AsyncWebServerRequest *request)
case POST:
{
int params = request->params();
std::string networkName;
std::string ssid;
std::string password;
uint8_t channel = 0;
@ -79,6 +79,10 @@ 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();
}
if (param->name() == "ssid")
{
ssid = param->value().c_str();
@ -99,7 +103,7 @@ void BaseAPI::setWiFi(AsyncWebServerRequest *request)
log_i("%s[%s]: %s\n", _networkMethodsMap[request->method()].c_str(), param->name().c_str(), param->value().c_str());
}
// note: We're passing empty params by design, this is done to reset specific fields
projectConfig->setWifiConfig(ssid, ssid, password, &channel, adhoc, true);
projectConfig->setWifiConfig(networkName,ssid, password, &channel, adhoc, true);
/* if (WiFiStateManager->getCurrentState() == WiFiState_e::WiFiState_ADHOC)
{