Switch to internal HAFAS client for connection details

This commit is contained in:
Daniel Friesel 2022-11-09 18:14:07 +01:00
parent fc05987b14
commit c26c8820f2
No known key found for this signature in database
GPG key ID: 100D5BFB5166E005
4 changed files with 47 additions and 63 deletions

View file

@ -15,12 +15,6 @@
# via multiple URLs, use any one of them. # via multiple URLs, use any one of them.
base_url => Mojo::URL->new('https://FIXME.local'), base_url => Mojo::URL->new('https://FIXME.local'),
# travelynx relies on several backend projects. You may override the
# defaults to use other (e.g. self-hosted) instances.
backend => {
hafas_rest_api => 'https://v5.db.transport.rest',
},
# Cache directories for schedule and realtime data. Mandatory. The parent # Cache directories for schedule and realtime data. Mandatory. The parent
# directory ('/var/cache/travelynx' in this case) must already exist. # directory ('/var/cache/travelynx' in this case) must already exist.
cache => { cache => {

View file

@ -93,8 +93,6 @@ sub startup {
$self->secrets( $self->config->{secrets} ); $self->secrets( $self->config->{secrets} );
} }
$self->config->{backend}{hafas_rest_api} //= 'https://v5.db.transport.rest';
chomp $self->config->{version}; chomp $self->config->{version};
$self->plugin( $self->plugin(
@ -300,7 +298,6 @@ sub startup {
my ($self) = @_; my ($self) = @_;
state $hafas = Travelynx::Helper::HAFAS->new( state $hafas = Travelynx::Helper::HAFAS->new(
log => $self->app->log, log => $self->app->log,
hafas_rest_api => $self->app->config->{backend}{hafas_rest_api},
main_cache => $self->app->cache_iris_main, main_cache => $self->app->cache_iris_main,
realtime_cache => $self->app->cache_iris_rt, realtime_cache => $self->app->cache_iris_rt,
root_url => $self->base_url_for('/')->to_abs, root_url => $self->base_url_for('/')->to_abs,

View file

@ -241,15 +241,14 @@ sub get_connecting_trains_p {
} }
my $hafas_promise = Mojo::Promise->new; my $hafas_promise = Mojo::Promise->new;
my $rest_api = $self->config->{backend}{hafas_rest_api}; $self->hafas->get_departures_p(
$self->hafas->get_json_p( eva => $eva,
"${rest_api}/stops/${eva}/departures?results=120&duration=${lookahead}&stopovers=true&when=10 minutes ago", lookbehind => 10,
realtime => 1, lookahead => $lookahead
encoding => 'utf-8'
)->then( )->then(
sub { sub {
my ($json) = @_; my ($status) = @_;
$hafas_promise->resolve($json); $hafas_promise->resolve( [ $status->results ] );
return; return;
} }
)->catch( )->catch(
@ -268,11 +267,6 @@ sub get_connecting_trains_p {
my @hafas_trains = @{ $hafas->[0] }; my @hafas_trains = @{ $hafas->[0] };
my @transit_fyi; my @transit_fyi;
my $strp = DateTime::Format::Strptime->new(
pattern => '%Y-%m-%dT%H:%M:%S%z',
time_zone => 'Europe/Berlin',
);
# We've already got a list of connecting trains; this function # We've already got a list of connecting trains; this function
# only adds further information to them. We ignore errors, as # only adds further information to them. We ignore errors, as
# partial data is better than no data. # partial data is better than no data.
@ -282,25 +276,19 @@ sub get_connecting_trains_p {
next; next;
} }
for my $hafas_train (@hafas_trains) { for my $hafas_train (@hafas_trains) {
if ( $hafas_train->{line}{fahrtNr} if ( $hafas_train->number
and $hafas_train->number
== $iris_train->[0]->train_no ) == $iris_train->[0]->train_no )
{ {
for my $stop ( for my $stop ( $hafas_train->route ) {
@{ $hafas_train->{nextStopovers} // [] } ) if ( $stop->{name}
{ and $stop->{name} eq $iris_train->[1]
if ( $stop->{stop}{name} and $stop->{arr} )
and $stop->{stop}{name} eq $iris_train->[1]
and $stop->{arrival} )
{ {
$iris_train->[2] = $strp->parse_datetime( $iris_train->[2] = $stop->{arr};
$stop->{arrival} ); if ( $iris_train->[0]->departure_delay
if ( $iris_train->[2] and not $stop->{arr_delay} )
and $iris_train->[0]->departure_delay
and $stop->{arrival} eq
$stop->{plannedArrival} )
{ {
# If the departure is delayed, but the arrival supposedly on time, we assume that this is an API issue and manually compute the expected arrival time.
# This avoids cases where a connection is shown as arriving at its destination before having departed at a previous stop.
$iris_train->[2] $iris_train->[2]
->add( minutes => $iris_train->[0] ->add( minutes => $iris_train->[0]
->departure_delay ); ->departure_delay );
@ -315,29 +303,20 @@ sub get_connecting_trains_p {
if ( $use_history & 0x04 and @{$lt_stops} ) { if ( $use_history & 0x04 and @{$lt_stops} ) {
my %via_count = map { $_ => 0 } @{$lt_stops}; my %via_count = map { $_ => 0 } @{$lt_stops};
for my $hafas_train (@hafas_trains) { for my $hafas_train (@hafas_trains) {
for for my $stop ( $hafas_train->route ) {
my $stop ( @{ $hafas_train->{nextStopovers} // [] } )
{
for my $dest ( @{$lt_stops} ) { for my $dest ( @{$lt_stops} ) {
if ( $stop->{stop}{name} if ( $stop->{name}
and $stop->{stop}{name} eq $dest and $stop->{name} eq $dest
and $via_count{$dest} < 2 and $via_count{$dest} < 2
and $hafas_train->{when} ) and $hafas_train->datetime )
{ {
my $departure = $strp->parse_datetime( my $departure = $hafas_train->datetime;
$hafas_train->{when} ); my $arrival = $stop->{arr};
my $arrival my $delay = $hafas_train->delay;
= $strp->parse_datetime( if ( $delay
$stop->{arrival} ); and $stop->{arr} == $stop->{sched_arr} )
my $delay = undef; {
if ( defined $hafas_train->{delay} ) { $arrival->add( minutes => $delay );
$delay = $hafas_train->{delay} / 60;
if ( $delay
and $stop->{arrival} eq
$stop->{plannedArrival} )
{
$arrival->add( minutes => $delay );
}
} }
if ( $departure->epoch >= $exclude_before ) if ( $departure->epoch >= $exclude_before )
{ {
@ -346,9 +325,7 @@ sub get_connecting_trains_p {
@transit_fyi, @transit_fyi,
[ [
{ {
line => line => $hafas_train->line,
$hafas_train->{line}
{name},
departure => $departure, departure => $departure,
departure_delay => $delay departure_delay => $delay
}, },
@ -1164,18 +1141,18 @@ sub station {
} }
)->catch( )->catch(
sub { sub {
my ($status) = @_; my ($err) = @_;
if ( $status->{errstr} ) { if ( ref($err) eq 'HASH' ) {
$self->render( $self->render(
'landingpage', 'landingpage',
version => $self->app->config->{version} // 'UNKNOWN', version => $self->app->config->{version} // 'UNKNOWN',
with_autocomplete => 1, with_autocomplete => 1,
with_geolocation => 1, with_geolocation => 1,
error => $status->{errstr} error => $err->{errstr},
); );
} }
else { else {
$self->render( 'exception', exception => $status ); $self->render( 'exception', exception => $err );
} }
} }
)->wait; )->wait;

View file

@ -83,6 +83,22 @@ sub get_json_p {
return $promise; return $promise;
} }
sub get_departures_p {
my ( $self, %opt ) = @_;
my $when = DateTime->now( time_zone => 'Europe/Berlin' )
->subtract( minutes => $opt{lookbehind} );
return Travel::Status::DE::HAFAS->new_p(
station => $opt{eva},
datetime => $when,
duration => $opt{lookahead},
results => 120,
cache => $self->{realtime_cache},
promise => 'Mojo::Promise',
user_agent => $self->{user_agent}->request_timeout(5),
);
}
sub get_route_timestamps_p { sub get_route_timestamps_p {
my ( $self, %opt ) = @_; my ( $self, %opt ) = @_;