Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HAFAS backend: handle passing the same stop more than once #122

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions lib/Travelynx.pm
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ sub startup {
my ( $self, %opt ) = @_;

my $station = $opt{station};
my $departure = $opt{ts};
my $train_id = $opt{train_id};
my $uid = $opt{uid} // $self->current_user->{id};
my $db = $opt{db} // $self->pg->db;
Expand All @@ -539,15 +540,13 @@ sub startup {
$self->hafas->get_journey_p( trip_id => $train_id )->then(
sub {
my ($journey) = @_;
my $found;
for my $stop ( $journey->route ) {
if ( $stop->loc->name eq $station
or $stop->loc->eva == $station )
{
$found = $stop;
last;
}
}

my $found = List::Util::first {
($_->loc->name eq $station
or $_->loc->eva == $station)
and Travelynx::Helper::HAFAS::lenient_compare_dts($departure, $_->sched_dep)
} $journey->route;

if ( not $found ) {
$promise->reject(
"Did not find journey $train_id at $station");
Expand Down Expand Up @@ -965,6 +964,7 @@ sub startup {
my ( $self, %opt ) = @_;

my $station = $opt{station};
my $arrival = $opt{ts} // 0;
my $force = $opt{force};
my $uid = $opt{uid} // $self->current_user->{id};
my $db = $opt{db} // $self->pg->db;
Expand All @@ -986,7 +986,8 @@ sub startup {
my $found;
my $has_arrived;
for my $stop ( @{ $journey->{route_after} } ) {
if ( $station eq $stop->[0] or $station eq $stop->[1] ) {
if ( ($station eq $stop->[0] or $station eq $stop->[1])
and Travelynx::Helper::HAFAS::lenient_compare_dts($arrival, $stop->[2]{sched_arr})) {
$found = 1;
$self->in_transit->set_arrival_eva(
uid => $uid,
Expand Down Expand Up @@ -1911,7 +1912,8 @@ sub startup {
train_id => $traewelling->{trip_id},
uid => $uid,
in_transaction => 1,
db => $db
db => $db,
ts => $traewelling->{dep_dt}->epoch
)->then(
sub {
$self->log->debug("... handled origin");
Expand All @@ -1920,7 +1922,8 @@ sub startup {
train_id => $traewelling->{trip_id},
uid => $uid,
in_transaction => 1,
db => $db
db => $db,
ts => $traewelling->{arr_dt}->epoch
);
}
)->then(
Expand Down
7 changes: 4 additions & 3 deletions lib/Travelynx/Command/work.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ sub run {
my $found_dep;
my $found_arr;
for my $stop ( $journey->route ) {
if ( $stop->loc->eva == $dep ) {
if ( $stop->loc->eva == $dep && Travelynx::Helper::HAFAS::lenient_compare_dts($stop->sched_dep, $entry->{sched_dep_ts})) {
$found_dep = $stop;
}
if ( $arr and $stop->loc->eva == $arr ) {
if ( $arr and $stop->loc->eva == $arr && Travelynx::Helper::HAFAS::lenient_compare_dts($stop->sched_arr, $entry->{sched_arr_ts})) {
$found_arr = $stop;
last;
}
Expand Down Expand Up @@ -103,7 +103,8 @@ sub run {
force => 2,
dep_eva => $dep,
arr_eva => $arr,
uid => $uid
uid => $uid,
ts => $entry->{sched_arr_ts}
)->wait;
}
next;
Expand Down
20 changes: 13 additions & 7 deletions lib/Travelynx/Controller/Traveling.pm
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ sub get_connecting_trains_p {
and $stop->arr )
{
$iris_train->[2] = $stop->arr;
$iris_train->[4] = $stop->arr;
if ( $iris_train->[0]->departure_delay
and not $stop->arr_delay )
{
Expand Down Expand Up @@ -354,7 +355,7 @@ sub get_connecting_trains_p {
if ( $departure->epoch >= $exclude_before ) {
$via_count{ $dest->{name} }++;
push( @hafas_trains,
[ $hafas_train, $dest, $arrival ] );
[ $hafas_train, $dest, $arrival, $stop->arr ] );
}
}
}
Expand Down Expand Up @@ -674,7 +675,7 @@ sub travel_action {
and $status->{arr_eva}
and $status->{arrival_countdown} <= 0 )
{
$promise = $self->checkout_p( station => $status->{arr_eva} );
$promise = $self->checkout_p( station => $status->{arr_eva}, ts => $params->{ts});
}
else {
$promise = Mojo::Promise->resolve;
Expand All @@ -685,7 +686,8 @@ sub travel_action {
sub {
return $self->checkin_p(
station => $params->{station},
train_id => $params->{train}
train_id => $params->{train},
ts => $params->{ts}
);
}
)->then(
Expand All @@ -705,7 +707,8 @@ sub travel_action {
# them when selecting the destination manually.
return $self->checkout_p(
station => $destination,
force => 0
force => 0,
ts => $params->{dest_ts}
);
}
)->then(
Expand Down Expand Up @@ -744,7 +747,8 @@ sub travel_action {
my $status = $self->get_user_status;
$self->checkout_p(
station => $params->{station},
force => $params->{force}
force => $params->{force},
ts => $params->{ts}
)->then(
sub {
my ( $still_checked_in, $error ) = @_;
Expand Down Expand Up @@ -819,7 +823,8 @@ sub travel_action {
$self->render_later;
$self->checkin_p(
station => $params->{station},
train_id => $params->{train}
train_id => $params->{train},
ts => $params->{ts}
)->then(
sub {
$self->render(
Expand All @@ -845,7 +850,8 @@ sub travel_action {
$self->render_later;
$self->checkout_p(
station => $params->{station},
force => 1
force => 1,
ts => $params->{ts}
)->then(
sub {
my ( undef, $error ) = @_;
Expand Down
26 changes: 26 additions & 0 deletions lib/Travelynx/Helper/HAFAS.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@ sub _epoch {
return $dt ? $dt->epoch : 0;
}

# for making sure we have the right stop selected if it appears multiple times
# on the route we need to compare the arrival/departure times.
# depending on context they might be undef, stored as DateTime or just a plain
# number. this helper plasters over these numerous edge cases and does a
# fuzzy match on the values provided, also returning true if any values are
# undef or 0.
sub lenient_compare_dts {
my ( $a, $b ) = @_;

if (not defined($a) or not defined($b)) {
return 1;
}

if (ref($a) eq 'DateTime') {
$a = $a->epoch;
}
if (ref($b) eq 'DateTime') {
$b = $b->epoch;
}
if ($a == 0 or $b == 0) {
return 1;
} else {
return $a == $b;
}
}

sub new {
my ( $class, %opt ) = @_;

Expand Down
5 changes: 4 additions & 1 deletion lib/Travelynx/Model/InTransit.pm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use 5.020;
use DateTime;
use JSON;

use Travelynx::Helper::HAFAS qw(lenient_compare_dts);

my %visibility_itoa = (
100 => 'public',
80 => 'travelynx',
Expand Down Expand Up @@ -207,7 +209,8 @@ sub postprocess {
push( @route_after, $station );
}
if ( $ret->{dep_name}
and $station->[0] eq $ret->{dep_name} )
and $station->[0] eq $ret->{dep_name}
and Travelynx::Helper::HAFAS::lenient_compare_dts($station->[2]{sched_dep}, $ret->{sched_dep_ts}))
{
$is_after = 1;
if ( @{$station} > 1 and not $dep_info ) {
Expand Down
16 changes: 14 additions & 2 deletions lib/Travelynx/Model/Journeys.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use utf8;
use DateTime;
use JSON;

use Travelynx::Helper::HAFAS qw(lenient_compare_dts);

my %visibility_itoa = (
100 => 'public',
80 => 'travelynx',
Expand Down Expand Up @@ -1066,9 +1068,11 @@ sub get_travel_distance {
my $from = $journey->{from_name};
my $from_eva = $journey->{from_eva};
my $from_latlon = $journey->{from_latlon};
my $departure = $journey->{sched_dep_ts};
my $to = $journey->{to_name};
my $to_eva = $journey->{to_eva};
my $to_latlon = $journey->{to_latlon};
my $arrival = $journey->{sched_arr_ts};
my $route_ref = $journey->{route};
my $polyline_ref = $journey->{polyline};

Expand All @@ -1088,8 +1092,16 @@ sub get_travel_distance {
my $skipped = 0;
my $geo = GIS::Distance->new();
my @stations = map { $_->[0] } @{$route_ref};
my @route = after_incl { $_ eq $from } @stations;
@route = before_incl { $_ eq $to } @route;
my @route = map { $_->[0] }
before_incl {
$_->[0] eq $to and
Travelynx::Helper::HAFAS::lenient_compare_dts($arrival, $_->[2]{sched_arr})
}
after_incl {
$_->[0] eq $from and
Travelynx::Helper::HAFAS::lenient_compare_dts($departure, $_->[2]{sched_dep})
}
@{$route_ref};

if ( @route < 2 ) {

Expand Down
3 changes: 3 additions & 0 deletions public/static/js/travelynx-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ function tvly_reg_handlers() {
train: link.data('train'),
dest: link.data('dest'),
ts: link.data('ts'),
dest_ts: link.data('dest-ts')
};
tvly_run(link, req);
});
Expand All @@ -195,6 +196,7 @@ function tvly_reg_handlers() {
action: 'checkout',
station: link.data('station'),
force: link.data('force'),
ts: link.data('ts'),
};
tvly_run(link, req, function() {
if (!link.data('force')) {
Expand Down Expand Up @@ -235,6 +237,7 @@ function tvly_reg_handlers() {
action: 'cancelled_to',
station: link.data('station'),
force: true,
ts: link.data('ts'),
};
tvly_run(link, req);
});
Expand Down
2 changes: 1 addition & 1 deletion public/static/js/travelynx-actions.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading