feat: add DELETE method to wifi endpoint

This commit is contained in:
ZanzyTHEbar 2023-02-27 15:38:11 +00:00
parent af6b3dc2c1
commit de3ab8b4f5
3 changed files with 455 additions and 424 deletions

View File

@ -1,221 +1,208 @@
#include "project_config.hpp" #include "project_config.hpp"
ProjectConfig::ProjectConfig(const std::string &name, ProjectConfig::ProjectConfig(const std::string& name,
const std::string &mdnsName) : _name(std::move(name)), const std::string& mdnsName)
_mdnsName(std::move(mdnsName)), : _name(std::move(name)),
_already_loaded(false) {} _mdnsName(std::move(mdnsName)),
_already_loaded(false) {}
ProjectConfig::~ProjectConfig() {} ProjectConfig::~ProjectConfig() {}
/** /**
*@brief Initializes the structures with blank data to prevent empty memory sectors and nullptr errors. *@brief Initializes the structures with blank data to prevent empty memory
*sectors and nullptr errors.
*@brief This is to be called in setup() before loading the config. *@brief This is to be called in setup() before loading the config.
*/ */
void ProjectConfig::initConfig() void ProjectConfig::initConfig() {
{ if (_name.empty()) {
if (_name.empty()) log_e("Config name is null\n");
{ _name = "openiris";
log_e("Config name is null\n"); }
_name = "openiris";
}
bool success = begin(_name.c_str()); bool success = begin(_name.c_str());
log_i("Config name: %s", _name.c_str()); log_i("Config name: %s", _name.c_str());
log_i("Config loaded: %s", success ? "true" : "false"); log_i("Config loaded: %s", success ? "true" : "false");
/* /*
* If the config is not loaded, * If the config is not loaded,
* we need to initialize the config with default data * we need to initialize the config with default data
! Do not initialize the WiFiConfig_t struct here, ! Do not initialize the WiFiConfig_t struct here,
! as it will create a blank network which breaks the WiFiManager ! as it will create a blank network which breaks the WiFiManager
*/ */
this->config.device = { this->config.device = {
"12345678", "12345678",
3232, 3232,
}; };
if (_mdnsName.empty()) if (_mdnsName.empty()) {
{ log_e("MDNS name is null\n Autoassigning name to 'openiristracker'");
log_e("MDNS name is null\n Autoassigning name to 'openiristracker'"); _mdnsName = "openiristracker";
_mdnsName = "openiristracker"; }
} this->config.mdns = {
this->config.mdns = { _mdnsName,
_mdnsName, "openiristracker",
"openiristracker", };
};
log_i("MDNS name: %s", _mdnsName.c_str()); log_i("MDNS name: %s", _mdnsName.c_str());
this->config.ap_network = { this->config.ap_network = {
"", "",
"", "",
1, 1,
false, false,
}; };
this->config.camera = { this->config.camera = {
.vflip = 0, .vflip = 0,
.href = 0, .href = 0,
.framesize = 4, .framesize = 4,
.quality = 7, .quality = 7,
.brightness = 2, .brightness = 2,
}; };
} }
void ProjectConfig::save() void ProjectConfig::save() {
{ log_d("Saving project config");
log_d("Saving project config"); deviceConfigSave();
deviceConfigSave(); mdnsConfigSave();
mdnsConfigSave(); cameraConfigSave();
cameraConfigSave(); wifiConfigSave();
wifiConfigSave(); wifiTxPowerConfigSave();
wifiTxPowerConfigSave(); end(); // we call end() here to close the connection to the NVS partition, we
end(); // we call end() here to close the connection to the NVS partition, we only do this because we call ESP.restart() next. // only do this because we call ESP.restart() next.
OpenIrisTasks::ScheduleRestart(2000); OpenIrisTasks::ScheduleRestart(2000);
} }
void ProjectConfig::wifiConfigSave() void ProjectConfig::wifiConfigSave() {
{ log_d("Saving wifi config");
log_d("Saving wifi config");
/* WiFi Config */ /* WiFi Config */
putInt("networkCount", this->config.networks.size()); putInt("networkCount", this->config.networks.size());
std::string name = "name"; std::string name = "name";
std::string ssid = "ssid"; std::string ssid = "ssid";
std::string password = "pass"; std::string password = "pass";
std::string channel = "channel"; std::string channel = "channel";
std::string power = "power"; std::string power = "power";
for (int i = 0; i < this->config.networks.size(); i++) for (int i = 0; i < this->config.networks.size(); i++) {
{ char buffer[2];
char buffer[2]; std::string iter_str = Helpers::itoa(i, buffer, 10);
std::string iter_str = Helpers::itoa(i, buffer, 10);
name.append(iter_str); name.append(iter_str);
ssid.append(iter_str); ssid.append(iter_str);
password.append(iter_str); password.append(iter_str);
channel.append(iter_str); channel.append(iter_str);
power.append(iter_str); power.append(iter_str);
putString(name.c_str(), this->config.networks[i].name.c_str()); putString(name.c_str(), this->config.networks[i].name.c_str());
putString(ssid.c_str(), this->config.networks[i].ssid.c_str()); putString(ssid.c_str(), this->config.networks[i].ssid.c_str());
putString(password.c_str(), this->config.networks[i].password.c_str()); putString(password.c_str(), this->config.networks[i].password.c_str());
putUInt(channel.c_str(), this->config.networks[i].channel); putUInt(channel.c_str(), this->config.networks[i].channel);
putUInt(power.c_str(), this->config.networks[i].power); putUInt(power.c_str(), this->config.networks[i].power);
} }
/* AP Config */ /* AP Config */
putString("apSSID", this->config.ap_network.ssid.c_str()); putString("apSSID", this->config.ap_network.ssid.c_str());
putString("apPass", this->config.ap_network.password.c_str()); putString("apPass", this->config.ap_network.password.c_str());
putUInt("apChannel", this->config.ap_network.channel); putUInt("apChannel", this->config.ap_network.channel);
log_i("Project config saved and system is rebooting"); log_i("Project config saved and system is rebooting");
} }
void ProjectConfig::deviceConfigSave() void ProjectConfig::deviceConfigSave() {
{ /* Device Config */
/* Device Config */ putString("OTAPassword", this->config.device.OTAPassword.c_str());
putString("OTAPassword", this->config.device.OTAPassword.c_str()); putInt("OTAPort", this->config.device.OTAPort);
putInt("OTAPort", this->config.device.OTAPort);
} }
void ProjectConfig::mdnsConfigSave() 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());
putString("service", this->config.mdns.service.c_str());
} }
void ProjectConfig::wifiTxPowerConfigSave() void ProjectConfig::wifiTxPowerConfigSave() {
{ /* Device Config */
/* Device Config */ putInt("power", this->config.txpower.power);
putInt("power", this->config.txpower.power);
} }
void ProjectConfig::cameraConfigSave() void ProjectConfig::cameraConfigSave() {
{ /* Camera Config */
/* Camera Config */ putInt("vflip", this->config.camera.vflip);
putInt("vflip", this->config.camera.vflip); putInt("href", this->config.camera.href);
putInt("href", this->config.camera.href); putInt("framesize", this->config.camera.framesize);
putInt("framesize", this->config.camera.framesize); putInt("quality", this->config.camera.quality);
putInt("quality", this->config.camera.quality); putInt("brightness", this->config.camera.brightness);
putInt("brightness", this->config.camera.brightness);
} }
bool ProjectConfig::reset() bool ProjectConfig::reset() {
{ log_w("Resetting project config");
log_w("Resetting project config"); return clear();
return clear();
} }
void ProjectConfig::load() void ProjectConfig::load() {
{ log_d("Loading project config");
log_d("Loading project config"); if (this->_already_loaded) {
if (this->_already_loaded) log_w("Project config already loaded");
{ return;
log_w("Project config already loaded"); }
return;
}
/* Device Config */ /* Device Config */
this->config.device.OTAPassword = getString("OTAPassword", "12345678").c_str(); this->config.device.OTAPassword =
this->config.device.OTAPort = getInt("OTAPort", 3232); getString("OTAPassword", "12345678").c_str();
this->config.device.OTAPort = getInt("OTAPort", 3232);
/* 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(); this->config.mdns.service = getString("service").c_str();
/* Wifi TX Power Config */ /* Wifi TX Power Config */
this->config.txpower.power = getUInt("power", 52); this->config.txpower.power = getUInt("power", 52);
/* WiFi Config */ /* WiFi Config */
int networkCount = getInt("networkCount", 0); int networkCount = getInt("networkCount", 0);
std::string name = "name"; std::string name = "name";
std::string ssid = "ssid"; std::string ssid = "ssid";
std::string password = "pass"; std::string password = "pass";
std::string channel = "channel"; std::string channel = "channel";
std::string power = "power"; std::string power = "power";
for (int i = 0; i < networkCount; i++) for (int i = 0; i < networkCount; i++) {
{ char buffer[2];
char buffer[2]; std::string iter_str = Helpers::itoa(i, buffer, 10);
std::string iter_str = Helpers::itoa(i, buffer, 10);
name.append(iter_str); name.append(iter_str);
ssid.append(iter_str); ssid.append(iter_str);
password.append(iter_str); password.append(iter_str);
channel.append(iter_str); channel.append(iter_str);
power.append(iter_str); power.append(iter_str);
const std::string &temp_1 = getString(name.c_str()).c_str(); const std::string& temp_1 = getString(name.c_str()).c_str();
const std::string &temp_2 = getString(ssid.c_str()).c_str(); const std::string& temp_2 = getString(ssid.c_str()).c_str();
const std::string &temp_3 = getString(password.c_str()).c_str(); const std::string& temp_3 = getString(password.c_str()).c_str();
uint8_t temp_4 = getUInt(channel.c_str()); uint8_t temp_4 = getUInt(channel.c_str());
uint8_t temp_5 = getUInt(power.c_str()); uint8_t temp_5 = getUInt(power.c_str());
//! push_back creates a copy of the object, so we need to use emplace_back //! push_back creates a copy of the object, so we need to use emplace_back
this->config.networks.emplace_back( this->config.networks.emplace_back(
temp_1, temp_1, temp_2, temp_3, temp_4, temp_5,
temp_2, false); // false because the networks we store in the config are the
temp_3, // ones we want the esp to connect to, rather than host as AP
temp_4, }
temp_5,
false); // false because the networks we store in the config are the ones we want the esp to connect to, rather than host as AP
}
/* AP Config */ /* AP Config */
this->config.ap_network.ssid = getString("apSSID").c_str(); this->config.ap_network.ssid = getString("apSSID").c_str();
this->config.ap_network.password = getString("apPass").c_str(); this->config.ap_network.password = getString("apPass").c_str();
this->config.ap_network.channel = getUInt("apChannel"); this->config.ap_network.channel = getUInt("apChannel");
/* Camera Config */ /* Camera Config */
this->config.camera.vflip = getInt("vflip", 0); this->config.camera.vflip = getInt("vflip", 0);
this->config.camera.href = getInt("href", 0); this->config.camera.href = getInt("href", 0);
this->config.camera.framesize = getInt("framesize", 4); this->config.camera.framesize = getInt("framesize", 4);
this->config.camera.quality = getInt("quality", 7); this->config.camera.quality = getInt("quality", 7);
this->config.camera.brightness = getInt("brightness", 2); this->config.camera.brightness = getInt("brightness", 2);
this->_already_loaded = true; this->_already_loaded = true;
this->notify(ObserverEvent::configLoaded); this->notify(ObserverEvent::configLoaded);
} }
//********************************************************************************************************************** //**********************************************************************************************************************
@ -223,182 +210,194 @@ void ProjectConfig::load()
//! DeviceConfig //! DeviceConfig
//* //*
//********************************************************************************************************************** //**********************************************************************************************************************
void ProjectConfig::setDeviceConfig(const std::string &OTAPassword, int OTAPort, const std::string &binaryName, bool shouldNotify) void ProjectConfig::setDeviceConfig(const std::string& OTAPassword,
{ int OTAPort,
log_d("Updating device config"); const std::string& binaryName,
this->config.device.OTAPassword.assign(OTAPassword); bool shouldNotify) {
this->config.device.OTAPort = OTAPort; log_d("Updating device config");
// check if binary name is empty this->config.device.OTAPassword.assign(OTAPassword);
if (binaryName.empty()) this->config.device.OTAPort = OTAPort;
log_w("Binary name is empty, using previous value"); // check if binary name is empty
else if (binaryName.empty())
this->config.device.binaryName.assign(binaryName); log_w("Binary name is empty, using previous value");
else
this->config.device.binaryName.assign(binaryName);
if (shouldNotify) if (shouldNotify)
this->notify(ObserverEvent::deviceConfigUpdated); this->notify(ObserverEvent::deviceConfigUpdated);
} }
void ProjectConfig::setMDNSConfig(const std::string &hostname, const std::string &service, bool shouldNotify) void ProjectConfig::setMDNSConfig(const std::string& hostname,
{ const std::string& service,
log_d("Updating MDNS config"); bool shouldNotify) {
this->config.mdns.hostname.assign(hostname); log_d("Updating MDNS config");
this->config.mdns.service.assign(service); this->config.mdns.hostname.assign(hostname);
this->config.mdns.service.assign(service);
if (shouldNotify) if (shouldNotify)
this->notify(ObserverEvent::mdnsConfigUpdated); this->notify(ObserverEvent::mdnsConfigUpdated);
} }
void ProjectConfig::setCameraConfig(uint8_t *vflip, uint8_t *framesize, uint8_t *href, uint8_t *quality, uint8_t *brightness, bool shouldNotify) void ProjectConfig::setCameraConfig(uint8_t* vflip,
{ uint8_t* framesize,
log_d("Updating camera config"); uint8_t* href,
this->config.camera.vflip = *vflip; uint8_t* quality,
this->config.camera.href = *href; uint8_t* brightness,
this->config.camera.framesize = *framesize; bool shouldNotify) {
this->config.camera.quality = *quality; log_d("Updating camera config");
this->config.camera.brightness = *brightness; this->config.camera.vflip = *vflip;
this->config.camera.href = *href;
this->config.camera.framesize = *framesize;
this->config.camera.quality = *quality;
this->config.camera.brightness = *brightness;
log_d("Updating Camera config"); log_d("Updating Camera config");
if (shouldNotify) if (shouldNotify)
this->notify(ObserverEvent::cameraConfigUpdated); this->notify(ObserverEvent::cameraConfigUpdated);
} }
void ProjectConfig::setWifiConfig(const std::string &networkName, const std::string &ssid, const std::string &password, uint8_t *channel, uint8_t *power, bool adhoc, bool shouldNotify) void ProjectConfig::setWifiConfig(const std::string& networkName,
{ const std::string& ssid,
// 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 const std::string& password,
size_t size = this->config.networks.size(); uint8_t* channel,
uint8_t* power,
bool adhoc,
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();
// we're allowing to store up to three additional networks // we're allowing to store up to three additional networks
if (size == 0) if (size == 0) {
{ Serial.println("No networks, We're adding a new network");
Serial.println("No networks, We're adding a new network"); this->config.networks.emplace_back(networkName, ssid, password, *channel,
this->config.networks.emplace_back( *power, false);
networkName, }
ssid,
password, int networkToUpdate = -1;
*channel, for (int i = 0; i < size; i++) {
*power, if (this->config.networks[i].name == networkName) {
false); // we've found a preexisting network, let's upate it
networkToUpdate = i;
break;
} }
}
int networkToUpdate = -1; if (networkToUpdate >= 0) {
for (int i = 0; i < size; i++) this->config.networks[networkToUpdate].name = networkName;
{ this->config.networks[networkToUpdate].ssid = ssid;
if (strcmp(this->config.networks[i].name.c_str(), networkName.c_str()) == 0) this->config.networks[networkToUpdate].password = password;
{ this->config.networks[networkToUpdate].channel = *channel;
// we've found a preexisting network, let's upate it this->config.networks[networkToUpdate].power = *power;
networkToUpdate = i; this->config.networks[networkToUpdate].adhoc = false;
break; } 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,
*power, false);
}
if (shouldNotify)
this->notify(ObserverEvent::networksConfigUpdated);
}
void ProjectConfig::deleteWifiConfig(const std::string& networkName,
bool shouldNotify) {
size_t size = this->config.networks.size();
if (size == 0) {
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
} }
}
if (networkToUpdate >= 0) if (networkToDelete >= 0) {
{ this->config.networks.erase(this->config.networks.begin() +
this->config.networks[networkToUpdate].name = networkName; networkToDelete);
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)
{
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,
*power,
false);
}
if (shouldNotify) if (shouldNotify)
this->notify(ObserverEvent::networksConfigUpdated); this->notify(ObserverEvent::networksConfigUpdated);
} }
void ProjectConfig::setAPWifiConfig(const std::string &ssid, const std::string &password, uint8_t *channel, bool adhoc, bool shouldNotify) void ProjectConfig::setWiFiTxPower(uint8_t* power, bool shouldNotify) {
{ this->config.txpower.power = *power;
this->config.ap_network.ssid.assign(ssid);
this->config.ap_network.password.assign(password);
this->config.ap_network.channel = *channel;
this->config.ap_network.adhoc = adhoc;
log_d("Updating access point config"); log_d("Updating wifi tx power");
if (shouldNotify) if (shouldNotify)
this->notify(ObserverEvent::networksConfigUpdated); this->notify(ObserverEvent::wifiTxPowerUpdated);
} }
void ProjectConfig::setWiFiTxPower(uint8_t *power, bool shouldNotify) void ProjectConfig::setAPWifiConfig(const std::string& ssid,
{ const std::string& password,
this->config.txpower.power = *power; uint8_t* channel,
bool adhoc,
bool shouldNotify) {
this->config.ap_network.ssid.assign(ssid);
this->config.ap_network.password.assign(password);
this->config.ap_network.channel = *channel;
this->config.ap_network.adhoc = adhoc;
log_d("Updating wifi tx power"); log_d("Updating access point config");
if (shouldNotify) if (shouldNotify)
this->notify(ObserverEvent::wifiTxPowerUpdated); this->notify(ObserverEvent::networksConfigUpdated);
} }
std::string ProjectConfig::DeviceConfig_t::toRepresentation() std::string ProjectConfig::DeviceConfig_t::toRepresentation() {
{ std::string json = Helpers::format_string(
std::string json = Helpers::format_string( "\"device_config\": {\"OTAPassword\": \"%s\", \"OTAPort\": %u}",
"\"device_config\": {\"OTAPassword\": \"%s\", \"OTAPort\": %u}", this->OTAPassword.c_str(), this->OTAPort);
this->OTAPassword.c_str(), return json;
this->OTAPort);
return json;
} }
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\", \"service\": \"%s\"}", this->hostname.c_str(), this->service.c_str());
this->hostname.c_str(), return json;
this->service.c_str());
return json;
} }
std::string ProjectConfig::CameraConfig_t::toRepresentation() std::string ProjectConfig::CameraConfig_t::toRepresentation() {
{ std::string json = Helpers::format_string(
std::string json = Helpers::format_string( "\"camera_config\": {\"vflip\": %d,\"framesize\": %d,\"href\": "
"\"camera_config\": {\"vflip\": %d,\"framesize\": %d,\"href\": %d,\"quality\": %d,\"brightness\": %d}", "%d,\"quality\": %d,\"brightness\": %d}",
this->vflip, this->vflip, this->framesize, this->href, this->quality,
this->framesize, this->brightness);
this->href, return json;
this->quality,
this->brightness);
return json;
} }
std::string ProjectConfig::WiFiConfig_t::toRepresentation() 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\": %u, \"power\": %u,\"adhoc\": %s}", "\"channel\": "
this->name.c_str(), "%u, \"power\": %u,\"adhoc\": %s}",
this->ssid.c_str(), this->name.c_str(), this->ssid.c_str(), this->password.c_str(),
this->password.c_str(), this->channel, this->power, this->adhoc ? "true" : "false");
this->channel, return json;
this->power,
this->adhoc ? "true" : "false");
return json;
} }
std::string ProjectConfig::AP_WiFiConfig_t::toRepresentation() 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, \"adhoc\": %s}",
this->ssid.c_str(), this->ssid.c_str(), this->password.c_str(), this->channel,
this->password.c_str(), this->adhoc ? "true" : "false");
this->channel, return json;
this->adhoc ? "true" : "false");
return json;
} }
std::string ProjectConfig::WiFiTxPower_t::toRepresentation() std::string ProjectConfig::WiFiTxPower_t::toRepresentation() {
{ std::string json =
std::string json = Helpers::format_string( Helpers::format_string("\"wifi_tx_power\": {\"power\": %u}", this->power);
"\"wifi_tx_power\": {\"power\": %u}", return json;
this->power);
return json;
} }
//********************************************************************************************************************** //**********************************************************************************************************************
@ -407,9 +406,21 @@ std::string ProjectConfig::WiFiTxPower_t::toRepresentation()
//* //*
//********************************************************************************************************************** //**********************************************************************************************************************
ProjectConfig::DeviceConfig_t *ProjectConfig::getDeviceConfig() { return &this->config.device; } ProjectConfig::DeviceConfig_t* ProjectConfig::getDeviceConfig() {
ProjectConfig::CameraConfig_t *ProjectConfig::getCameraConfig() { return &this->config.camera; } return &this->config.device;
std::vector<ProjectConfig::WiFiConfig_t> *ProjectConfig::getWifiConfigs() { return &this->config.networks; } }
ProjectConfig::AP_WiFiConfig_t *ProjectConfig::getAPWifiConfig() { return &this->config.ap_network; } ProjectConfig::CameraConfig_t* ProjectConfig::getCameraConfig() {
ProjectConfig::MDNSConfig_t *ProjectConfig::getMDNSConfig() { return &this->config.mdns; } return &this->config.camera;
ProjectConfig::WiFiTxPower_t *ProjectConfig::getWiFiTxPowerConfig() { return &this->config.txpower; } }
std::vector<ProjectConfig::WiFiConfig_t>* ProjectConfig::getWifiConfigs() {
return &this->config.networks;
}
ProjectConfig::AP_WiFiConfig_t* ProjectConfig::getAPWifiConfig() {
return &this->config.ap_network;
}
ProjectConfig::MDNSConfig_t* ProjectConfig::getMDNSConfig() {
return &this->config.mdns;
}
ProjectConfig::WiFiTxPower_t* ProjectConfig::getWiFiTxPowerConfig() {
return &this->config.txpower;
}

