summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2020-01-22 19:40:45 +0100
committerDaniel Friesel <derf@finalrewind.org>2020-01-22 19:40:45 +0100
commit0913658790c2e6b2e22b874337e6a614964e2693 (patch)
treed739aa6409076398d22309f04e6893c163761ba5
parent98537fa46e42244837fd5e4c815ba119ff4d8521 (diff)
fetch wagon order asynchronously2.5.12
-rw-r--r--lib/DBInfoscreen/Controller/Wagenreihung.pm116
1 files changed, 95 insertions, 21 deletions
diff --git a/lib/DBInfoscreen/Controller/Wagenreihung.pm b/lib/DBInfoscreen/Controller/Wagenreihung.pm
index 8816dda..a87bb29 100644
--- a/lib/DBInfoscreen/Controller/Wagenreihung.pm
+++ b/lib/DBInfoscreen/Controller/Wagenreihung.pm
@@ -4,38 +4,112 @@ use Mojo::Base 'Mojolicious::Controller';
# Copyright (C) 2011-2019 Daniel Friesel <derf+dbf@finalrewind.org>
# License: 2-Clause BSD
+use Encode qw(decode);
+use JSON;
+use Mojo::Promise;
use Travel::Status::DE::DBWagenreihung;
+my $dbf_version = qx{git describe --dirty} || 'experimental';
+
+chomp $dbf_version;
+
+sub get_wagenreihung_p {
+ my ( $self, $train_no, $api_ts ) = @_;
+
+ my $url
+ = "https://www.apps-bahn.de/wr/wagenreihung/1.0/${train_no}/${api_ts}";
+
+ my $cache = $self->app->cache_iris_rt;
+
+ my $promise = Mojo::Promise->new;
+
+ if ( my $content = $cache->thaw($url) ) {
+ $promise->resolve($content);
+ return $promise;
+ }
+
+ $self->ua->request_timeout(10)
+ ->get_p( $url, { 'User-Agent' => "dbf.finalrewind.org/${dbf_version}" } )
+ ->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;
+}
+
sub wagenreihung {
my ($self) = @_;
my $train = $self->stash('train');
my $departure = $self->stash('departure');
- my $wr = Travel::Status::DE::DBWagenreihung->new(
- cache => $self->app->cache_iris_rt,
- departure => $departure,
- train_number => $train,
- );
+ $self->render_later;
- if ( $wr->has_bad_wagons ) {
+ $self->get_wagenreihung_p( $train, $departure )->then(
+ sub {
+ my ($json) = @_;
+ my $wr;
+ eval {
+ $wr
+ = Travel::Status::DE::DBWagenreihung->new(
+ from_json => $json );
+ };
+ if ($@) {
+ $self->render(
+ 'wagenreihung',
+ title => "Zug $train",
+ wr_error => scalar $@,
+ train_no => $train,
+ wr => undef,
+ hide_opts => 1,
+ );
+ }
- # create fake positions as the correct ones are not available
- my $pos = 0;
- for my $wagon ( $wr->wagons ) {
- $wagon->{position}{start_percent} = $pos;
- $wagon->{position}{end_percent} = $pos + 4;
- $pos += 4;
+ if ( $wr->has_bad_wagons ) {
+
+ # create fake positions as the correct ones are not available
+ my $pos = 0;
+ for my $wagon ( $wr->wagons ) {
+ $wagon->{position}{start_percent} = $pos;
+ $wagon->{position}{end_percent} = $pos + 4;
+ $pos += 4;
+ }
+ }
+
+ $self->render(
+ 'wagenreihung',
+ wr_error => undef,
+ title => join( ' / ',
+ map { $wr->train_type . ' ' . $_ } $wr->train_numbers ),
+ train_no => $train,
+ wr => $wr,
+ hide_opts => 1,
+ );
}
- }
+ )->catch(
+ sub {
+ my ($err) = @_;
+ $self->render(
+ 'wagenreihung',
+ title => "Zug $train",
+ wr_error => scalar $err,
+ train_no => $train,
+ wr => undef,
+ hide_opts => 1,
+ );
+ }
+ )->wait;
- $self->render(
- 'wagenreihung',
- title =>
- join( ' / ', map { $wr->train_type . ' ' . $_ } $wr->train_numbers ),
- train_no => $train,
- wr => $wr,
- hide_opts => 1,
- );
}
1;