From d2f3efbd0c2839c287a6e9b43b5383816b162a56 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 6 Sep 2020 21:23:36 +0200 Subject: Move HAFAS polyline request to HAFAS helper --- lib/DBInfoscreen/Helper/HAFAS.pm | 57 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'lib/DBInfoscreen/Helper') diff --git a/lib/DBInfoscreen/Helper/HAFAS.pm b/lib/DBInfoscreen/Helper/HAFAS.pm index ba6295f..8007ff4 100644 --- a/lib/DBInfoscreen/Helper/HAFAS.pm +++ b/lib/DBInfoscreen/Helper/HAFAS.pm @@ -7,6 +7,7 @@ use 5.020; use DateTime; use Encode qw(decode encode); use Mojo::JSON qw(decode_json); +use Mojo::Promise; use XML::LibXML; sub new { @@ -297,4 +298,60 @@ sub get_tripid { return; } +# Input: (HAFAS TripID, line number) +# Output: Promise returning a +# https://github.com/public-transport/hafas-client/blob/4/docs/trip.md instance +# on success +sub get_polyline_p { + my ( $self, $trip_id, $line ) = @_; + + my $url + = "https://2.db.transport.rest/trips/${trip_id}?lineName=${line}&polyline=true"; + my $cache = $self->{realtime_cache}; + my $promise = Mojo::Promise->new; + + if ( my $content = $cache->thaw($url) ) { + $promise->resolve($content); + $self->{log}->debug("GET $url (cached)"); + return $promise; + } + + $self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} ) + ->then( + sub { + my ($tx) = @_; + $self->{log}->debug("GET $url (OK)"); + my $json = decode_json( $tx->res->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 $ret = { + name => $json->{line}{name} // '?', + polyline => [@coordinate_list], + raw => $json, + }; + + $cache->freeze( $url, $ret ); + $promise->resolve($ret); + } + )->catch( + sub { + my ($err) = @_; + $self->{log}->debug("GET $url (error: $err)"); + $promise->reject($err); + } + )->wait; + + return $promise; +} + 1; -- cgit v1.2.3