Move wagonorder request to a separate helper

This is in preparation for HAFAS wagonorder support
This commit is contained in:
Birte Kristina Friesel 2024-05-29 20:17:01 +02:00
parent eef53c6940
commit 1ad8fa3a51
No known key found for this signature in database
GPG key ID: 19E6E524EBB177BA
2 changed files with 113 additions and 93 deletions

View file

@ -506,6 +506,8 @@ sub startup {
# mustn't be called during a transaction # mustn't be called during a transaction
if ( not $opt{in_transaction} ) { if ( not $opt{in_transaction} ) {
$self->add_route_timestamps( $uid, $train, 1 ); $self->add_route_timestamps( $uid, $train, 1 );
$self->add_wagonorder( $uid, 1, $train->train_id,
$train->sched_departure, $train->train_no );
$self->run_hook( $uid, 'checkin' ); $self->run_hook( $uid, 'checkin' );
} }
@ -992,6 +994,8 @@ sub startup {
if ( not $opt{in_transaction} ) { if ( not $opt{in_transaction} ) {
$self->run_hook( $uid, 'update' ); $self->run_hook( $uid, 'update' );
$self->add_route_timestamps( $uid, $train, 0, 1 ); $self->add_route_timestamps( $uid, $train, 0, 1 );
$self->add_wagonorder( $uid, 0, $train->train_id,
$train->sched_departure, $train->train_no );
} }
$promise->resolve( 1, undef ); $promise->resolve( 1, undef );
return; return;
@ -1218,6 +1222,111 @@ sub startup {
} }
); );
$self->helper(
'add_wagonorder' => sub {
my ( $self, $uid, $is_departure, $train_id, $sched_departure,
$train_no )
= @_;
$uid //= $self->current_user->{id};
my $db = $self->pg->db;
if ($sched_departure) {
$self->dbdb->has_wagonorder_p( $sched_departure, $train_no )
->then(
sub {
my ($api) = @_;
return $self->dbdb->get_wagonorder_p( $api,
$sched_departure, $train_no );
}
)->then(
sub {
my ($wagonorder) = @_;
my $data = {};
my $user_data = {};
if ( $is_departure and not exists $wagonorder->{error} )
{
$data->{wagonorder_dep} = $wagonorder;
$user_data->{wagongroups} = [];
for my $group (
@{
$wagonorder->{data}{istformation}
{allFahrzeuggruppe} // []
}
)
{
my @wagons;
for
my $wagon ( @{ $group->{allFahrzeug} // [] } )
{
push(
@wagons,
{
id => $wagon->{fahrzeugnummer},
number =>
$wagon->{wagenordnungsnummer},
type => $wagon->{fahrzeugtyp},
}
);
}
push(
@{ $user_data->{wagongroups} },
{
name =>
$group->{fahrzeuggruppebezeichnung},
from =>
$group->{startbetriebsstellename},
to => $group->{zielbetriebsstellename},
no => $group->{verkehrlichezugnummer},
wagons => [@wagons],
}
);
if ( $group->{fahrzeuggruppebezeichnung}
and $group->{fahrzeuggruppebezeichnung} eq
'ICE0304' )
{
$data->{wagonorder_pride} = 1;
}
}
$self->in_transit->update_data(
uid => $uid,
db => $db,
data => $data,
train_id => $train_id,
);
$self->in_transit->update_user_data(
uid => $uid,
db => $db,
user_data => $user_data,
train_id => $train_id,
);
}
elsif ( not $is_departure
and not exists $wagonorder->{error} )
{
$data->{wagonorder_arr} = $wagonorder;
$self->in_transit->update_data(
uid => $uid,
db => $db,
data => $data,
train_id => $train_id,
);
}
return;
}
)->catch(
sub {
# no wagonorder? no problem.
return;
}
)->wait;
}
}
);
# This helper is only ever called from an IRIS context. # This helper is only ever called from an IRIS context.
# HAFAS already has all relevant information. # HAFAS already has all relevant information.
$self->helper( $self->helper(
@ -1365,99 +1474,6 @@ sub startup {
} }
)->wait; )->wait;
if ( $train->sched_departure ) {
$self->dbdb->has_wagonorder_p( $train->sched_departure,
$train->train_no )->then(
sub {
my ($api) = @_;
return $self->dbdb->get_wagonorder_p( $api,
$train->sched_departure, $train->train_no );
}
)->then(
sub {
my ($wagonorder) = @_;
my $data = {};
my $user_data = {};
if ( $is_departure and not exists $wagonorder->{error} )
{
$data->{wagonorder_dep} = $wagonorder;
$user_data->{wagongroups} = [];
for my $group (
@{
$wagonorder->{data}{istformation}
{allFahrzeuggruppe} // []
}
)
{
my @wagons;
for
my $wagon ( @{ $group->{allFahrzeug} // [] } )
{
push(
@wagons,
{
id => $wagon->{fahrzeugnummer},
number =>
$wagon->{wagenordnungsnummer},
type => $wagon->{fahrzeugtyp},
}
);
}
push(
@{ $user_data->{wagongroups} },
{
name =>
$group->{fahrzeuggruppebezeichnung},
from =>
$group->{startbetriebsstellename},
to => $group->{zielbetriebsstellename},
no => $group->{verkehrlichezugnummer},
wagons => [@wagons],
}
);
if ( $group->{fahrzeuggruppebezeichnung}
and $group->{fahrzeuggruppebezeichnung} eq
'ICE0304' )
{
$data->{wagonorder_pride} = 1;
}
}
$self->in_transit->update_data(
uid => $uid,
db => $db,
data => $data,
train_id => $train_id,
);
$self->in_transit->update_user_data(
uid => $uid,
db => $db,
user_data => $user_data,
train_id => $train_id,
);
}
elsif ( not $is_departure
and not exists $wagonorder->{error} )
{
$data->{wagonorder_arr} = $wagonorder;
$self->in_transit->update_data(
uid => $uid,
db => $db,
data => $data,
train_id => $train_id,
);
}
return;
}
)->catch(
sub {
# no wagonorder? no problem.
return;
}
)->wait;
}
if ($is_departure) { if ($is_departure) {
$self->dbdb->get_stationinfo_p( $in_transit->{dep_eva} )->then( $self->dbdb->get_stationinfo_p( $in_transit->{dep_eva} )->then(
sub { sub {

View file

@ -171,6 +171,8 @@ sub run {
} }
else { else {
$self->app->add_route_timestamps( $uid, $train, 1 ); $self->app->add_route_timestamps( $uid, $train, 1 );
$self->app->add_wagonorder( $uid, 1, $train->train_id,
$train->sched_departure, $train->train_no );
} }
} }
}; };
@ -243,6 +245,8 @@ sub run {
and $now->epoch > $entry->{real_arr_ts} and $now->epoch > $entry->{real_arr_ts}
) ? 1 : 0 ) ? 1 : 0
); );
$self->app->add_wagonorder( $uid, 0, $train->train_id,
$train->sched_departure, $train->train_no );
} }
} }
elsif ( $entry->{real_arr_ts} ) { elsif ( $entry->{real_arr_ts} ) {