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

use T-S-DE-HAFAS for train details

parent 3f4fc57b
Loading
Loading
Loading
Loading
+94 −135
Original line number Diff line number Diff line
@@ -698,22 +698,20 @@ sub render_train {

	$self->hafas->get_route_timestamps_p( train => $result )->then(
		sub {
			my ( $route_ts, $route_info, $trainsearch ) = @_;
			my ( $route_ts, $journey ) = @_;

			$departure->{trip_id} = $trainsearch->{trip_id};
			$departure->{trip_id} = $journey->id;

			# If a train number changes on the way, IRIS routes are incomplete,
			# whereas HAFAS data has all stops -> merge HAFAS stops into IRIS
			# stops. This is a rare case, one point where it can be observed is
			# the TGV service at Frankfurt/Karlsruhe/Mannheim.
			if ( $route_info
				and my @hafas_stations = @{ $route_info->{stations} // [] } )
			{
			my @hafas_stations = $journey->route;
			if ( my @iris_stations = @{ $departure->{route_pre_diff} } ) {
				my @missing_pre;
				for my $station (@hafas_stations) {
					if (
							List::MoreUtils::any { $_->{name} eq $station }
						List::MoreUtils::any { $_->{name} eq $station->{name} }
						@iris_stations
					  )
					{
@@ -726,7 +724,7 @@ sub render_train {
					push(
						@missing_pre,
						{
								name  => $station,
							name  => $station->{name},
							hafas => 1
						}
					);
@@ -736,7 +734,7 @@ sub render_train {
				my @missing_post;
				for my $station ( reverse @hafas_stations ) {
					if (
							List::MoreUtils::any { $_->{name} eq $station }
						List::MoreUtils::any { $_->{name} eq $station->{name} }
						@iris_stations
					  )
					{
@@ -749,13 +747,13 @@ sub render_train {
					unshift(
						@missing_post,
						{
								name  => $station,
							name  => $station->{name},
							hafas => 1
						}
					);
				}
			}
			}

			if ($route_ts) {
				if ( $route_ts->{ $result->station }{rt_bogus} ) {

@@ -770,54 +768,31 @@ sub render_train {
					{
						$elem->{$key} = $route_ts->{ $elem->{name} }{$key};
					}
					if ( $elem->{rt_bogus} ) {
						$departure->{partially_missing_realtime} = 1;
					}
				}
			}
			if ( $route_info and @{ $route_info->{messages} // [] } ) {
				my $him = $route_info->{messages};

			my @him_messages;
				$departure->{messages}{him} = $him;
				for my $message ( @{$him} ) {
					if ( $message->{display} ) {
						push(
							@him_messages,
							[
								$message->{header}, { text => $message->{lead} }
							]
						);
						if ( $message->{lead} =~ m{zuginfo.nrw/?\?msg=(\d+)} ) {
							push(
								@{ $departure->{links} },
								[
									"Großstörung",
									"https://zuginfo.nrw/?msg=$1"
								]
			for my $message ( $journey->messages ) {
				if ( not $message->code ) {
					push( @him_messages,
						[ $message->short // q{}, { text => $message->text } ]
					);
				}
			}
				}
				for my $message ( @{ $departure->{moreinfo} // [] } ) {
					my $m = $message->[1];
					@him_messages
					  = grep { $_->[0] !~ m{Information\. $m\.$} }
					  @him_messages;
				}
			for my $m (@him_messages) {
				if ( $m->[0] =~ s{: Information.}{:} ) {
					$m->[1]{icon} = 'info_outline';
				}
					elsif ( $m->[0] =~ s{: (?:Großs|S)törung.}{: } ) {
				elsif ( $m->[0] =~ s{: Störung.}{: } ) {
					$m->[1]{icon} = 'warning';
				}
				elsif ( $m->[0] =~ s{: Bauarbeiten.}{: } ) {
					$m->[1]{icon} = 'build';
				}
				$m->[0] =~ s{(?!<)->}{ → };
			}
			unshift( @{ $departure->{moreinfo} }, @him_messages );
		}
		}
	)->catch(
		sub {
			# nop
@@ -1029,62 +1004,46 @@ sub train_details {
	my $linetype = 'bahn';

	$self->hafas->get_route_timestamps_p(
		train_req => "${train_type} $train_no" )->then(
		train_type => $train_type,
		train_no   => $train_no
	)->then(
		sub {
			my ( $route_ts, $route_info, $trainsearch ) = @_;
			my ( $route_ts, $journey ) = @_;

			$res->{trip_id} = $trainsearch->{trip_id};
			$res->{trip_id} = $journey->id;

			if ( not defined $trainsearch->{trainClass} ) {
			if ( not defined $journey->class ) {
				$linetype = 'ext';
			}
			elsif ( $trainsearch->{trainClass} <= 2 ) {
			elsif ( $journey->class <= 2 ) {
				$linetype = 'fern';
			}
			elsif ( $trainsearch->{trainClass} <= 8 ) {
			elsif ( $journey->class <= 8 ) {
				$linetype = 'bahn';
			}
			elsif ( $trainsearch->{trainClass} <= 16 ) {
			elsif ( $journey->class <= 16 ) {
				$linetype = 'sbahn';
			}

			$res->{origin}      = $route_info->{stations}[0];
			$res->{destination} = $route_info->{stations}[-1];
			$res->{origin}      = $journey->route_start;
			$res->{destination} = $journey->route_end;

			$res->{route_post_diff}
			  = [ map { { name => $_ } } @{ $route_info->{stations} } ];

			if ($route_ts) {
			  = [ map { { name => $_->{name} } } $journey->route ];
			for my $elem ( @{ $res->{route_post_diff} } ) {
					for my $key ( keys %{ $route_ts->{ $elem->{name} } // {} } )
					{
				for my $key ( keys %{ $route_ts->{ $elem->{name} } // {} } ) {
					$elem->{$key} = $route_ts->{ $elem->{name} }{$key};
				}
			}
			}

			if ( $route_info and @{ $route_info->{messages} // [] } ) {
				my $him = $route_info->{messages};
			my @him_messages;
				for my $message ( @{$him} ) {
					if ( $message->{display} ) {
						push(
							@him_messages,
							[
								$message->{header}, { text => $message->{lead} }
							]
						);
						if ( $message->{lead} =~ m{zuginfo.nrw/?\?msg=(\d+)} ) {
							push(
								@{ $res->{links} },
								[
									"Großstörung",
									"https://zuginfo.nrw/?msg=$1"
								]
			for my $message ( $journey->messages ) {
				if ( not $message->code ) {
					push( @him_messages,
						[ $message->short // q{}, { text => $message->text } ]
					);
				}
			}
				}
			for my $m (@him_messages) {
				if ( $m->[0] =~ s{: Information.}{:} ) {
					$m->[1]{icon} = 'info_outline';
@@ -1096,6 +1055,7 @@ sub train_details {
					$m->[1]{icon} = 'build';
				}
			}
			if (@him_messages) {
				$res->{moreinfo} = [@him_messages];
			}

@@ -1106,8 +1066,6 @@ sub train_details {
				icetype   => $self->app->ice_type_map->{ $res->{train_no} },
				details   => {},    #$departure->{composition} // {},
				dt_now    => DateTime->now( time_zone => 'Europe/Berlin' ),

				#station_name => "FIXME",#$station_name,
			);
		}
	)->catch(
@@ -1116,7 +1074,8 @@ sub train_details {
			if ($e) {
				$self->render(
					'exception',
					exception => $e,
					message   => $e,
					exception => undef,
					snapshot  => {}
				);
			}
+44 −85
Original line number Diff line number Diff line
@@ -326,89 +326,48 @@ sub get_route_timestamps_p {
		$opt{train_origin} = $opt{train}->origin;
	}
	else {
		$opt{train_req} = $opt{train_type} . ' ' . $opt{train_no};
		$opt{date_yy}   = $now->strftime('%d.%m.%y');
		$opt{date_yyyy} = $now->strftime('%d.%m.%Y');
	}

	my $base = 'https://reiseauskunft.bahn.de/bin/traininfo.exe/dn';
	my ( $trainsearch_result, $trainlink );

	$self->trainsearch_p(%opt)->then(
		sub {
			($trainsearch_result) = @_;
			$trainlink = $trainsearch_result->{trainLink};
			return Mojo::Promise->all(
				$self->get_json_p(
					$self->{realtime_cache},
					"${base}/${trainlink}?rt=1&date=$opt{date_yy}&L=vs_json"
				),
				$self->get_xml_p(
					$self->{realtime_cache},
					"${base}/${trainlink}?rt=1&date=$opt{date_yy}&L=vs_java3"
				)
			my ($trainsearch_result) = @_;
			my $trip_id = $trainsearch_result->{trip_id};
			return Travel::Status::DE::HAFAS->new_p(
				journey => {
					id => $trip_id,

					# name => $opt{train_no},
				},
				cache      => $self->{realtime_cache},
				promise    => 'Mojo::Promise',
				user_agent => $self->{user_agent}->request_timeout(10)
			);
		}
	)->then(
		sub {
			my ( $traininfo, $traindelay ) = @_;
			$traininfo  = $traininfo->[0];
			$traindelay = $traindelay->[0];
			if ( not $traininfo or $traininfo->{error} ) {
				$promise->reject;
				return;
			}
			$trainsearch_result->{trainClass}
			  = $traininfo->{suggestions}[0]{trainClass};
			my ($hafas) = @_;
			my $journey = $hafas->result;
			my $ret     = {};

			my $strp = DateTime::Format::Strptime->new(
				pattern   => '%d.%m.%y %H:%M',
				time_zone => 'Europe/Berlin',
			);

			my $station_is_past = 1;

			for
			  my $station ( @{ $traininfo->{suggestions}[0]{locations} // [] } )
			{
				my $name = $station->{name};
				my $arr  = $station->{arrDate} . ' ' . $station->{arrTime};
				my $dep  = $station->{depDate} . ' ' . $station->{depTime};
			for my $stop ( $journey->route ) {
				my $name = $stop->{name};
				$ret->{$name} = {
					sched_arr => scalar $strp->parse_datetime($arr),
					sched_dep => scalar $strp->parse_datetime($dep),
					sched_arr   => $stop->{sched_arr},
					sched_dep   => $stop->{sched_dep},
					rt_arr      => $stop->{rt_arr},
					rt_dep      => $stop->{rt_dep},
					arr_delay   => $stop->{arr_delay},
					dep_delay   => $stop->{dep_delay},
					isCancelled => (
						( $stop->{arr_cancelled} or not $stop->{sched_arr} )
						  and
						  ( $stop->{dep_cancelled} or not $stop->{sched_dep} )
					),
				};
				if ( exists $traindelay->{station}{$name} ) {
					my $delay = $traindelay->{station}{$name};
					if (    $ret->{$name}{sched_arr}
						and $delay->{adelay}
						and $delay->{adelay} =~ m{^\d+$} )
					{
						$ret->{$name}{rt_arr} = $ret->{$name}{sched_arr}
						  ->clone->add( minutes => $delay->{adelay} );
					}
					if (    $ret->{$name}{sched_dep}
						and $delay->{ddelay}
						and $delay->{ddelay} =~ m{^\d+$} )
					{
						$ret->{$name}{rt_dep} = $ret->{$name}{sched_dep}
						  ->clone->add( minutes => $delay->{ddelay} );
						if (
							(
								defined $delay->{adelay}
								and $delay->{adelay} eq q{}
							)
							or ( defined $delay->{ddelay}
								and $delay->{ddelay} eq q{} )
						  )
						{
							$ret->{$name}{rt_bogus} = 1;
						}
						if ( $delay->{ddelay} and $delay->{ddelay} eq 'cancel' )
						{
							$ret->{$name}{isCancelled} = 1;
						}
					}
				if (
					    $station_is_past
					and not $ret->{$name}{isCancelled}
@@ -423,14 +382,14 @@ sub get_route_timestamps_p {
				}
				$ret->{$name}{isPast} = $station_is_past;
			}
			}

			$promise->resolve( $ret, $traindelay // {}, $trainsearch_result );
			$promise->resolve( $ret, $journey );
			return;
		}
	)->catch(
		sub {
			$promise->reject;
			my ($err) = @_;
			$promise->reject($err);
			return;
		}
	)->wait;
@@ -453,7 +412,7 @@ sub get_polyline_p {
		with_polyline => 1,
		cache         => $self->{realtime_cache},
		promise       => 'Mojo::Promise',
		user_agent    => $self->{user_agent}->request_timeout(5)
		user_agent    => $self->{user_agent}->request_timeout(10)
	)->then(
		sub {
			my ($hafas) = @_;
+3 −3
Original line number Diff line number Diff line
@@ -165,7 +165,7 @@
      <ul class="messages">
%     for my $pair (@{$departure->{moreinfo}}) {
        <li>
%       if ($pair->[0]->isa('DateTime')) {
%       if (ref($pair->[0]) eq 'DateTime') {
%         if ($pair->[0]->day != $dt_now->day) {
%           $pair->[0]->set_locale('de_DE');
%=          $pair->[0]->strftime('%a %H:%M')
@@ -214,7 +214,7 @@
%         else {
            generic-stop
%         }
%         if ($stop->{rt_dep}) {
%         if ($stop->{rt_dep} and $stop->{dep_delay}) {
            "><%= $stop->{sched_dep}->strftime('%H:%M') %> (heute <%= $stop->{rt_dep}->strftime('%H:%M') %>) <%= $stop->{name} %></a>
%         }
%         else {
@@ -259,7 +259,7 @@
%         else {
            generic-stop
%         }
%         if ($stop->{rt_arr}) {
%         if ($stop->{rt_arr} and $stop->{arr_delay}) {
            "><%= $stop->{sched_arr}->strftime('%H:%M') %> (heute <%= $stop->{rt_arr}->strftime('%H:%M') %>) <%= $stop->{name} %></a>
%         }
%         else {
+5 −6
Original line number Diff line number Diff line
@@ -25,17 +25,16 @@
		zu:<br/>
		• Abfahrtstafel: DB IRIS via <a href="https://finalrewind.org/projects/Travel-Status-DE-IRIS/">Travel::Status::DE::IRIS</a>
		v<%= $Travel::Status::DE::IRIS::VERSION %><br/>
		• Meldungen und Ankunft/Abfahrt an Unterwegshalten: DB HAFAS via <a href="https://git.finalrewind.org/db-fakedisplay/tree/lib/DBInfoscreen/Helper/HAFAS.pm">Helper/HAFAS.pm</a><br/>
		• Zugdetails: <a href="https://finalrewind.org/projects/Travel-Status-DE-DeutscheBahn/">Travel::Status::DE::HAFAS</a>
		% if ($Travel::Status::DE::HAFAS::VERSION) {
			v<%= $Travel::Status::DE::HAFAS::VERSION %>
		% }
		<br/>
		• Wagenreihung: <a href="https://finalrewind.org/projects/Travel-Status-DE-DBWagenreihung/">Travel::Status::DE::DBWagenreihung</a>
		% if ($Travel::Status::DE::DBWagenreihung::VERSION) {
			v<%= $Travel::Status::DE::DBWagenreihung::VERSION %>
		% }
		<br/>
		• Karte: <a href="https://finalrewind.org/projects/Travel-Status-DE-DeutscheBahn/">Travel::Status::DE::HAFAS</a>
		% if ($Travel::Status::DE::HAFAS::VERSION) {
			v<%= $Travel::Status::DE::HAFAS::VERSION %>
		% }
		<br/>
		• Zugauslastung Fernverkehr: DB HAFAS via <a href="https://docs.bahn.expert/">bahn.expert</a><br/>
		• Zugauslastung Regionalverkehr: VRR EFA via <a href="https://github.com/derf/eva-to-efa-gw">eva-to-efa-gw</a><br/>
		<br/>
+5 −0
Original line number Diff line number Diff line
@@ -4,9 +4,14 @@ Beim Bearbeiten der Anfrage ist ein Fehler aufgetreten.<br/>

<pre>
----------[Debug start]----------
% if ($exception) {
%= $exception->message
Stash:
%= dumper $snapshot
% }
% else {
%= stash('message')
% }
----------[Debug end]----------
</pre>
</div>
+1 −1

File changed.

Contains only whitespace changes.

Loading