summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBirte Kristina Friesel <derf@finalrewind.org>2023-08-29 20:54:11 +0200
committerBirte Kristina Friesel <derf@finalrewind.org>2023-08-29 20:54:54 +0200
commit6a671d45390c883d994defdaa544405223b8dc29 (patch)
tree31d7e142891749e2e3256e1cab6b325fbb360d05
parentfab688d0da2242a2dc9145c82eafc0a9877d3f74 (diff)
geostop: always show hafas and iris stops
-rw-r--r--cpanfile1
-rw-r--r--lib/DBInfoscreen/Controller/Static.pm15
-rw-r--r--lib/DBInfoscreen/Controller/Stationboard.pm113
-rw-r--r--public/static/js/geostop.js11
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);
});