-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimulatorExchangeSender.cpp
306 lines (290 loc) · 13.9 KB
/
simulatorExchangeSender.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "simulatorExchangeSender.hpp"
#include "telegramDescriber.hpp"
#include <fstream>
#include <memory>
#include <rapidjson/document.h>
#include <rapidjson/istreamwrapper.h>
static sepreference::DescriberValidationResult
validateDescriber(rapidjson::Document &describer) {
if (!describer.HasMember("telegrams"))
return sepreference::DescriberValidationResult::
DESCRIBER_NO_TOPLEVEL_TELEGRAMS;
auto &json_telegrams = describer["telegrams"];
if (!json_telegrams.IsArray())
return sepreference::DescriberValidationResult::
DESCRIBER_TOPLEVEL_TELEGRAMS_NOT_ARRAY;
for (auto &json_telegram : json_telegrams.GetArray()) {
size_t bitsize = 0;
if (!json_telegram.HasMember("IP"))
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAM_NO_IP;
if (!json_telegram["IP"].IsString())
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAM_IP_NOT_STRING;
if (!json_telegram.HasMember("port"))
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAM_NO_PORT;
if (!json_telegram["port"].IsInt())
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAM_PORT_NOT_INT;
if (json_telegram.HasMember("cycle") && !json_telegram["cycle"].IsInt())
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAM_CYCLE_NOT_INT;
if (!json_telegram.HasMember("format"))
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAM_NO_FORMAT;
auto &format = json_telegram["format"];
if (!format.IsArray())
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAM_FORMAT_NOT_ARRAY;
for (auto &json_tp : format.GetArray()) {
if (!json_tp.HasMember("name"))
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAMPART_NO_NAME;
if (!json_tp["name"].IsString())
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAMPART_NAME_NOT_STRING;
if (!json_tp.HasMember("type"))
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAMPART_NO_TYPE;
auto &json_tp_type = json_tp["type"];
if (!json_tp_type.IsString())
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAMPART_TYPE_NOT_STRING;
const sepreference::TelegramPartType partType =
sepreference::getTelegramPartType(json_tp_type.GetString());
if (partType == sepreference::TelegramPartType::unknown)
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAMPART_TYPE_UNKNOWN;
struct sepreference::TelegramPart tp;
if (partType == sepreference::TelegramPartType::string) {
if (!json_tp.HasMember("length"))
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAMPART_STRING_NO_LENGTH;
if (!json_tp["length"].IsInt())
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAMPART_STRING_LENGTH_NOT_INT;
if (json_tp.HasMember("factor"))
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAMPART_STRING_ILLEGAL_FACTOR;
if (json_tp.HasMember("default"))
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAMPART_STRING_ILLEGAL_DEFAULT;
if (json_tp.HasMember("hysteresis"))
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAMPART_STRING_ILLEGAL_HYSTERESIS;
tp.len = json_tp["length"].GetInt();
} else {
if (json_tp.HasMember("factor") && !json_tp["factor"].IsInt())
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAMPART_FACTOR_NOT_INT;
if (json_tp.HasMember("default") && !json_tp["default"].IsInt())
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAMPART_DEFAULT_NOT_INT;
if (json_tp.HasMember("hysteresis") &&
!json_tp["hysteresis"].IsInt())
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAMPART_HYSTERESIS_NOT_INT;
if (json_tp.HasMember("length"))
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAMPART_NONSTRING_ILLEGAL_LENGTH;
}
tp.type = partType;
bitsize += tp.size();
}
if (bitsize > sepreference::Telegram::maxBytesize * 8)
return sepreference::DescriberValidationResult::
DESCRIBER_TELEGRAM_TOO_LONG;
}
return sepreference::DescriberValidationResult::DESCRIBER_VALID;
}
namespace sepreference {
SimulatorExchangeSenderState SimulatorExchangeSender::state =
SimulatorExchangeSenderState::STATE_OFF;
std::unique_ptr<TelegramDescriber> SimulatorExchangeSender::describer =
std::unique_ptr<TelegramDescriber>(nullptr);
DescriberValidationResult SimulatorExchangeSender::validationResult =
DescriberValidationResult::DESCRIBER_NOT_PROVIDED;
bool SimulatorExchangeSender::init(std::string &filename) {
if (state == SimulatorExchangeSenderState::STATE_OFF) {
rapidjson::Document j;
std::ifstream i(filename);
if (!i.fail()) {
rapidjson::IStreamWrapper isw(i);
j.ParseStream(isw);
if (j.GetParseError() !=
rapidjson::ParseErrorCode::kParseErrorNone) {
validationResult =
DescriberValidationResult::DESCRIBER_INVALID_JSON;
return false;
}
validationResult = validateDescriber(j);
if (validationResult != DescriberValidationResult::DESCRIBER_VALID)
return false;
describer =
std::unique_ptr<TelegramDescriber>(new TelegramDescriber(j));
state = SimulatorExchangeSenderState::STATE_INITIALISED;
return true;
} else {
validationResult =
DescriberValidationResult::DESCRIBER_CANNOT_READ_FILE;
}
} else {
return true;
}
return false;
}
bool SimulatorExchangeSender::allowSending(bool allowed) {
if (allowed) {
switch (state) {
case SimulatorExchangeSenderState::STATE_INITIALISED:
state = SimulatorExchangeSenderState::STATE_SENDING;
describer->setSending(true);
case SimulatorExchangeSenderState::STATE_SENDING:
return true;
default:
return false;
}
} else {
switch (state) {
case SimulatorExchangeSenderState::STATE_SENDING:
state = SimulatorExchangeSenderState::STATE_INITIALISED;
describer->setSending(false);
case SimulatorExchangeSenderState::STATE_INITIALISED:
return true;
default:
return false;
}
}
}
void SimulatorExchangeSender::updateValue(const std::string &name, float val) {
if (state != SimulatorExchangeSenderState::STATE_OFF)
describer->updateValue<float>(name, val);
}
void SimulatorExchangeSender::updateValue(const std::string &name,
int8_t val) {
if (state != SimulatorExchangeSenderState::STATE_OFF) {
auto uval = static_cast<uint8_t>(val);
describer->updateValue<uint8_t>(name, uval);
}
}
void SimulatorExchangeSender::updateValue(const std::string &name,
uint8_t val) {
if (state != SimulatorExchangeSenderState::STATE_OFF) {
describer->updateValue<uint8_t>(name, val);
}
}
void SimulatorExchangeSender::updateValue(const std::string &name,
int16_t val) {
if (state != SimulatorExchangeSenderState::STATE_OFF) {
auto uval = static_cast<uint16_t>(val);
describer->updateValue<uint16_t>(name, uval);
}
}
void SimulatorExchangeSender::updateValue(const std::string &name,
uint16_t val) {
if (state != SimulatorExchangeSenderState::STATE_OFF) {
describer->updateValue<uint16_t>(name, val);
}
}
void SimulatorExchangeSender::updateValue(const std::string &name,
int32_t val) {
if (state != SimulatorExchangeSenderState::STATE_OFF) {
auto uval = static_cast<uint32_t>(val);
describer->updateValue<uint32_t>(name, uval);
}
}
void SimulatorExchangeSender::updateValue(const std::string &name,
uint32_t val) {
if (state != SimulatorExchangeSenderState::STATE_OFF) {
describer->updateValue<uint32_t>(name, val);
}
}
void SimulatorExchangeSender::updateValue(const std::string &name,
std::wstring &val) {
if (state != SimulatorExchangeSenderState::STATE_OFF)
describer->updateStringValue(name, val);
}
void SimulatorExchangeSender::updateValue(const std::string &name,
std::string &val) {
if (state != SimulatorExchangeSenderState::STATE_OFF)
describer->updateStringValue(name, val);
}
void SimulatorExchangeSender::updateValue(const std::string &name,
std::basic_string<char16_t> &val) {
if (state != SimulatorExchangeSenderState::STATE_OFF)
describer->updateStringValue(name, val);
}
const std::string SimulatorExchangeSender::getErrorMsg() {
switch (validationResult) {
case DescriberValidationResult::DESCRIBER_VALID:
return "Valid telegram describer.";
case DescriberValidationResult::DESCRIBER_NOT_PROVIDED:
return "No describer provided yet.";
case DescriberValidationResult::DESCRIBER_CANNOT_READ_FILE:
return "Cannot read provided file.";
case DescriberValidationResult::DESCRIBER_INVALID_JSON:
return "Provided file is no valid json.";
case DescriberValidationResult::DESCRIBER_NO_TOPLEVEL_TELEGRAMS:
return "No single top-level element \"telegrams\".";
case DescriberValidationResult::DESCRIBER_TOPLEVEL_TELEGRAMS_NOT_ARRAY:
return "Top-level element \"telegrams\" is not an array.";
case DescriberValidationResult::DESCRIBER_TELEGRAM_NO_IP:
return "Telegram does not have mandatory element \"IP\".";
case DescriberValidationResult::DESCRIBER_TELEGRAM_IP_NOT_STRING:
return "Telegram element \"IP\" is not a string.";
case DescriberValidationResult::DESCRIBER_TELEGRAM_NO_PORT:
return "Telegram does not have mandatory element \"port\".";
case DescriberValidationResult::DESCRIBER_TELEGRAM_PORT_NOT_INT:
return "Telegram element \"port\" is not an int.";
case DescriberValidationResult::DESCRIBER_TELEGRAM_CYCLE_NOT_INT:
return "Telegram element \"port\" is not an int.";
case DescriberValidationResult::DESCRIBER_TELEGRAM_NO_FORMAT:
return "Telegram does not have mandatory element \"port\".";
case DescriberValidationResult::DESCRIBER_TELEGRAM_FORMAT_NOT_ARRAY:
return "Telegram element \"format\" is not an array.";
case DescriberValidationResult::DESCRIBER_TELEGRAM_TOO_LONG:
return "Telegram is too long.";
case DescriberValidationResult::DESCRIBER_TELEGRAMPART_NO_NAME:
return "Telegram part does not have mandatory element \"name\".";
case DescriberValidationResult::DESCRIBER_TELEGRAMPART_NAME_NOT_STRING:
return "Telegram part element \"name\" is not a string.";
case DescriberValidationResult::DESCRIBER_TELEGRAMPART_NO_TYPE:
return "Telegram part does not have mandatory element \"type\".";
case DescriberValidationResult::DESCRIBER_TELEGRAMPART_TYPE_NOT_STRING:
return "Telegram part element \"type\" is not a string.";
case DescriberValidationResult::DESCRIBER_TELEGRAMPART_TYPE_UNKNOWN:
return "Telegram part element \"type\" value is unknown.";
case DescriberValidationResult::DESCRIBER_TELEGRAMPART_FACTOR_NOT_INT:
return "Telegram part element \"factor\" is not an int.";
case DescriberValidationResult::DESCRIBER_TELEGRAMPART_DEFAULT_NOT_INT:
return "Telegram part element \"default\" is not an int.";
case DescriberValidationResult::DESCRIBER_TELEGRAMPART_HYSTERESIS_NOT_INT:
return "Telegram part element \"hysteresis\" is not an int.";
case DescriberValidationResult::DESCRIBER_TELEGRAMPART_STRING_NO_LENGTH:
return "Telegram part type is string, but mandatory element \"length\" "
"is not present.";
case DescriberValidationResult::
DESCRIBER_TELEGRAMPART_STRING_LENGTH_NOT_INT:
return "Telegram part element \"length\" is not an int.";
case DescriberValidationResult::
DESCRIBER_TELEGRAMPART_STRING_ILLEGAL_FACTOR:
return "Telegram part type is string, for that, the element \"factor\" "
"is illegal.";
case DescriberValidationResult::
DESCRIBER_TELEGRAMPART_STRING_ILLEGAL_DEFAULT:
return "Telegram part type is string, for that, the element "
"\"default\" is illegal.";
case DescriberValidationResult::
DESCRIBER_TELEGRAMPART_STRING_ILLEGAL_HYSTERESIS:
return "Telegram part type is string, for that, the element "
"\"hysteresis\" is illegal.";
case DescriberValidationResult::
DESCRIBER_TELEGRAMPART_NONSTRING_ILLEGAL_LENGTH:
return "Telegram part type is not string, for that, the element "
"\"length\" is illegal.";
default:
return "Unknown error.";
}
}
} // namespace sepreference