Skip to content

Commit

Permalink
refactor: clean up project manager
Browse files Browse the repository at this point in the history
- fix building issue with deep+
- clean up project config hard-coded strings
- migrate to inline static strings for keys, for added type safety and intellisense
- remove basic OTA and Ethernet logic
- migrate all config structs to dedicated file
  • Loading branch information
ZanzyTHEbar committed Mar 30, 2024
1 parent 732be47 commit 395ce7b
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 136 deletions.
File renamed without changes.
File renamed without changes.
15 changes: 8 additions & 7 deletions NetworkManager/include/EasyNetworkManager.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#pragma once

//! Required header files
#include <api/rest_api_handler.hpp> //! (*)
#include <data/config/config_handler.hpp> //! (*)
#include <utilities/states.hpp> //! (*)
#include <api/rest_api_handler.hpp>
#include <data/config/config_handler.hpp>
#include <utilities/states.hpp>

#include <network/mdns/mdns_manager.hpp>

#if ENABLE_ETHERNET
# include <network/wifihandler/ethernet/ethernet_handler.hpp> //! (*)
/* #if ENABLE_ETHERNET
#include <network/wifihandler/ethernet/ethernet_handler.hpp>
#else
# include <network/wifihandler/wifi_handler.hpp> //! (*)
#endif
#endif */

#include <network/wifihandler/wifi_handler.hpp>

