From 75e54f73e5d9e935a9355c427422697bfd97d6f1 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 5 Jan 2020 09:56:52 +0100 Subject: Fetch map asynchronously --- lib/DBInfoscreen/Controller/Map.pm | 172 ++++++++++++++++++++++--------------- templates/route_map.html.ep | 6 +- 2 files changed, 104 insertions(+), 74 deletions(-) diff --git a/lib/DBInfoscreen/Controller/Map.pm b/lib/DBInfoscreen/Controller/Map.pm index 81c3b20..6337432 100644 --- a/lib/DBInfoscreen/Controller/Map.pm +++ b/lib/DBInfoscreen/Controller/Map.pm @@ -1,53 +1,67 @@ package DBInfoscreen::Controller::Map; + use Mojo::Base 'Mojolicious::Controller'; use Mojo::JSON qw(decode_json); +use Mojo::Promise; + +use Encode qw(decode); my $dbf_version = qx{git describe --dirty} || 'experimental'; chomp $dbf_version; -sub get_hafas_polyline { - my ( $ua, $cache, $trip_id, $line ) = @_; +sub get_hafas_polyline_p { + my ( $self, $trip_id, $line ) = @_; my $url = "https://2.db.transport.rest/trips/${trip_id}?lineName=${line}&polyline=true"; + my $cache = $self->app->cache_iris_main; + my $promise = Mojo::Promise->new; + + say $url; if ( my $content = $cache->thaw($url) ) { - return $content; + $promise->resolve($content); + return $promise; } - $ua->request_timeout(2); - - #say $url; - my $res - = $ua->get( + $self->ua->request_timeout(5) + ->get_p( $url => { 'User-Agent' => "dbf.finalrewind.org/${dbf_version}" } ) - ->result; - if ( $res->is_error ) { - return; - } + ->then( + sub { + my ($tx) = @_; + my $body = decode( 'utf-8', $tx->res->body ); + my $json = decode_json($body); + my @coordinate_list; + + for my $feature ( @{ $json->{polyline}{features} } ) { + if ( exists $feature->{geometry}{coordinates} ) { + push( @coordinate_list, $feature->{geometry}{coordinates} ); + } + + #if ($feature->{type} eq 'Feature') { + # say "Feature " . $feature->{properties}{name}; + #} + } - my $json = decode_json( $res->body ); - my @coordinate_list; + my $ret = { + name => $json->{line}{name} // '?', + polyline => [@coordinate_list], + stopovers => $json->{stopovers}, + }; - for my $feature ( @{ $json->{polyline}{features} } ) { - if ( exists $feature->{geometry}{coordinates} ) { - push( @coordinate_list, $feature->{geometry}{coordinates} ); + $cache->freeze( $url, $ret ); + $promise->resolve($ret); } + )->catch( + sub { + my ($err) = @_; + $promise->reject($err); + } + )->wait; - #if ($feature->{type} eq 'Feature') { - # say "Feature " . $feature->{properties}{name}; - #} - } - - my $ret = { - name => $json->{line}{name} // '?', - polyline => [@coordinate_list], - stopovers => $json->{stopovers}, - }; - - $cache->freeze( $url, $ret ); - return $ret; + return $promise; } sub route { @@ -55,50 +69,66 @@ sub route { my $trip_id = $self->stash('tripid'); my $line_no = $self->stash('lineno'); - my $pl = get_hafas_polyline( $self->ua, $self->app->cache_iris_main, - $trip_id, $line_no ); - my @polyline = @{ $pl->{polyline} }; - my @line_pairs; - my @station_coordinates; - - for my $i ( 1 .. $#polyline ) { - push( - @line_pairs, - [ - [ $polyline[ $i - 1 ][1], $polyline[ $i - 1 ][0] ], - [ $polyline[$i][1], $polyline[$i][0] ] - ] - ); - } + $self->render_later; - for my $stop ( @{ $pl->{stopovers} // [] } ) { - push( - @station_coordinates, - [ - [ - $stop->{stop}{location}{latitude}, - $stop->{stop}{location}{longitude} - ], - $stop->{stop}{name} - ] - ); - } + $self->get_hafas_polyline_p( $trip_id, $line_no )->then( + sub { + my ($pl) = @_; + + my @polyline = @{ $pl->{polyline} }; + my @line_pairs; + my @station_coordinates; + + for my $i ( 1 .. $#polyline ) { + push( + @line_pairs, + [ + [ $polyline[ $i - 1 ][1], $polyline[ $i - 1 ][0] ], + [ $polyline[$i][1], $polyline[$i][0] ] + ] + ); + } - $self->render( - 'route_map', - title => $pl->{name}, - hide_opts => 1, - with_map => 1, - polyline_groups => [ - { - polylines => \@line_pairs, - color => '#00838f', - opacity => 0.6, - fit_bounds => 1, + for my $stop ( @{ $pl->{stopovers} // [] } ) { + push( + @station_coordinates, + [ + [ + $stop->{stop}{location}{latitude}, + $stop->{stop}{location}{longitude} + ], + $stop->{stop}{name} + ] + ); } - ], - station_coordinates => [@station_coordinates], - ); + + $self->render( + 'route_map', + title => $pl->{name}, + hide_opts => 1, + with_map => 1, + polyline_groups => [ + { + polylines => \@line_pairs, + color => '#00838f', + opacity => 0.6, + fit_bounds => 1, + } + ], + station_coordinates => [@station_coordinates], + ); + } + )->catch( + sub { + my ($err) = @_; + $self->render( + 'route_map', + title => "Fehler", + hide_opts => 1, + ); + + } + )->wait; } 1; diff --git a/templates/route_map.html.ep b/templates/route_map.html.ep index 505cddb..72ef215 100644 --- a/templates/route_map.html.ep +++ b/templates/route_map.html.ep @@ -1,8 +1,8 @@ -% if (0) { +% if (my $err = stash('error')) {
- Fehler! - Baz + Fehler bei der Routenabfrage +%= $err
% } -- cgit v1.2.3