2024-12-17 19:41:00 +00:00
const locationFallback = ( id , name , fallbackLocations ) => {
if ( fallbackLocations && ( id && fallbackLocations [ id ] || name && fallbackLocations [ name ] ) ) {
return fallbackLocations [ id ] || fallbackLocations [ name ] ;
}
2024-12-07 16:16:31 +00:00
return {
2024-12-17 19:41:00 +00:00
type : 'location' ,
2024-12-07 16:16:31 +00:00
id : id ,
name : name ,
2024-12-08 21:42:57 +00:00
location : null ,
} ;
2024-02-06 22:58:49 +01:00
} ;
2017-11-20 15:43:13 +01:00
2024-12-17 19:41:00 +00:00
const parseJourneyLeg = ( ctx , pt , date , fallbackLocations ) => { // pt = raw leg
2024-02-06 22:58:49 +01:00
const { profile , opt } = ctx ;
2019-10-20 00:19:11 +02:00
2025-01-14 18:13:58 +00:00
const stops = pt . halte ? . length && pt . halte || pt . stops ? . length && pt . stops || [ ] ;
2019-10-20 00:19:11 +02:00
const res = {
2025-01-14 19:48:14 +00:00
origin : stops . length && profile . parseLocation ( ctx , stops [ 0 ] . ort || stops [ 0 ] . station || stops [ 0 ] )
2025-02-16 23:48:29 +01:00
|| pt . abgangsOrt ? . name && profile . parseLocation ( ctx , pt . abgangsOrt )
|| locationFallback ( pt . abfahrtsOrtExtId , pt . abfahrtsOrt , fallbackLocations ) ,
2025-01-14 19:48:14 +00:00
destination : stops . length && profile . parseLocation ( ctx , stops [ stops . length - 1 ] . ort || stops [ stops . length - 1 ] . station || stops [ stops . length - 1 ] )
2025-02-16 23:48:29 +01:00
|| pt . ankunftsOrt ? . name && profile . parseLocation ( ctx , pt . ankunftsOrt )
|| locationFallback ( pt . ankunftsOrtExtId , pt . ankunftsOrt , fallbackLocations ) ,
2024-02-06 22:58:49 +01:00
} ;
2019-06-08 12:54:59 +02:00
2025-01-14 18:13:58 +00:00
const cancelledDep = stops . length && profile . parseCancelled ( stops [ 0 ] ) ;
2025-01-14 19:48:14 +00:00
const dep = profile . parseWhen ( ctx , date , pt . abfahrtsZeitpunkt || pt . abgangsDatum || stops . length && ( stops [ 0 ] . abgangsDatum || stops [ 0 ] . departureTime ? . target ) , pt . ezAbfahrtsZeitpunkt || pt . ezAbgangsDatum || stops . length && ( stops [ 0 ] . ezAbgangsDatum || stops [ 0 ] . departureTime ? . timeType != 'SCHEDULE' && stops [ 0 ] . departureTime ? . predicted ) , cancelledDep ,
) ;
2024-02-06 22:58:49 +01:00
res . departure = dep . when ;
res . plannedDeparture = dep . plannedWhen ;
res . departureDelay = dep . delay ;
if ( dep . prognosedWhen ) {
res . prognosedDeparture = dep . prognosedWhen ;
}
add planned(Arrival|Departure|When), scheduled* -> planned*/prognosed* :boom:
when not cancelled:
{when, delay} -> {when, plannedWhen, delay}
{arrival, arrivalDelay} -> {arrival, plannedArrival, arrivalDelay}
{departure, departureDelay} -> {departure, plannedDeparture, departureDelay}
when cancelled:
{when: null, delay: null, scheduledWhen} -> {when: null, plannedWhen, prognosedWhen, delay}
{arrival: null, arrivalDelay: null, scheduledArrival, formerArrivalDelay} -> {arrival: null, plannedArrival, arrivalDelay, prognosedArrival}
{departure: null, departureDelay: null, scheduledDeparture, formerDepartureDelay} -> {departure: null, plannedDeparture, departureDelay, prognosedDeparture}
2019-08-23 18:51:03 +02:00
2025-01-14 18:13:58 +00:00
const cancelledArr = stops . length && profile . parseCancelled ( stops [ stops . length - 1 ] ) ;
2025-01-14 19:48:14 +00:00
const arr = profile . parseWhen ( ctx , date , pt . ankunftsZeitpunkt || pt . ankunftsDatum || stops . length && ( stops [ stops . length - 1 ] . ankunftsDatum || stops [ stops . length - 1 ] . arrivalTime ? . target ) , pt . ezAnkunftsZeitpunkt || pt . ezAnkunftsDatum || stops . length && ( stops [ stops . length - 1 ] . ezAnkunftsDatum || stops [ stops . length - 1 ] . arrivalTime ? . timeType != 'SCHEDULE' && stops [ stops . length - 1 ] . arrivalTime ? . predicted ) , cancelledArr ,
) ;
2024-02-06 22:58:49 +01:00
res . arrival = arr . when ;
res . plannedArrival = arr . plannedWhen ;
res . arrivalDelay = arr . delay ;
if ( arr . prognosedWhen ) {
res . prognosedArrival = arr . prognosedWhen ;
}
2020-09-10 23:57:03 +02:00
2024-12-07 16:16:31 +00:00
/ * T O D O r e s . r e a c h a b l e r i s N o t i z e n [
{
"key" : "text.realtime.connection.brokentrip" ,
"value" : "Due to delays a connecting service may not be reachable."
}
] * /
2017-11-20 15:43:13 +01:00
2024-12-18 01:16:57 +00:00
if ( ( opt . polylines || opt . polyline ) && pt . polylineGroup ) {
2024-12-17 19:41:00 +00:00
res . polyline = profile . parsePolyline ( ctx , pt . polylineGroup ) ; // TODO polylines not returned anymore, set "poly": true in request, apparently only works for /reiseloesung/verbindung
2019-10-20 00:19:11 +02:00
}
2018-04-30 12:49:58 +02:00
2025-01-03 10:57:24 +00:00
const type = pt . verkehrsmittel ? . typ || pt . typ ;
2025-01-11 18:39:32 +00:00
if ( type == 'WALK' || type == 'FUSSWEG' || type == 'TRANSFER' ) {
2025-01-03 10:57:24 +00:00
if ( res . origin ? . id == res . destination ? . id ) {
res . arrival = res . departure ;
res . plannedArrival = res . plannedDeparture ;
res . arrivalDelay = res . departureDelay ;
}
2024-02-06 22:58:49 +01:00
res . public = true ;
res . walking = true ;
2024-12-07 16:16:31 +00:00
res . distance = pt . distanz || null ;
2025-01-03 10:57:24 +00:00
if ( type == 'TRANSFER' ) {
res . transfer = true ;
}
2025-01-11 18:39:32 +00:00
// TODO res.checkin
2024-12-07 16:16:31 +00:00
} else {
2025-01-03 10:57:24 +00:00
res . tripId = pt . journeyId || pt . zuglaufId ;
2024-12-07 16:16:31 +00:00
res . line = profile . parseLine ( ctx , pt ) || null ;
2025-01-03 10:57:24 +00:00
res . direction = pt . verkehrsmittel ? . richtung || pt . richtung || null ;
2024-12-07 16:16:31 +00:00
// TODO res.currentLocation
2025-01-14 18:13:58 +00:00
// TODO trainStartDate?
2025-01-14 19:48:14 +00:00
2025-01-14 18:13:58 +00:00
if ( stops . length ) {
2025-01-14 19:48:14 +00:00
const arrPl = profile . parsePlatform ( ctx ,
stops [ stops . length - 1 ] . gleis || stops [ stops . length - 1 ] . track ? . target ,
stops [ stops . length - 1 ] . ezGleis || stops [ stops . length - 1 ] . track ? . prediction ,
cancelledArr ,
) ;
2024-12-07 16:16:31 +00:00
res . arrivalPlatform = arrPl . platform ;
res . plannedArrivalPlatform = arrPl . plannedPlatform ;
if ( arrPl . prognosedPlatform ) {
res . prognosedArrivalPlatform = arrPl . prognosedPlatform ;
2018-07-16 12:18:23 +02:00
}
2024-12-08 21:42:57 +00:00
// res.arrivalPrognosisType = null; // TODO
2018-06-25 13:54:30 +02:00
2025-01-14 19:48:14 +00:00
const depPl = profile . parsePlatform ( ctx ,
stops [ 0 ] . gleis || stops [ 0 ] . track ? . target ,
stops [ 0 ] . ezGleis || stops [ 0 ] . track ? . prediction ,
cancelledDep ,
) ;
2024-12-07 16:16:31 +00:00
res . departurePlatform = depPl . platform ;
res . plannedDeparturePlatform = depPl . plannedPlatform ;
if ( depPl . prognosedPlatform ) {
res . prognosedDeparturePlatform = depPl . prognosedPlatform ;
}
2024-12-08 21:42:57 +00:00
// res.departurePrognosisType = null; // TODO
2024-12-07 16:16:31 +00:00
if ( opt . stopovers ) {
2025-01-14 18:13:58 +00:00
res . stopovers = stops . map ( s => profile . parseStopover ( ctx , s , date ) ) ;
2024-12-07 16:16:31 +00:00
// filter stations the train passes without stopping, as this doesn't comply with fptf (yet)
res . stopovers = res . stopovers . filter ( ( x ) => ! x . passBy ) ;
2024-12-08 21:42:57 +00:00
}
2024-12-07 16:16:31 +00:00
if ( opt . remarks ) {
2025-01-02 14:00:45 +00:00
res . remarks = profile . parseRemarks ( ctx , pt ) ;
2024-02-06 22:58:49 +01:00
}
2019-10-20 00:19:11 +02:00
}
2018-12-02 01:05:01 +01:00
2024-12-07 16:16:31 +00:00
// TODO cycle, alternatives
2019-10-20 00:19:11 +02:00
}
2017-11-20 15:43:13 +01:00
2025-01-03 10:57:24 +00:00
if ( cancelledDep || cancelledArr || pt . cancelled || pt . canceled ) {
2024-02-06 22:58:49 +01:00
res . cancelled = true ;
Object . defineProperty ( res , 'canceled' , { value : true } ) ;
2017-11-20 15:43:13 +01:00
}
2025-01-03 10:57:24 +00:00
const load = profile . parseLoadFactor ( opt , pt . auslastungsmeldungen || pt . auslastungsInfos ) ;
2024-12-21 15:26:49 +00:00
if ( load ) {
res . loadFactor = load ;
}
2024-02-06 22:58:49 +01:00
return res ;
} ;
2017-11-20 15:43:13 +01:00
2022-05-07 16:17:37 +02:00
export {
parseJourneyLeg ,
2024-02-06 22:58:49 +01:00
} ;