Config class as singleton with initializer in setup

This commit is contained in:
Emanuele Trabattoni
2025-07-17 20:57:50 +02:00
parent 92de57a760
commit b7881355a2
2 changed files with 21 additions and 9 deletions

View File

@@ -41,11 +41,21 @@ public:
class Config
{
public:
static Config &getInstance()
{
static Config instance;
return instance;
}
private:
Config() = default;
Config(const Config &) = delete;
Config &operator=(const Config &) = delete;
Config()
public:
void init()
{
FSmount mount; // scoped mount of the filesystem
@@ -102,7 +112,7 @@ public:
LOG_ERROR("Unable to remove config.json");
}
LOG_WARN("Configuration reset, Restarting");
}; //filesystem is unmounted here
}; // filesystem is unmounted here
delay(500);
esp_restart();
}

View File

@@ -18,6 +18,10 @@
#include "utils.h"
/////////////// GLOBALS ///////////////
Config& conf = Config::getInstance();
/////////////// GLOBALS ///////////////
void callback(char *topic, uint8_t *payload, unsigned int length)
{
std::string pl;
@@ -42,13 +46,11 @@ void setup()
{
Serial.begin(9600);
LOG_ATTACH_SERIAL(Serial);
conf.init();
}
void loop()
{
/////////////// GLOBALS ///////////////
Config conf = Config();
/////////////// GLOBALS ///////////////
const uint8_t baseRegister(0x00);
uint16_t k(0);
uint8_t sensors(0);
@@ -79,11 +81,11 @@ void loop()
mqtt.setServer(conf.m_mqttHost.c_str(), conf.m_mqttPort);
mqtt.setCallback(callback);
//////////////// NETWORK ////////////////
//////////////// NETWORK ////////////////
/////////////// CALLBACK ////////////////
//////////////// NETWORK ////////////////
/////////////// CALLBACK ////////////////
Network.onEvent(
[&conf, &eth, &rtc, &mqtt, &buzzer, &led](arduino_event_id_t event, arduino_event_info_t info) -> void
[&eth, &rtc, &mqtt, &buzzer, &led](arduino_event_id_t event, arduino_event_info_t info) -> void
{
eth.onEvent(event, info); // Arduino Ethernet event handler
if (!eth.isConnected())