-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDCCpp_commands.cpp
251 lines (227 loc) · 7.35 KB
/
DCCpp_commands.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//
// Created by Mathieu Andrade on 23/05/2021.
//
#include <sstream>
#include <regex>
#include "DCCpp_commands.h"
#include "DCCpp_utils.h"
#include "DCCpp_emul.h"
std::list<std::string> DCCpp_commands::cmdToParse;
std::list<std::string> DCCpp_commands::cmdToSend;
CMD_WT_RSP_VECTOR DCCpp_commands::listOfCmdWaitingResp;
std::clock_t DCCpp_commands::timeElapsedSinceLastCmd;
bool DCCpp_commands::cmdStationReady = false;
void DCCpp_commands::waitSerialCommand()
{
char buffer[128];
bool msgClosed = false, result;
DWORD nbOfBytesRead = 0;
int i = 0;
DCCpp_utils::setTimeouts(0);
result = ReadFile(DCCpp::comPort, &buffer[i], 1, &nbOfBytesRead, nullptr);
if (result && nbOfBytesRead != 0)
{
do
{
nbOfBytesRead = 0;
i++;
result = ReadFile(DCCpp::comPort, &buffer[i], 1, &nbOfBytesRead, nullptr);
if (result && buffer[i] == '\n')
{
// DCCpp_utils::printDebugMessage("DCCpp_commands::waitSerialCommand -> buffer : " + std::string(buffer));
if (strstr(buffer, "ready") || strstr(buffer, "SERIAL"))
{
DCCpp_utils::printDebugMessage("DCCpp_commands::waitSerialCommand -> ready state found");
DCCpp_commands::cmdStationReady = true;
}
if (buffer[0] == '<')
{
msgClosed = true;
// Convert command char to string and save to stack
std::string command((const char *)buffer);
DCCpp_utils::printDebugMessage("DCCpp_commands::waitSerialCommand -> received serial message : " + command);
DCCpp_commands::cmdToParse.push_back(command);
// Reset buffer
strcpy(buffer, "");
}
else
{
// Reset buffer
strcpy(buffer, "");
msgClosed = true;
}
}
} while (!msgClosed);
}
}
void DCCpp_commands::waitWsCommands(const std::string &message)
{
if (message[0] == '<' && message[message.size() - 1] == '>')
{
DCCpp_commands::cmdToParse.push_back(const_cast<std::string &>(message));
}
}
bool DCCpp_commands::buildCommand(const DCC_CMD_TYPE &cmdType, const CMD_ARG args, bool emergency)
{
std::stringstream cmd;
std::string msg;
switch (cmdType)
{
case POWER_ON:
cmd << "<1>";
DCCpp_commands::sendCommand(cmd.str());
break;
case POWER_OFF:
cmd << "<0>";
DCCpp_commands::sendCommand(cmd.str());
break;
case CMD_STATION_VERSION_REQUEST:
cmd << "<s>";
DCCpp_commands::sendCommand(cmd.str());
break;
case ACCESSORY_OPERATION:
if (!args)
{
return false;
}
// <X 15 1>
cmd << "<" << DCCpp::accessoryCmdType << " " << args[0] << " " << args[1] << ">";
// This command need a feedback
DCCpp_utils::saveCmdWtRsp(ACCESSORY_OPERATION, ACCESSORY_EVENT, args);
DCCpp_commands::cmdToSend.push_back(cmd.str());
break;
case LOCO_SPEED:
if (!args)
{
return false;
}
// <t 1 1 0 1>
cmd << "<t"
<< " " << args[0] << " " << args[1] << " " << args[2] << " " << args[3] << ">";
// This command need a feedback
DCCpp_utils::saveCmdWtRsp(LOCO_SPEED, LOCO_SPEED_EVENT, args);
if (emergency)
{
DCCpp_commands::sendCommand(cmd.str());
}
else
{
DCCpp_commands::cmdToSend.push_back(cmd.str());
}
break;
case LOCO_FUNCTION:
if (!args)
{
return false;
}
// <f 3 144>
cmd << "<f"
<< " " << args[0] << " " << args[1] << ">";
// This command need a feedback
DCCpp_utils::saveCmdWtRsp(LOCO_FUNCTION, LOCO_FUNCTION_EVENT, args);
DCCpp_commands::cmdToSend.push_back(cmd.str());
break;
case INIT_S88:
if (!args)
{
return false;
}
// <Y 8 1>
cmd << "<Y"
<< " " << args[0] << " " << 1 << ">"; // 1 for hexadecimal format
DCCpp_commands::cmdToSend.push_back(cmd.str());
break;
case PING:
cmd << "<g1>";
DCCpp_commands::sendCommand(cmd.str());
break;
default:
cmd << "";
break;
}
return true;
}
void DCCpp_commands::sendCommand(const std::string &command)
{
if (DCCpp::emulation)
{
DCCpp_emul::emulResponse(command);
DCCpp_utils::printDebugMessage("DCCpp_commands::sendCommand : " + command);
return;
}
const char *str = command.c_str();
if (DCCpp::usbMode)
{
DWORD dwBytesWritten;
WriteFile(DCCpp::comPort, str, strlen(str), &dwBytesWritten, nullptr);
DCCpp_utils::printDebugMessage("DCCpp_commands::sendCommand : " + command);
}
else if (DCCpp::ws->getReadyState() != WebSocket::CLOSED)
{
DCCpp::ws->send(str);
DCCpp_utils::printDebugMessage("DCCpp_commands::sendCommand : " + command);
}
else if (DCCpp::ws->getReadyState() == WebSocket::CLOSED)
{
DCCpp_utils::printDebugMessage("DCCpp_commands::sendCommand : /!\\ Websocket server has been closed ");
}
}
void DCCpp_commands::checkCmdToSend()
{
auto elapsed = (std::clock() - DCCpp_commands::timeElapsedSinceLastCmd) / (double)(CLOCKS_PER_SEC / 1000);
if (!DCCpp_commands::cmdToSend.empty() && DCCpp::commandStationStatus == 0 && elapsed > DCCpp::cmdTimer)
{
std::string command = *DCCpp_commands::cmdToSend.begin();
DCCpp_commands::cmdToSend.remove(command);
DCCpp_commands::sendCommand(command);
DCCpp_commands::timeElapsedSinceLastCmd = std::clock();
}
}
void DCCpp_commands::parse()
{
if (!DCCpp_commands::cmdToParse.empty())
{
const std::regex reg("\\<.*?\\>");
std::cmatch match;
std::string command = *DCCpp_commands::cmdToParse.begin();
// Remove command from stack
DCCpp_commands::cmdToParse.remove(command);
// Clean the command to get only important things
std::regex_search(command.c_str(), match, reg);
// And reassign it for next steps
command = match[0].str();
DCCpp_utils::printDebugMessage(command);
if (command.rfind("<p0>", 1) == 0)
{
DCCpp::handleCommandStationStatus(-1);
}
else if (command.rfind("<p1>", 1) == 0)
{
DCCpp::handleCommandStationStatus(1);
}
else if (command.rfind("<g1>", 1) == 0)
{
DCCpp_commands::buildCommand(PING);
}
else if (command.rfind("<y", 0) == 0)
{
DCCpp::handleDetectorUpdate(command);
}
else if (command.rfind("<H", 0) == 0)
{
DCCpp::handleAccessoryEvent(command);
}
else if (command.rfind("<T", 0) == 0 && command.size() > 9)
{
DCCpp::handleLocoEvent(command, LOCO_SPEED_EVENT);
}
else if (command.rfind("<F", 0) == 0)
{
DCCpp::handleLocoEvent(command, LOCO_FUNCTION_EVENT);
}
else if (command.rfind("<iDCCpp", 0) == 0)
{
DCCpp::handleCommandStationVersion(command);
}
}
}