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

Rebase VR extractor on top of ERA SSB types

This means we can now also produce meaningful data if we only have a VR
barcode without the surrounding PDF, e.g. when scanning tickets bought
at vending machines. We also get a few more details (class, reservation
number) for free.
parent 5500facc
Pipeline #61466 canceled with stages
......@@ -4,49 +4,47 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
// see https://community.kde.org/KDE_PIM/KItinerary/vr.fi_Barcode
function readStationCode(bitarray, offset)
function parseSsbBarcode(ssb, node)
{
var s = "";
for (var i = 0; i < 5; ++i) {
var n = bitarray.readNumberMSB(offset + i * 6, 6);
if (n != 36)
s += String.fromCharCode(n + 55);
if (ssb.version != 1) {
return;
}
return s;
}
function parseTicket(pdf) {
var res = JsonLd.newTrainReservation();
var bitarray = Barcode.toBitArray(Context.barcode);
var trainNum = bitarray.readNumberMSB(22 * 8, 14) + "";
if (trainNum == 0)
return null; // TODO this misses bus legs!
if (ssb.trainNumber > 0) {
res.reservationFor.trainNumber = ssb.trainNumber;
}
res.reservationFor.departureTime = ssb.departureTime(node.contextDateTime);
res.reservationFor.departureStation.name = ssb.departureStationAlpha;
res.reservationFor.arrivalStation.name = ssb.arrivalStationAlpha;
// for station codes see: https://rata.digitraffic.fi/api/v1/metadata/stations
res.reservationFor.departureStation.identifier = "vrfi:" + ssb.departureStationAlpha;
res.reservationFor.arrivalStation.identifier = "vrfi:" + ssb.arrivalStationAlpha;
var text = pdf.pages[Context.pdfPageNumber].text;
var trip = text.match("(.*) - (.*)\n.*(\\d{4}).*?(\\d{2}:\\d{2}).*?(\\d{2}:\\d{2})\n(.*?" + trainNum + ")");
res.reservationFor.trainNumber = trip[6];
res.reservedTicket.ticketToken = "aztectbin:" + Barcode.toBase64(ssb.rawData);
res.reservedTicket.ticketedSeat.seatingType = ssb.classOfTransport;
var departureDay = bitarray.readNumberMSB(4 * 8 + 7, 9) - 1;
var day = new Date(0);
day.setYear(trip[3]);
day.setTime(day.getTime() + departureDay * 24 * 60 * 60 * 1000);
res.reservationFor.departureTime = JsonLd.toDateTime(day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + day.getDate() + '-' + trip[4], "yyyy-M-d-hh:mm", "en");
res.reservationFor.arrivalTime = JsonLd.toDateTime(day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + day.getDate() + '-' + trip[5], "yyyy-M-d-hh:mm", "en");
var coachNumber = bitarray.readNumberMSB(30 * 8, 6);
if (coachNumber > 0) {
res.reservedTicket.ticketedSeat.seatSection = coachNumber + "";
res.reservedTicket.ticketedSeat.seatNumber = bitarray.readNumberMSB(30 * 8 + 6, 7) + "";
if (ssb.coachNumber > 0) {
res.reservedTicket.ticketedSeat.seatSection = ssb.coachNumber;
res.reservedTicket.ticketedSeat.seatNumber = ssb.seatNumber;
}
// for station codes see: https://rata.digitraffic.fi/api/v1/metadata/stations
res.reservationNumber = ssb.reservationReference + "";
return res;
}
function parseTicket(pdf, node, trigger) {
var res = trigger.result[0];
if (trigger.content.trainNumber == 0)
return; // TODO this misses bus legs!
var text = pdf.pages[trigger.location].text;
var trip = text.match("(.*) - (.*)\n.*(\\d{4}).*?(\\d{2}:\\d{2}).*?(\\d{2}:\\d{2})\n(.*?" + trigger.content.trainNumber + ")");
res.reservationFor.trainNumber = trip[6];
var date = trigger.content.firstDayOfValidity(node.contextDateTime).toISOString().substr(0, 10);
res.reservationFor.departureTime = JsonLd.toDateTime(date + trip[4], "yyyy-MM-ddhh:mm", "en");
res.reservationFor.arrivalTime = JsonLd.toDateTime(date + trip[5], "yyyy-MM-ddhh:mm", "en");
res.reservationFor.departureStation.name = trip[1];
res.reservationFor.departureStation.identifier = "vrfi:" + readStationCode(bitarray, 13*8 + 2);
res.reservationFor.arrivalStation.name = trip[2];
res.reservationFor.arrivalStation.identifier = "vrfi:" + readStationCode(bitarray, 17*8 + 1);
res.reservedTicket.ticketToken = "aztectbin:" + Barcode.toBase64(Context.barcode);
return res;
}
{
"filter": [
{
"match": "^\\x{10}",
"mimeType": "application/octet-stream",
"scope": "Descendants"
}
],
"function": "parseTicket",
"script": "vr.fi.js",
"mimeType": "application/pdf"
}
[
{
"filter": [
{
"field": "issuerCode",
"match": "10",
"mimeType": "internal/era-ssb",
"scope": "Descendants"
}
],
"function": "parseTicket",
"script": "vr.fi.js",
"mimeType": "application/pdf"
},
{
"filter": [
{
"field": "issuerCode",
"match": "10",
"mimeType": "internal/era-ssb",
"scope": "Current"
}
],
"function": "parseSsbBarcode",
"script": "vr.fi.js",
"mimeType": "internal/era-ssb"
}
]
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