Use HAFAS routes as single source of truth. Overwrite initial IRIS route.

This commit is contained in:
Birte Kristina Friesel 2024-05-22 19:04:57 +02:00
parent dc4f61c2aa
commit cd6df2aa9b
No known key found for this signature in database
GPG key ID: 19E6E524EBB177BA
2 changed files with 57 additions and 45 deletions

View file

@ -1243,6 +1243,7 @@ sub startup {
my $route = $in_transit->{route}; my $route = $in_transit->{route};
# TODO get_tripid_p is only needed on the first call, afterwards the tripid is known.
$self->hafas->get_tripid_p( train => $train )->then( $self->hafas->get_tripid_p( train => $train )->then(
sub { sub {
my ($trip_id) = @_; my ($trip_id) = @_;
@ -1253,7 +1254,7 @@ sub startup {
data => { trip_id => $trip_id } data => { trip_id => $trip_id }
); );
return $self->hafas->get_route_timestamps_p( return $self->hafas->get_route_p(
train => $train, train => $train,
trip_id => $trip_id, trip_id => $trip_id,
with_polyline => ( with_polyline => (
@ -1264,42 +1265,53 @@ sub startup {
} }
)->then( )->then(
sub { sub {
my ( $route_data, $journey, $polyline ) = @_; my ( $new_route, $journey, $polyline ) = @_;
my $db_route;
for my $station ( @{$route} ) { for my $i ( 0 .. $#{$new_route} ) {
if ( $station->[0] my $old_name = $route->[$i][0];
=~ m{^Betriebsstelle nicht bekannt (\d+)$} ) my $old_eva = $route->[$i][1];
{ my $old_entry = $route->[$i][2];
my $eva = $1; my $new_name = $new_route->[$i]->{name};
if ( $route_data->{$eva} ) { my $new_eva = $new_route->[$i]->{eva};
$station->[0] = $route_data->{$eva}{name}; my $new_entry = $new_route->[$i];
$station->[1] = $route_data->{$eva}{eva};
if ( $old_name eq $new_name ) {
if ( $old_entry->{rt_arr}
and not $new_entry->{rt_arr} )
{
$new_entry->{rt_arr} = $old_entry->{rt_arr};
$new_entry->{arr_delay}
= $old_entry->{arr_delay};
}
if ( $old_entry->{rt_dep}
and not $new_entry->{rt_dep} )
{
$new_entry->{rt_dep} = $old_entry->{rt_dep};
$new_entry->{dep_delay}
= $old_entry->{dep_delay};
} }
} }
if ( my $sd = $route_data->{ $station->[0] } ) {
$station->[1] = $sd->{eva};
if ( $station->[2]{isAdditional} ) {
$sd->{isAdditional} = 1;
}
if ( $station->[2]{isCancelled} ) {
$sd->{isCancelled} = 1;
}
# keep rt_dep / rt_arr if they are no longer present push(
my %old; @{$db_route},
for my $k (qw(rt_arr rt_dep arr_delay dep_delay)) { [
$old{$k} = $station->[2]{$k}; $new_name,
} $new_eva,
$station->[2] = $sd; {
if ( not $station->[2]{rt_arr} ) { sched_arr => $new_entry->{sched_arr},
$station->[2]{rt_arr} = $old{rt_arr}; rt_arr => $new_entry->{rt_arr},
$station->[2]{arr_delay} = $old{arr_delay}; arr_delay => $new_entry->{arr_delay},
} sched_dep => $new_entry->{sched_dep},
if ( not $station->[2]{rt_dep} ) { rt_dep => $new_entry->{rt_dep},
$station->[2]{rt_dep} = $old{rt_dep}; dep_delay => $new_entry->{dep_delay},
$station->[2]{dep_delay} = $old{dep_delay}; tz_offset => $new_entry->{tz_offset},
} isAdditional => $new_entry->{isAdditional},
} isCancelled => $new_entry->{isCancelled},
load => $new_entry->{load},
}
]
);
} }
my @messages; my @messages;
@ -1318,7 +1330,7 @@ sub startup {
$self->in_transit->set_route_data( $self->in_transit->set_route_data(
uid => $uid, uid => $uid,
db => $db, db => $db,
route => $route, route => $db_route,
delay_messages => [ delay_messages => [
map { [ $_->[0]->epoch, $_->[1] ] } map { [ $_->[0]->epoch, $_->[1] ] }
$train->delay_messages $train->delay_messages

View file

@ -199,7 +199,7 @@ sub get_journey_p {
return $promise; return $promise;
} }
sub get_route_timestamps_p { sub get_route_p {
my ( $self, %opt ) = @_; my ( $self, %opt ) = @_;
my $promise = Mojo::Promise->new; my $promise = Mojo::Promise->new;
@ -219,13 +219,12 @@ sub get_route_timestamps_p {
sub { sub {
my ($hafas) = @_; my ($hafas) = @_;
my $journey = $hafas->result; my $journey = $hafas->result;
my $ret = {}; my $ret = [];
my $polyline; my $polyline;
my $station_is_past = 1; my $station_is_past = 1;
for my $stop ( $journey->route ) { for my $stop ( $journey->route ) {
my $name = $stop->loc->name; my $entry = {
$ret->{$name} = $ret->{ $stop->loc->eva } = {
name => $stop->loc->name, name => $stop->loc->name,
eva => $stop->loc->eva, eva => $stop->loc->eva,
sched_arr => _epoch( $stop->sched_arr ), sched_arr => _epoch( $stop->sched_arr ),
@ -237,26 +236,27 @@ sub get_route_timestamps_p {
load => $stop->load load => $stop->load
}; };
if ( $stop->tz_offset ) { if ( $stop->tz_offset ) {
$ret->{$name}{tz_offset} = $stop->tz_offset; $entry->{tz_offset} = $stop->tz_offset;
} }
if ( ( $stop->arr_cancelled or not $stop->sched_arr ) if ( ( $stop->arr_cancelled or not $stop->sched_arr )
and ( $stop->dep_cancelled or not $stop->sched_dep ) ) and ( $stop->dep_cancelled or not $stop->sched_dep ) )
{ {
$ret->{$name}{isCancelled} = 1; $entry->{isCancelled} = 1;
} }
if ( if (
$station_is_past $station_is_past
and not $ret->{$name}{isCancelled} and not $entry->{isCancelled}
and $now->epoch < ( and $now->epoch < (
$ret->{$name}{rt_arr} // $ret->{$name}{rt_dep} $entry->{rt_arr} // $entry->{rt_dep}
// $ret->{$name}{sched_arr} // $entry->{sched_arr} // $entry->{sched_dep}
// $ret->{$name}{sched_dep} // $now->epoch // $now->epoch
) )
) )
{ {
$station_is_past = 0; $station_is_past = 0;
} }
$ret->{$name}{isPast} = $station_is_past; $entry->{isPast} = $station_is_past;
push( @{$ret}, $entry );
} }
if ( $journey->polyline ) { if ( $journey->polyline ) {