summaryrefslogtreecommitdiff
path: root/lib/DBInfoscreen/Helper/HAFAS.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/DBInfoscreen/Helper/HAFAS.pm')
-rw-r--r--lib/DBInfoscreen/Helper/HAFAS.pm57
1 files changed, 57 insertions, 0 deletions
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;