Skip to content

Commit

Permalink
fix: clean up some extranious left overs
Browse files Browse the repository at this point in the history
- move init spiffs out of the constructor
- migrate some logging functionalities
  • Loading branch information
ZanzyTHEbar committed Apr 6, 2024
1 parent 1b133b5 commit 38eff9d
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 67 deletions.
8 changes: 4 additions & 4 deletions NetworkManager/extras/ota/basic_ota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void OTA::begin() {
type = "filesystem";
})
.onEnd([]() {
Serial.println("[Basic OTA]: OTA updated finished successfully!");
this->log("[Basic OTA]: OTA updated finished successfully!");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("[Basic OTA]: Progress: %u%%\r",
Expand All @@ -52,8 +52,8 @@ void OTA::begin() {
}
});

Serial.println("[Basic OTA]: Starting up basic OTA server");
Serial.println(
this->log("[Basic OTA]: Starting up basic OTA server");
this->log(
"[Basic OTA]: OTA will be live for 5mins, after which it will be "
"disabled until "
"restart");
Expand All @@ -68,7 +68,7 @@ void OTA::handleOTAUpdate() {
// we're disabling ota after first 5 minutes so that nothing bad
// happens during runtime
_isOtaEnabled = false;
Serial.println("[Basic OTA]: From now on, OTA is disabled");
this->log("[Basic OTA]: From now on, OTA is disabled");
return;
}
ArduinoOTA.handle();
Expand Down
5 changes: 4 additions & 1 deletion NetworkManager/include/data/config/project_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <data/config/states.hpp>
#include <helpers/helpers.hpp>
#include <helpers/logger.hpp>
#include <helpers/observer.hpp>
#include <utilities/network_utilities.hpp>

Expand All @@ -19,7 +20,9 @@ class CustomConfigInterface {
virtual void save() = 0;
};

class ProjectConfig : public Helpers::ISubject<StateVariant>, public Preferences {
class ProjectConfig : public Helpers::Logger,
public Helpers::ISubject<StateVariant>,
public Preferences {
private:
virtual void initConfig();
Project_Config::ProjectConfig_t config;
Expand Down
7 changes: 4 additions & 3 deletions NetworkManager/src/api/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ AsyncServer_t::AsyncServer_t(const int CONTROL_PORT,
wifimanager_url(std::move(wifimanager_url)),
user_commands(std::move(user_commands)),
json_url(std::move(json_url)),
spiffsMounted(false) {
spiffsMounted = initSPIFFS();
}
spiffsMounted(false) {}
AsyncServer_t::~AsyncServer_t() {}

void AsyncServer_t::begin() {
// TODO: Migrate to LittleFS
spiffsMounted = initSPIFFS();

server.on("/", XHTTP_GET,
[&](AsyncWebServerRequest* request) { request->send(200); });

Expand Down
60 changes: 27 additions & 33 deletions NetworkManager/src/data/config/project_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,34 @@ ProjectConfig::ProjectConfig(const std::string& configName,
const std::string& mdnsName)
: _configName(std::move(configName)),
_mdnsName(std::move(mdnsName)),
_already_loaded(false) {}
_already_loaded(false) {
this->setLabel("ProjectConfig");
}

ProjectConfig::~ProjectConfig() {
this->detachAll();
}

/**
*@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.
*/
void ProjectConfig::initConfig() {
if (_configName.empty()) {
log_e("[Project Config]: Config name is null\n");
log_e("Config name is null\n");
_configName = "easynetwork";
}

bool success = begin(_configName.c_str());

if (_mdnsName.empty()) {
log_e(
"[Project Config]: MDNS name is null\n Auto-assigning name to "
"MDNS name is null\n Auto-assigning name to "
"'easynetwork'");
_mdnsName = "easynetwork";
}

this->config.mdns.hostname.assign(_mdnsName);

log_i("[Project Config]: MDNS name: %s", _mdnsName.c_str());
log_i("[Project Config]: Config name: %s", _configName.c_str());
log_i("[Project Config]: Config loaded: %s", success ? "true" : "false");
log_i("MDNS name: %s", _mdnsName.c_str());
log_i("Config name: %s", _configName.c_str());
log_i("Config loaded: %s", success ? "true" : "false");
}

void ProjectConfig::deviceConfigSave() {
Expand Down Expand Up @@ -97,7 +94,7 @@ void ProjectConfig::wifiTxPowerConfigSave() {
}

void ProjectConfig::save() {
log_d("[Project Config]: Saving project config");
log_d("Saving project config");

deviceConfigSave();
wifiConfigSave();
Expand All @@ -117,30 +114,29 @@ void ProjectConfig::save() {
end();

if (this->reboot) {
log_i("[Project Config]: Project config saved and system is rebooting");
log_i("Project config saved and system is rebooting");
ESP.restart();
return;
}

log_w("[Project Config]: Reboot is disabled, triggering observer");
log_w("Reboot is disabled, triggering observer");
this->_already_loaded = false;
this->notifyAll(Event_e::configSaved);
}

bool ProjectConfig::reset() {
log_w("[Project Config]: Resetting project config");
log_w("Resetting project config");
return clear();
}

void ProjectConfig::load() {
log_d("[Project Config]: Loading project config");
log_d("Loading project config");

if (this->_already_loaded) {
log_w("[Project Config]: Project config already loaded");
log_w("Project config already loaded");
return;
}

// call before load to initialise the structs
initConfig();

/* Custom Load */
Expand Down Expand Up @@ -214,12 +210,12 @@ void ProjectConfig::load() {

//**********************************************************************************************************************
//*
//! DeviceConfig
//! Setters
//*
//**********************************************************************************************************************
void ProjectConfig::setDeviceConfig(const std::string& ota_pass, int ota_port,
bool shouldNotify) {
log_d("[Project Config]: Updating device config");
log_d("Updating device config");
this->config.device.ota_password.assign(ota_pass);
this->config.device.ota_port = ota_port;

Expand All @@ -230,7 +226,7 @@ void ProjectConfig::setDeviceConfig(const std::string& ota_pass, int ota_port,

bool ProjectConfig::setMDNSConfig(const std::string& hostname,
bool shouldNotify) {
log_d("[Project Config]: Updating MDNS config");
log_d("Updating MDNS config");
for (int i = 0; i < this->config.mdns.hostname.size(); i++) {
if (this->config.mdns.hostname[i] == '-' ||
this->config.mdns.hostname[i] == '.')
Expand All @@ -247,7 +243,7 @@ bool ProjectConfig::setMDNSConfig(const std::string& hostname,
else if (this->config.mdns.hostname[i] == 0 && i > 0)
break;
log_i(
"[Project Config]: Invalid hostname, please use only alphanumeric "
"Invalid hostname, please use only alphanumeric "
"characters");
return false;
}
Expand All @@ -273,8 +269,7 @@ void ProjectConfig::setWifiConfig(const std::string& networkName,
for (auto it = this->config.networks.begin();
it != this->config.networks.end();) {
if (it->ssid == ssid) {
log_i("[Project Config]: Found network %s, updating it ...",
it->name.c_str());
log_i("Found network %s, updating it ...", it->name.c_str());

it->name = networkName;
it->ssid = ssid;
Expand All @@ -297,7 +292,7 @@ void ProjectConfig::setWifiConfig(const std::string& networkName,
}

if (size < 3 && size > 0) {
Serial.println("[Project Config]: We're adding a new network");
this->log("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
Expand All @@ -307,8 +302,7 @@ void ProjectConfig::setWifiConfig(const std::string& networkName,

// we're allowing to store up to three additional networks
if (size == 0) {
Serial.println(
"[Project Config]: No networks, We're adding a new network");
this->log("No networks, We're adding a new network");
this->config.networks.emplace_back(networkName, ssid, password, channel,
power, false);
}
Expand All @@ -325,15 +319,15 @@ void ProjectConfig::deleteWifiConfig(const std::string& networkName,
bool shouldNotify) {
size_t size = this->config.networks.size();
if (size == 0) {
Serial.println("[Project Config]: No networks, nothing to delete");
this->log("No networks, nothing to delete");
}

for (auto it = this->config.networks.begin();
it != this->config.networks.end();) {
if (it->name == networkName) {
log_i("[Project Config]: Found network %s", it->name.c_str());
log_i("Found network %s", it->name.c_str());
it = this->config.networks.erase(it);
log_i("[Project Config]: Deleted network %s", networkName.c_str());
log_i("Deleted network %s", networkName.c_str());

} else {
++it;
Expand All @@ -353,7 +347,7 @@ void ProjectConfig::setAPWifiConfig(const std::string& ssid,
this->config.ap_network.channel = channel;
this->config.ap_network.adhoc = adhoc;

log_d("[Project Config]: Updating access point config");
log_d("Updating access point config");
if (shouldNotify) {
this->notifyAll(Event_e::apConfigUpdated);
}
Expand All @@ -362,7 +356,7 @@ void ProjectConfig::setAPWifiConfig(const std::string& ssid,
void ProjectConfig::setWiFiTxPower(uint8_t power, bool shouldNotify) {
this->config.wifi_tx_power.power = power;

log_d("[Project Config]: Updating wifi tx power");
log_d("Updating wifi tx power");
if (shouldNotify)
this->notifyAll(Event_e::wifiTxPowerUpdated);
}
Expand All @@ -371,7 +365,7 @@ void ProjectConfig::setDeviceDataJson(const std::string& data,
bool shouldNotify) {
this->config.device_data.deviceJson.assign(data);

log_d("[Project Config]: Updating device data json");
log_d("Updating device data json");
if (shouldNotify)
this->notifyAll(Event_e::deviceDataJsonUpdated);
}
Expand Down
32 changes: 16 additions & 16 deletions NetworkManager/src/network/wifihandler/wifi_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ void WiFiHandler::begin() {

// at this point, we've tried every network, let's just
// setup adhoc
Serial.printf(
this->log(
"We've gone through every network, each timed out. "
"Trying to connect to the hardcoded network one last time: %s \n\r",
this->ssid.c_str());
"Trying to connect to the hardcoded network one last time: ",
this->ssid);
if (this->iniSTA(this->ssid, this->password, this->channel,
(wifi_power_t)txpower.power)) {
Serial.print(
this->log(
"Successfully connected to the hardcoded "
"network. \n\r");
"network.");
return;
}
log_e(
Expand All @@ -75,13 +75,13 @@ void WiFiHandler::begin() {

void WiFiHandler::adhoc(const std::string& ssid, uint8_t channel,
const std::string& password) {
log_i("\n[INFO]: Setting Access Point...\n");
log_i("\n[INFO]: Configuring access point...\n");
this->log("Setting Access Point...\n");
this->log("Configuring access point...\n");
WiFi.mode(WIFI_AP);
WiFi.setSleep(WIFI_PS_NONE);
Serial.printf("\r\nStarting AP. \r\nAP IP address: ");
this->log("Starting AP. \r\nAP IP address: ");
IPAddress IP = WiFi.softAPIP();
Serial.printf("[INFO]: AP IP address: %s.\r\n", IP.toString().c_str());
this->log("AP IP address: %s.\r\n", IP.toString().c_str());
// You can remove the password parameter if you want the
// AP to be open.
WiFi.softAP(ssid.c_str(), password.c_str(),
Expand All @@ -90,7 +90,7 @@ void WiFiHandler::adhoc(const std::string& ssid, uint8_t channel,
}

void WiFiHandler::setUpADHOC() {
log_i("\n[INFO]: Setting Access Point...\n");
this->log("Setting Access Point... ");
size_t ssidLen = this->configManager.getAPWifiConfig().ssid.length();
size_t passwordLen =
this->configManager.getAPWifiConfig().password.length();
Expand All @@ -100,9 +100,9 @@ void WiFiHandler::setUpADHOC() {
}

if (passwordLen <= 0) {
log_i(
"\n[INFO]: Configuring access point without a "
"password\n");
this->log(
"Configuring access point without a "
"password...");
this->adhoc(this->configManager.getAPWifiConfig().ssid,
this->configManager.getAPWifiConfig().channel);
return;
Expand All @@ -111,7 +111,7 @@ void WiFiHandler::setUpADHOC() {
this->adhoc(this->configManager.getAPWifiConfig().ssid,
this->configManager.getAPWifiConfig().channel,
this->configManager.getAPWifiConfig().password);
log_i("\n[INFO]: Configuring access point...\n");
this->log("Configuring access point...");
log_d("\n[DEBUG]: ssid: %s\n",
this->configManager.getAPWifiConfig().ssid.c_str());
log_d("\n[DEBUG]: password: %s\n",
Expand All @@ -126,7 +126,7 @@ bool WiFiHandler::iniSTA(const std::string& ssid, const std::string& password,
unsigned long startingMillis = currentMillis;
int connectionTimeout = 45000; // 30 seconds
int progress = 0;
Serial.printf("Trying to connect to: %s \n\r", ssid.c_str());
this->log("Trying to connect to: ", ssid);
auto mdnsConfig = this->configManager.getMDNSConfig();
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE,
INADDR_NONE); // need to call before
Expand All @@ -147,8 +147,8 @@ bool WiFiHandler::iniSTA(const std::string& ssid, const std::string& password,
}
}

this->configManager.notifyAll(WiFiState_e::WiFiState_Connected);
this->log("Successfully connected to ", ssid);
this->configManager.notifyAll(WiFiState_e::WiFiState_Connected);
// Serial.printf("Setting TX power to: %d \n\r", (uint8_t)power);
// WiFi.setTxPower(power);

Expand Down
5 changes: 2 additions & 3 deletions NetworkManager/src/utilities/network_utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void Network_Utilities::setupWifiScan() {
WiFi.disconnect(); // Disconnect from the access point if connected before
delay(100);

Serial.println("Setup done");
log_i("[INFO]: Setup done");
}

/**
Expand Down Expand Up @@ -47,8 +47,7 @@ bool Network_Utilities::loopWifiScan() {
* @param points int Number of points to average
* @return int
*/
int Network_Utilities::getStrength(int points) // TODO: add to JSON doc
{
int Network_Utilities::getStrength(int points) {
int32_t rssi = 0, averageRSSI = 0;

for (int i = 0; i < points; i++) {
Expand Down
4 changes: 2 additions & 2 deletions NetworkManager/wokwi.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[wokwi]
version = 1
elf = ".pio/build/esp32s3_debug/EasyNetworkManager-v5.3.5-esp32s3_debug-de48ed7-main.elf"
firmware = ".pio/build/esp32s3_debug/EasyNetworkManager-v5.3.5-esp32s3_debug-de48ed7-main.bin"
elf = ".pio/build/esp32s3_debug/EasyNetworkManager-v5.4.0-esp32s3_debug-1b133b5-main.elf"
firmware = ".pio/build/esp32s3_debug/EasyNetworkManager-v5.4.0-esp32s3_debug-1b133b5-main.bin"
[[net.forward]]
from = "localhost:8180"
to = "target:80"
Loading

0 comments on commit 38eff9d

Please sign in to comment.