Skip to content

Commit

Permalink
HAFAS departures: add earlier / later links
Browse files Browse the repository at this point in the history
  • Loading branch information
derf committed Mar 31, 2024
1 parent 9f0d72b commit 970e302
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
39 changes: 28 additions & 11 deletions lib/Travelynx/Controller/Traveling.pm
Original file line number Diff line number Diff line change
Expand Up @@ -915,11 +915,12 @@ sub travel_action {
}

sub station {
my ($self) = @_;
my $station = $self->stash('station');
my $train = $self->param('train');
my $trip_id = $self->param('trip_id');
my $uid = $self->current_user->{id};
my ($self) = @_;
my $station = $self->stash('station');
my $train = $self->param('train');
my $trip_id = $self->param('trip_id');
my $timestamp = $self->param('timestamp');
my $uid = $self->current_user->{id};

my @timeline = $self->in_transit->get_timeline(
uid => $uid,
Expand All @@ -934,11 +935,22 @@ sub station {

$self->render_later;

if ( $timestamp and $timestamp =~ m{ ^ \d+ $ }x ) {
$timestamp = DateTime->from_epoch(
epoch => $timestamp,
time_zone => 'Europe/Berlin'
);
}
else {
$timestamp = DateTime->now( time_zone => 'Europe/Berlin' );
}

my $use_hafas = $self->param('hafas');
my $promise;
if ($use_hafas) {
$promise = $self->hafas->get_departures_p(
eva => $station,
timestamp => $timestamp,
lookbehind => 30,
lookahead => 30,
);
Expand All @@ -957,6 +969,10 @@ sub station {
my $api_link;
my @results;

my $now = $self->now->epoch;
my $now_within_range
= abs( $timestamp->epoch - $now ) < 1800 ? 1 : 0;

if ($use_hafas) {

my $iris_eva = List::Util::min grep { $_ >= 1000000 }
Expand All @@ -965,14 +981,9 @@ sub station {
$api_link = '/s/' . $iris_eva;
}

my $now = $self->now->epoch;
@results = map { $_->[0] }
sort { $b->[1] <=> $a->[1] }
map { [ $_, $_->datetime->epoch ] }
grep {
( $_->datetime // $_->sched_datetime )->epoch
< $now + 30 * 60
} $status->results;
map { [ $_, $_->datetime->epoch ] } $status->results;
$self->stations->add_meta(
eva => $status->station->{eva},
meta => $status->station->{evas} // []
Expand Down Expand Up @@ -1049,6 +1060,8 @@ sub station {
$self->render(
'departures',
eva => $status->{station_eva},
datetime => $timestamp,
now_in_range => $now_within_range,
results => \@results,
hafas => $use_hafas,
station => $status->{station_name},
Expand All @@ -1066,6 +1079,8 @@ sub station {
$self->render(
'departures',
eva => $status->{station_eva},
datetime => $timestamp,
now_in_range => $now_within_range,
results => \@results,
hafas => $use_hafas,
station => $status->{station_name},
Expand All @@ -1082,6 +1097,8 @@ sub station {
$self->render(
'departures',
eva => $status->{station_eva},
datetime => $timestamp,
now_in_range => $now_within_range,
results => \@results,
hafas => $use_hafas,
station => $status->{station_name},
Expand Down
4 changes: 3 additions & 1 deletion lib/Travelynx/Helper/HAFAS.pm
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ sub get_json_p {
sub get_departures_p {
my ( $self, %opt ) = @_;

my $when = DateTime->now( time_zone => 'Europe/Berlin' )
my $when
= ( $opt{timestamp}->clone
// DateTime->now( time_zone => 'Europe/Berlin' ) )
->subtract( minutes => $opt{lookbehind} );
return Travel::Status::DE::HAFAS->new_p(
station => $opt{eva},
Expand Down
24 changes: 21 additions & 3 deletions templates/departures.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@
% }
</div>
</div>
% if ($api_link) {
<div class="row">
<div class="col s12">
% if ($api_link) {
% if (param('hafas')) {
<a href="<%= $api_link %>" class="btn-small"><i class="material-icons left" aria-hidden="true">train</i>zum Schienenverkehr</a>
% }
% else {
<a href="<%= $api_link %>" class="btn-small"><i class="material-icons left" aria-hidden="true">directions</i>zum Nahverkehr</a>
% }
% }
<a class="btn-small" href="#now"><i class="material-icons left" aria-hidden="true">vertical_align_center</i>Jetzt</a>
</div>
</div>
% }

% my $have_connections = 0;
% if ($user_status->{checked_in}) {
Expand Down Expand Up @@ -90,6 +89,25 @@
</div>
</div>
% }

<div class="row">
<div class="col s4 center-align">
% if ($hafas) {
<a class="btn-small" href="<%= url_for('sstation', station => stash('station'))->query({hafas => 1, timestamp => $datetime->clone->subtract(hours => 1)->epoch}) %>"><i class="material-icons left" aria-hidden="true">chevron_left</i>früher</a>
% }
</div>
<div class="col s4 center-align">
% if ($now_in_range) {
<a class="btn-small" href="#now"><i class="material-icons left" aria-hidden="true">vertical_align_center</i>Jetzt</a>
% }
</div>
<div class="col s4 center-align">
% if ($hafas) {
<a class="btn-small" href="<%= url_for('sstation', station => stash('station'))->query({hafas => 1, timestamp => $datetime->clone->add(hours => 1)->epoch}) %>">später<i class="material-icons right" aria-hidden="true">chevron_right</i></a>
% }
</div>
</div>

<div class="row">
<div class="col s12">
<p>
Expand Down

0 comments on commit 970e302

Please sign in to comment.