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

Export the new ERA SSB ticket parser to JS

Replaces manual decoding in the Thalys extractor.
parent 1316f8ee
Pipeline #51948 passed with stage
in 10 minutes and 51 seconds
......@@ -31,6 +31,11 @@ SSBTicket::SSBTicket(const QByteArray &data)
SSBTicket::~SSBTicket() = default;
bool SSBTicket::isValid() const
{
return !m_data.isEmpty();
}
int SSBTicket::readNumber(int start, int length) const
{
if (start < 0 || length < 1 || start / 8 >= m_data.size() || (start + length) / 8 >= m_data.size() || length > 31) {
......
......@@ -115,6 +115,9 @@ public:
explicit SSBTicket(const QByteArray &data);
~SSBTicket();
/** Returns @c true if this is a valid SSB ticket. */
bool isValid() const;
/** Date of issue. */
Q_INVOKABLE QDate issueDate(const QDate &contextDate = QDate::currentDate());
/** Departure day for type 1 (IRT/RES/BOA) tickets. */
......
......@@ -4,16 +4,6 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
// see https://community.kde.org/KDE_PIM/KItinerary/Thalys_Barcode
function readEncodedString(bitArray, startIdx, size)
{
var id = "";
for (var i = 0; i < size; ++i) {
id += String.fromCharCode(bitArray.readNumberMSB(startIdx + i * 6, 6) + 32);
}
return id;
}
function parseReservation(html) {
var res = JsonLd.newTrainReservation();
......@@ -45,9 +35,9 @@ function parseReservation(html) {
var name = passengerElem.recursiveContent.match(/\n(.*)/);
res.underName.name = name[1];
var bitArray = Barcode.toBitArray(Barcode.fromBase64(token[1]));
res.reservationFor.departureStation.identifier = "benerail:" + readEncodedString(bitArray, 18 * 8 + 4, 5);
res.reservationFor.arrivalStation.identifier = "benerail:" + readEncodedString(bitArray, 22 * 8 + 2, 5);
var ssb = Barcode.decodeEraSsbTicket(Barcode.fromBase64(token[1]));
res.reservationFor.departureStation.identifier = "benerail:" + ssb.type1DepartureStationAlpha;
res.reservationFor.arrivalStation.identifier = "benerail:" + ssb.type1ArrivalStationAlpha;
return res;
}
......@@ -7,6 +7,7 @@
#include "barcode.h"
#include "bitarray.h"
#include <generic/genericpdfextractor_p.h>
#include <era/ssbticket.h>
#include <KItinerary/BarcodeDecoder>
#include <KItinerary/IataBcbpParser>
......@@ -113,6 +114,12 @@ QVariant JsApi::Barcode::decodeVdvTicket(const QVariant &s) const
return QVariant::fromValue(p.ticket());
}
QVariant JsApi::Barcode::decodeEraSsbTicket(const QVariant &s) const
{
SSBTicket ticket(s.toByteArray());
return ticket.isValid() ? QVariant::fromValue(ticket) : QVariant();
}
QString JsApi::Barcode::toBase64(const QVariant &b) const
{
return QString::fromUtf8(b.toByteArray().toBase64());
......
......@@ -57,6 +57,11 @@ public:
* @returns An instance of VdvTicket.
*/
Q_INVOKABLE QVariant decodeVdvTicket(const QVariant &s) const;
/** Decode an ERA SSB ticket barcode.
* @param s A QByteArray containing the raw ERA SSB barcode data.
* @returns An instance of SSBTicket.
*/
Q_INVOKABLE QVariant decodeEraSsbTicket(const QVariant &s) const;
/** Converts the given QByteArray into an base64 encoded string. */
Q_INVOKABLE QString toBase64(const QVariant &b) const;
......
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