From 6a671d45390c883d994defdaa544405223b8dc29 Mon Sep 17 00:00:00 2001 From: Birte Kristina Friesel Date: Tue, 29 Aug 2023 20:54:11 +0200 Subject: geostop: always show hafas and iris stops --- cpanfile | 1 + lib/DBInfoscreen/Controller/Static.pm | 15 ---- lib/DBInfoscreen/Controller/Stationboard.pm | 113 +++++++++++++++------------- public/static/js/geostop.js | 11 ++- 4 files changed, 70 insertions(+), 70 deletions(-) diff --git a/cpanfile b/cpanfile index df8800e..e493208 100644 --- a/cpanfile +++ b/cpanfile @@ -6,6 +6,7 @@ requires 'GIS::Distance'; requires 'GIS::Distance::Fast'; requires 'JSON'; requires 'JSON::XS'; +requires 'List::UtilsBy'; requires 'LWP::UserAgent'; requires 'LWP::Protocol::https'; requires 'Mojolicious'; diff --git a/lib/DBInfoscreen/Controller/Static.pm b/lib/DBInfoscreen/Controller/Static.pm index 4560d02..22f2034 100644 --- a/lib/DBInfoscreen/Controller/Static.pm +++ b/lib/DBInfoscreen/Controller/Static.pm @@ -42,23 +42,8 @@ sub redirect { sub geostop { my ($self) = @_; - my ( $api_link, $api_text, $api_icon ); - if ( $self->param('hafas') ) { - $api_link = '/_autostop'; - $api_text = 'Auf Bahnverkehr wechseln'; - $api_icon = 'directions_bus'; - } - else { - $api_link = '/_autostop?hafas=1'; - $api_text = 'Auf Nahverkehr wechseln'; - $api_icon = 'train'; - } - $self->render( 'geostop', - api_link => $api_link, - api_text => $api_text, - api_icon => $api_icon, with_geostop => 1, hide_opts => 1 ); diff --git a/lib/DBInfoscreen/Controller/Stationboard.pm b/lib/DBInfoscreen/Controller/Stationboard.pm index 1a03b10..d22443e 100644 --- a/lib/DBInfoscreen/Controller/Stationboard.pm +++ b/lib/DBInfoscreen/Controller/Stationboard.pm @@ -11,6 +11,7 @@ use DateTime::Format::Strptime; use Encode qw(decode encode); use File::Slurp qw(read_file write_file); use List::Util qw(max uniq); +use List::UtilsBy qw(uniq_by); use List::MoreUtils qw(); use Mojo::JSON qw(decode_json encode_json); use Mojo::Promise; @@ -1781,63 +1782,71 @@ sub stations_by_coordinates { if ( not $lon or not $lat ) { $self->render( json => { error => 'Invalid lon/lat received' } ); + return; } - if ($hafas) { - $self->render_later; - Travel::Status::DE::HAFAS->new_p( - promise => 'Mojo::Promise', - user_agent => $self->ua, - geoSearch => { - lat => $lat, - lon => $lon - } - )->then( - sub { - my ($hafas) = @_; - my @candidates = map { - { - name => $_->name, - eva => $_->eva, - distance => $_->distance_m / 1000, - hafas => 1 - } - } $hafas->results; - $self->render( - json => { - candidates => [@candidates], - } - ); - } - )->catch( - sub { - my ($err) = @_; - $self->render( json => { error => $err } ); - } - )->wait; - } - else { - my @candidates = map { - { - ds100 => $_->[0][0], - name => $_->[0][1], - eva => $_->[0][2], - lon => $_->[0][3], - lat => $_->[0][4], - distance => $_->[1], - hafas => 0, - } - } Travel::Status::DE::IRIS::Stations::get_station_by_location( $lon, - $lat, 10 ); - $self->render( - json => { - candidates => [@candidates], + + $self->render_later; + + my @iris = map { + { + ds100 => $_->[0][0], + name => $_->[0][1], + eva => $_->[0][2], + lon => $_->[0][3], + lat => $_->[0][4], + distance => $_->[1], + hafas => 0, + } + } Travel::Status::DE::IRIS::Stations::get_station_by_location( $lon, + $lat, 10 ); + + @iris = uniq_by { $_->{name} } @iris; + + Travel::Status::DE::HAFAS->new_p( + promise => 'Mojo::Promise', + user_agent => $self->ua, + geoSearch => { + lat => $lat, + lon => $lon + } + )->then( + sub { + my ($hafas) = @_; + my @hafas = map { + { + name => $_->name, + eva => $_->eva, + distance => $_->distance_m / 1000, + hafas => 1 + } + } $hafas->results; + if ( @hafas > 10 ) { + @hafas = @hafas[ 0 .. 9 ]; } - ); - } + my @results = map { $_->[0] } + sort { $a->[1] <=> $b->[1] } + map { [ $_, $_->{distance} ] } ( @iris, @hafas ); + $self->render( + json => { + candidates => [@results], + } + ); + } + )->catch( + sub { + my ($err) = @_; + $self->render( + json => { + candidates => [@iris], + warning => $err, + } + ); + } + )->wait; } sub autocomplete { - my $self = shift; + my ($self) = @_; $self->res->headers->cache_control('max-age=31536000, immutable'); diff --git a/public/static/js/geostop.js b/public/static/js/geostop.js index f91057a..80e8311 100644 --- a/public/static/js/geostop.js +++ b/public/static/js/geostop.js @@ -43,20 +43,25 @@ $(function() { const stationlink = $(document.createElement('a')); stationlink.attr('href', eva + '?hafas=' + hafas); - stationlink.text(name); + stationlink.text(name + ' '); const distancenode = $(document.createElement('div')); distancenode.attr('class', 'distance'); distancenode.text(distance); + const icon = $(document.createElement('i')); + icon.attr('class', 'material-icons'); + icon.text(hafas ? 'directions' : 'train'); + + stationlink.append(icon); stationlink.append(distancenode); $('div.candidatelist').append(stationlink); }); } }; - var processLocation = function(loc) { - $.post('/_geolocation', {lon: loc.coords.longitude, lat: loc.coords.latitude, hafas: window.location.href.match('hafas=1') ? 1 : 0}, processResult).fail(function(jqXHR, textStatus, errorThrown) { + const processLocation = function(loc) { + $.post('/_geolocation', {lon: loc.coords.longitude, lat: loc.coords.latitude}, processResult).fail(function(jqXHR, textStatus, errorThrown) { removeStatus(); showError("Netzwerkfehler: ", textStatus, errorThrown); }); -- cgit v1.2.3