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

Use HAFAS utilization directly

parent daa8cc5a
Loading
Loading
Loading
Loading
+9 −26
Original line number Diff line number Diff line
@@ -568,15 +568,12 @@ sub render_train {
	$self->render_later;

	my $wagonorder_req  = Mojo::Promise->new;
	my $utilization_req = Mojo::Promise->new;
	my $occupancy_req   = Mojo::Promise->new;
	my $stationinfo_req = Mojo::Promise->new;
	my $route_req       = Mojo::Promise->new;

	my @requests = (
		$wagonorder_req,  $utilization_req, $occupancy_req,
		$stationinfo_req, $route_req
	);
	my @requests
	  = ( $wagonorder_req, $occupancy_req, $stationinfo_req, $route_req );

	if ( $departure->{wr_link} ) {
		$self->wagonorder->is_available_p( $result, $departure->{wr_link} )
@@ -595,30 +592,9 @@ sub render_train {
				return;
			}
		)->wait;

		# Looks like utilization data is only available for long-distance trains
		# – and the few regional trains which also have wagon order data (e.g.
		# around Stuttgart). Funky.
		$self->marudor->get_train_utilization( train => $result )->then(
			sub {
				my ( $first, $second ) = @_;
				$departure->{utilization} = [ $first, $second ];
				return;
			},
			sub {
				$departure->{utilization} = undef;
				return;
			}
		)->finally(
			sub {
				$utilization_req->resolve;
				return;
			}
		)->wait;
	}
	else {
		$wagonorder_req->resolve;
		$utilization_req->resolve;
	}

	$self->marudor->get_efa_occupancy(
@@ -702,6 +678,13 @@ sub render_train {

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

			if ( my $load = $route_ts->{$station_name}{load} ) {
				if ( %{$load} ) {
					$departure->{utilization}
					  = [ $load->{FIRST}, $load->{SECOND} ];
				}
			}

			# 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
+1 −0
Original line number Diff line number Diff line
@@ -362,6 +362,7 @@ sub get_route_timestamps_p {
					rt_dep      => $stop->{rt_dep},
					arr_delay   => $stop->{arr_delay},
					dep_delay   => $stop->{dep_delay},
					load        => $stop->{load},
					isCancelled => (
						( $stop->{arr_cancelled} or not $stop->{sched_arr} )
						  and
+0 −54
Original line number Diff line number Diff line
@@ -116,58 +116,4 @@ sub get_efa_occupancy {
	return $promise;
}

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

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

	if ( not $train->sched_departure ) {
		$promise->reject("train has no departure");
		return $promise;
	}

	my $train_no     = $train->train_no;
	my $this_station = $train->station;
	my @route        = $train->route_post;
	my $next_station;
	my $dep = $train->sched_departure->iso8601;

	if ( @route > 1 ) {
		$next_station = $route[1];
	}
	else {
		$next_station = $route[0];
	}

	if ( not $next_station ) {
		$promise->reject("train has no next_station");
		return $promise;
	}

	$this_station
	  = url_escape( encode( 'utf-8', decode( 'iso-8859-15', $this_station ) ) );
	$next_station
	  = url_escape( encode( 'utf-8', decode( 'iso-8859-15', $next_station ) ) );

	$self->get_json_p( $self->{realtime_cache},
"https://bahn.expert/api/hafas/v2/auslastung/${this_station}/${next_station}/${train_no}/${dep}"
	)->then(
		sub {
			my ($utilization_json) = @_;

			$promise->resolve( $utilization_json->{first},
				$utilization_json->{second} );
			return;
		}
	)->catch(
		sub {
			$promise->reject;
			return;
		}
	)->wait;

	return $promise;
}

1;
+0 −1
Original line number Diff line number Diff line
@@ -35,7 +35,6 @@
			v<%= $Travel::Status::DE::DBWagenreihung::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/>
		Sie nutzt zusätzlich die folgenden Open Data-Ressourcen:<br/>