class EasyNetworkManager {
public:
Expand Down
63 changes: 2 additions & 61 deletions NetworkManager/include/data/config/project_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <Arduino.h>
#include <Preferences.h>

#include "structs.hpp"

#include <functional>
#include <string>
#include <vector>
Expand All @@ -11,67 +13,6 @@
#include <utilities/observer.hpp>
#include <utilities/states.hpp>

namespace Project_Config {
struct DeviceConfig_t {
std::string ota_login;
std::string ota_password;
int ota_port;
std::string toRepresentation();
};

struct DeviceDataJson_t {
std::string deviceJson;
std::string toRepresentation();
};

struct MDNSConfig_t {
std::string hostname;
std::string toRepresentation();
};

struct WiFiConfig_t {
//! Constructor for WiFiConfig_t - allows us to use emplace_back
WiFiConfig_t(const std::string& name, const std::string& ssid,
const std::string& password, uint8_t channel, uint8_t power,
bool adhoc)
: name(std::move(name)),
ssid(std::move(ssid)),
password(std::move(password)),
channel(channel),
power(power),
adhoc(adhoc) {}
std::string name;
std::string ssid;
std::string password;
uint8_t channel;
uint8_t power;
bool adhoc;
std::string toRepresentation();
};

struct WiFiTxPower_t {
uint8_t power;
std::string toRepresentation();
};

struct AP_WiFiConfig_t {
std::string ssid;
std::string password;
uint8_t channel;
bool adhoc;
std::string toRepresentation();
};

struct ProjectConfig_t {
DeviceConfig_t device;
DeviceDataJson_t device_data;
MDNSConfig_t mdns;
std::vector<WiFiConfig_t> networks;
AP_WiFiConfig_t ap_network;
WiFiTxPower_t wifi_tx_power;
};
} // namespace Project_Config

class CustomConfigInterface {
public:
virtual void load() = 0;
Expand Down
153 changes: 153 additions & 0 deletions NetworkManager/include/data/config/structs.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#pragma once
#include <string>
#include <vector>

namespace Project_Config {
struct DeviceConfig_t {
struct Keys_t {
inline static const std::string ota_login = "ota_login";
inline static const std::string ota_password = "ota_password";
inline static const std::string ota_port = "ota_port";
} keys;

DeviceConfig_t(const std::string& login, const std::string& password,
int port)
: ota_login(std::move(login)),
ota_password(std::move(password)),
ota_port(port) {}

DeviceConfig_t()
: ota_login("admin"), ota_password("12345678"), ota_port(3232) {}

std::string ota_login;
std::string ota_password;
int ota_port;
std::string toRepresentation();
};

struct DeviceDataJson_t {
struct Keys_t {
inline static const std::string deviceJson = "deviceJson";
} keys;

DeviceDataJson_t(const std::string& json) : deviceJson(std::move(json)) {}

DeviceDataJson_t() : deviceJson("") {}

std::string deviceJson;
std::string toRepresentation();
};

struct MDNSConfig_t {
struct Keys_t {
inline static const std::string hostname = "hostname";
} keys;

MDNSConfig_t(const std::string& hostname) : hostname(std::move(hostname)) {}

MDNSConfig_t() : hostname("") {}

std::string hostname;
std::string toRepresentation();
};

struct PreferencesWiFiConfigKeys_t {
inline static const std::string networkCount = "networkCount";
inline static const std::string name = "name";
inline static const std::string ssid = "ssid";
inline static const std::string password = "password";
inline static const std::string channel = "channel";
inline static const std::string power = "power";
inline static const std::string adhoc = "adhoc";
};

struct WiFiConfig_t {
//! Constructor for WiFiConfig_t - allows us to use emplace_back
WiFiConfig_t(const std::string& name, const std::string& ssid,
const std::string& password, uint8_t channel, uint8_t power,
bool adhoc)
: name(std::move(name)),
ssid(std::move(ssid)),
password(std::move(password)),
channel(channel),
power(power),
adhoc(adhoc) {}
std::string name;
std::string ssid;
std::string password;
uint8_t channel;
uint8_t power;
bool adhoc;
std::string toRepresentation();
};

struct WiFiTxPower_t {
struct Keys_t {
inline static const std::string power = "power";
} keys;

WiFiTxPower_t(uint8_t power) : power(power) {}

WiFiTxPower_t() : power(52) {}

uint8_t power;
std::string toRepresentation();
};

struct AP_WiFiConfig_t {
struct Keys_t {
inline static const std::string ssid = "ap_ssid";
inline static const std::string password = "ap_password";
inline static const std::string channel = "ap_channel";
inline static const std::string adhoc = "ap_adhoc";
} keys;

AP_WiFiConfig_t(const std::string& ssid, const std::string& password,
uint8_t channel, bool adhoc)
: ssid(std::move(ssid)),
password(std::move(password)),
channel(channel),
adhoc(adhoc) {}

AP_WiFiConfig_t()
: ssid("ESP32-AP"), password("12345678"), channel(1), adhoc(false) {}

std::string ssid;
std::string password;
uint8_t channel;
bool adhoc;
std::string toRepresentation();
};

struct ProjectConfig_t {
DeviceConfig_t device;
DeviceDataJson_t device_data;
MDNSConfig_t mdns;
std::vector<WiFiConfig_t> networks;
AP_WiFiConfig_t ap_network;
WiFiTxPower_t wifi_tx_power;

ProjectConfig_t(const DeviceConfig_t& device,
const DeviceDataJson_t& device_data,
const MDNSConfig_t& mdns,
const std::vector<WiFiConfig_t>& networks,
const AP_WiFiConfig_t& ap_network,
const WiFiTxPower_t& wifi_tx_power)
: device(device),
device_data(device_data),
mdns(mdns),
networks(networks),
ap_network(ap_network),
wifi_tx_power(wifi_tx_power) {}

ProjectConfig_t()
: device(DeviceConfig_t()),
device_data(DeviceDataJson_t()),
mdns(MDNSConfig_t()),
networks(std::vector<WiFiConfig_t>()),
ap_network(AP_WiFiConfig_t()),
wifi_tx_power(WiFiTxPower_t()) {}

PreferencesWiFiConfigKeys_t wifKeys;
};
} // namespace Project_Config
2 changes: 1 addition & 1 deletion NetworkManager/ini/dev_config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ monitor_filters =
esp32_exception_decoder
board_build.partitions = min_spiffs.csv
;build_unflags = -Os ; disable optimization for size
lib_ldf_mode = deep+
lib_ldf_mode = deep
;115200 is used for compatability - if you are on windows and want the code to flash faster use 921600
upload_speed = 921600
extra_scripts =
Expand Down
Loading

0 comments on commit 395ce7b

Please sign in to comment.