diff --git a/CMakeLists.txt b/CMakeLists.txt index 098a1c2556a24fdf290059f977f87291f7928ca8..96c755bad5b9cee3d30ba8714a772008502ef5ed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ add_subdirectory(libs/spdlog) include_directories(src) include_directories(libs/spdlog/include) +include_directories(src/observ) include_directories(src/com) add_library(soc_com diff --git a/src/observ/ObserverPattern.hpp b/src/observ/ObserverPattern.hpp new file mode 100644 index 0000000000000000000000000000000000000000..fe79426d4d7a5b9373a73e4a42ff70d37b3cb34a --- /dev/null +++ b/src/observ/ObserverPattern.hpp @@ -0,0 +1,35 @@ +/// Subject of oberver pattern + +#ifndef OBSERVER_PATTERN_HPP +#define OBSERVER_PATTERN_HPP + +#include <vector> +#include <memory> + +/// Type sub has to be an observable subject. +template<typename T> +class IObserver +{ +public: + virtual void update(const T &subject) = 0; +}; + +template<typename T> +class Subject +{ +public: + virtual ~Subject() = default; + void attach(std::shared_ptr<IObserver<T>> observer){ + m_observers.push_back(observer); + } + + void notify() const{ + for (auto &observer : m_observers){ + observer.update(this); + } + } +private: + std::vector<std::shared_ptr<IObserver<T>>> m_observers; +}; + +#endif \ No newline at end of file diff --git a/src/smart_grid/ModelState.cpp b/src/smart_grid/ModelState.cpp index 241b3cca325cc61ff5ebb48ccda12370ba09571b..6e5bf829f21da4058dfdba892496d867f927fb95 100644 --- a/src/smart_grid/ModelState.cpp +++ b/src/smart_grid/ModelState.cpp @@ -114,5 +114,4 @@ void ModelState::next_hour() update_power_production(); update_modell(); print_states(); - notify(); } \ No newline at end of file diff --git a/src/smart_grid/ModelState.hpp b/src/smart_grid/ModelState.hpp index 86c90127d986126416f284fb9fb38049a091aa24..70269324603161beef214a11cd4b90fcb49b676d 100644 --- a/src/smart_grid/ModelState.hpp +++ b/src/smart_grid/ModelState.hpp @@ -10,6 +10,7 @@ #include "spdlog/spdlog.h" #include "SmartGridModell.hpp" +#include "ObserverPattern.hpp" struct MaxPower { static constexpr double village = 600; @@ -56,7 +57,8 @@ struct PowerUsage double sum() { return village + industry; } }; -class ModelState + +class ModelState : Subject<ModelState> { public: ModelState(SmartGridModell &modell); @@ -72,6 +74,7 @@ private: void update_modell(); void print_states(); + static constexpr double solar_size{10}; static constexpr double windpark_size{1.0}; static constexpr double village_size{10}; diff --git a/src/smart_grid/SmgStateMachine.hpp b/src/smart_grid/SmgStateMachine.hpp new file mode 100644 index 0000000000000000000000000000000000000000..6dcb770b1159677629f3af0088bd5b9ea1754cdc --- /dev/null +++ b/src/smart_grid/SmgStateMachine.hpp @@ -0,0 +1,40 @@ +/// + +#ifndef SMG_STATE_MACHINE_HPP +#define SMG_STATE_MACHINE_HPP + +#include "ModelState.hpp" +#include "SmartGridModell.hpp" + +enum class MainState +{ + Simulation, + ManualControl +}; + +enum class SimSate +{ + Entry, + Do, + Exit +}; + +enum class ManualSate +{ + Entry, + Do, + Exit +}; + + +class SmgStateMachine +{ +public: + void run(); + +private: + SmartGridModell m_modell; + ModelState m_state; +}; + +#endif \ No newline at end of file diff --git a/src/smart_grid/StateLogger.hpp b/src/smart_grid/StateLogger.hpp new file mode 100644 index 0000000000000000000000000000000000000000..2ab362f9d1e8ee0f2a43b9517eccccadc7bb9ed6 --- /dev/null +++ b/src/smart_grid/StateLogger.hpp @@ -0,0 +1,15 @@ +/// + +#ifndef STATE_LOGGER_HPP +#define STATE_LOGGER_HPP + +#include "ModelState.hpp" +#include "ObserverPattern.hpp" + +class StateLogger : IObserver<ModelState> +{ +public: + virtual void update(const ModelState &state) override; +}; + +#endif \ No newline at end of file