Commit e8b610f3 authored by Daniel Friesel's avatar Daniel Friesel
Browse files

fetch trip ID when checked in

parent 172c8a3d
Loading
Loading
Loading
Loading
+83 −1
Original line number Diff line number Diff line
@@ -1916,6 +1916,67 @@ sub startup {
		}
	);

	$self->helper(
		'get_hafas_tripid_p' => sub {
			my ( $self, $train ) = @_;

			my $promise = Mojo::Promise->new;
			my $cache   = $self->app->cache_iris_main;
			my $eva     = $train->station_uic;

			my $dep_ts = DateTime->now( time_zone => 'Europe/Berlin' );
			my $url
			  = "https://2.db.transport.rest/stations/${eva}/departures?duration=5&when=$dep_ts";

			if ( $train->sched_departure ) {
				$dep_ts = $train->sched_departure->epoch;
				$url
				  = "https://2.db.transport.rest/stations/${eva}/departures?duration=5&when=$dep_ts";
			}
			elsif ( $train->sched_arrival ) {
				$dep_ts = $train->sched_arrival->epoch;
				$url
				  = "https://2.db.transport.rest/stations/${eva}/arrivals?duration=5&when=$dep_ts";
			}

			if ( my $content = $cache->get($url) ) {
				$promise->resolve($content);
				return $promise;
			}

			$self->ua->request_timeout(5)->get_p(
				$url => {
					'User-Agent' => 'travelynx/' . $self->app->config->{version}
				}
			)->then(
				sub {
					my ($tx) = @_;
					my $body = decode( 'utf-8', $tx->res->body );
					my $json = JSON->new->decode($body);

					for my $result ( @{$json} ) {
						if (    $result->{line}
							and $result->{line}{fahrtNr} == $train->train_no )
						{
							my $trip_id = $result->{tripId};
							$cache->set( $url, $trip_id );
							$promise->resolve($trip_id);
							return;
						}
					}
					$promise->reject;
				}
			)->catch(
				sub {
					my ($err) = @_;
					$promise->reject($err);
				}
			)->wait;

			return $promise;
		}
	);

	$self->helper(
		'get_hafas_json_p' => sub {
			my ( $self, $url ) = @_;
@@ -2035,7 +2096,7 @@ sub startup {

			my $journey = $db->select(
				'in_transit_str',
				[ 'arr_eva', 'dep_eva', 'route' ],
				[ 'arr_eva', 'dep_eva', 'route', 'data' ],
				{ user_id => $uid }
			)->expand->hash;

@@ -2043,6 +2104,27 @@ sub startup {
				return;
			}

			if ( not $journey->{data}{trip_id} ) {
				$self->get_hafas_tripid_p($train)->then(
					sub {
						my ($trip_id) = @_;

						my $res = $db->select( 'in_transit', ['data'],
							{ user_id => $uid } );
						my $res_h = $res->expand->hash;
						my $data  = $res_h->{data} // {};

						$data->{trip_id} = $trip_id;

						$db->update(
							'in_transit',
							{ data    => JSON->new->encode($data) },
							{ user_id => $uid }
						);
					}
				)->wait;
			}

			my ($platform) = ( ( $train->platform // 0 ) =~ m{(\d+)} );

			my $route = $journey->{route};