View File

@ -3,125 +3,138 @@
#define PROJECT_CONFIG_HPP #define PROJECT_CONFIG_HPP
#include <Arduino.h> #include <Arduino.h>
#include <Preferences.h> #include <Preferences.h>
#include <vector>
#include <string> #include <string>
#include <vector>
#include "tasks/tasks.hpp"
#include "data/utilities/Observer.hpp" #include "data/utilities/Observer.hpp"
#include "data/utilities/helpers.hpp" #include "data/utilities/helpers.hpp"
#include "tasks/tasks.hpp"
class ProjectConfig : public Preferences, public ISubject class ProjectConfig : public Preferences, public ISubject {
{ public:
public: ProjectConfig(const std::string& name = std::string(),
ProjectConfig( const std::string& mdnsName = std::string());
const std::string &name = std::string(), virtual ~ProjectConfig();
const std::string &mdnsName = std::string() void load();
); void save();
virtual ~ProjectConfig(); void wifiConfigSave();
void load(); void cameraConfigSave();
void save(); void deviceConfigSave();
void wifiConfigSave(); void mdnsConfigSave();
void cameraConfigSave(); void wifiTxPowerConfigSave();
void deviceConfigSave(); bool reset();
void mdnsConfigSave(); void initConfig();
void wifiTxPowerConfigSave();
bool reset();
void initConfig();
struct DeviceConfig_t struct DeviceConfig_t {
{ std::string OTAPassword;
std::string OTAPassword; int OTAPort;
int OTAPort; std::string binaryName;
std::string binaryName; std::string toRepresentation();
std::string toRepresentation(); };
};
struct MDNSConfig_t struct MDNSConfig_t {
{ std::string hostname;
std::string hostname; std::string service;
std::string service; std::string toRepresentation();
std::string toRepresentation(); };
};
struct CameraConfig_t struct CameraConfig_t {
{ uint8_t vflip;
uint8_t vflip; uint8_t href;
uint8_t href; uint8_t framesize;
uint8_t framesize; uint8_t quality;
uint8_t quality; uint8_t brightness;
uint8_t brightness;
std::string toRepresentation(); std::string toRepresentation();
}; };
struct WiFiConfig_t struct WiFiConfig_t {
{ //! Constructor for WiFiConfig_t - allows us to use emplace_back
//! Constructor for WiFiConfig_t - allows us to use emplace_back WiFiConfig_t(const std::string& name,
WiFiConfig_t(const std::string &name, const std::string& ssid,
const std::string &ssid, const std::string& password,
const std::string &password, uint8_t channel,
uint8_t channel, uint8_t power,
uint8_t power, bool adhoc)
bool adhoc) : name(std::move(name)), : name(std::move(name)),
ssid(std::move(ssid)), ssid(std::move(ssid)),
password(std::move(password)), password(std::move(password)),
channel(channel), channel(channel),
adhoc(adhoc), adhoc(adhoc),
power(power) {} power(power) {}
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; bool adhoc;
std::string toRepresentation(); std::string toRepresentation();
}; };
struct AP_WiFiConfig_t struct AP_WiFiConfig_t {
{ std::string ssid;
std::string ssid; std::string password;
std::string password; uint8_t channel;
uint8_t channel; bool adhoc;
bool adhoc; std::string toRepresentation();
std::string toRepresentation(); };
};
struct WiFiTxPower_t struct WiFiTxPower_t {
{ uint8_t power;
uint8_t power; std::string toRepresentation();
std::string toRepresentation(); };
};
struct TrackerConfig_t struct TrackerConfig_t {
{ DeviceConfig_t device;
DeviceConfig_t device; CameraConfig_t camera;
CameraConfig_t camera; std::vector<WiFiConfig_t> networks;
std::vector<WiFiConfig_t> networks; AP_WiFiConfig_t ap_network;
AP_WiFiConfig_t ap_network; MDNSConfig_t mdns;
MDNSConfig_t mdns; WiFiTxPower_t txpower;
WiFiTxPower_t txpower; };
};
DeviceConfig_t *getDeviceConfig(); DeviceConfig_t* getDeviceConfig();
CameraConfig_t *getCameraConfig(); CameraConfig_t* getCameraConfig();
std::vector<WiFiConfig_t> *getWifiConfigs(); std::vector<WiFiConfig_t>* getWifiConfigs();
AP_WiFiConfig_t *getAPWifiConfig(); AP_WiFiConfig_t* getAPWifiConfig();
MDNSConfig_t *getMDNSConfig(); MDNSConfig_t* getMDNSConfig();
WiFiTxPower_t *getWiFiTxPowerConfig(); WiFiTxPower_t* getWiFiTxPowerConfig();
void setDeviceConfig(const std::string &OTAPassword, int OTAPort, const std::string &binaryName, bool shouldNotify); void setDeviceConfig(const std::string& OTAPassword,
void setMDNSConfig(const std::string &hostname, const std::string &service, bool shouldNotify); int OTAPort,
void setCameraConfig(uint8_t *vflip, uint8_t *framesize, uint8_t *href, uint8_t *quality, uint8_t *brightness, bool shouldNotify); const std::string& binaryName,
void setWifiConfig(const std::string &networkName, const std::string &ssid, const std::string &password, uint8_t *channel, uint8_t *power, bool adhoc, bool shouldNotify); bool shouldNotify);
void setAPWifiConfig(const std::string &ssid, const std::string &password, uint8_t *channel, bool adhoc, bool shouldNotify); void setMDNSConfig(const std::string& hostname,
void setWiFiTxPower(uint8_t *power, bool shouldNotify); const std::string& service,
bool shouldNotify);
void setCameraConfig(uint8_t* vflip,
uint8_t* framesize,
uint8_t* href,
uint8_t* quality,
uint8_t* brightness,
bool shouldNotify);
void setWifiConfig(const std::string& networkName,
const std::string& ssid,
const std::string& password,
uint8_t* channel,
uint8_t* power,
bool adhoc,
bool shouldNotify);
void setAPWifiConfig(const std::string& ssid,
const std::string& password,
uint8_t* channel,
bool adhoc,
bool shouldNotify);
void setWiFiTxPower(uint8_t* power, bool shouldNotify);
private: void deleteWifiConfig(const std::string& networkName, bool shouldNotify);
TrackerConfig_t config;
std::string _name; private:
std::string _mdnsName; TrackerConfig_t config;
bool _already_loaded; std::string _name;
std::string _mdnsName;
bool _already_loaded;
}; };
#endif // PROJECT_CONFIG_HPP #endif // PROJECT_CONFIG_HPP

View File

@ -114,6 +114,13 @@ void BaseAPI::setWiFi(AsyncWebServerRequest* request) {
"{\"msg\":\"Done. Wifi Creds have been set.\"}"); "{\"msg\":\"Done. Wifi Creds have been set.\"}");
break; break;
} }
case DELETE: {
projectConfig->deleteWifiConfig(request->arg("networkName").c_str(),
true);
request->send(200, MIMETYPE_JSON,
"{\"msg\":\"Done. Wifi Creds have been deleted.\"}");
break;
}
default: { default: {
request->send(400, MIMETYPE_JSON, "{\"msg\":\"Invalid Request\"}"); request->send(400, MIMETYPE_JSON, "{\"msg\":\"Invalid Request\"}");
request->redirect("/"); request->redirect("/");