Skip to content
Snippets Groups Projects
Commit b6e5cb57 authored by Armin Co's avatar Armin Co
Browse files

Update

parent 54ab4496
Branches
No related tags found
No related merge requests found
......@@ -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);
}
......
......@@ -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)
{
......
/// @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
/// @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
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment