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

Switch to internal HAFAS client for connection details

parent fc05987b
Loading
Loading
Loading
Loading
+0 −6
Original line number Diff line number Diff line
@@ -15,12 +15,6 @@
	# via multiple URLs, use any one of them.
	base_url => Mojo::URL->new('https://FIXME.local'),

	# travelynx relies on several backend projects. You may override the
	# defaults to use other (e.g. self-hosted) instances.
	backend => {
		hafas_rest_api => 'https://v5.db.transport.rest',
	},

	# Cache directories for schedule and realtime data. Mandatory.  The parent
	# directory ('/var/cache/travelynx' in this case) must already exist.
	cache => {
+0 −3
Original line number Diff line number Diff line
@@ -93,8 +93,6 @@ sub startup {
		$self->secrets( $self->config->{secrets} );
	}

	$self->config->{backend}{hafas_rest_api} //= 'https://v5.db.transport.rest';

	chomp $self->config->{version};

	$self->plugin(
@@ -300,7 +298,6 @@ sub startup {
			my ($self) = @_;
			state $hafas = Travelynx::Helper::HAFAS->new(
				log            => $self->app->log,
				hafas_rest_api => $self->app->config->{backend}{hafas_rest_api},
				main_cache     => $self->app->cache_iris_main,
				realtime_cache => $self->app->cache_iris_rt,
				root_url       => $self->base_url_for('/')->to_abs,
+31 −54
Original line number Diff line number Diff line
@@ -241,15 +241,14 @@ sub get_connecting_trains_p {
	}

	my $hafas_promise = Mojo::Promise->new;
	my $rest_api      = $self->config->{backend}{hafas_rest_api};
	$self->hafas->get_json_p(
"${rest_api}/stops/${eva}/departures?results=120&duration=${lookahead}&stopovers=true&when=10 minutes ago",
		realtime => 1,
		encoding => 'utf-8'
	$self->hafas->get_departures_p(
		eva        => $eva,
		lookbehind => 10,
		lookahead  => $lookahead
	)->then(
		sub {
			my ($json) = @_;
			$hafas_promise->resolve($json);
			my ($status) = @_;
			$hafas_promise->resolve( [ $status->results ] );
			return;
		}
	)->catch(
@@ -268,11 +267,6 @@ sub get_connecting_trains_p {
			my @hafas_trains = @{ $hafas->[0] };
			my @transit_fyi;

			my $strp = DateTime::Format::Strptime->new(
				pattern   => '%Y-%m-%dT%H:%M:%S%z',
				time_zone => 'Europe/Berlin',
			);

			# We've already got a list of connecting trains; this function
			# only adds further information to them. We ignore errors, as
			# partial data is better than no data.
@@ -282,25 +276,19 @@ sub get_connecting_trains_p {
						next;
					}
					for my $hafas_train (@hafas_trains) {
						if ( $hafas_train->{line}{fahrtNr}
						if (    $hafas_train->number
							and $hafas_train->number
							== $iris_train->[0]->train_no )
						{
							for my $stop (
								@{ $hafas_train->{nextStopovers} // [] } )
							for my $stop ( $hafas_train->route ) {
								if (    $stop->{name}
									and $stop->{name} eq $iris_train->[1]
									and $stop->{arr} )
								{
								if (    $stop->{stop}{name}
									and $stop->{stop}{name} eq $iris_train->[1]
									and $stop->{arrival} )
									$iris_train->[2] = $stop->{arr};
									if ( $iris_train->[0]->departure_delay
										and not $stop->{arr_delay} )
									{
									$iris_train->[2] = $strp->parse_datetime(
										$stop->{arrival} );
									if (    $iris_train->[2]
										and $iris_train->[0]->departure_delay
										and $stop->{arrival} eq
										$stop->{plannedArrival} )
									{
# If the departure is delayed, but the arrival supposedly on time, we assume that this is an API issue and manually compute the expected arrival time.
# This avoids cases where a connection is shown as arriving at its destination before having departed at a previous stop.
										$iris_train->[2]
										  ->add( minutes => $iris_train->[0]
											  ->departure_delay );
@@ -315,30 +303,21 @@ sub get_connecting_trains_p {
				if ( $use_history & 0x04 and @{$lt_stops} ) {
					my %via_count = map { $_ => 0 } @{$lt_stops};
					for my $hafas_train (@hafas_trains) {
						for
						  my $stop ( @{ $hafas_train->{nextStopovers} // [] } )
						{
						for my $stop ( $hafas_train->route ) {
							for my $dest ( @{$lt_stops} ) {
								if (    $stop->{stop}{name}
									and $stop->{stop}{name} eq $dest
								if (    $stop->{name}
									and $stop->{name} eq $dest
									and $via_count{$dest} < 2
									and $hafas_train->{when} )
									and $hafas_train->datetime )
								{
									my $departure = $strp->parse_datetime(
										$hafas_train->{when} );
									my $arrival
									  = $strp->parse_datetime(
										$stop->{arrival} );
									my $delay = undef;
									if ( defined $hafas_train->{delay} ) {
										$delay = $hafas_train->{delay} / 60;
									my $departure = $hafas_train->datetime;
									my $arrival   = $stop->{arr};
									my $delay     = $hafas_train->delay;
									if (    $delay
											and $stop->{arrival} eq
											$stop->{plannedArrival} )
										and $stop->{arr} == $stop->{sched_arr} )
									{
										$arrival->add( minutes => $delay );
									}
									}
									if ( $departure->epoch >= $exclude_before )
									{
										$via_count{$dest}++;
@@ -346,9 +325,7 @@ sub get_connecting_trains_p {
											@transit_fyi,
											[
												{
													line =>
													  $hafas_train->{line}
													  {name},
													line => $hafas_train->line,
													departure => $departure,
													departure_delay => $delay
												},
@@ -1164,18 +1141,18 @@ sub station {
		}
	)->catch(
		sub {
			my ($status) = @_;
			if ( $status->{errstr} ) {
			my ($err) = @_;
			if ( ref($err) eq 'HASH' ) {
				$self->render(
					'landingpage',
					version => $self->app->config->{version} // 'UNKNOWN',
					with_autocomplete => 1,
					with_geolocation  => 1,
					error             => $status->{errstr}
					error             => $err->{errstr},
				);
			}
			else {
				$self->render( 'exception', exception => $status );
				$self->render( 'exception', exception => $err );
			}
		}
	)->wait;
+16 −0
Original line number Diff line number Diff line
@@ -83,6 +83,22 @@ sub get_json_p {
	return $promise;
}

sub get_departures_p {
	my ( $self, %opt ) = @_;

	my $when = DateTime->now( time_zone => 'Europe/Berlin' )
	  ->subtract( minutes => $opt{lookbehind} );
	return Travel::Status::DE::HAFAS->new_p(
		station    => $opt{eva},
		datetime   => $when,
		duration   => $opt{lookahead},
		results    => 120,
		cache      => $self->{realtime_cache},
		promise    => 'Mojo::Promise',
		user_agent => $self->{user_agent}->request_timeout(5),
	);
}

sub get_route_timestamps_p {
	my ( $self, %opt ) = @_;