prepare for wagon order support

This commit is contained in:
Daniel Friesel 2019-06-17 20:59:57 +02:00
parent c35548e909
commit 0bbdd768f4
2 changed files with 77 additions and 13 deletions

View file

@ -414,7 +414,7 @@ sub startup {
return ( undef, 'INSERT failed: ' . $@ );
}
$self->add_route_timestamps( $self->current_user->{id},
$train );
$train, 1 );
$self->run_hook( $self->current_user->{id}, 'checkin' );
return ( $train, undef );
}
@ -677,7 +677,7 @@ sub startup {
return ( 0, undef );
}
$self->run_hook( $uid, 'update' );
$self->add_route_timestamps( $self->current_user->{id}, $train );
$self->add_route_timestamps( $self->current_user->{id}, $train, 0 );
return ( 1, undef );
}
);
@ -1537,6 +1537,40 @@ sub startup {
}
);
$self->helper(
'get_wagonorder_p' => sub {
my ( $self, $ts, $train_no ) = @_;
my $api_ts = $ts->strftime('%Y%m%d%H%M');
my $url
= "https://www.apps-bahn.de/wr/wagenreihung/1.0/${train_no}/${api_ts}";
my $cache = $self->app->cache_iris_main;
my $promise = Mojo::Promise->new;
if ( my $content = $cache->thaw($url) ) {
$promise->resolve($content);
return $promise;
}
$self->ua->request_timeout(5)->get_p($url)->then(
sub {
my ($tx) = @_;
my $body = decode( 'utf-8', $tx->res->body );
my $json = JSON->new->decode($body);
$cache->freeze( $url, $json );
$promise->resolve($json);
}
)->catch(
sub {
my ($err) = @_;
$promise->reject($err);
}
)->wait;
return $promise;
}
);
$self->helper(
'get_hafas_json_p' => sub {
my ( $self, $url ) = @_;
@ -1648,7 +1682,7 @@ sub startup {
$self->helper(
'add_route_timestamps' => sub {
my ( $self, $uid, $train ) = @_;
my ( $self, $uid, $train, $is_departure ) = @_;
$uid //= $self->current_user->{id};
@ -1771,7 +1805,17 @@ sub startup {
$extra_data->{him_msg} = $traininfo2->{messages};
$db->update(
my $res = $db->select( 'in_transit', ['data'],
{ user_id => $uid } );
my $res_h = $res->expand->hash;
if ( $res_h
and $res_h->{data}
and $res_h->{data}{wagonorder} )
{
$extra_data->{wagonorder} = $res_h->{data}{wagonorder};
}
return $db->update_p(
'in_transit',
{
route => JSON->new->encode($route),
@ -1780,6 +1824,26 @@ sub startup {
{ user_id => $uid }
);
}
)->then(
sub {
if ($is_departure) {
return $self->get_wagonorder_p( $train->sched_departure,
$train->train_no );
}
return Mojo::Promise->reject;
}
)->then(
sub {
my ($wagonorder) = @_;
$extra_data->{wagonorder} = $wagonorder;
$db->update(
'in_transit',
{ data => JSON->new->encode($extra_data) },
{ user_id => $uid }
);
}
)->wait;
}
);

View file

@ -66,7 +66,7 @@ sub run {
},
{ user_id => $uid }
);
$self->app->add_route_timestamps( $uid, $train );
$self->app->add_route_timestamps( $uid, $train, 1 );
}
};
if ($@) {
@ -113,7 +113,7 @@ sub run {
},
{ user_id => $uid }
);
$self->app->add_route_timestamps( $uid, $train );
$self->app->add_route_timestamps( $uid, $train, 0 );
}
elsif ( $entry->{real_arr_ts} ) {
$self->app->log->debug(" - checking out");