Unverified Commit 3f4fc57b authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

Use T-S-DE-HAFAS rather than transport.rest for map

Squashed commit of the following:

commit ebba74fa
Author: Daniel Friesel <derf@finalrewind.org>
Date:   Fri Oct 28 22:50:52 2022 +0200

    update cpanfile.snapshot

commit a6248cca
Author: Daniel Friesel <derf@finalrewind.org>
Date:   Fri Oct 28 22:30:38 2022 +0200

    cpanfile: requires T-S-DE-HAFAS

commit c4b83577
Author: Daniel Friesel <derf@finalrewind.org>
Date:   Fri Oct 28 22:28:41 2022 +0200

    map: handle cancelled arrivals / stops

commit 70bbd2bd
Author: Daniel Friesel <derf@finalrewind.org>
Date:   Thu Oct 27 23:01:24 2022 +0200

    transport.rest is no longer in use

commit d43e9857
Author: Daniel Friesel <derf@finalrewind.org>
Date:   Thu Oct 27 22:59:28 2022 +0200

    about: map is now served via T-S-DE-HAFAS

commit 382262e2
Author: Daniel Friesel <derf@finalrewind.org>
Date:   Wed Oct 26 22:44:36 2022 +0200

    map: update T-S-DE-HAFAS API

commit 7e5f1e5c
Author: Daniel Friesel <derf@finalrewind.org>
Date:   Tue Oct 25 21:19:16 2022 +0200

    Use Travel::Status::DE::HAFAS 4.00 (unreleased) instead of transport.rest

commit 70617901
Author: Daniel Friesel <derf@finalrewind.org>
Date:   Sun Oct 16 22:44:22 2022 +0200

    remove unused and unmaintained train intersection code
parent 1d2cdb99
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -10,5 +10,6 @@ requires 'LWP::UserAgent';
requires 'LWP::Protocol::https';
requires 'Mojolicious';
requires 'Travel::Status::DE::DBWagenreihung', '0.06';
requires 'Travel::Status::DE::HAFAS', '4.00';
requires 'Travel::Status::DE::IRIS';
requires 'XML::LibXML';
+565 −534

File changed.

Preview size limit exceeded, changes collapsed.

+0 −3
Original line number Diff line number Diff line
@@ -22,8 +22,6 @@ sub startup {
	my ($self) = @_;

	$self->config(
		hafas_rest_api => $ENV{DBFAKEDISPLAY_HAFAS_API}
		  // 'https://v5.db.transport.rest',
		hypnotoad => {
			accepts  => $ENV{DBFAKEDISPLAY_ACCEPTS} // 100,
			clients  => $ENV{DBFAKEDISPLAY_CLIENTS} // 10,
@@ -130,7 +128,6 @@ sub startup {
		hafas => sub {
			my ($self) = @_;
			state $hafas = DBInfoscreen::Helper::HAFAS->new(
				api            => $self->config->{hafas_rest_api},
				log            => $self->app->log,
				main_cache     => $self->app->cache_iris_main,
				realtime_cache => $self->app->cache_iris_rt,
+74 −469

File changed.

Preview size limit exceeded, changes collapsed.

+16 −50
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ use 5.020;

use DateTime;
use Encode qw(decode encode);
use Travel::Status::DE::HAFAS;
use Mojo::JSON qw(decode_json);
use Mojo::Promise;
use XML::LibXML;
@@ -438,68 +439,33 @@ sub get_route_timestamps_p {
}

# Input: (HAFAS TripID, line number)
# Output: Promise returning a
# https://github.com/public-transport/hafas-client/blob/4/docs/trip.md instance
# on success
# Output: Promise returning a Travel::Status::DE::HAFAS::Journey instance on success
sub get_polyline_p {
	my ( $self, $trip_id, $line ) = @_;

	my $api     = $self->{api};
	my $url     = "${api}/trips/${trip_id}?lineName=${line}&polyline=true";
	my $log_url = $url;
	my $cache   = $self->{realtime_cache};
	my $promise = Mojo::Promise->new;

	$log_url =~ s{://\K[^:]+:[^@]+\@}{***@};

	if ( my $content = $cache->thaw($url) ) {
		$promise->resolve($content);
		$self->{log}->debug("GET $log_url (cached)");
		return $promise;
	}

	$self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} )
	  ->then(
	Travel::Status::DE::HAFAS->new_p(
		journey => {
			id   => $trip_id,
			name => $line,
		},
		with_polyline => 1,
		cache         => $self->{realtime_cache},
		promise       => 'Mojo::Promise',
		user_agent    => $self->{user_agent}->request_timeout(5)
	)->then(
		sub {
			my ($tx) = @_;

			if ( my $err = $tx->error ) {
				$self->{log}->warn(
"hafas->get_polyline_p($log_url): HTTP $err->{code} $err->{message}"
				);
				$promise->reject(
					"GET $log_url returned HTTP $err->{code} $err->{message}");
				return;
			}

			$self->{log}->debug("GET $log_url (OK)");
			my $json = decode_json( $tx->res->body );
			my @coordinate_list;
			my ($hafas) = @_;
			my $journey = $hafas->result;

			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);
			$promise->resolve($journey);
			return;
		}
	)->catch(
		sub {
			my ($err) = @_;
			$self->{log}->debug("GET $log_url (error: $err)");
			$self->{log}->debug("HAFAS->new_p($trip_id, $line) error: $err");
			$promise->reject($err);
			return;
		}
Loading