Commit e8c5ed6e authored by Volker Krause's avatar Volker Krause
Browse files

Implement OJP trip request generation

Support for previous/next requests is still missing, but the basics work
and at least for identifier-based locations also return usful results.
parent 0bb756a9
<?xml version="1.0" encoding="UTF-8"?>
<siri:OJP xmlns:siri="http://www.siri.org.uk/siri" xmlns:ojp="http://www.vdv.de/ojp" version="1.0">
<siri:OJPRequest>
<siri:ServiceRequest>
<siri:RequestRef>KPublicTransport</siri:RequestRef>
<ojp:OJPTripRequest>
<ojp:Origin>
<ojp:PlaceRef>
<ojp:StopPlaceRef>8501687</ojp:StopPlaceRef>
</ojp:PlaceRef>
</ojp:Origin>
<ojp:Destination>
<ojp:PlaceRef>
<ojp:StopPlaceRef>8500010</ojp:StopPlaceRef>
</ojp:PlaceRef>
<ojp:DepArrTime>2020-09-06T20:54:00Z</ojp:DepArrTime>
</ojp:Destination>
<ojp:Params>
<ojp:IncludeTrackSections>true</ojp:IncludeTrackSections>
<ojp:IncludeLegProjection>true</ojp:IncludeLegProjection>
<ojp:IncludeTurnDescription>true</ojp:IncludeTurnDescription>
<ojp:IncludeAccessibility>true</ojp:IncludeAccessibility>
<ojp:IncludeIntermediateStops>false</ojp:IncludeIntermediateStops>
<ojp:IncludeFares>false</ojp:IncludeFares>
<ojp:NumberOfResults>3</ojp:NumberOfResults>
</ojp:Params>
</ojp:OJPTripRequest>
</siri:ServiceRequest>
</siri:OJPRequest>
</siri:OJP>
<?xml version="1.0" encoding="UTF-8"?>
<siri:OJP xmlns:siri="http://www.siri.org.uk/siri" xmlns:ojp="http://www.vdv.de/ojp" version="1.0">
<siri:OJPRequest>
<siri:ServiceRequest>
<siri:RequestRef>KPublicTransport</siri:RequestRef>
<ojp:OJPTripRequest>
<ojp:Origin>
<ojp:PlaceRef>
<ojp:StopPlaceRef>8501687</ojp:StopPlaceRef>
</ojp:PlaceRef>
<ojp:DepArrTime>2020-09-06T20:54:00Z</ojp:DepArrTime>
</ojp:Origin>
<ojp:Destination>
<ojp:PlaceRef>
<ojp:StopPlaceRef>8500010</ojp:StopPlaceRef>
</ojp:PlaceRef>
</ojp:Destination>
<ojp:Params>
<ojp:IncludeTrackSections>false</ojp:IncludeTrackSections>
<ojp:IncludeLegProjection>true</ojp:IncludeLegProjection>
<ojp:IncludeTurnDescription>false</ojp:IncludeTurnDescription>
<ojp:IncludeAccessibility>true</ojp:IncludeAccessibility>
<ojp:IncludeIntermediateStops>true</ojp:IncludeIntermediateStops>
<ojp:IncludeFares>false</ojp:IncludeFares>
<ojp:NumberOfResults>3</ojp:NumberOfResults>
</ojp:Params>
</ojp:OJPTripRequest>
</siri:ServiceRequest>
</siri:OJPRequest>
</siri:OJP>
......@@ -103,6 +103,46 @@ private Q_SLOTS:
QVERIFY(!res.isEmpty());
QCOMPARE(res, ref);
}
void testJourneyRequest_data()
{
QTest::addColumn<JourneyRequest>("request");
QTest::addColumn<QString>("refFileName");
Location from;
from.setIdentifier(QStringLiteral("uic"), QStringLiteral("8501687"));
Location to;
to.setIdentifier(QStringLiteral("uic"), QStringLiteral("8500010"));
JourneyRequest req;
req.setFrom(from);
req.setTo(to);
req.setDateTime(QDateTime({2020, 9, 6}, {20, 54}, Qt::UTC));
req.setDateTimeMode(JourneyRequest::Departure);
req.setMaximumResults(3);
req.setIncludeIntermediateStops(true);
req.setIncludePaths(false);
QTest::newRow("journey-departure") << req << s(SOURCE_DIR "/data/ojp-request/journey-departure.xml");
req.setIncludeIntermediateStops(false);
req.setIncludePaths(true);
req.setDateTimeMode(JourneyRequest::Arrival);
QTest::newRow("stopover-arrival") << req << s(SOURCE_DIR "/data/ojp-request/journey-arrival.xml");
}
void testJourneyRequest()
{
QFETCH(JourneyRequest, request);
QFETCH(QString, refFileName);
OpenJourneyPlannerRequestBuilder builder;
builder.setTestMode(true);
const auto res = builder.buildTripRequest(request);
const auto ref = readFile(refFileName);
if (res != ref) {
qDebug().noquote() << res;
}
QVERIFY(!res.isEmpty());
QCOMPARE(res, ref);
}
};
QTEST_GUILESS_MAIN(OjpRequestTest)
......
......@@ -31,7 +31,12 @@ AbstractBackend::Capabilities OpenJourneyPlannerBackend::capabilities() const
bool OpenJourneyPlannerBackend::needsLocationQuery(const Location &loc, AbstractBackend::QueryType type) const
{
return loc.identifier(QStringLiteral("uic")).isEmpty(); // ### TODO configure identifier type
if (!loc.identifier(QStringLiteral("uic")).isEmpty()) {// ### TODO configure identifier type
return false;
}
// TODO according to the docs this is supposed to work?
//return !loc.hasCoordinate() || type != AbstractBackend::QueryType::Journey;
return true;
}
bool OpenJourneyPlannerBackend::queryLocation(const LocationRequest &request, LocationReply *reply, QNetworkAccessManager *nam) const
......@@ -83,11 +88,9 @@ bool OpenJourneyPlannerBackend::queryStopover(const StopoverRequest &request, St
bool OpenJourneyPlannerBackend::queryJourney(const JourneyRequest &request, JourneyReply *reply, QNetworkAccessManager *nam) const
{
OpenJourneyPlannerRequestBuilder builder;
builder.setTestMode(true); // ### for development only
const auto postData = builder.buildTripRequest(request);
const auto netReq = networkRequest();
logRequest(request, netReq, postData);
qDebug().noquote() << postData; // ### for development only
// TODO
return false;
}
......
......@@ -71,9 +71,7 @@ QByteArray OpenJourneyPlannerRequestBuilder::buildStopEventRequest(const Stopove
w.writeStartElement(ojpNS(), QStringLiteral("OJPStopEventRequest"));
w.writeStartElement(ojpNS(), QStringLiteral("Location"));
w.writeStartElement(ojpNS(), QStringLiteral("PlaceRef"));
w.writeTextElement(ojpNS(), QStringLiteral("StopPlaceRef"), req.stop().identifier(QStringLiteral("uic"))); // ### TODO configure id type
w.writeEndElement(); // </ojp:PlaceRef>
writePlaceRef(w, req.stop());
w.writeTextElement(ojpNS(), QStringLiteral("DepArrTime"), req.dateTime().toUTC().toString(Qt::ISODate));
w.writeEndElement(); // </ojp:Location>
......@@ -98,11 +96,38 @@ QByteArray OpenJourneyPlannerRequestBuilder::buildTripRequest(const JourneyReque
setupWriter(w);
writeStartServiceRequest(w);
w.writeTextElement(siriNS(), QStringLiteral("RequestRef"), QStringLiteral("KPublicTransport"));
w.writeStartElement(ojpNS(), QStringLiteral("OJPTripRequest"));
// TODO
w.writeStartElement(ojpNS(), QStringLiteral("Origin"));
writePlaceRef(w, req.from());
if (req.dateTimeMode() == JourneyRequest::Departure) {
w.writeTextElement(ojpNS(), QStringLiteral("DepArrTime"), req.dateTime().toUTC().toString(Qt::ISODate));
}
w.writeEndElement(); // </ojp:Origin>
w.writeStartElement(ojpNS(), QStringLiteral("Destination"));
writePlaceRef(w, req.to());
if (req.dateTimeMode() == JourneyRequest::Arrival) {
w.writeTextElement(ojpNS(), QStringLiteral("DepArrTime"), req.dateTime().toUTC().toString(Qt::ISODate));
}
w.writeEndElement(); // </ojp:Destination>
// TODO IndividualTransportOptions
w.writeStartElement(ojpNS(), QStringLiteral("Params"));
w.writeTextElement(ojpNS(), QStringLiteral("IncludeTrackSections"), req.includePaths() ? QStringLiteral("true") : QStringLiteral("false"));
w.writeTextElement(ojpNS(), QStringLiteral("IncludeLegProjection"), QStringLiteral("true")); // ???
w.writeTextElement(ojpNS(), QStringLiteral("IncludeTurnDescription"), req.includePaths() ? QStringLiteral("true") : QStringLiteral("false"));
w.writeTextElement(ojpNS(), QStringLiteral("IncludeAccessibility"), QStringLiteral("true")); // ???
w.writeTextElement(ojpNS(), QStringLiteral("IncludeIntermediateStops"), req.includeIntermediateStops() ? QStringLiteral("true") : QStringLiteral("false"));
w.writeTextElement(ojpNS(), QStringLiteral("IncludeFares"), QStringLiteral("false")); // TODO
w.writeTextElement(ojpNS(), QStringLiteral("NumberOfResults"), QString::number(req.maximumResults()));
// TODO NumberOfResultsBefore|After for next/prev requests
w.writeEndElement(); // </ojp:Params>
w.writeEndElement(); // </ojp:OJPTripRequest>
writeEndServiceRequest(w);
return {};
return output;
}
void OpenJourneyPlannerRequestBuilder::setupWriter(QXmlStreamWriter &w) const
......@@ -131,3 +156,18 @@ void OpenJourneyPlannerRequestBuilder::writeEndServiceRequest(QXmlStreamWriter &
w.writeEndElement(); // </siri:OJP>
w.writeEndDocument();
}
void OpenJourneyPlannerRequestBuilder::writePlaceRef(QXmlStreamWriter &w, const Location &loc) const
{
w.writeStartElement(ojpNS(), QStringLiteral("PlaceRef"));
const auto id = loc.identifier(QStringLiteral("uic")); // ### TODO configure id type
if (!id.isEmpty()) {
w.writeTextElement(ojpNS(), QStringLiteral("StopPlaceRef"), id);
} else if (loc.hasCoordinate()) {
w.writeStartElement(ojpNS(), QStringLiteral("GeoPosition"));
w.writeTextElement(siriNS(), QStringLiteral("Longitude"), QString::number(loc.longitude()));
w.writeTextElement(siriNS(), QStringLiteral("Latitude"), QString::number(loc.latitude()));
w.writeEndElement(); // </ojp:GeoPosition>
}
w.writeEndElement(); // </ojp:PlaceRef>
}
......@@ -13,6 +13,7 @@ class QXmlStreamWriter;
namespace KPublicTransport {
class JourneyRequest;
class Location;
class LocationRequest;
class StopoverRequest;
......@@ -31,6 +32,7 @@ private:
void setupWriter(QXmlStreamWriter &w) const;
void writeStartServiceRequest(QXmlStreamWriter &w) const;
void writeEndServiceRequest(QXmlStreamWriter &w) const;
void writePlaceRef(QXmlStreamWriter &w, const Location &loc) const;
bool m_testMode = false;
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment