From dfa0bef9983578ed705d742e550ce5e6158d8f60 Mon Sep 17 00:00:00 2001
From: Armin Co <armin.co@hs-bochum.de>
Date: Sat, 3 Oct 2020 13:55:31 +0200
Subject: [PATCH] Reorganized structure

Fixed sgm protocol
---
 CMakeLists.txt                              | 15 +++--
 src/ControlMessags.hpp                      | 14 -----
 src/{ => apps/SmgOPC_server}/opc_server.cpp |  0
 src/com/ProtocolSgm.cpp                     | 34 +++++------
 src/com/ProtocolSgm.hpp                     | 22 +++----
 src/manual_control/sample_server.cpp        |  3 +
 src/{ => smart_grid}/ModelState.cpp         |  0
 src/{ => smart_grid}/ModelState.hpp         |  0
 src/{ => smart_grid}/SmartGridModell.cpp    |  0
 src/{ => smart_grid}/SmartGridModell.hpp    |  0
 src/smg_server.cpp                          | 64 +++++++++++----------
 11 files changed, 68 insertions(+), 84 deletions(-)
 delete mode 100644 src/ControlMessags.hpp
 rename src/{ => apps/SmgOPC_server}/opc_server.cpp (100%)
 create mode 100644 src/manual_control/sample_server.cpp
 rename src/{ => smart_grid}/ModelState.cpp (100%)
 rename src/{ => smart_grid}/ModelState.hpp (100%)
 rename src/{ => smart_grid}/SmartGridModell.cpp (100%)
 rename src/{ => smart_grid}/SmartGridModell.hpp (100%)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 40f247c..098a1c2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -7,6 +7,13 @@ set(CMAKE_CXX_STANDARD 17)
 add_subdirectory(libs/spdlog)
 
 include_directories(src)
+include_directories(libs/spdlog/include)
+
+include_directories(src/com)
+add_library(soc_com
+    src/com/Protocol.cpp
+    src/com/ProtocolSgm.cpp
+    src/com/Socket.cpp)
 
 # Test application to manually send commands to the Arduino
 add_executable(manual_control src/manual_control/ComTest.cpp)
@@ -21,15 +28,15 @@ target_link_libraries(sample_client spdlog)
 
 # Smart Grid Simulation
 include_directories(src/i2c)
-include_directories(src/com)
+include_directories(src/smart_grid)
 
 add_executable(smart_grid.exe
     src/smg_server.cpp
-    src/SmartGridModell.cpp
+    src/smart_grid/SmartGridModell.cpp
     src/i2c/Node.cpp
     src/com/Socket.cpp
     src/com/Protocol.cpp
-    src/ModelState.cpp
+    src/smart_grid/ModelState.cpp
 )
 
 target_link_libraries(smart_grid.exe spdlog)
@@ -50,6 +57,6 @@ option(BUILD_TESTING "Build and run tests" OFF)
 option(BUILD_SHARED_LIBS "Build shared libraries." ON)
 add_subdirectory(libs/freeopcua)
 
-add_executable(opc_server.exe src/opc_server.cpp)
+add_executable(opc_server.exe src/apps/SmgOPC_server/opc_server.cpp)
 target_include_directories(opc_server.exe PUBLIC libs/freeopcua/include)
 target_link_libraries(opc_server.exe opcuacore opcuaserver)
\ No newline at end of file
diff --git a/src/ControlMessags.hpp b/src/ControlMessags.hpp
deleted file mode 100644
index 3807edd..0000000
--- a/src/ControlMessags.hpp
+++ /dev/null
@@ -1,14 +0,0 @@
-/// @file   ControlMessages.hpp
-///
-
-#ifndef CONTROL_MESSAGES_HPP
-#define CONTROL_MESSAGES_HPP
-
-#include <string>
-
-enum class CtrlMsgs
-{
-
-};
-
-#endif
\ No newline at end of file
diff --git a/src/opc_server.cpp b/src/apps/SmgOPC_server/opc_server.cpp
similarity index 100%
rename from src/opc_server.cpp
rename to src/apps/SmgOPC_server/opc_server.cpp
diff --git a/src/com/ProtocolSgm.cpp b/src/com/ProtocolSgm.cpp
index 694a0f4..d0e35e1 100644
--- a/src/com/ProtocolSgm.cpp
+++ b/src/com/ProtocolSgm.cpp
@@ -5,34 +5,22 @@
 #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);
