File indexing completed on 2024-12-29 04:51:06

0001 /*
0002    SPDX-FileCopyrightText: 2023 Luca Weiss <luca@z3ntu.xyz>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 // List of stations present in Taiwan High Speed Rail network.
0008 // "code" value matches wikidata "station code" (P296)
0009 const stations = {
0010     1: {"code": "NAG/1", "name": "南港", "nameEn": "Nangang"},
0011     2: {"code": "TPE/2", "name": "台北", "nameEn": "Taipei"},
0012     3: {"code": "BAQ/3", "name": "板橋", "nameEn": "Banqiao"},
0013     4: {"code": "TAY/4", "name": "桃園", "nameEn": "Taoyuan"},
0014     5: {"code": "HSC/5", "name": "新竹", "nameEn": "Hsinchu"},
0015     6: {"code": "MIL/6", "name": "苗栗", "nameEn": "Miaoli"},
0016     7: {"code": "TAC/7", "name": "台中", "nameEn": "Taichung"},
0017     8: {"code": "CHH/8", "name": "彰化", "nameEn": "Changhua"},
0018     9: {"code": "YUL/9", "name": "雲林", "nameEn": "Yunlin"},
0019     10: {"code": "CHY/10", "name": "嘉義", "nameEn": "Chiayi"},
0020     11: {"code": "TAN/11", "name": "台南", "nameEn": "Tainan"},
0021     12: {"code": "ZUY/12", "name": "左營", "nameEn": "Zuoying"},
0022 }
0023 
0024 function main(text) {
0025     /*
0026      * Unknown parts of data:
0027      * 21-23: 000, possibly space for larger train numbers?
0028      * 43-44: 00
0029      * 60-74: various values, unknown
0030      * 94-99: various values, unknown
0031      * 100-105: "INTIRS" or empty (000000), ticket type?
0032      * 106-109: 0000
0033      * 110-117: date of travel?
0034      * 118-123: various values, unknown
0035     */
0036     const ticketNumber = text.substr(0, 13);
0037     const reservationCode = text.substr(13, 8);
0038     const trainNumber = parseInt(text.substr(24, 4));
0039     const departureStationNr = parseInt(text.substr(28, 3));
0040     const departureDateTime = JsonLd.toDateTime(text.substr(31, 12), "yyyyMMddhhmm", "zh");
0041     const arrivalStationNr = parseInt(text.substr(45, 3));
0042     const arrivalDateTime = JsonLd.toDateTime(text.substr(48, 12), "yyyyMMddhhmm", "zh");
0043     const seatData = text.substr(75, 9);
0044     const price = parseInt(text.substr(84, 10));
0045 
0046     const departureStation = stations[departureStationNr];
0047     const arrivalStation = stations[arrivalStationNr];
0048 
0049     // seatData is 3 numbers each: car, row & seat (A-E but represented as 1-5)
0050     const seatCar = parseInt(seatData.substr(0, 3));
0051     const seatRow = parseInt(seatData.substr(3, 3));
0052     const seatNumber = parseInt(seatData.substr(6, 3));
0053     const seatLetter = String.fromCharCode(65 + (seatNumber-1));
0054     const seat = seatRow + seatLetter;
0055 
0056     var res = JsonLd.newTrainReservation();
0057     res.reservationFor.departureStation.name = departureStation["name"] + " (" + departureStation["nameEn"] + ")";
0058     res.reservationFor.departureStation.identifier = departureStation["code"];
0059     res.reservationFor.departureTime = departureDateTime;
0060     res.reservationFor.arrivalStation.name = arrivalStation["name"] + " (" + arrivalStation["nameEn"] + ")";
0061     res.reservationFor.arrivalStation.identifier = arrivalStation["code"];
0062     res.reservationFor.arrivalTime = arrivalDateTime;
0063     res.reservationFor.trainNumber = trainNumber;
0064     res.reservedTicket.ticketedSeat.seatSection = seatCar;
0065     res.reservedTicket.ticketedSeat.seatNumber = seat;
0066     res.reservedTicket.ticketToken = "qrCode:" + text;
0067     res.totalPrice = price;
0068     res.priceCurrency = "TWD";
0069 
0070     return res;
0071 }