Skip to content

Commit

Permalink
feat: update to new Helpers library
Browse files Browse the repository at this point in the history
- remove state manager
- manage state exclusively via observer pattern
- manage ID's via IId interface
- update examples to remove updateWrapper handler
  • Loading branch information
ZanzyTHEbar committed Apr 5, 2024
1 parent de48ed7 commit 3d10979
Show file tree
Hide file tree
Showing 21 changed files with 125 additions and 323 deletions.
4 changes: 3 additions & 1 deletion EasyNetworkManager.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@
"xtr1common": "cpp",
"xtree": "cpp",
"xutility": "cpp",
"variant": "cpp"
"variant": "cpp",
"set": "cpp",
"future": "cpp"
}
}
}
29 changes: 1 addition & 28 deletions NetworkManager/examples/captivePortal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,34 +79,7 @@ void setup() {
networkManager.begin();
dnsServer.start(53, "*", WiFi.softAPIP());

/**
* @brief This Function is used to handle the state changes of the WiFi
*/
updateWrapper<WiFiState_e>(
networkManager.configHandler->config.getState(
networkManager.wifiHandler->getID()),
[](WiFiState_e state) {
switch (state) {
//! intentional fallthrough case
case WiFiState_e::WiFiState_ADHOC:
case WiFiState_e::WiFiState_Connected: {
setupServer();
break;
}
case WiFiState_e::WiFiState_Disconnected: {
break;
}
case WiFiState_e::WiFiState_Disconnecting: {
break;
}
case WiFiState_e::WiFiState_Connecting: {
break;
}
case WiFiState_e::WiFiState_Error: {
break;
}
}
});
setupServer();
}

