diff options
author | Daniel Friesel <derf@finalrewind.org> | 2020-09-20 10:43:16 +0200 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2020-09-20 10:43:16 +0200 |
commit | 93256284a230bda3c8f04e4a9d3ae689a5fe7ff1 (patch) | |
tree | e7cc9cb3ecd765ffdc58d63d505f62bc2bb12a51 | |
parent | c8f5ba2493b6d9bcceab871d41b7ec5692d41058 (diff) |
show direction of travel if available
-rw-r--r-- | lib/DBInfoscreen/Controller/Stationboard.pm | 49 | ||||
-rw-r--r-- | lib/DBInfoscreen/Helper/Wagonorder.pm | 39 | ||||
-rw-r--r-- | templates/_train_details.html.ep | 12 |
3 files changed, 98 insertions, 2 deletions
diff --git a/lib/DBInfoscreen/Controller/Stationboard.pm b/lib/DBInfoscreen/Controller/Stationboard.pm index c79a743..220e630 100644 --- a/lib/DBInfoscreen/Controller/Stationboard.pm +++ b/lib/DBInfoscreen/Controller/Stationboard.pm @@ -458,6 +458,55 @@ sub render_train { )->wait; } + # Same for stationinfo (direction of travel). If it's too late and + # therefore missing, that's okay. + $self->wagonorder->get_stationinfo_p( $result->station_uic )->then( + sub { + my ($station_info) = @_; + my ($platform_number) = ( $result->platform =~ m{(\d+)} ); + if ( not defined $platform_number ) { + return; + } + my $platform_info = $station_info->{$platform_number}; + if ( not $platform_info ) { + return; + } + my $prev_stop = ( $result->route_pre )[-1]; + my $next_stop = ( $result->route_post )[0]; + my $direction; + + if ( $platform_info->{kopfgleis} and $next_stop ) { + $direction = $platform_info->{direction} eq 'r' ? 'l' : 'r'; + } + elsif ( $platform_info->{kopfgleis} ) { + $direction = $platform_info->{direction}; + } + elsif ( $prev_stop + and exists $platform_info->{direction_from}{$prev_stop} ) + { + $direction = $platform_info->{direction_from}{$prev_stop}; + } + elsif ( $next_stop + and exists $platform_info->{direction_from}{$next_stop} ) + { + $direction + = $platform_info->{direction_from}{$next_stop} eq 'r' + ? 'l' + : 'r'; + } + + if ($direction) { + $departure->{direction} = $direction; + } + + return; + }, + sub { + # errors don't matter here + return; + } + )->wait; + $self->hafas->get_route_timestamps_p( train => $result )->then( sub { my ( $route_ts, $route_info, $trainsearch ) = @_; diff --git a/lib/DBInfoscreen/Helper/Wagonorder.pm b/lib/DBInfoscreen/Helper/Wagonorder.pm index 5f0555d..1e9324a 100644 --- a/lib/DBInfoscreen/Helper/Wagonorder.pm +++ b/lib/DBInfoscreen/Helper/Wagonorder.pm @@ -142,4 +142,43 @@ sub get_p { return $promise; } +sub get_stationinfo_p { + my ( $self, $eva ) = @_; + + my $url = "https://lib.finalrewind.org/dbdb/s/${eva}.json"; + + my $cache = $self->{main_cache}; + my $promise = Mojo::Promise->new; + + if ( my $content = $cache->thaw($url) ) { + return $promise->resolve($content); + } + + $self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} ) + ->then( + sub { + my ($tx) = @_; + + if ( my $err = $tx->error ) { + $cache->freeze( $url, {} ); + $promise->reject("HTTP $err->{code} $err->{message}"); + return; + } + + my $json = $tx->result->json; + $cache->freeze( $url, $json ); + $promise->resolve($json); + return; + } + )->catch( + sub { + my ($err) = @_; + $cache->freeze( $url, {} ); + $promise->reject($err); + return; + } + )->wait; + return $promise; +} + 1; diff --git a/templates/_train_details.html.ep b/templates/_train_details.html.ep index 2816af0..a7ad86e 100644 --- a/templates/_train_details.html.ep +++ b/templates/_train_details.html.ep @@ -66,12 +66,20 @@ <span class="minfo">Fahrt fällt aus</span> % } % else { +% my $left = ''; +% my $right = ''; +% if ($departure->{direction} and $departure->{direction} eq 'l') { +% $left = '◀ '; +% } +% elsif ($departure->{direction} and $departure->{direction} eq 'r') { +% $right = ' ▶'; +% } % if ($departure->{scheduled_platform} and $departure->{platform} % and $departure->{scheduled_platform} ne $departure->{platform}) { - <span class="minfo">Gleis <%= $departure->{platform} %></span> + <span class="minfo"><%= $left %>Gleis <%= $departure->{platform} %><%= $right %></span> % } % elsif ($departure->{scheduled_platform} or $departure->{platform}) { - Gleis <%= $departure->{platform} // $departure->{scheduled_platform} %> + <%= $left %>Gleis <%= $departure->{platform} // $departure->{scheduled_platform} %><%= $right %> % } % } </div> |