Commit 84bbb431 authored by Volker Krause's avatar Volker Krause
Browse files

Adapt the journey GraphQL query for OTP2

There's minor changes in available fields, and new support for querying
next/previous journeys.
parent 16c9692a
Pipeline #64026 passed with stage
in 25 seconds
......@@ -129,16 +129,23 @@ bool OpenTripPlannerGraphQLBackend::queryJourney(const JourneyRequest &req, Jour
gqlReq.setVariable(QStringLiteral("fromLon"), req.from().longitude());
gqlReq.setVariable(QStringLiteral("toLat"), req.to().latitude());
gqlReq.setVariable(QStringLiteral("toLon"), req.to().longitude());
auto dt = req.dateTime();
const auto context = requestContext(req).value<OpenTripPlannerRequestContext>();
if (context.dateTime.isValid()) {
dt = context.dateTime;
}
if (timeZone().isValid()) {
dt = dt.toTimeZone(timeZone());
}
gqlReq.setVariable(QStringLiteral("date"), dt.toString(QStringLiteral("yyyy-MM-dd")));
gqlReq.setVariable(QStringLiteral("time"), dt.toString(QStringLiteral("hh:mm:ss")));
gqlReq.setVariable(QStringLiteral("dateTime"), dt.toString(Qt::ISODate));
gqlReq.setVariable(QStringLiteral("arriveBy"), req.dateTimeMode() == JourneyRequest::Arrival);
gqlReq.setVariable(QStringLiteral("maxResults"), req.maximumResults());
gqlReq.setVariable(QStringLiteral("lang"), preferredLanguage());
// TODO set context.searchWindow?
QJsonArray modes;
QJsonObject walkMode;
......@@ -172,6 +179,12 @@ bool OpenTripPlannerGraphQLBackend::queryJourney(const JourneyRequest &req, Jour
OpenTripPlannerParser p(backendId());
p.setKnownRentalVehicleNetworks(m_rentalNetworks);
addResult(reply, this, p.parseJourneys(gqlReply.data()));
if (p.m_nextJourneyContext.dateTime.isValid()) {
setNextRequestContext(reply, p.m_nextJourneyContext);
}
if (p.m_prevJourneyContext.dateTime.isValid()) {
setPreviousRequestContext(reply, p.m_prevJourneyContext);
}
}
});
......
......@@ -471,15 +471,20 @@ Journey OpenTripPlannerParser::parseJourney(const QJsonObject &obj) const
return journey;
}
std::vector<Journey> OpenTripPlannerParser::parseJourneys(const QJsonObject& obj) const
std::vector<Journey> OpenTripPlannerParser::parseJourneys(const QJsonObject& obj)
{
std::vector<Journey> journeys;
const auto journeysArray = obj.value(QLatin1String("plan")).toObject().value(QLatin1String("itineraries")).toArray();
const auto plan = obj.value(QLatin1String("plan")).toObject();
const auto journeysArray = plan.value(QLatin1String("itineraries")).toArray();
journeys.reserve(journeysArray.size());
for (const auto &journeyObj : journeysArray) {
journeys.push_back(parseJourney(journeyObj.toObject()));
}
m_nextJourneyContext.dateTime = parseJourneyDateTime(plan.value(QLatin1String("nextDateTime")));
m_prevJourneyContext.dateTime = parseJourneyDateTime(plan.value(QLatin1String("prevDateTime")));
m_nextJourneyContext.searchWindow = m_prevJourneyContext.searchWindow = plan.value(QLatin1String("searchWindowUsed")).toInt();
return journeys;
}
......@@ -8,6 +8,7 @@
#define KPUBLICTRANSPORT_OPENTRIPPLANNERPARSER_H
#include "kpublictransport_export.h"
#include "opentripplannerrequestcontext.h"
#include <KPublicTransport/Location>
#include <KPublicTransport/RentalVehicle>
......@@ -45,8 +46,10 @@ public:
std::vector<Location> parseGeocodeResult(const QJsonArray &array) const;
std::vector<Stopover> parseDepartures(const QJsonObject &obj) const;
std::vector<Stopover> parseDeparturesArray(const QJsonArray &array) const;
std::vector<Journey> parseJourneys(const QJsonObject &obj) const;
std::vector<Journey> parseJourneys(const QJsonObject &obj);
OpenTripPlannerRequestContext m_nextJourneyContext;
OpenTripPlannerRequestContext m_prevJourneyContext;
private:
QVariant parseRentalVehicleData(const QJsonObject &obj) const;
/// @return @c false for Location objects that should be discarded entirely
......
/*
SPDX-FileCopyrightText: 2021 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#ifndef KPUBLICTRANSPORT_OPENTRIPPLANNERREQUESTCONTEXT_H
#define KPUBLICTRANSPORT_OPENTRIPPLANNERREQUESTCONTEXT_H
#include <QDateTime>
#include <QMetaType>
#include <QVariant>
namespace KPublicTransport {
/** Journey request context object for OTP2. */
class OpenTripPlannerRequestContext
{
public:
QDateTime dateTime;
int searchWindow;
inline operator QVariant() const {
return QVariant::fromValue(*this);
}
};
}
Q_DECLARE_METATYPE(KPublicTransport::OpenTripPlannerRequestContext)
#endif // KPUBLICTRANSPORT_OPENTRIPPLANNERREQUESTCONTEXT_H
......@@ -17,5 +17,7 @@
<file>entur/journey.graphql</file>
<file>entur/stationByCoordinate.graphql</file>
<file>entur/stationByName.graphql</file>
<file>otp2/journey.graphql</file>
</qresource>
</RCC>
# SPDX-FileCopyrightText: 2020-2021 Volker Krause <vkrause@kde.org>
# SPDX-License-Identifier: CC0-1.0
query journeys(
$fromLat: Float!,
$fromLon: Float!,
$toLat: Float!,
$toLon: Float!,
$date: String!,
$time: String!,
$arriveBy: Boolean!,
$modes: [TransportMode]!,
$maxResults: Int!,
$lang: String!
) {
plan (
from: { lat: $fromLat, lon: $fromLon }
to: { lat: $toLat, lon: $toLon }
date: $date
time: $time
arriveBy: $arriveBy
numItineraries: $maxResults
transportModes: $modes
locale: $lang
) {
itineraries {
legs {
startTime
endTime
departureDelay
arrivalDelay
realTime
distance
mode
transitLeg
from {
name
lat
lon
stop {
id: gtfsId
platformCode
}
bikeRentalStation {
id
networks,
bikesAvailable,
spacesAvailable
}
}
to {
name
lat
lon
stop {
id: gtfsId
platformCode
}
bikeRentalStation {
id
stationId,
networks,
bikesAvailable
}
}
trip {
route {
type
desc
shortName
longName
color
textColor
#bikesAllowed
alerts {
alertHeaderTextTranslations {
language
text
}
alertDescriptionTextTranslations {
language
text
}
alertEffect
alertCause
alertSeverityLevel
}
}
tripHeadsign
tripShortName
}
intermediateStops: intermediatePlaces {
stop {
id: gtfsId
name
lat
lon
platformCode
}
scheduledArrivalTime: arrivalTime
scheduledDepartureTime: departureTime
}
legGeometry {
length
points
}
}
}
prevDateTime
nextDateTime
searchWindowUsed
}
}
Markdown is supported
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