-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeviceinteractor.cpp
More file actions
118 lines (104 loc) · 3.58 KB
/
Copy pathdeviceinteractor.cpp
File metadata and controls
118 lines (104 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "deviceinteractor.h"
#include "tcpclient.h"
#include "deviceconfiguration.h"
#include "deviceconfigurationstore.h"
DeviceInteractor::DeviceInteractor(DevicePresenter& Presenter, DeviceConfigurationStore& Store) :
m_DevicePresenter(Presenter),
m_ConfigurationStore(Store)
{
Store.GetAllDeviceConfigurations();
}
DeviceInteractor::~DeviceInteractor()
{
for(auto const& Iterator : m_DeviceMap)
{
delete Iterator.second;
}
}
void DeviceInteractor::InitFromStore(boost::asio::io_service& IOService)
{
auto StoredConfigurations = m_ConfigurationStore.GetAllDeviceConfigurations();
for(auto& Configuration : StoredConfigurations)
{
CreateNewDevice(Configuration, IOService);
}
}
void DeviceInteractor::CreateNewTcpDevice(const std::string& Name,
const std::string& Host,
unsigned int Port,
boost::asio::io_service& IOService)
{
if (m_DeviceMap.find(Name) != m_DeviceMap.end()) { return; } // key exists
TcpClient* TcpDevice = new TcpClient(IOService, Host, Port, *this);
m_DeviceMap[Name] = TcpDevice;
PresentDevices();
}
void DeviceInteractor::CreateNewDevice(const DeviceConfiguration& Configuration,
boost::asio::io_service& IOService)
{
std::string DeviceType;
std::string Name;
if(Configuration.Get("Type", DeviceType) && DeviceType == "TCP_CLIENT" && // TODO Remove constant
Configuration.Get("Name", Name))
{
if (m_DeviceMap.find(Name) != m_DeviceMap.end()) { return; } // key exists
TcpClient* TcpDevice = new TcpClient(IOService, Configuration, *this);
m_DeviceMap[Name] = TcpDevice;
m_ConfigurationStore.StoreDeviceConfiguration(Name, Configuration);
PresentDevices();
}
}
void DeviceInteractor::StartDevice(const std::string& Name)
{
if (m_DeviceMap.find(Name) == m_DeviceMap.end()) return; // Key Doesn't Exist
if(m_DeviceMap[Name]->ReadyToStart()) m_DeviceMap[Name]->Start();
}
void DeviceInteractor::StopDevice(const std::string& Name)
{
if (m_DeviceMap.find(Name) == m_DeviceMap.end()) return; // Key Doesn't Exist
m_DeviceMap[Name]->Reset();
}
void DeviceInteractor::RemoveDevice(const std::string& Name)
{
if (m_DeviceMap.find(Name) == m_DeviceMap.end()) return; // Key Doesn't Exist
delete m_DeviceMap[Name];
m_DeviceMap.erase(Name);
m_ConfigurationStore.RemoveDeviceConfiguration(Name);
PresentDevices();
}
void DeviceInteractor::WriteToDevice(const std::string& Name, const std::string& Message)
{
if (m_DeviceMap.find(Name) == m_DeviceMap.end()) return; // Key Doesn't Exist
m_DeviceMap[Name]->Write(Message);
}
// Device State Change Listener
void DeviceInteractor::OnStateChange()
{
PresentDevices();
}
void DeviceInteractor::PresentDevices()
{
std::map<std::string, std::string> DeviceStates;
for (auto& Iterator : m_DeviceMap)
{
auto CurrentDeviceState = Iterator.second->GetState();
switch(CurrentDeviceState)
{
case ClientState::READY:
DeviceStates[Iterator.first] = "Ready";
break;
case ClientState::CONNECTING:
DeviceStates[Iterator.first] = "Connecting";
break;
case ClientState::CONNECTED:
DeviceStates[Iterator.first] = "Connected";
break;
case ClientState::ERROR:
DeviceStates[Iterator.first] = "Error";
break;
default:
DeviceStates[Iterator.first] = "Unknown";
}
}
m_DevicePresenter.PresentDeviceState(DeviceStates);
}