diff --git a/src/SmartGridModell.cpp b/src/SmartGridModell.cpp index c51bbef76854351c6f8e6f218dfc110e3055d0d6..89697a1159b29f095ddd2d07f9abdc2bdf73a9e4 100644 --- a/src/SmartGridModell.cpp +++ b/src/SmartGridModell.cpp @@ -14,7 +14,7 @@ SmartGridModell::SmartGridModell(i2c::Node& node, DefaultState initial_state) SmartGridModell::~SmartGridModell() { - put_modell_into_state(DefaultState::Off); + // put_modell_into_state(DefaultState::Off); } diff --git a/src/com/Protocol.cpp b/src/com/Protocol.cpp index b0ce233c3bd712964bc9b7b278a6ad4f22d14c14..85ded7397bbd2d997ad85c8643a336108a2f08fc 100644 --- a/src/com/Protocol.cpp +++ b/src/com/Protocol.cpp @@ -7,12 +7,6 @@ IProtocol::IProtocol(DataSocket& socket) : m_socket{socket} {} -void ProtocolSimple::send_message(std::string const& url, std::string const& message) -{ - m_socket.put_data(url.c_str(), url.size()); - m_socket.put_data(message.c_str(), message.size()); - m_socket.end_transmission(); -} /// @brief Resizeable string wrapper /// @@ -40,6 +34,13 @@ public: } }; +void ProtocolSimple::send_message(std::string const& url, std::string const& message) +{ + m_socket.put_data(url.c_str(), url.size()); + m_socket.put_data(message.c_str(), message.size()); + m_socket.end_transmission(); +} + void ProtocolSimple::recv_message(std::string& message) { diff --git a/src/com/ProtocolSgm.cpp b/src/com/ProtocolSgm.cpp new file mode 100644 index 0000000000000000000000000000000000000000..694a0f49665ea868e0a299d95afb19184135bc4f --- /dev/null +++ b/src/com/ProtocolSgm.cpp @@ -0,0 +1,76 @@ +/// @file Protocol.cpp +/// + +#include <ProtocolSgm.hpp> +#include <com/Protocol.hpp> + + +CmdQueue::CmdQueue() +{ + m_queue = std::make_shared<QueueRef>(); +} + + +CmdQueue::QueueRef CmdQueue::get_queue() +{ + return m_queue; +} + + +ProtocolSgm::ProtocolSgm(std::string const& address) + : m_com_addr{address} +{ + m_sending = std::thread(&ProtocolSgm::sending, this); + m_receving = std::thread(&ProtocolSgm::receving, this); +} + +void ProtocolSgm::sending() +{ + if (m_send_queue.empty() == true) + { + std::this_thread::sleep_for(std::chrono::millisecons(1)); + } + else + { + auto cmd = m_queue.back(); + ConnectSocket connect{m_com_addr, sgm_com_port}; + ProtocolSimple prot{connect}; + + prot.send_message("", cmd.cmd); + prot.send_message("", cmd.val); + prot.send_message("", cmd.ack); + + std::string ack; + prot.recv_message(ack); // done + if (ack != cmd.ack) + { + // something went wrong + } + m_send_queue.pop_back(); + } +} + +void ProtocolSgm::receiving() +{ + ServerSocket server{sgm_com_port}; + while (true) + { + DataSocket sock = server.accept(); + ProtocolSimple prot{sock}; + + CommandSgm cmd; + prot.recv_message(cmd.cmd); + prot.recv_message(cmd.val); + prot.recv_message(cmd.ack); + + m_recv_queue.push_back(cmd); + + prot.send_message(cmd.ack); + } +} + + +void ProtocolSgm::send_cmd(CommandSgm const& cmd) +{ + m_queue.push_back(cmd); +} \ No newline at end of file diff --git a/src/com/ProtocolSgm.hpp b/src/com/ProtocolSgm.hpp new file mode 100644 index 0000000000000000000000000000000000000000..963c944749b00469bf725fa0901e2aa24cc00b3f --- /dev/null +++ b/src/com/ProtocolSgm.hpp @@ -0,0 +1,51 @@ +/// @file ProtocolSgm.hpp +/// + + +#ifndef PROTOCOL_SGM_HPP +#define PROTOCOL_SGM_HPP + + +#include <memory> +#include <vector> +#include <thread> + + +struct CommandSgm +{ + std::string cmd; + std::string val; + std::string ack; +}; + +class CmdQueue +{ +public: + CmdQueue(); + using QueueRef = std::shared_ptr<std::vector<CommandSgm>; + QueueRef get_queue(); +private: + QueueRef m_queue; +}; + + +class ProtocolSgm +{ +public: + static const int sgm_com_port {8080}; + ProtocolSgm(std::string const& address); + void send_cmd(CommandSgm const& cmd); + +private: + void sending(); + void receving(); + + std::thread m_sending; + std::thread m_receving; + std::string m_com_addr; + CmdQueue::QueueRef m_send_queue; + CmdQueue::QueueRef m_recv_queue; +}; + + +#endif \ No newline at end of file diff --git a/src/smg_server.cpp b/src/smg_server.cpp index b6e3cad4cf1d92de83f152782f5608a2ee5323c7..a85818fbd7665a1ab7cbadbf3b7c013f076c839f 100644 --- a/src/smg_server.cpp +++ b/src/smg_server.cpp @@ -11,6 +11,25 @@ #include "ModelState.hpp" +enum class MainState +{ + Simulation, + ManualControl +}; + +enum class SimState +{ + Entry, + Do, + Exit +}; + +bool sim_state_do(SmartGridModell &modell, ModelState &day) +{ + day.next_hour(); + std::this_thread::sleep_for(std::chrono::milliseconds(50)); +} + void run_sim() { i2c::Node i2c_device{0x14}; @@ -26,12 +45,47 @@ void run_sim() SmartGridModell modell{i2c_device}; ModelState day{modell}; + MainState main_state {MainState::Simulation}; + SimState sim_state {SimState::Entry}; + bool active{true}; while (active) { - day.next_hour(); - std::this_thread::sleep_for(std::chrono::milliseconds(50)); + switch (main_state) + { + case MainState::Simulation: + switch (sim_state) + { + case SimState::Entry: + modell.put_modell_into_state(SmartGridModell::DefaultState::Off); + sim_state = SimState::Do; + break; + case SimState::Do: + auto exit_state = sim_state_do(modell, day); + if (exit_state == true) + { + sim_state = SimState::Exit; + } + break; + case SimState::Exit: + modell.put_modell_into_state(SmartGridModell::DefaultState::Off); + sim_state = SimState::Entry; + main_state = MainState::ManualControl; + break; + } + break; + + case MainState::ManualControl: + + break; + } + + + + } + + modell.put_modell_into_state(SmartGridModell::DefaultState::Off); } void log_args(int argc, char **argv)