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

View file

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