From d701442306ebdac5d7d6a865003a1a8a1f6ac334 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sat, 1 Jun 2019 11:52:42 +0200 Subject: [PATCH] Set arrival time via HAFAS if not available from IRIS --- lib/Travelynx.pm | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index 3bc9e3c..7b87b99 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -530,7 +530,8 @@ sub startup { my $now = DateTime->now( time_zone => 'Europe/Berlin' ); my $journey - = $db->select( 'in_transit', '*', { user_id => $uid } )->hash; + = $db->select( 'in_transit', '*', { user_id => $uid } ) + ->expand->hash; my ($train) = first { $_->train_id eq $train_id } @{ $status->{results} }; @@ -568,6 +569,31 @@ sub startup { if ( not( defined $train or $force ) ) { $self->run_hook( $uid, 'update' ); + + # Arrival time via IRIS is unknown, try falling back to HAFAS + if ( my $station_data + = first { $_->[0] eq $station } @{ $journey->{route} } ) + { + $station_data = $station_data->[1]; + if ( $station_data->{sched_arr} ) { + my $sched_arr + = epoch_to_dt( $station_data->{sched_arr} ); + my $rt_arr = $sched_arr->clone; + if ( $station_data->{adelay} + and $station_data->{adelay} =~ m{^\d+$} ) + { + $rt_arr->add( minutes => $station_data->{adelay} ); + } + $db->update( + 'in_transit', + { + sched_arrival => $sched_arr, + real_arrival => $rt_arr + }, + { user_id => $uid } + ); + } + } return ( 1, undef ); }