-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhafasmgateparser.cpp
619 lines (541 loc) · 25.1 KB
/
hafasmgateparser.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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*
SPDX-FileCopyrightText: 2018 Volker Krause <[email protected]>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "hafasmgateparser.h"
#include "hafasvehiclelayoutparser.h"
#include "logging.h"
#include "../geo/polylinedecoder_p.h"
#include <KPublicTransport/Journey>
#include <KPublicTransport/Line>
#include <KPublicTransport/Platform>
#include <KPublicTransport/Stopover>
#include <KPublicTransport/Vehicle>
#include <QColor>
#include <QDateTime>
#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
using namespace KPublicTransport;
// REM or HIM elements
struct Message {
QVariant content;
Disruption::Effect effect = Disruption::NormalService;
};
HafasMgateParser::HafasMgateParser() = default;
HafasMgateParser::~HafasMgateParser() = default;
static std::vector<Ico> parseIcos(const QJsonArray &icoL)
{
std::vector<Ico> icos;
icos.reserve(icoL.size());
for (const auto &icoV : icoL) {
const auto icoObj = icoV.toObject();
Ico ico;
const auto fg = icoObj.value(QLatin1String("fg")).toObject();
if (!fg.isEmpty()) {
ico.fg = QColor(fg.value(QLatin1String("r")).toInt(), fg.value(QLatin1String("g")).toInt(), fg.value(QLatin1String("b")).toInt());
}
const auto bg = icoObj.value(QLatin1String("bg")).toObject();
if (!bg.isEmpty()) {
ico.bg = QColor(bg.value(QLatin1String("r")).toInt(), bg.value(QLatin1String("g")).toInt(), bg.value(QLatin1String("b")).toInt());
}
icos.push_back(ico);
}
return icos;
}
static const struct {
const char *type;
const char *code;
} ignored_remarks[] = {
{ "A", "1" }, // different name formats for the line, used by SBB
{ "A", "2" },
{ "A", "3" },
{ "A", "4" }, // same as above, containing product, line number and journey number
{ "A", "OPERATOR" }, // operator information should be a dedicated field if we ever need it
{ "H", "wagenstand_v2" }, // contains a pointless note about checking trip details
{ "I", "FD" }, // SBB line number?
{ "I", "RN" }, // SBB: some unknown number for buses
{ "I", "TC" }, // SBB: some unknown number for buses
{ "I", "XC" }, // SBB: some XML structure of unknown content
{ "I", "XG" }, // SBB: some XML structure of unknown content
{ "I", "XT" }, // SBB: some XML structure of unknown content
};
static std::vector<Message> parseRemarks(const QJsonArray &remL)
{
std::vector<Message> rems;
rems.reserve(remL.size());
for (const auto &remV : remL) {
const auto remObj = remV.toObject();
const auto type = remObj.value(QLatin1String("type")).toString();
const auto code = remObj.value(QLatin1String("code")).toString();
bool skip = false;
for (const auto &ignored_remark : ignored_remarks) {
if (type == QLatin1String(ignored_remark.type) && code == QLatin1String(ignored_remark.code)) {
skip = true;
break;
}
}
if (skip) {
rems.push_back({}); // make sure the indices still match!
continue;
}
Message m;
if (type == QLatin1Char('I') && code == QLatin1String("JF")) {
m.content = HafasVehicleLayoutParser::parseTrainFormation(remObj.value(QLatin1String("txtN")).toString().toUtf8());
} else if (type == QLatin1Char('I') && code == QLatin1String("XP")) {
m.content = HafasVehicleLayoutParser::parsePlatformSectors(remObj.value(QLatin1String("txtN")).toString().toUtf8());
} else {
// generic text
m.content = remObj.value(QLatin1String("txtN")).toString();
if (code == QLatin1String("text.realtime.stop.cancelled")) {
m.effect = Disruption::NoService;
}
}
rems.push_back(std::move(m));
}
return rems;
}
static std::vector<Message> parseWarnings(const QJsonArray &himL)
{
std::vector<Message> hims;
hims.reserve(himL.size());
for (const auto &himV : himL) {
const auto himObj = himV.toObject();
Message m;
m.content = QString(himObj.value(QLatin1String("head")).toString() + QLatin1Char('\n')
+ himObj.value(QLatin1String("lead")).toString() + QLatin1Char('\n')
+ himObj.value(QLatin1String("text")).toString());
hims.push_back(m);
}
return hims;
}
template <typename T>
static void parseMessageList(T &elem, const QJsonObject &obj, const std::vector<Message> &remarks, const std::vector<Message> &warnings)
{
const auto msgL = obj.value(QLatin1String("msgL")).toArray();
QStringList notes;
for (const auto &msgV : msgL) {
const auto msgObj = msgV.toObject();
const auto msgType = msgObj.value(QLatin1String("type")).toString();
const std::vector<Message> *source = nullptr;
if (msgType == QLatin1String("REM")) {
source = &remarks;
} else if (msgType == QLatin1String("HIM")) {
source = &warnings;
} else {
qDebug() << "unsupported message type:" << msgType;
continue;
}
const auto remX = msgObj.value(QLatin1String("remX")).toInt();
if (static_cast<size_t>(remX) >= source->size()) {
qCDebug(Log) << "Invalid message index:" << remX << msgType;
continue;
}
const auto msg = (*source)[remX];
if (msg.content.type() == QVariant::String) {
elem.addNote(msg.content.toString());
}
if (msg.effect == Disruption::NoService) {
elem.setDisruptionEffect(msg.effect);
}
}
}
static constexpr const Load::Category load_value_map[] = {
Load::Unknown,
Load::Low, // 1
Load::Medium, // 2
Load::High, // 3
Load::Full // 4
};
static std::vector<LoadInfo> parseLoadInformation(const QJsonArray &tcocL)
{
std::vector<LoadInfo> loadInfos;
loadInfos.reserve(tcocL.size());
for (const auto &tcocV : tcocL) {
const auto tcocObj = tcocV.toObject();
LoadInfo loadInfo;
const auto r = qBound(0, tcocObj.value(QLatin1String("r")).toInt(), 4);
loadInfo.setLoad(load_value_map[r]);
const auto c = tcocObj.value(QLatin1String("c")).toString();
loadInfo.setSeatingClass(c == QLatin1String("FIRST") ? QStringLiteral("1") : QStringLiteral("2"));
loadInfos.push_back(std::move(loadInfo));
}
return loadInfos;
}
std::vector<Location> HafasMgateParser::parseLocations(const QJsonArray &locL) const
{
std::vector<Location> locs;
locs.reserve(locL.size());
for (const auto &locV : locL) {
const auto locObj = locV.toObject();
// resolve references to the master location
const auto masterIdx = locObj.value(QLatin1String("mMastLocX")).toInt(-1);
if (masterIdx >= 0 && masterIdx < (int)locs.size()) {
locs.push_back(locs[masterIdx]);
continue;
}
Location loc;
loc.setName(locObj.value(QLatin1String("name")).toString());
loc.setType(locObj.value(QLatin1String("type")).toString() == QLatin1Char('S') ? Location::Stop : Location::Place);
setLocationIdentifier(loc, locObj.value(QLatin1String("extId")).toString());
const auto coordObj = locObj.value(QLatin1String("crd")).toObject();
loc.setCoordinate(coordObj.value(QLatin1String("y")).toDouble() / 1000000.0, coordObj.value(QLatin1String("x")).toDouble() / 1000000.0);
locs.push_back(loc);
}
return locs;
}
std::vector<Line> HafasMgateParser::parseLines(const QJsonArray &prodL, const std::vector<Ico> &icos) const
{
std::vector<Line> lines;
lines.reserve(prodL.size());
for (const auto &prodV : prodL) {
const auto prodObj = prodV.toObject();
Line line;
line.setName(prodObj.value(QLatin1String("name")).toString());
line.setMode(parseLineMode(prodObj.value(QLatin1String("cls")).toInt()));
const auto icoIdx = prodObj.value(QLatin1String("icoX")).toInt();
if ((unsigned int)icoIdx < icos.size()) {
line.setColor(icos[icoIdx].bg);
line.setTextColor(icos[icoIdx].fg);
}
lines.push_back(line);
}
return lines;
}
std::vector<Stopover> HafasMgateParser::parseStationBoardResponse(const QJsonObject &obj) const
{
const auto commonObj = obj.value(QLatin1String("common")).toObject();
const auto icos = parseIcos(commonObj.value(QLatin1String("icoL")).toArray());
const auto locs = parseLocations(commonObj.value(QLatin1String("locL")).toArray());
const auto lines = parseLines(commonObj.value(QLatin1String("prodL")).toArray(), icos);
const auto remarks = parseRemarks(commonObj.value(QLatin1String("remL")).toArray());
const auto warnings = parseWarnings(commonObj.value(QLatin1String("himL")).toArray());
std::vector<Stopover> res;
const auto jnyL = obj.value(QLatin1String("jnyL")).toArray();
res.reserve(jnyL.size());
for (const auto &jny : jnyL) {
const auto jnyObj = jny.toObject();
const auto stbStop = jnyObj.value(QLatin1String("stbStop")).toObject();
Stopover dep;
Route route;
route.setDirection(jnyObj.value(QLatin1String("dirTxt")).toString());
const auto lineIdx = jnyObj.value(QLatin1String("prodX")).toInt(-1);
if (lineIdx >= 0 && (unsigned int)lineIdx < lines.size()) {
route.setLine(lines[lineIdx]);
}
const auto dateStr = jnyObj.value(QLatin1String("date")).toString();
dep.setScheduledDepartureTime(parseDateTime(dateStr, stbStop.value(QLatin1String("dTimeS")), stbStop.value(QLatin1String("dTZOffset"))));
dep.setExpectedDepartureTime(parseDateTime(dateStr, stbStop.value(QLatin1String("dTimeR")), stbStop.value(QLatin1String("dTZOffset"))));
dep.setScheduledArrivalTime(parseDateTime(dateStr, stbStop.value(QLatin1String("aTimeS")), stbStop.value(QLatin1String("aTZOffset"))));
dep.setExpectedArrivalTime(parseDateTime(dateStr, stbStop.value(QLatin1String("aTimeR")), stbStop.value(QLatin1String("aTZOffset"))));
dep.setScheduledPlatform(stbStop.value(QLatin1String("dPlatfS")).toString());
dep.setExpectedPlatform(stbStop.value(QLatin1String("dPlatfR")).toString());
if (dep.scheduledPlatform().isEmpty()) {
dep.setScheduledPlatform(stbStop.value(QLatin1String("aPlatfS")).toString());
}
if (dep.expectedPlatform().isEmpty()) {
dep.setExpectedPlatform(stbStop.value(QLatin1String("aPlatfR")).toString());
}
if (stbStop.value(QLatin1String("dCncl")).toBool()) {
dep.setDisruptionEffect(Disruption::NoService);
}
const auto startLocIdx = stbStop.value(QLatin1String("locX")).toInt(-1);
if (startLocIdx >= 0 && (unsigned int)startLocIdx < locs.size()) {
dep.setStopPoint(locs[startLocIdx]);
}
const auto stopL = jnyObj.value(QLatin1String("stopL")).toArray();
bool foundLoop = false; // check for loops, circular lines have no destination
for (int i = 1; i < stopL.size() && !foundLoop; ++i) {
const auto locX = stopL.at(i).toObject().value(QLatin1String("locX")).toInt(-1);
if (locX == startLocIdx) {
foundLoop = true;
}
}
const auto destLocX = stopL.last().toObject().value(QLatin1String("locX")).toInt(-1);
if (!foundLoop && destLocX >= 0 && (unsigned int)destLocX < locs.size() && startLocIdx != destLocX) {
route.setDestination(locs[destLocX]);
}
parseMessageList(dep, jnyObj, remarks, warnings);
parseMessageList(dep, stbStop, remarks, warnings);
dep.setRoute(route);
res.push_back(dep);
}
return res;
}
bool HafasMgateParser::parseError(const QJsonObject& obj) const
{
const auto err = obj.value(QLatin1String("err")).toString();
if (!err.isEmpty() && err != QLatin1String("OK")) {
m_error = err == QLatin1String("LOCATION") ? Reply::NotFoundError : Reply::UnknownError;
m_errorMsg = obj.value(QLatin1String("errTxt")).toString();
if (m_errorMsg.isEmpty()) {
m_errorMsg = err;
}
return false;
}
m_error = Reply::NoError;
m_errorMsg.clear();
return true;
}
std::vector<Stopover> HafasMgateParser::parseDepartures(const QByteArray &data) const
{
const auto topObj = QJsonDocument::fromJson(data).object();
if (!parseError(topObj)) {
return {};
}
const auto svcResL = topObj.value(QLatin1String("svcResL")).toArray();
for (const auto &v : svcResL) {
const auto obj = v.toObject();
if (obj.value(QLatin1String("meth")).toString() == QLatin1String("StationBoard")) {
if (parseError(obj)) {
return parseStationBoardResponse(obj.value(QLatin1String("res")).toObject());
}
return {};
}
}
return {};
}
std::vector<Location> HafasMgateParser::parseLocations(const QByteArray &data) const
{
const auto topObj = QJsonDocument::fromJson(data).object();
if (!parseError(topObj)) {
return {};
}
const auto svcResL = topObj.value(QLatin1String("svcResL")).toArray();
for (const auto &v : svcResL) {
const auto obj = v.toObject();
const auto meth = obj.value(QLatin1String("meth")).toString();
if (meth == QLatin1String("LocMatch") || meth == QLatin1String("LocGeoPos")) {
if (parseError(obj)) {
const auto resObj = obj.value(QLatin1String("res")).toObject();
if (resObj.contains(QLatin1String("locL"))) {
return parseLocations(resObj.value(QLatin1String("locL")).toArray());
}
if (resObj.contains(QLatin1String("match"))) {
return parseLocations(resObj.value(QLatin1String("match")).toObject().value(QLatin1String("locL")).toArray());
}
qCDebug(Log).noquote() << "Failed to parse location query response:" << QJsonDocument(obj).toJson();
return {};
}
return {};
}
}
return {};
}
std::vector<Journey> HafasMgateParser::parseJourneys(const QByteArray &data) const
{
const auto topObj = QJsonDocument::fromJson(data).object();
if (!parseError(topObj)) {
return {};
}
const auto svcResL = topObj.value(QLatin1String("svcResL")).toArray();
for (const auto &v : svcResL) {
const auto obj = v.toObject();
if (obj.value(QLatin1String("meth")).toString() == QLatin1String("TripSearch")) {
if (parseError(obj)) {
return parseTripSearch(obj.value(QLatin1String("res")).toObject());
}
return {};
}
}
return {};
}
static std::vector<LoadInfo> parseLoad(const QJsonObject &obj, const std::vector<LoadInfo> &loadInfos)
{
const auto dTrnCmpSX = obj.value(QLatin1String("dTrnCmpSX")).toObject();
const auto tcocX = dTrnCmpSX.value(QLatin1String("tcocX")).toArray();
std::vector<LoadInfo> load;
load.reserve(tcocX.size());
for (const auto &v : tcocX) {
const auto i = v.toInt();
if (i >= 0 && i < (int)loadInfos.size()) {
load.push_back(loadInfos[i]);
}
}
return load;
}
static std::vector<Path> parsePaths(const QJsonArray &polyL, const std::vector<Location> &locs)
{
std::vector<Path> paths;
paths.reserve(polyL.size());
for (const auto &polyV : polyL) {
const auto polyObj = polyV.toObject();
// path coordinate index to location mapping
const auto ppLocRefL = polyObj.value(QLatin1String("ppLocRefL")).toArray();
// 2-dimensional differential encoded coordinates
const auto crdEncYX = polyObj.value(QLatin1String("crdEncYX")).toString().toUtf8();
PolylineDecoder<2> crdEncYXDecoder(crdEncYX.constData());
// crdEncDist: 1-dimensional integer values with differential encoding containing distances in meters
// crdEncZ: 1-dimensional, always 0?
// crdEncS: 1-dimensional, unknown meaning, but very low-entropy data
// crdEncF: 1-dimensional, always 0?
std::vector<PathSection> sections;
sections.reserve(ppLocRefL.size() - 1);
int prevPpIdx = 0;
QPointF prevCoord;
for (const auto ppLocRefV : ppLocRefL) {
const auto ppLocRef = ppLocRefV.toObject();
const auto ppIdx = ppLocRef.value(QLatin1String("ppIdx")).toInt();
if (ppIdx == 0 || ppIdx < prevPpIdx) {
continue;
}
QPolygonF poly;
poly.reserve(prevPpIdx - ppIdx + 2);
if (!prevCoord.isNull()) {
poly.push_back(prevCoord);
}
crdEncYXDecoder.readPolygon(poly, ppIdx - prevPpIdx + 1);
if (!poly.empty()) {
prevCoord = poly.back();
}
PathSection section;
section.setPath(std::move(poly));
prevPpIdx = ppIdx;
const auto locX = ppLocRef.value(QLatin1String("locX")).toInt();
if (locX >= 0 && locX < (int)locs.size()) {
section.setDescription(locs[locX].name());
}
sections.push_back(std::move(section));
}
Path path;
path.setSections(std::move(sections));
paths.push_back(std::move(path));
}
return paths;
}
static Path parsePolyG(const QJsonObject &obj, const std::vector<Path> &paths)
{
const auto polyG = obj.value(QLatin1String("polyG")).toObject();
const auto polyXL = polyG.value(QLatin1String("polyXL")).toArray();
if (polyXL.size() != 1) {
return {};
}
const auto polyX = polyXL.at(0).toInt();
if (polyX >= 0 && polyX < (int)paths.size()) {
return paths[polyX];
}
return {};
}
std::vector<Journey> HafasMgateParser::parseTripSearch(const QJsonObject &obj) const
{
const auto commonObj = obj.value(QLatin1String("common")).toObject();
const auto icos = parseIcos(commonObj.value(QLatin1String("icoL")).toArray());
const auto locs = parseLocations(commonObj.value(QLatin1String("locL")).toArray());
const auto lines = parseLines(commonObj.value(QLatin1String("prodL")).toArray(), icos);
const auto remarks = parseRemarks(commonObj.value(QLatin1String("remL")).toArray());
const auto warnings = parseWarnings(commonObj.value(QLatin1String("himL")).toArray());
const auto loadInfos = parseLoadInformation(commonObj.value(QLatin1String("tcocL")).toArray());
const auto paths = parsePaths(commonObj.value(QLatin1String("polyL")).toArray(), locs);
const auto platforms = HafasVehicleLayoutParser::parsePlatforms(commonObj);
const auto vehicles = HafasVehicleLayoutParser::parseVehicleLayouts(commonObj);
std::vector<Journey> res;
const auto outConL = obj.value(QLatin1String("outConL")).toArray();
res.reserve(outConL.size());
for (const auto &outConV: outConL) {
const auto outCon = outConV.toObject();
const auto dateStr = outCon.value(QLatin1String("date")).toString();
const auto secL = outCon.value(QLatin1String("secL")).toArray();
std::vector<JourneySection> sections;
sections.reserve(secL.size());
for (const auto &secV : secL) {
const auto secObj = secV.toObject();
JourneySection section;
const auto dep = secObj.value(QLatin1String("dep")).toObject();
section.setScheduledDepartureTime(parseDateTime(dateStr, dep.value(QLatin1String("dTimeS")), dep.value(QLatin1String("dTZOffset"))));
section.setExpectedDepartureTime(parseDateTime(dateStr, dep.value(QLatin1String("dTimeR")), dep.value(QLatin1String("dTZOffset"))));
auto locIdx = dep.value(QLatin1String("locX")).toInt();
if ((unsigned int)locIdx < locs.size()) {
section.setFrom(locs[locIdx]);
}
section.setScheduledDeparturePlatform(dep.value(QLatin1String("dPlatfS")).toString());
section.setExpectedDeparturePlatform(dep.value(QLatin1String("dPlatfR")).toString());
if (dep.value(QLatin1String("dCncl")).toBool()) {
section.setDisruptionEffect(Disruption::NoService);
}
const auto arr = secObj.value(QLatin1String("arr")).toObject();
section.setScheduledArrivalTime(parseDateTime(dateStr, arr.value(QLatin1String("aTimeS")), arr.value(QLatin1String("aTZOffset"))));
section.setExpectedArrivalTime(parseDateTime(dateStr, arr.value(QLatin1String("aTimeR")), arr.value(QLatin1String("aTZOffset"))));
locIdx = arr.value(QLatin1String("locX")).toInt();
if ((unsigned int)locIdx < locs.size()) {
section.setTo(locs[locIdx]);
}
section.setScheduledArrivalPlatform(arr.value(QLatin1String("aPlatfS")).toString());
section.setExpectedArrivalPlatform(arr.value(QLatin1String("aPlatfR")).toString());
if (arr.value(QLatin1String("aCncl")).toBool()) {
section.setDisruptionEffect(Disruption::NoService);
}
const auto typeStr = secObj.value(QLatin1String("type")).toString();
if (typeStr == QLatin1String("JNY")) {
section.setMode(JourneySection::PublicTransport);
const auto jnyObj = secObj.value(QLatin1String("jny")).toObject();
Route route;
route.setDirection(jnyObj.value(QLatin1String("dirTxt")).toString());
const auto lineIdx = jnyObj.value(QLatin1String("prodX")).toInt();
if ((unsigned int)lineIdx < lines.size()) {
route.setLine(lines[lineIdx]);
}
section.setRoute(route);
if (jnyObj.value(QLatin1String("isCncl")).toBool()) {
section.setDisruptionEffect(Disruption::NoService);
}
const auto stopL = jnyObj.value(QLatin1String("stopL")).toArray();
if (stopL.size() > 2) { // we don't want departure/arrival stops in here
std::vector<Stopover> stops;
stops.reserve(stopL.size() - 2);
for (auto it = std::next(stopL.begin()); it != std::prev(stopL.end()); ++it) {
const auto stopObj = (*it).toObject();
Stopover stop;
const auto locIdx = stopObj.value(QLatin1String("locX")).toInt();
if ((unsigned int)locIdx < locs.size()) {
stop.setStopPoint(locs[locIdx]);
}
stop.setScheduledDepartureTime(parseDateTime(dateStr, stopObj.value(QLatin1String("dTimeS")), stopObj.value(QLatin1String("dTZOffset"))));
stop.setExpectedDepartureTime(parseDateTime(dateStr, stopObj.value(QLatin1String("dTimeR")), stopObj.value(QLatin1String("dTZOffset"))));
stop.setScheduledArrivalTime(parseDateTime(dateStr, stopObj.value(QLatin1String("aTimeS")), stopObj.value(QLatin1String("aTZOffset"))));
stop.setExpectedArrivalTime(parseDateTime(dateStr, stopObj.value(QLatin1String("aTimeR")), stopObj.value(QLatin1String("aTZOffset"))));
stop.setScheduledPlatform(stopObj.value(QLatin1String("dPlatfS")).toString());
stop.setExpectedPlatform(stopObj.value(QLatin1String("dPlatfR")).toString());
if (stopObj.value(QLatin1String("aCncl")).toBool() || stopObj.value(QLatin1String("dCncl")).toBool()) {
stop.setDisruptionEffect(Disruption::NoService);
}
parseMessageList(stop, stopObj, remarks, warnings);
stop.setLoadInformation(parseLoad(stopObj, loadInfos));
stops.push_back(stop);
}
section.setIntermediateStops(std::move(stops));
}
parseMessageList(section, jnyObj, remarks, warnings);
section.setLoadInformation(parseLoad(dep, loadInfos));
section.setPath(parsePolyG(jnyObj, paths));
} else if (typeStr == QLatin1String("WALK") || typeStr == QLatin1String("TRSF")) {
const auto gis = secObj.value(QLatin1String("gis")).toObject();
section.setDistance(gis.value(QLatin1String("dist")).toInt());
section.setPath(parsePolyG(gis, paths));
section.setMode(typeStr == QLatin1String("WALK") ? JourneySection::Walking : JourneySection::Transfer);
}
sections.push_back(section);
}
Journey journey;
journey.setSections(std::move(sections));
res.push_back(journey);
}
return res;
}
QDateTime HafasMgateParser::parseDateTime(const QString &date, const QJsonValue &time, const QJsonValue &tzOffset)
{
const auto timeStr = time.toString();
if (date.isEmpty() || timeStr.isEmpty()) {
return {};
}
int dayOffset = 0;
if (timeStr.size() > 6) {
dayOffset = timeStr.leftRef(timeStr.size() - 6).toInt();
}
auto dt = QDateTime::fromString(date + timeStr.rightRef(6), QStringLiteral("yyyyMMddhhmmss"));
dt = dt.addDays(dayOffset);
if (!tzOffset.isNull() && !tzOffset.isUndefined()) {
dt.setOffsetFromUtc(tzOffset.toInt() * 60);
}
return dt;
}