-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogiccontroller.cpp
109 lines (95 loc) · 2.8 KB
/
logiccontroller.cpp
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
#include "logiccontroller.h"
/*!
Создаёт экземпляр класса логики управления роботом.
*/
LogicController::LogicController()
{
config = new ConfigProcessor(); // Load config file
fillFieldsByConfig(); // Fill fields by config file
printVersion(); // Print info about current AmurClient configuration
periphery = new PeripheralController(&controls, &sensors);
network = new NetworkController(&controls, &sensors);
network->startArpingService(bcastPort, arpingPort);
connectToServer();
worker();
}
LogicController::~LogicController()
{
std::cout << "Close LogicController... " << std::endl;
delete periphery;
delete network;
delete config;
delete configName;
}
/*!
Функция проверяющая и заполняющая значения полей из конфиг файла
*/
void LogicController::fillFieldsByConfig()
{
std::string ip ;
int result = config->searchString("Amur.Network.address", ip);
if(ip != ""){
this->address = ip;
}
}
/*!
Функция печати версии программы
*/
int LogicController::printVersion()
{
std::string version;
if(config->searchString("version", version) == 0)
std::cout << "Version: " << version << std::endl;
else
return -1;
return 0;
}
/*!
Главный цикл
*/
void LogicController::worker()
{
while(!workerStopped)
{
// std::string s2 = controlsPrev.DebugString();
// std::string s1 = controls.DebugString();
// std::string s4 = controlsPrev.SerializeAsString();
// std::string s3 = controls.SerializeAsString();
// if( s1 != s2 )
{
periphery->updateData();
controlsPrev = controls;
}
std::this_thread::sleep_for(std::chrono::milliseconds(10)); // Wait 10 milliseconds
}
}
/*!
Инициирует подключение к серверу.
\param[in] host Адрес сервера
\param[in] port Порт сервера
*/
void LogicController::connectToServer(std::string host, unsigned int port)
{
this->address = host + ":" + std::to_string(port);
connectToServer();
}
/*!
Инициирует подключение к серверу с адресом и портом по умолчанию.
*/
void LogicController::connectToServer()
{
if(address == ""){
std::cout << "No IP in config. Wait answer from server..." << std::endl;
while(true){
if(network->runClient() == 0){
std::cout << "Connected" << std::endl;
network->stopArpingService();
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Wait 10 milliseconds
}
}
else{
network->runClient(address);
}
}