-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwialon_units_migrate.cpp
539 lines (446 loc) · 18.5 KB
/
wialon_units_migrate.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
// Copyright (c) 2020 Dmitriy Bassamykin.
// Licensed under the MIT license <http://opensource.org/licenses/MIT>.
/**
@file
@brief Основной файл программы
*/
#include <iostream>
#include <filesystem>
#include <string>
#include <iostream>
#include <clocale>
#include "CWLUnit.h"
#include "CCmdArgs.h"
#include "pugixml.hpp"
using namespace pugi;
namespace fs = std::filesystem;
static bool isDirExist(const std::string& dirPath);
static bool isFileExist(const std::string& fileName);
static bool processXmlFile(const std::string& xmlFilename, const std::string& wlpFilename);
static void addGeneral(const xml_node& node, CWLUnit& wlp);
static void addCustomFields(const xml_node& node, CWLUnit& wlp);
static void addCounters(const xml_node& node, CWLUnit& wlp);
static void addSensor(const xml_node& node, CWLUnit& wlp, const xml_node& fuelConfigMathNode = xml_node());
static void addTripDetector(const xml_node& node, CWLUnit& wlp);
static void addFuelConsumpt(const xml_node& node, CWLUnit& wlp);
static size_t convertPath(const std::string& inDirPath, const std::string& outDirPath, const bool overwrite = false);
static bool convertSingleFile(const std::string& inFileName, const std::string& outDirPath, const bool overwrite = false);
static void printHelp();
/**
@brief Основная функция программы
@param[in] argc Число аргументов командной строки
@param[in] argv An argument vector of the command line arguments
@param [in] envp Переменные окружения
@return Код возврата. 0 в случае успеха
*/
int main(int argc, char* argv[], char* envp[])
{
//Set correct russian characters output
setlocale(LC_ALL, "");
setlocale(LC_NUMERIC, "C");
CCmdArgs args(argc, argv);
if (args.isArgExist("--help") || args.isArgExist("-h")) {
printHelp();
return 0;
}
if (args.isArgExist("--ipath") && args.isArgExist("--ifile")) {
std::cout << "You must specify only one parameter of --ipath or --ifile" << std::endl;
return -1;
}
if (!args.isArgExist("--opath")) {
std::cout << "You must specity ouput directory path" << std::endl;
return -1;
}
std::string inFileName = args.argValue("--ifile");
std::string inDirPath = args.argValue("--ipath");
std::string outDirPath = args.argValue("--opath");
bool overwrite = false;
if (args.isArgExist("--overwrite")) {
overwrite = true;
}
if (!inFileName.empty()) {
//std::cout << "In filename " << inFileName << std::endl;
if (!isFileExist(inFileName)) {
std::cout << "Input file not exists" << std::endl;
return -1;
}
}
if (!inDirPath.empty()) {
//std::cout << "In dir path " << inDirPath << std::endl;
if (!isDirExist(inDirPath)) {
std::cout << "Input dir path is not valid" << std::endl;
return -1;
}
}
if (!outDirPath.empty()) {
//std::cout << "Out dir path " << outDirPath << std::endl;
if (!isDirExist(outDirPath)) {
std::cout << "Output dir path is not valid" << std::endl;
return -1;
}
}
//Convert single xml file
if (!inFileName.empty()) {
return (convertSingleFile(inFileName, outDirPath, overwrite)) ? 0 : -1;
}
//Convert files in path
if (!inDirPath.empty()) {
size_t successConvert = convertPath(inDirPath, outDirPath, overwrite);
std::cout << std::endl << "Complete " << successConvert << " files" << std::endl;
return (successConvert != 0) ? 0 : -1;
}
}
/**
@brief Проверяем, существует ли указанная директория
@param[in] dirPath Ссылка на строку пути к директории
@return true если директрония существует
*/
static bool isDirExist(const std::string& dirPath)
{
fs::path pathToCheck(dirPath);
std::error_code error;
if (fs::exists(pathToCheck, error) && fs::is_directory(pathToCheck, error)) {
return true;
}
return false;
}
/**
@brief Проверяем, существует ли указанный файл
@param[in] fileName Ссылка на строку пути к файлу
@return true если файл существует
*/
static bool isFileExist(const std::string& fileName)
{
fs::path pathToCheck(fileName);
std::error_code error;
if (fs::exists(pathToCheck, error) && fs::is_regular_file(pathToCheck, error)) {
return true;
}
return false;
}
/**
@brief Конвертация файла xml (Wialon Pro) в файл wlp (Wialon Local)
@param[in] xmlFilename Путь к файлу xml
@param[in] wlpFilename Путь к файлу wlp
@return true в случае успеха
*/
static bool processXmlFile(const std::string& xmlFilename, const std::string& wlpFilename)
{
if (xmlFilename.empty() || wlpFilename.empty()) {
return false;
}
xml_document doc;
CWLUnit wlp;
if (!doc.load_file(xmlFilename.c_str())) {
return false;
}
xml_node device = doc.child("devices").child("device");
if (device.empty()) {
return false;
}
//General settings
addGeneral(device, wlp);
xml_node node = device.child("custom");
//Custom fields
addCustomFields(node, wlp);
//Counters
node = device.child("counters");
addCounters(node, wlp);
//Sensors
node = device.child("sensors");
for (auto sensor = node.child("sensor"); sensor; sensor = sensor.next_sibling("sensor")) {
addSensor(sensor, wlp, device.child("fuelc").child("math"));
}
//Trip detector
node = device.child("tripd");
if (node) {
addTripDetector(node, wlp);
}
//Fiel consumption
node = device.child("fuelc");
if (node) {
addFuelConsumpt(node, wlp);
}
if (!wlp.wlpSave(wlpFilename)) {
return false;
}
return true;
}
/**
@brief Добавить основные свойства объекта
@param[in] node Раздел xml документа с основными свойствами
@param[in] wlp
*/
static void addGeneral(const xml_node& node, CWLUnit& wlp)
{
wlpGeneral general;
general.name = node.attribute("name").value();
general.phone1 = node.attribute("phone").value();
general.hw = node.attribute("type").value();
general.uid = node.attribute("unique_id").value();
wlp.addGeneral(general);
}
/**
@brief Добавить проивользое поле
@param[in] node Раздел xml документа с произвольными полями
@param[in] wlp
*/
static void addCustomFields(const xml_node& node, CWLUnit& wlp)
{
for (auto customField = node.child("field"); customField; customField = customField.next_sibling("field")) {
wlp.addAfield(customField.attribute("name").value(), customField.attribute("value").value());
}
}
/**
@brief Добавить счетчики
@param[in] node Раздел xml документа со значениями счетчиков
@param[in] wlp
*/
static void addCounters(const xml_node& node, CWLUnit& wlp)
{
for (auto counter = node.first_child(); counter; counter = counter.next_sibling()) {
std::string counterName = counter.name();
if (counterName == "engine_hours") {
wlp.addCounterEngineHours(
counter.attribute("value").as_double(),
counter.attribute("auto").as_bool(),
counter.attribute("type").value()
);
}
else if (counterName == "mileage") {
wlp.addCounterMileage(
counter.attribute("value").as_double(),
counter.attribute("auto").as_bool(),
counter.attribute("type").value()
);
}
else if (counterName == "bytes") {
wlp.addCounterBytes(
counter.attribute("value").as_double() / 1024.0,
counter.attribute("auto").as_bool()
);
}
}
}
/**
@brief Добавить датчик
@param[in] node Раздел xml документа с основными свойствами
@param[in] wlp
@param[in] fuelConfigMathNode Раздел xml документа с данными по расходу топлива
*/
static void addSensor(const xml_node& node, CWLUnit& wlp, const xml_node& fuelConfigMathNode)
{
wlpSensor wlp_sensor;
wlp.createDefaultSensorConfig(wlp_sensor.config);
wlp_sensor.description = node.attribute("description").value();
std::string stmp = node.attribute("flags").value();
wlp_sensor.flags = std::stoul(stmp, nullptr, 16);
wlp_sensor.id = node.attribute("id").as_int();
wlp_sensor.unit = node.attribute("unit").value();
wlp_sensor.name = node.attribute("name").value();
wlp_sensor.parameter = node.attribute("parameter").value();
wlp_sensor.type = node.attribute("type").value();
wlp_sensor.validation_sensor_id = node.attribute("validation_sensor_id").as_int();
wlp_sensor.validation_type = node.attribute("validation_type").as_int();
if (wlp_sensor.type == "engine operation") { //add consumption to sensor config
wlp_sensor.config.consumption = fuelConfigMathNode.attribute("idle").as_double(2.0);
}
//Calibration table
auto ctable = node.child("ctable");
wlp_sensor.table.clear();
for (auto row = ctable.child("row"); row; row = row.next_sibling("row")) {
wlpSensorCalibRow cRow = { 0 };
cRow.a = row.attribute("a").as_double();
cRow.b = row.attribute("b").as_double();
cRow.x = row.attribute("x").as_double();
wlp_sensor.table.push_back(cRow);
}
wlp.addSensor(wlp_sensor);
}
/**
@brief Добавить детектор поездок
@param[in] node Раздел xml документа с детектором поездок
@param[in] wlp
*/
static void addTripDetector(const xml_node& node, CWLUnit& wlp)
{
wlpTripDetector tripd = { 0 };
tripd.type = node.attribute("type").as_uint(1UL);
tripd.gpsCorrection = (node.attribute("gps_correction").as_bool(true)) ? 1 : 0;
tripd.minSat = node.attribute("satellites").as_uint(4UL);
tripd.minMovingSpeed = node.attribute("moving_speed").as_uint(1UL);
tripd.minStayTime = node.attribute("stay_time").as_uint(300UL);
tripd.maxMessagesDistance = node.attribute("message_distance").as_uint(1000UL);
tripd.minTripTime = node.attribute("trip_time").as_uint(60UL);
tripd.minTripDistance = node.attribute("trip_distance").as_uint(100UL);
wlp.addTripDetector(tripd);
}
/**
@brief Добавить информацию по расходу топлива
@param[in] node Раздел xml документа с данными по расходу топлива
@param[in] wlp
*/
static void addFuelConsumpt(const xml_node& node, CWLUnit& wlp)
{
wlpFuelConsumption fuelCons = { 0 };
for (auto fuelRow = node.first_child(); fuelRow; fuelRow = fuelRow.next_sibling()) {
std::string stmp = fuelRow.name();
if (stmp == "absolute") {
if (fuelRow.attribute("active").as_bool(false)) {
fuelCons.calcTypes |= 0x08UL;
}
}
else if (stmp == "impulse") {
if (fuelRow.attribute("active").as_bool(false)) {
fuelCons.calcTypes |= 0x10UL;
}
fuelCons.fuelConsImpulse.maxImpulses = fuelRow.attribute("impulses").as_uint();
fuelCons.fuelConsImpulse.skipZero = (fuelRow.attribute("skip_first_zero").as_bool()) ? 1 : 0;
}
else if (stmp == "instant") {
if (fuelRow.attribute("active").as_bool(false)) {
fuelCons.calcTypes |= 0x20UL;
}
}
else if (stmp == "level") {
if (fuelRow.attribute("active").as_bool(false)) {
fuelCons.calcTypes |= 0x02UL;
}
if (fuelRow.attribute("correct_invalid_values").as_bool(false)) {
fuelCons.calcTypes |= 0x04UL;
}
}
else if (stmp == "math") {
if (fuelRow.attribute("active").as_bool(false)) {
fuelCons.calcTypes |= 0x01UL;
}
fuelCons.fuelConsMath.idling = fuelRow.attribute("idle").as_double(2.0);
fuelCons.fuelConsMath.urban = fuelRow.attribute("urban").as_double(10.0);
fuelCons.fuelConsMath.suburban = fuelRow.attribute("suburban").as_double(7.0);
}
else if (stmp == "rates") {
if (fuelRow.attribute("active").as_bool(false)) {
fuelCons.calcTypes |= 0x40UL;
}
fuelCons.fuelConsRates.consSummer = fuelRow.attribute("summer_consumption").as_double(10.0);
fuelCons.fuelConsRates.consWinter = fuelRow.attribute("winter_consumption").as_double(12.0);
fuelCons.fuelConsRates.winterMonthFrom = fuelRow.attribute("winter_begin_month").as_uint(11);
fuelCons.fuelConsRates.winterDayFrom = fuelRow.attribute("winter_begin_day").as_uint(1);
fuelCons.fuelConsRates.winterMonthTo = fuelRow.attribute("winter_end_month").as_uint(1);
fuelCons.fuelConsRates.winterDayTo = fuelRow.attribute("winter_end_day").as_uint(30);
}
else if (stmp == "general") {
if (fuelRow.attribute("filter_level_values").as_bool(false)) {
fuelCons.fuelLevelParams.flags |= 0x02UL;
}
if (fuelRow.attribute("ignore_filtration_when_calc_filling_volume").as_bool(false)) {
fuelCons.fuelLevelParams.flags |= 0x40UL;
}
if (fuelRow.attribute("ignore_filtration_when_calc_theft_volume").as_bool(false)) {
fuelCons.fuelLevelParams.flags |= 0x80UL;
}
if (fuelRow.attribute("merge_consumption_sensors").as_bool(false)) {
fuelCons.fuelLevelParams.flags |= 0x04UL;
}
if (fuelRow.attribute("merge_level_sensors").as_bool(false)) {
fuelCons.fuelLevelParams.flags |= 0x01UL;
}
if (fuelRow.attribute("time_based_calculation_of_consumption").as_bool(false)) {
//fuelSettings.fuelLevelParams.flags |= 0x10UL;
fuelCons.fuelLevelParams.flags |= 0x800UL;
}
if (fuelRow.attribute("time_based_calculation_of_fillings").as_bool(false)) {
fuelCons.fuelLevelParams.flags |= 0x200UL;
}
if (fuelRow.attribute("time_based_calculation_of_thefts").as_bool(false)) {
fuelCons.fuelLevelParams.flags |= 0x400UL;
}
fuelCons.fuelLevelParams.filterQuality = fuelRow.attribute("filter_quality").as_uint() & 0xff;
}
else if (stmp == "theft") {
if (fuelRow.attribute("detect_fill_when_stopped_only").as_bool(false)) {
fuelCons.fuelLevelParams.flags |= 0x08UL;
}
if (fuelRow.attribute("detect_theft_in_motion").as_bool(false)) {
fuelCons.fuelLevelParams.flags |= 0x100UL;
}
if (fuelRow.attribute("ignore_filtration").as_bool(false)) {
//fuelSettings.fuelLevelParams.flags |= 0x100UL;
}
fuelCons.fuelLevelParams.minFillingVolume = fuelRow.attribute("fill_volume").as_double(10.0);
fuelCons.fuelLevelParams.ignoreStayTimeout = fuelRow.attribute("ignore_stay_timeout").as_uint(20);
fuelCons.fuelLevelParams.minTheftTimeout = fuelRow.attribute("theft_timeout").as_uint(0);
fuelCons.fuelLevelParams.minTheftVolume = fuelRow.attribute("theft_volume").as_double(10.0);
//Default values
fuelCons.fuelLevelParams.fillingsJoinInterval = 300;
fuelCons.fuelLevelParams.theftsJoinInterval = 300;
fuelCons.fuelLevelParams.extraFillingTimeout = 0;
}
}
wlp.addFuelSettings(fuelCons);
}
/**
@brief Множественное преобразование файлов в директории
@param[in] inDirPath Путь к входной директории
@param[in] outDirPath Путь к выходной директории
@param[in] overwrite true - перезаписывать существующие файлы
@return Число успешно сконвертированных файлов
*/
static size_t convertPath(const std::string& inDirPath, const std::string& outDirPath, const bool overwrite)
{
fs::path inPath(inDirPath);
fs::path outPath(outDirPath);
size_t fileCnt = 0;
for (auto& p : fs::directory_iterator(inDirPath)) {
fs::path file(p);
if ((file.extension() == ".xml" || file.extension() == ".XML") &&
isFileExist(file.string())) {
if (convertSingleFile(file.string(), outDirPath, overwrite)) {
++fileCnt;
}
}
}
return fileCnt;
}
/**
@brief Конвертация одного файла
@param[in] inFileName Путь к входному файлу
@param[in] outDirPath Путь к выходной директории
@param[in] overwrite true - перезаписывать существующие файлы
@return true в случае успеха
*/
static bool convertSingleFile(const std::string& inFileName, const std::string& outDirPath, const bool overwrite)
{
fs::path inPath(inFileName);
fs::path outPath(outDirPath);
if (inPath.extension() != ".xml" && inPath.extension() != ".XML") {
std::cout << "Input file is not xml" << std::endl;
return false;
}
outPath /= inPath.filename();
outPath.replace_extension(".wlp");
if (!overwrite && isFileExist(outPath.string())) {
std::cout << "File " << outPath.filename() << " already exists. Skip" << std::endl;
return false;
}
std::cout << "Convert " << inPath.filename() << "... ";
if (processXmlFile(inFileName, outPath.string())) {
std::cout << "Success" << std::endl;
return true;
}
else {
std::cout << "Error" << std::endl;
return false;
}
}
/**
@brief Вывести в консоль справочную информацию
*/
static void printHelp()
{
std::cout << "Migrate units created in Wialon Pro to Wialon Local" << std::endl;
std::cout <<
"Usage: wialon_units_migrate.exe [--help | -h]" << std::endl <<
" [--ipath=<path>] [--ifile=<filepath>]" << std::endl <<
" [--opath = <path>] [--overwrite] " << std::endl;
}