From 1ad8fa3a51b930807d76d92a2d42bdd7f4ef5b80 Mon Sep 17 00:00:00 2001 From: Birte Kristina Friesel Date: Wed, 29 May 2024 20:17:01 +0200 Subject: Move wagonorder request to a separate helper This is in preparation for HAFAS wagonorder support --- lib/Travelynx.pm | 202 +++++++++++++++++++++++------------------- lib/Travelynx/Command/work.pm | 4 + 2 files changed, 113 insertions(+), 93 deletions(-) diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index 902241d..19d4094 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -506,6 +506,8 @@ sub startup { # mustn't be called during a transaction if ( not $opt{in_transaction} ) { $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' ); } @@ -992,6 +994,8 @@ sub startup { if ( not $opt{in_transaction} ) { $self->run_hook( $uid, 'update' ); $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 ); 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. # HAFAS already has all relevant information. $self->helper( @@ -1365,99 +1474,6 @@ sub startup { } )->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) { $self->dbdb->get_stationinfo_p( $in_transit->{dep_eva} )->then( sub { diff --git a/lib/Travelynx/Command/work.pm b/lib/Travelynx/Command/work.pm index 10b1b69..eea6d1b 100644 --- a/lib/Travelynx/Command/work.pm +++ b/lib/Travelynx/Command/work.pm @@ -171,6 +171,8 @@ sub run { } else { $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} ) ? 1 : 0 ); + $self->app->add_wagonorder( $uid, 0, $train->train_id, + $train->sched_departure, $train->train_no ); } } elsif ( $entry->{real_arr_ts} ) { -- cgit v1.2.3