void loop() {
Expand Down
29 changes: 1 addition & 28 deletions NetworkManager/examples/class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,34 +90,7 @@ void setup() {
Serial.println("\nHello, EasyNetworkManager!");
networkManager.begin();

/**
* @brief This Function is used to handle the state changes of the WiFi
*/
updateWrapper<WiFiState_e>(
networkManager.configHandler->config.getState(
networkManager.wifiHandler->getID()),
[](WiFiState_e state) {
switch (state) {
//! intentional fallthrough case
case WiFiState_e::WiFiState_ADHOC:
case WiFiState_e::WiFiState_Connected: {
setupServer();
break;
}
case WiFiState_e::WiFiState_Disconnected: {
break;
}
case WiFiState_e::WiFiState_Disconnecting: {
break;
}
case WiFiState_e::WiFiState_Connecting: {
break;
}
case WiFiState_e::WiFiState_Error: {
break;
}
}
});
setupServer();
}

void loop() {}
30 changes: 1 addition & 29 deletions NetworkManager/examples/customConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,35 +88,7 @@ void setup() {
//* Optionally register a custom config - this will be saved and loaded
networkManager.configHandler->config.registerUserConfig(&customConfig);
networkManager.begin();

/**
* @brief This Function is used to handle the state changes of the WiFi
*/
updateWrapper<WiFiState_e>(
networkManager.configHandler->config.getState(
networkManager.wifiHandler->getID()),
[](WiFiState_e state) {
switch (state) {
//! intentional fallthrough case
case WiFiState_e::WiFiState_ADHOC:
case WiFiState_e::WiFiState_Connected: {
setupServer();
break;
}
case WiFiState_e::WiFiState_Disconnected: {
break;
}
case WiFiState_e::WiFiState_Disconnecting: {
break;
}
case WiFiState_e::WiFiState_Connecting: {
break;
}
case WiFiState_e::WiFiState_Error: {
break;
}
}
});
setupServer();
}

void loop() {}
29 changes: 1 addition & 28 deletions NetworkManager/examples/customHTML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,34 +71,7 @@ void setup() {
Serial.println("\nHello, EasyNetworkManager!");
networkManager.begin();

/**
* @brief This Function is used to handle the state changes of the WiFi
*/
updateWrapper<WiFiState_e>(
networkManager.configHandler->config.getState(
networkManager.wifiHandler->getID()),
[](WiFiState_e state) {
switch (state) {
//! intentional fallthrough case
case WiFiState_e::WiFiState_ADHOC:
case WiFiState_e::WiFiState_Connected: {
setupServer();
break;
}
case WiFiState_e::WiFiState_Disconnected: {
break;
}
case WiFiState_e::WiFiState_Disconnecting: {
break;
}
case WiFiState_e::WiFiState_Connecting: {
break;
}
case WiFiState_e::WiFiState_Error: {
break;
}
}
});
setupServer();
}

void loop() {}
29 changes: 1 addition & 28 deletions NetworkManager/examples/customHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,34 +74,7 @@ void setup() {
Serial.println("\nHello, EasyNetworkManager!");
networkManager.begin();

/**
* @brief This Function is used to handle the state changes of the WiFi
*/
updateWrapper<WiFiState_e>(
networkManager.configHandler->config.getState(
networkManager.wifiHandler->getID()),
[](WiFiState_e state) {
switch (state) {
//! intentional fallthrough case
case WiFiState_e::WiFiState_ADHOC:
case WiFiState_e::WiFiState_Connected: {
setupServer();
break;
}
case WiFiState_e::WiFiState_Disconnected: {
break;
}
case WiFiState_e::WiFiState_Disconnecting: {
break;
}
case WiFiState_e::WiFiState_Connecting: {
break;
}
case WiFiState_e::WiFiState_Error: {
break;
}
}
});
setupServer();
}

void loop() {}
29 changes: 1 addition & 28 deletions NetworkManager/examples/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,34 +97,7 @@ void setup() {

networkManager.begin();

/**
* @brief This Function is used to handle the state changes of the WiFi
*/
updateWrapper<WiFiState_e>(
networkManager.configHandler->config.getState(
networkManager.wifiHandler->getID()),
[](WiFiState_e state) {
switch (state) {
//! intentional fallthrough case
case WiFiState_e::WiFiState_ADHOC:
case WiFiState_e::WiFiState_Connected: {
setupServer();
break;
}
case WiFiState_e::WiFiState_Disconnected: {
break;
}
case WiFiState_e::WiFiState_Disconnecting: {
break;
}
case WiFiState_e::WiFiState_Connecting: {
break;
}
case WiFiState_e::WiFiState_Error: {
break;
}
}
});
setupServer();
}

void loop() {}
29 changes: 1 addition & 28 deletions NetworkManager/examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,34 +162,7 @@ void setup() {
networkManager.configHandler->config.registerUserConfig(&customConfig);
networkManager.begin();

/**
* @brief This Function is used to handle the state changes of the WiFi
*/
updateWrapper<WiFiState_e>(
networkManager.configHandler->config.getState(
networkManager.wifiHandler->getID()),
[](WiFiState_e state) {
switch (state) {
//! intentional fallthrough case
case WiFiState_e::WiFiState_ADHOC:
case WiFiState_e::WiFiState_Connected: {
setupServer();
break;
}
case WiFiState_e::WiFiState_Disconnected: {
break;
}
case WiFiState_e::WiFiState_Disconnecting: {
break;
}
case WiFiState_e::WiFiState_Connecting: {
break;
}
case WiFiState_e::WiFiState_Error: {
break;
}
}
});
setupServer();
}

void loop() {}
2 changes: 1 addition & 1 deletion NetworkManager/include/EasyNetworkManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Required header files
#include <api/rest_api_handler.hpp>
#include <data/config/config_handler.hpp>
#include <utilities/states.hpp>
#include <data/config/states.hpp>

#include <network/mdns/mdns_manager.hpp>

Expand Down
9 changes: 4 additions & 5 deletions NetworkManager/include/data/config/config_handler.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#pragma once
#include <data/config/project_config.hpp>
#include <data/config/states.hpp>
#include <helpers/logger.hpp>
#include <helpers/observer.hpp>
#include <utilities/states.hpp>

class ConfigHandler : public Helpers::IObserver<StateVariant> {
class ConfigHandler : public Helpers::Logger,
public Helpers::IObserver<StateVariant> {
public:
ConfigHandler(const std::string& configName = std::string(),
const std::string& mdnsName = std::string());
virtual ~ConfigHandler();
virtual void begin();
virtual void update(const StateVariant& event) override;
std::string getID() const override {
return "ConfigHandler";
}
ProjectConfig config;
};
4 changes: 2 additions & 2 deletions NetworkManager/include/data/config/project_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
#include <string>
#include <vector>

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

class CustomConfigInterface {
public:
virtual void load() = 0;
virtual void save() = 0;
};

class ProjectConfig : public StateManager<StateVariant>, public Preferences {
class ProjectConfig : public Helpers::ISubject<StateVariant>, public Preferences {
private:
virtual void initConfig();
Project_Config::ProjectConfig_t config;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#pragma once

#include <algorithm>
#include <type_traits>
#include <variant>
#include "state_manager.hpp"

/* \
* StateManager \
* All Project States are managed here \
*/
namespace ProgramStates {
enum State_e { Starting, Started, Stopping, Stopped, Error };
enum ProgramState_e { Starting, Started, Stopping, Stopped, Error };
enum Event_e {
configLoaded,
configSaved,
Expand Down Expand Up @@ -49,15 +51,35 @@ enum MDNSState_e {
MDNSState_Stopped,
MDNSState_Error
};

enum ProjectConfigEventIDs_e {
ProjectConfigEventID_ConfigHandler,
ProjectConfigEventID_MDNSHandler,
ProjectConfigEventID_WifiHandler,
};

}; // namespace ProgramStates

typedef ProgramStates::State_e State_e;
typedef ProgramStates::Event_e Event_e;
typedef ProgramStates::WiFiState_e WiFiState_e;
typedef ProgramStates::WebServerState_e WebServerState_e;
typedef ProgramStates::MDNSState_e MDNSState_e;
using State_e = ProgramStates::ProgramState_e;
using Event_e = ProgramStates::Event_e;
using WiFiState_e = ProgramStates::WiFiState_e;
using WebServerState_e = ProgramStates::WebServerState_e;
using MDNSState_e = ProgramStates::MDNSState_e;
using ProjectConfigEventIDs_e = ProgramStates::ProjectConfigEventIDs_e;

//* Define a broad variant
using StateVariant =
std::variant<State_e, Event_e, WiFiState_e, WebServerState_e, MDNSState_e>;
// extern StateManager<StateVariant> stateManager;

// Define a generic function that applies a switch-case logic to an enum
// variant.
template <typename EnumType, typename VariantType, typename Func>
void updateStateWrapper(const VariantType& _variant, Func&& _switchCaseFunc) {
std::visit(
[&](auto&& arg) {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, EnumType>) {
_switchCaseFunc(arg);
}
},
_variant);
}
3 changes: 3 additions & 0 deletions NetworkManager/include/data/config/structs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,7 @@ struct ProjectConfig_t {

PreferencesWiFiConfigKeys_t wifKeys;
};



} // namespace Project_Config
Loading

0 comments on commit 3d10979

Please sign in to comment.