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

Use UIC operator company code from ticket codes to select KPT backends

This is particularly useful for locations where we don't have geo
coordinates yet. In that case we can now chose a single applicable
backend right away.
parent bcf8906a
......@@ -11,6 +11,8 @@
#include <KItinerary/Ticket>
#include <KPublicTransport/Journey>
#include <KPublicTransport/JourneyRequest>
#include <KPublicTransport/Manager>
#include <QtTest/qtest.h>
#include <QStandardPaths>
......@@ -112,6 +114,33 @@ private Q_SLOTS:
section.setDistance(2000);
QVERIFY(PublicTransport().warnAboutSection(section));
}
void testBackendSelection()
{
Organization provider;
provider.setIdentifier(QStringLiteral("uic:1181"));
TrainTrip trip;
trip.setProvider(provider);
TrainReservation res;
res.setReservationFor(trip);
KPublicTransport::Manager mgr;
KPublicTransport::JourneyRequest req;
mgr.setBackendEnabled(QLatin1String("at_oebb"), true);
PublicTransport::selectBackends(req, &mgr, res);
QCOMPARE(req.backendIds(), QStringList({ QLatin1String("at_oebb") }));
mgr.setBackendEnabled(QLatin1String("at_oebb"), false);
PublicTransport::selectBackends(req, &mgr, res);
QCOMPARE(req.backendIds(), QStringList());
provider.setIdentifier(QStringLiteral("uic:80"));
trip.setProvider(provider);
res.setReservationFor(trip);
mgr.setBackendEnabled(QLatin1String("de_db"), true);
PublicTransport::selectBackends(req, &mgr, res);
QCOMPARE(req.backendIds(), QStringList({ QLatin1String("de_db") }));
}
};
QTEST_GUILESS_MAIN(PublicTransportTest)
......
......@@ -188,6 +188,7 @@ void LiveDataManager::checkReservation(const QVariant &res, const QString& resId
req.setDateTimeMode(JourneyRequest::Departure);
req.setIncludeIntermediateStops(true);
req.setIncludePaths(true);
PublicTransport::selectBackends(req, m_ptMgr, res);
auto reply = m_ptMgr->queryJourney(req);
connect(reply, &Reply::finished, this, [this, resId, reply]() { journeyQueryFinished(reply, resId); });
return;
......@@ -197,6 +198,7 @@ void LiveDataManager::checkReservation(const QVariant &res, const QString& resId
StopoverRequest req(PublicTransport::locationFromPlace(LocationUtil::departureLocation(res), res));
req.setMode(StopoverRequest::QueryDeparture);
req.setDateTime(SortUtil::startDateTime(res));
PublicTransport::selectBackends(req, m_ptMgr, res);
auto reply = m_ptMgr->queryStopover(req);
connect(reply, &Reply::finished, this, [this, resId, reply]() { stopoverQueryFinished(reply, LiveData::Departure, resId); });
}
......@@ -205,6 +207,7 @@ void LiveDataManager::checkReservation(const QVariant &res, const QString& resId
StopoverRequest req(PublicTransport::locationFromPlace(LocationUtil::arrivalLocation(res), res));
req.setMode(StopoverRequest::QueryArrival);
req.setDateTime(SortUtil::endDateTime(res));
PublicTransport::selectBackends(req, m_ptMgr, res);
auto reply = m_ptMgr->queryStopover(req);
connect(reply, &Reply::finished, this, [this, resId, reply]() { stopoverQueryFinished(reply, LiveData::Arrival, resId); });
}
......
......@@ -5,6 +5,7 @@
*/
#include "publictransport.h"
#include "reservationhelper.h"
#include "logging.h"
#include <KItinerary/BusTrip>
......@@ -13,6 +14,7 @@
#include <KItinerary/Ticket>
#include <KPublicTransport/Attribution>
#include <KPublicTransport/Backend>
#include <KPublicTransport/Journey>
#include <KPublicTransport/Line>
#include <KPublicTransport/Location>
......@@ -417,4 +419,29 @@ KPublicTransport::JourneySection PublicTransport::lastTransportSection(const KPu
return {};
}
QStringList PublicTransport::suitableBackendsForReservation(KPublicTransport::Manager *mgr, const QVariant &res)
{
using namespace KPublicTransport;
const auto companyCode = ReservationHelper::uicCompanyCode(res);
if (companyCode.size() != 4) {
return {};
}
QStringList backendIds;
for (const auto &backend : mgr->backends()) {
if (!mgr->isBackendEnabled(backend.identifier())) {
continue;
}
for (const auto cov : { CoverageArea::Realtime, CoverageArea::Regular, CoverageArea::Any }) {
if (backend.coverageArea(cov).uicCompanyCodes().contains(companyCode)) {
backendIds.push_back(backend.identifier());
break;
}
}
}
return backendIds;
}
#include "moc_publictransport.cpp"
......@@ -15,6 +15,7 @@
#include <KPublicTransport/Location>
#include <KPublicTransport/Line>
#include <KPublicTransport/Manager>
namespace KPublicTransport {
class Journey;
......@@ -99,10 +100,15 @@ public:
/** Last public transport section of the given section. */
static KPublicTransport::JourneySection lastTransportSection(const KPublicTransport::Journey &journey);
/** Selects suitable backends based on UIC operator codes from the reservation. */
template <typename Request>
static void selectBackends(Request &request, KPublicTransport::Manager *mgr, const QVariant &res);
private:
// for use by the template code
static QString idenfitierFromLocation(const KPublicTransport::Location &loc);
static KItinerary::PostalAddress addressFromLocation(const KPublicTransport::Location &loc);
static QStringList suitableBackendsForReservation(KPublicTransport::Manager *mgr, const QVariant &res);
};
......@@ -132,4 +138,10 @@ inline T PublicTransport::updateToLocation(const T &place, const KPublicTranspor
return newPlace;
}
template <typename Request>
inline void PublicTransport::selectBackends(Request &request, KPublicTransport::Manager *mgr, const QVariant &res)
{
request.setBackendIds(suitableBackendsForReservation(mgr, res));
}
#endif // PUBLICTRANSPORT_H
......@@ -90,3 +90,21 @@ QDateTime ReservationHelper::arrivalTime(const QVariant &res)
}
return {};
}
QString ReservationHelper::uicCompanyCode(const QVariant &res)
{
QString id;
if (JsonLd::isA<TrainReservation>(res)) {
id = res.value<TrainReservation>().reservationFor().value<TrainTrip>().provider().identifier();
} // TODO in theory we can also have this for bus reservations
if (!id.startsWith(QLatin1String("uic:")) || id.size() > 8) {
return {};
}
id = id.mid(4);
while (id.size() < 4) {
id.insert(0, QLatin1Char('0'));
}
return id;
}
......@@ -30,6 +30,9 @@ namespace ReservationHelper
*/
QDateTime departureTime(const QVariant &res);
QDateTime arrivalTime(const QVariant &res);
/** Returns the UIC company code for @p res, if any. */
QString uicCompanyCode(const QVariant &res);
}
#endif // RESERVATIONHELPER_H
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