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

Add partial EFA map support

No live position yet
parent 027b9433
Loading
Loading
Loading
Loading
+134 −3
Original line number Diff line number Diff line
@@ -311,6 +311,116 @@ sub backpropagate_delay {
	}
}

sub route_efa {
	my ($self)  = @_;
	my $trip_id = $self->stash('tripid');
	my $backend = $self->param('efa');

	my $stopseq;
	if ( $trip_id =~ m{ ^ ([^@]*) @ ([^@]*) [(] ([^)]*) [)] (.*)  $ }x ) {
		$stopseq = {
			stateless => $1,
			stop_id   => $2,
			date      => $3,
			key       => $4
		};
	}
	else {
		$self->render(
			'route_map',
			title     => "DBF",
			hide_opts => 1,
			with_map  => 1,
			error     => "cannot parse trip ID: $trip_id",
		);
		return;
	}

	$self->efa->get_polyline_p(
		stopseq => $stopseq,
		service => $backend
	)->then(
		sub {
			my ($trip) = @_;
			my $now = DateTime->now( time_zone => 'Europe/Berlin' );
			my @polyline
			  = map { { lat => $_->[0], lon => $_->[1] } } $trip->polyline;
			my @line_pairs = polyline_to_line_pairs(@polyline);
			my @route      = $trip->route;

			my @station_coordinates;
			for my $stop (@route) {
				my @stop_lines = ( $stop->full_name );
				if ( $stop->platform ) {
					push( @stop_lines, 'Gleis ' . $stop->platform );
				}
				if ( $stop->arr ) {
					my $arr_line = $stop->arr->strftime('Ankunft: %H:%M');
					if ( $stop->arr_delay ) {
						$arr_line .= sprintf( ' (%+d)', $stop->arr_delay );
					}
					push( @stop_lines, $arr_line );
				}
				if ( $stop->dep ) {
					my $dep_line = $stop->dep->strftime('Abfahrt: %H:%M');
					if ( $stop->dep_delay ) {
						$dep_line .= sprintf( ' (%+d)', $stop->dep_delay );
					}
					push( @stop_lines, $dep_line );
				}

				push( @station_coordinates, [ $stop->latlon, [@stop_lines], ] );
			}

			$self->render(
				'route_map',
				description   => "Karte für " . $trip->name,
				title         => $trip->name,
				hide_opts     => 1,
				with_map      => 1,
				ajax_req      => "${trip_id}/0",
				ajax_route    => q{},
				ajax_polyline => q{},
				origin        => {
					name => ( $trip->route )[0]->full_name,
					ts   => ( $trip->route )[0]->dep,
				},
				destination => {
					name => ( $trip->route )[-1]->full_name,
					ts   => ( $trip->route )[-1]->arr,
				},
				train_no => $trip->number
				? ( $trip->type // q{} . ' ' . $trip->number )
				: undef,
				operator        => $trip->operator,
				next_stop       => q{},
				polyline_groups => [
					{
						polylines  => \@line_pairs,
						color      => '#00838f',
						opacity    => 0.6,
						fit_bounds => 1,
					}
				],
				station_coordinates => \@station_coordinates,
				station_radius      => 100,
				markers             => [],
			);
		}
	)->catch(
		sub {
			my ($err) = @_;
			$self->render(
				'route_map',
				title     => "DBF",
				hide_opts => 1,
				with_map  => 1,
				error     => $err,
			);
		}
	)->wait;
}

sub route {
	my ($self)  = @_;
	my $trip_id = $self->stash('tripid');
@@ -322,6 +432,10 @@ sub route {

	$self->render_later;

	if ( $self->param('efa') ) {
		return $self->route_efa;
	}

	my $service = 'DB';
	if (    $hafas
		and $hafas ne '1'
@@ -468,16 +582,33 @@ sub route {
	)->wait;
}

sub ajax_route {
sub ajax_route_efa {
	my ($self)  = @_;
	my $efa     = $self->param('efa');
	my $trip_id = $self->stash('tripid');
	my $line_no = $self->stash('lineno');
	my $hafas   = $self->param('hafas');

	my ($err) = @_;
	$self->render(
		'_error',
		error => 'not implemented yet',
	);
}

sub ajax_route {
	my ($self) = @_;

	delete $self->stash->{layout};

	$self->render_later;

	if ( $self->param('efa') ) {
		return $self->ajax_route_efa;
	}

	my $trip_id = $self->stash('tripid');
	my $line_no = $self->stash('lineno');
	my $hafas   = $self->param('hafas');

	my $service = 'DB';
	if (    $hafas
		and $hafas ne '1'
+33 −0
Original line number Diff line number Diff line
@@ -29,6 +29,39 @@ sub new {

}

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

	my $stopseq = $opt{stopseq};
	my $service = $opt{service};
	my $promise = Mojo::Promise->new;

	Travel::Status::DE::EFA->new_p(
		service    => $service,
		stopseq    => $stopseq,
		cache      => $self->{realtime_cache},
		promise    => 'Mojo::Promise',
		user_agent => $self->{user_agent}->request_timeout(10)
	)->then(
		sub {
			my ($efa) = @_;
			my $journey = $efa->result;

			$promise->resolve($journey);
			return;
		}
	)->catch(
		sub {
			my ($err) = @_;
			$self->{log}->debug("EFA->new_p($stopseq) error: $err");
			$promise->reject($err);
			return;
		}
	)->wait;

	return $promise;
}

sub get_coverage {
	my ( $self, $service ) = @_;

+4 −4
Original line number Diff line number Diff line
@@ -79,10 +79,10 @@ var marker;
</script>

<div class="container" style="margin-top: 1ex; margin-bottom: 1ex; color: #555;">
Die eingezeichnete Route stammt aus dem HAFAS und ist im Detail oft
fehlerbehaftet.<br/>
Die Zugposition auf der Karte ist eine DBF-eigene Schätzung und kann erheblich
von den tatsächlichen Gegebenheiten abweichen.
Die eingezeichnete Route stammt aus dem angefragten Backend und stimmt nicht
notwendigerweise mit der Realität überein.<br/>
Die Fahrzeugposition auf der Karte ist eine DBF-eigene Schätzung und kann
erheblich von den tatsächlichen Gegebenheiten abweichen.
% if (stash('intersection')) {
<br/>In dieser Ansicht sind Live-Updates der Zug- und Begegnungspositionen noch
nicht implementiert.