Commit 52d0ccc2 authored by Volker Krause's avatar Volker Krause
Browse files

Add Hafas mode group probing tool

This should help determining which endpoint needs which parameters for
searching for inter-modal or individual transport options. About half
the current endpoints seem to support this, almost all use different
names for this unfortunately.

Next step is coming up with a way to encode that in our config files.
parent b60e45ca
Pipeline #87566 passed with stage
in 1 minute and 18 seconds
......@@ -7,6 +7,8 @@
#ifndef KPUBLICTRANSPORT_ABSTRACTBACKEND_H
#define KPUBLICTRANSPORT_ABSTRACTBACKEND_H
#include "kpublictransport_export.h"
#include "reply.h"
#include "requestcontext_p.h"
......@@ -35,8 +37,10 @@ class StopoverRequest;
class VehicleLayoutReply;
class VehicleLayoutRequest;
/** Abstract base class for transport provider backends. */
class AbstractBackend
/** Abstract base class for transport provider backends.
* @internal exported for tooling only
*/
class KPUBLICTRANSPORT_EXPORT AbstractBackend
{
Q_GADGET
Q_PROPERTY(QString customCaCertificate WRITE setCustomCaCertificate)
......
......@@ -7,6 +7,8 @@
#ifndef KPUBLICTRANSPORT_HAFASMGATEBACKEND_H
#define KPUBLICTRANSPORT_HAFASMGATEBACKEND_H
#include "kpublictransport_export.h"
#include "hafasbackend.h"
#include "hafasmgateparser.h"
......@@ -19,8 +21,10 @@ namespace KPublicTransport {
class LocationRequest;
/** Backend for the Hafas mgate.exe interface. */
class HafasMgateBackend : public HafasBackend
/** Backend for the Hafas mgate.exe interface.
* @internal exported for tooling only
*/
class KPUBLICTRANSPORT_EXPORT HafasMgateBackend : public HafasBackend
{
Q_GADGET
/** The auth data block in every request. */
......@@ -46,8 +50,9 @@ public:
bool queryStopover(const StopoverRequest &request, StopoverReply *reply, QNetworkAccessManager *nam) const override;
bool queryLocation(const LocationRequest &req, LocationReply *reply, QNetworkAccessManager *nam) const override;
private:
QNetworkRequest makePostRequest(const QJsonObject &svcReq, QByteArray &postData) const;
private:
bool queryJourney(JourneyReply *reply, const QString &fromId, QNetworkAccessManager *nam) const;
bool queryJourney(JourneyReply *reply, const QString &fromId, const QString &toId, QNetworkAccessManager *nam) const;
void setAuthObject(const QJsonObject &obj);
......
......@@ -7,6 +7,8 @@
#ifndef KPUBLICTRANSPORT_BACKEND_P_H
#define KPUBLICTRANSPORT_BACKEND_P_H
#include "kpublictransport_export.h"
#include "coveragearea.h"
#include <QSharedData>
......@@ -23,7 +25,8 @@ class Backend;
class BackendPrivate : public QSharedData {
public:
static const AbstractBackend* impl(const Backend &b);
/** @internal exported for tooling. */
KPUBLICTRANSPORT_EXPORT static const AbstractBackend* impl(const Backend &b);
static void setImpl(Backend &b, std::unique_ptr<AbstractBackend> &&impl);
static Backend fromJson(const QJsonObject &obj);
......
......@@ -17,6 +17,12 @@ add_executable(gbfsprobe gbfsprobe.cpp)
target_link_libraries(gbfsprobe Qt5::Network KPublicTransport)
add_custom_target(run-gbfs-probe COMMAND gbfsprobe ${CMAKE_SOURCE_DIR}/src/lib/gbfs/gbfs-feeds.json WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_executable(hafas-mgate-probe
hafas-mgate-probe.cpp
)
target_link_libraries(hafas-mgate-probe PRIVATE Qt5::Gui Qt5::Network KPublicTransport)
add_custom_target(run-hafas-mgate-probe COMMAND hafas-mgate-probe)
if (TARGET PolyClipping::PolyClipping)
add_executable(transport-api-sync
transport-api-sync.cpp
......
/*
SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include <../lib/datatypes/backend_p.h>
#include <../lib/backends/hafasmgatebackend.h>
#include <KPublicTransport/Backend>
#include <KPublicTransport/Manager>
#include <QCoreApplication>
#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
using namespace KPublicTransport;
static void listConGroups(const QByteArray &data)
{
const auto svcResL = QJsonDocument::fromJson(data).object().value(QLatin1String("svcResL")).toArray();
if (svcResL.empty()) {
qDebug() << "empty reponse" << data;
return;
}
const auto res = svcResL.at(0).toObject().value(QLatin1String("res")).toObject();
const auto conGrpL = res.value(QLatin1String("conGrpL")).toArray();
for (const auto &conGrpV : conGrpL) {
const auto conGrpObj = conGrpV.toObject();
qDebug() << conGrpObj.value(QLatin1String("name")).toString();
const auto requests = conGrpObj.value(QLatin1String("requests")).toArray();
for (const auto requestV : requests) {
const auto requestObj = requestV.toObject();
qDebug() << " " << requestObj.value(QLatin1String("id")).toString();
}
}
}
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
QNetworkAccessManager nam;
nam.setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
Manager ptMgr;
int jobCount = 0;
for (const auto &backend : ptMgr.backends()) {
const auto impl = dynamic_cast<const HafasMgateBackend*>(BackendPrivate::impl(backend));
if (!impl) {
continue;
}
QJsonObject req;
req.insert(QLatin1String("getConGroups"), true);
QJsonObject serverInfo;
serverInfo.insert(QStringLiteral("meth"), QLatin1String("ServerInfo"));
serverInfo.insert(QStringLiteral("req"), req);
QByteArray postData;
const auto netReq = impl->makePostRequest(serverInfo, postData);
++jobCount;
auto netReply = nam.post(netReq, postData);
QObject::connect(netReply, &QNetworkReply::finished, &nam, [netReply, &jobCount, backend]() {
--jobCount;
netReply->deleteLater();
qDebug() << "************************************************" << backend.identifier();
if (netReply->error() != QNetworkReply::NoError) {
qWarning() << netReply->errorString();
} else {
listConGroups(netReply->readAll());
}
if (jobCount == 0) {
QCoreApplication::quit();
}
});
}
return app.exec();
}
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