Handle Berlin Ringbahn and other transfer-at-destination trains

Requires Travel::Status::DE::IRIS v1.38 (not released yet)
This commit is contained in:
Daniel Friesel 2019-12-15 13:42:11 +01:00
parent 7ae60f2ea4
commit cabf159f46
2 changed files with 28 additions and 11 deletions

View file

@ -272,6 +272,7 @@ sub startup {
station => $station, station => $station,
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,
keep_transfers => 1,
lookbehind => 20, lookbehind => 20,
datetime => DateTime->now( time_zone => 'Europe/Berlin' ) datetime => DateTime->now( time_zone => 'Europe/Berlin' )
->subtract( minutes => $lookbehind ), ->subtract( minutes => $lookbehind ),
@ -627,7 +628,18 @@ sub startup {
my $journey my $journey
= $db->select( 'in_transit', '*', { user_id => $uid } ) = $db->select( 'in_transit', '*', { user_id => $uid } )
->expand->hash; ->expand->hash;
my ($train) = List::Util::first { $_->train_id eq $train_id }
# Note that a train may pass the same station several times.
# Notable example: S41 / S42 ("Ringbahn") both starts and
# terminates at Berlin Südkreuz
my ($train) = List::Util::first {
$_->train_id eq $train_id
and $_->sched_arrival
and $_->sched_arrival->epoch > $user->{sched_departure}->epoch
}
@{ $status->{results} };
$train //= List::Util::first { $_->train_id eq $train_id }
@{ $status->{results} }; @{ $status->{results} };
# When a checkout is triggered by a checkin, there is an edge case # When a checkout is triggered by a checkin, there is an edge case

View file

@ -3,7 +3,7 @@ use Mojo::Base 'Mojolicious::Command';
use DateTime; use DateTime;
use JSON; use JSON;
use List::Util qw(first); use List::Util;
has description => has description =>
'Perform automatic checkout when users arrive at their destination'; 'Perform automatic checkout when users arrive at their destination';
@ -27,8 +27,6 @@ sub run {
my $arr = $entry->{arr_ds100}; my $arr = $entry->{arr_ds100};
my $train_id = $entry->{train_id}; my $train_id = $entry->{train_id};
$self->app->log->debug("Processing $uid");
# Note: IRIS data is not always updated in real-time. Both departure and # Note: IRIS data is not always updated in real-time. Both departure and
# arrival delays may take several minutes to appear, especially in case # arrival delays may take several minutes to appear, especially in case
# of large-scale disturbances. We work around this by continuing to # of large-scale disturbances. We work around this by continuing to
@ -37,14 +35,13 @@ sub run {
eval { eval {
if ( $now->epoch - $entry->{real_dep_ts} < 900 ) { if ( $now->epoch - $entry->{real_dep_ts} < 900 ) {
$self->app->log->debug(" - updating departure");
my $status = $self->app->get_departures( $dep, 30, 30 ); my $status = $self->app->get_departures( $dep, 30, 30 );
if ( $status->{errstr} ) { if ( $status->{errstr} ) {
die("get_departures($dep): $status->{errstr}\n"); die("get_departures($dep): $status->{errstr}\n");
} }
my ($train) my ($train) = List::Util::first { $_->train_id eq $train_id }
= first { $_->train_id eq $train_id } @{ $status->{results} }; @{ $status->{results} };
if ( not $train ) { if ( not $train ) {
die("could not find train $train_id at $dep\n"); die("could not find train $train_id at $dep\n");
@ -80,14 +77,23 @@ sub run {
or $now->epoch - $entry->{real_arr_ts} < 600 ) or $now->epoch - $entry->{real_arr_ts} < 600 )
) )
{ {
$self->app->log->debug(" - updating arrival");
my $status = $self->app->get_departures( $arr, 20, 220 ); my $status = $self->app->get_departures( $arr, 20, 220 );
if ( $status->{errstr} ) { if ( $status->{errstr} ) {
die("get_departures($arr): $status->{errstr}\n"); die("get_departures($arr): $status->{errstr}\n");
} }
my ($train) # Note that a train may pass the same station several times.
= first { $_->train_id eq $train_id } @{ $status->{results} }; # Notable example: S41 / S42 ("Ringbahn") both starts and
# terminates at Berlin Südkreuz
my ($train) = List::Util::first {
$_->train_id eq $train_id
and $_->sched_arrival
and $_->sched_arrival->epoch > $entry->{sched_dep_ts}
}
@{ $status->{results} };
$train //= List::Util::first { $_->train_id eq $train_id }
@{ $status->{results} };
if ( not $train ) { if ( not $train ) {
@ -116,7 +122,6 @@ sub run {
$self->app->add_route_timestamps( $uid, $train, 0 ); $self->app->add_route_timestamps( $uid, $train, 0 );
} }
elsif ( $entry->{real_arr_ts} ) { elsif ( $entry->{real_arr_ts} ) {
$self->app->log->debug(" - checking out");
my ( undef, $error ) = $self->app->checkout( $arr, 1, $uid ); my ( undef, $error ) = $self->app->checkout( $arr, 1, $uid );
if ($error) { if ($error) {
die("${error}\n"); die("${error}\n");