summaryrefslogtreecommitdiff
path: root/lib/DBInfoscreen/Controller
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2020-01-05 09:56:52 +0100
committerDaniel Friesel <derf@finalrewind.org>2020-01-05 09:56:52 +0100
commit75e54f73e5d9e935a9355c427422697bfd97d6f1 (patch)
treec277fad0d6ce4f775fab47b09d881cbf9d38f0df /lib/DBInfoscreen/Controller
parente540de179913a378b00a029f6eedc3b02d63a76b (diff)
Fetch map asynchronously
Diffstat (limited to 'lib/DBInfoscreen/Controller')
-rw-r--r--lib/DBInfoscreen/Controller/Map.pm172
1 files changed, 101 insertions, 71 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;