+    m_receiving = std::thread(&ProtocolSgm::receiving, this);
 }
 
 void ProtocolSgm::sending()
 {
-    if (m_send_queue.empty() == true)
+    if (m_send_queue->empty() == true)
     {
-        std::this_thread::sleep_for(std::chrono::millisecons(1));
+        std::this_thread::sleep_for(std::chrono::milliseconds(1));
     }
     else
     {
-        auto cmd = m_queue.back();
+        auto cmd = m_send_queue->back();
         ConnectSocket connect{m_com_addr, sgm_com_port};
         ProtocolSimple prot{connect};
 
@@ -46,31 +34,35 @@ void ProtocolSgm::sending()
         {
             // something went wrong
         }
-        m_send_queue.pop_back();
+        else
+        {
+            m_send_queue->pop_back();
+        }
     }
 }
 
 void ProtocolSgm::receiving()
 {
     ServerSocket server{sgm_com_port};
+    bool continue_rece {true};
     while (true)
     {
         DataSocket sock = server.accept();
         ProtocolSimple prot{sock};
 
-        CommandSgm cmd;
+        CommandSgm cmd {};
         prot.recv_message(cmd.cmd);
         prot.recv_message(cmd.val);
         prot.recv_message(cmd.ack);
 
-        m_recv_queue.push_back(cmd);
+        m_recv_queue->push_back(cmd);
 
-        prot.send_message(cmd.ack);
+        prot.send_message(m_com_addr, cmd.ack);
     }
 }
 
 
 void ProtocolSgm::send_cmd(CommandSgm const& cmd)
 {
-    m_queue.push_back(cmd);
+    m_send_queue->push_back(cmd);
 }
\ No newline at end of file
diff --git a/src/com/ProtocolSgm.hpp b/src/com/ProtocolSgm.hpp
index 963c944..0f588b3 100644
--- a/src/com/ProtocolSgm.hpp
+++ b/src/com/ProtocolSgm.hpp
@@ -18,33 +18,25 @@ struct CommandSgm
     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);
 
+    using QueueRef = std::shared_ptr<std::vector<CommandSgm>>;
+    static const int sgm_com_port {8080};
+
 private:
     void sending();
-    void receving();
+    void receiving();
 
     std::thread m_sending;
-    std::thread m_receving;
+    std::thread m_receiving;
     std::string m_com_addr;
-    CmdQueue::QueueRef m_send_queue;
-    CmdQueue::QueueRef m_recv_queue;
+    QueueRef m_send_queue;
+    QueueRef m_recv_queue;
 };
 
 
diff --git a/src/manual_control/sample_server.cpp b/src/manual_control/sample_server.cpp
new file mode 100644
index 0000000..e4e0c73
--- /dev/null
+++ b/src/manual_control/sample_server.cpp
@@ -0,0 +1,3 @@
+
+#include <com/Socket.hpp>
+#include <com/Protocol.hpp
\ No newline at end of file
diff --git a/src/ModelState.cpp b/src/smart_grid/ModelState.cpp
similarity index 100%
rename from src/ModelState.cpp
rename to src/smart_grid/ModelState.cpp
diff --git a/src/ModelState.hpp b/src/smart_grid/ModelState.hpp
similarity index 100%
rename from src/ModelState.hpp
rename to src/smart_grid/ModelState.hpp
diff --git a/src/SmartGridModell.cpp b/src/smart_grid/SmartGridModell.cpp
similarity index 100%
rename from src/SmartGridModell.cpp
rename to src/smart_grid/SmartGridModell.cpp
diff --git a/src/SmartGridModell.hpp b/src/smart_grid/SmartGridModell.hpp
similarity index 100%
rename from src/SmartGridModell.hpp
rename to src/smart_grid/SmartGridModell.hpp
diff --git a/src/smg_server.cpp b/src/smg_server.cpp
index 69a96f5..7d5af53 100644
--- a/src/smg_server.cpp
+++ b/src/smg_server.cpp
@@ -24,10 +24,14 @@ enum class SimState
     Exit
 };
 
-bool sim_state_do(SmartGridModell &modell, ModelState &day)
+bool sim_state_do(ModelState &day)
 {
+    bool exit_to_manual_control {false};
+
+    // check for new inputs
     day.next_hour();
-    std::this_thread::sleep_for(std::chrono::milliseconds(50));
+
+    return exit_to_manual_control;
 }
 
 void run_sim()
@@ -51,34 +55,34 @@ void run_sim()
     bool active{true};
     while (active)
     {
-        // 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;
-        // }
+        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:
+                std::this_thread::sleep_for(std::chrono::milliseconds(50));
+                if (sim_state_do(day) == 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);
-- 
GitLab