diff options
Diffstat (limited to 'index.pl')
-rw-r--r-- | index.pl | 215 |
1 files changed, 134 insertions, 81 deletions
@@ -11,7 +11,7 @@ use List::MoreUtils qw(); use App::VRR::Fakedisplay; use Travel::Status::DE::HAFAS; -use Travel::Status::DE::ASEAG; +use Travel::Status::DE::URA; use Travel::Status::DE::EFA; no warnings 'uninitialized'; @@ -34,6 +34,9 @@ my @efa_services my @hafas_services = map { $_->{shortname} } Travel::Status::DE::HAFAS::get_services(); +my @ura_services + = map { $_->{shortname} } Travel::Status::DE::URA::get_services(); + sub log_api_access { my $counter = 1; if ( -r $ENV{VRRFAKEDISPLAY_STATS} ) { @@ -43,7 +46,10 @@ sub log_api_access { } sub get_results { - my ( $backend, $city, $stop ) = @_; + my %opt = @_; + my $backend = $opt{backend}; + my $city = $opt{city}; + my $stop = $opt{stop}; my $sub_backend; my $expiry = 200; @@ -55,6 +61,9 @@ sub get_results { if ( $backend and $backend eq 'db' ) { $backend = 'hafas.DB'; } + if ( $backend and $backend eq 'aseag' ) { + $backend = 'ura.ASEAG'; + } if ( $backend =~ s{ [.] (.+) $ }{}x ) { $sub_backend = $1; @@ -71,6 +80,12 @@ sub get_results { errstr => "hafas sub-backend '$sub_backend' not supported" }; } + if ( $backend eq 'ura' and not $sub_backend ~~ \@ura_services ) { + return { + results => [], + errstr => "ura sub-backend '$sub_backend' not supported" + }; + } } if ( $backend eq 'hafas' ) { @@ -81,7 +96,7 @@ sub get_results { } my $cache = Cache::File->new( - cache_root => $ENV{VRRFAKEDISPLAY_CACHE} // '/tmp/vrr-fakedisplay', + cache_root => $ENV{VRRFAKEDISPLAY_CACHE} // '/tmp/vrr-fakedisplay', default_expires => "${expiry} sec", lock_level => Cache::File::LOCK_LOCAL(), ); @@ -98,22 +113,26 @@ sub get_results { my $status; if ( $backend eq 'hafas' ) { $status = Travel::Status::DE::HAFAS->new( - station => ( $city ? "${city} ${stop}" : $stop ), + station => ( $city ? "${city} ${stop}" : $stop ), excluded_mots => [qw[ice ic_ec d regio]], service => $sub_backend, ); } - elsif ( $backend eq 'aseag' ) { - $status = Travel::Status::DE::ASEAG->new( - stop => ( $city ? "${city} ${stop}" : $stop ), - calculate_routes => 1, - ); + elsif ( $backend eq 'ura' ) { + my $service + = first { lc( $_->{shortname} ) eq lc($sub_backend) } + Travel::Status::DE::URA::get_services(); + if ($service) { + $status = Travel::Status::DE::URA->new( + ura_base => $service->{ura_base}, + ura_version => $service->{ura_version}, + stop => ( $city ? "${city} ${stop}" : $stop ), + calculate_routes => 1, + ); + } } else { my $efa_url = 'http://efa.vrr.de/vrr/XSLT_DM_REQUEST'; - if ( not $city ) { - return { errstr => 'City must be specified for this backend' }; - } if ($sub_backend) { my $service = first { lc( $_->{shortname} ) eq lc($sub_backend) } @@ -123,11 +142,12 @@ sub get_results { } } $status = Travel::Status::DE::EFA->new( - efa_url => $efa_url, - place => $city, - name => $stop, - timeout => 3, - full_routes => 0, + efa_url => $efa_url, + place => $city, + name => $stop, + timeout => 3, + full_routes => 0, + proximity_search => $opt{proximity_search} ); } if ( not $status->errstr ) { @@ -173,8 +193,11 @@ sub handle_request { my $data; if ($stop) { - $data = get_results( $self->param('backend') // $default{backend}, - $city, $stop ); + $data = get_results( + backend => $self->param('backend') // $default{backend}, + city => $city, + stop => $stop + ); } if ( not $no_lines or $no_lines < 1 or $no_lines > 40 ) { @@ -254,7 +277,12 @@ sub get_filtered_departures { my ( @grep_line, @grep_platform, @filtered_results ); - my $data = get_results( $opt{backend}, $opt{city}, $opt{stop} ); + my $data = get_results( + backend => $opt{backend}, + city => $opt{city}, + stop => $opt{stop}, + proximity_search => $opt{proximity_search} + ); my $results = $data->{results}; @@ -284,9 +312,9 @@ sub get_filtered_departures { @grep_line and not( List::MoreUtils::any { $line =~ $_ } @grep_line ) ) - or ( @grep_platform and not( $platform ~~ \@grep_platform ) ) + or ( @grep_platform and not( $platform ~~ \@grep_platform ) ) or ( $opt{hide_regional} and $line =~ m{ ^ (RB | RE | IC | EC) }x ) - or ( $opt{offset} and $d->countdown < $opt{offset} ) + or ( $opt{offset} and $d->countdown < $opt{offset} ) ) { next; @@ -304,15 +332,16 @@ sub make_infoboard_lines { my (%opt) = @_; my ( @grep_line, @grep_platform ); - my $no_lines = $opt{no_lines} // $default{no_lines}; - my $max_lines = $opt{max_lines} // 40; - my $offset = $opt{offset} // 0; - my $results = $opt{data}; + my $no_lines = $opt{no_lines} // $default{no_lines}; + my $max_lines = $opt{max_lines} // 40; + my $offset = $opt{offset} // 0; + my $time_format = $opt{time_format} // 'countdown'; + my $results = $opt{data}; my $displayed_lines = 0; my $want_crop = $opt{want_crop}; my @fmt_departures; - my $dt_now = DateTime->now( time_zone => 'Europe/Berlin' ); + my $dt_now = DateTime->now( time_zone => 'Europe/Berlin' ); my $strp_simple = DateTime::Format::Strptime->new( pattern => '%H:%M', time_zone => 'floating', @@ -371,6 +400,9 @@ sub make_infoboard_lines { { next; } + elsif ( $time_format eq 'hhmm' ) { + $etr = $dt->strftime('%H:%M'); + } elsif ( $duration->in_units('minutes') == 0 ) { $etr = 'sofort'; } @@ -407,23 +439,25 @@ sub render_html { my $template = $frontend eq 'html' ? 'display' : 'infoscreen'; my $data = get_filtered_departures( - city => $self->stash('city') // q{}, - stop => $self->stash('stop'), - backend => scalar $self->param('backend'), - filter_line => scalar $self->param('line'), - filter_platform => scalar $self->param('platform'), - hide_regional => ( $template eq 'infoscreen' ? 0 : 1 ), - offset => scalar $self->param('offset'), + city => $self->stash('city') // q{}, + stop => $self->stash('stop'), + backend => scalar $self->param('backend'), + filter_line => scalar $self->param('line'), + filter_platform => scalar $self->param('platform'), + hide_regional => ( $template eq 'infoscreen' ? 0 : 1 ), + offset => scalar $self->param('offset'), + proximity_search => scalar $self->param('proximity_search'), ); my @departures = make_infoboard_lines( - city => $self->stash('city') // q{}, - stop => $self->stash('stop'), - backend => scalar $self->param('backend'), - no_lines => scalar $self->param('no_lines'), - offset => scalar $self->param('offset'), - want_crop => scalar $self->param('want_crop'), - data => $data->{filtered_results}, + city => $self->stash('city') // q{}, + stop => $self->stash('stop'), + backend => scalar $self->param('backend'), + no_lines => scalar $self->param('no_lines'), + offset => scalar $self->param('offset'), + time_format => scalar $self->param('time_format'), + want_crop => scalar $self->param('want_crop'), + data => $data->{filtered_results}, ); for my $d (@departures) { @@ -451,26 +485,30 @@ sub render_html { sub render_json { my $self = shift; + my $time_format = $self->param('time_format') // 'countdown'; + my $data = get_filtered_departures( - city => $self->stash('city') // q{}, - stop => $self->stash('stop'), - backend => scalar $self->param('backend'), - filter_line => scalar $self->param('line'), - filter_platform => scalar $self->param('platform'), - hide_regional => 0, - offset => scalar $self->param('offset'), + city => $self->stash('city') // q{}, + stop => $self->stash('stop'), + backend => scalar $self->param('backend'), + filter_line => scalar $self->param('line'), + filter_platform => scalar $self->param('platform'), + hide_regional => 0, + offset => scalar $self->param('offset'), + proximity_search => scalar $self->param('proximity_search'), ); my $raw_departures = $data->{filtered_results}; my $errstr = $data->{errstr}; my @departures = make_infoboard_lines( - no_lines => scalar $self->param('no_lines'), - offset => scalar $self->param('offset'), - want_crop => scalar $self->param('want_crop'), - data => $raw_departures, + no_lines => scalar $self->param('no_lines'), + offset => scalar $self->param('offset'), + time_format => $time_format, + want_crop => scalar $self->param('want_crop'), + data => $raw_departures, ); for my $d (@departures) { - if ( $d->[2] and $d->[2] ne 'sofort' ) { + if ( $d->[2] and $d->[2] ne 'sofort' and $time_format eq 'countdown' ) { $d->[2] .= ' min'; } } @@ -491,29 +529,32 @@ sub render_json { sub render_image { my $self = shift; - my $color = $self->param('color') || '255,208,0'; - my $scale = $self->param('scale'); + my $color = $self->param('color') || '255,208,0'; + my $scale = $self->param('scale'); + my $time_format = $self->param('time_format') // 'countdown'; my $data = get_filtered_departures( - city => $self->stash('city') // q{}, - stop => $self->stash('stop'), - backend => scalar $self->param('backend'), - filter_line => scalar $self->param('line'), - filter_platform => scalar $self->param('platform'), - hide_regional => 0, - offset => scalar $self->param('offset'), + city => $self->stash('city') // q{}, + stop => $self->stash('stop'), + backend => scalar $self->param('backend'), + filter_line => scalar $self->param('line'), + filter_platform => scalar $self->param('platform'), + hide_regional => 0, + offset => scalar $self->param('offset'), + proximity_search => scalar $self->param('proximity_search'), ); my $raw_departures = $data->{filtered_results}; my $errstr = $data->{errstr}; my @departures = make_infoboard_lines( - city => $self->stash('city') // q{}, - stop => $self->stash('stop'), - backend => scalar $self->param('backend'), - no_lines => scalar $self->param('no_lines'), - offset => scalar $self->param('offset'), - want_crop => scalar $self->param('want_crop'), - data => $raw_departures + city => $self->stash('city') // q{}, + stop => $self->stash('stop'), + backend => scalar $self->param('backend'), + no_lines => scalar $self->param('no_lines'), + offset => scalar $self->param('offset'), + time_format => $time_format, + want_crop => scalar $self->param('want_crop'), + data => $raw_departures ); if ( $scale > 30 ) { @@ -548,7 +589,10 @@ sub render_image { $png->draw_at( 0, $line ); $png->draw_at( 25, $destination ); - if ( length($etr) > 2 ) { + if ( $time_format eq 'hhmm' ) { + $png->draw_at( 153, $etr ); + } + elsif ( length($etr) > 2 ) { $png->draw_at( 145, $etr ); } elsif ( length($etr) > 1 ) { @@ -558,7 +602,7 @@ sub render_image { $png->draw_at( 154, $etr ); } - if ( $etr and $etr ne 'sofort' ) { + if ( $etr and $etr ne 'sofort' and $time_format eq 'countdown' ) { $png->draw_at( 161, 'min' ); } @@ -587,6 +631,12 @@ helper 'hafas_service_list' => sub { return @hafas_services; }; +helper 'ura_service_list' => sub { + my $self = shift; + + return @ura_services; +}; + helper 'handle_no_results' => sub { }; @@ -612,8 +662,11 @@ get '/_redirect' => sub { if ( $params->param('frontend') and $params->param('frontend') eq 'infoscreen' ) { - my $data = get_results( $self->param('backend') // $default{backend}, - $city, $stop ); + my $data = get_results( + backend => $self->param('backend') // $default{backend}, + city => $city, + stop => $stop + ); if ( not $data->{errstr} ) { $suffix = '.html'; } @@ -652,18 +705,18 @@ get '/_imprint' => sub { }; get '/' => \&handle_request; -get '/:city/(*stop).html' => \&render_html; -get '/:city/(*stop).json' => \&render_json; -get '/:city/(*stop).png' => \&render_image; +get '/:city/<*stop>.html' => \&render_html; +get '/:city/<*stop>.json' => \&render_json; +get '/:city/<*stop>.png' => \&render_image; get '/:city/*stop' => \&handle_request; -get '/(:stop).html' => \&render_html; -get '/(:stop).json' => \&render_json; -get '/(:stop).png' => \&render_image; -get '/:stop' => \&handle_request; +get '/<*stop>.html' => \&render_html; +get '/<*stop>.json' => \&render_json; +get '/<*stop>.png' => \&render_image; +get '/*stop' => \&handle_request; app->config( hypnotoad => { - listen => [ $ENV{VRRFAKEDISPLAY_LISTEN} // 'http://127.0.0.1:8091' ], + listen => [ $ENV{VRRFAKEDISPLAY_LISTEN} // 'http://*:8091' ], pid_file => '/tmp/vrr-fakedisplay.pid', workers => $ENV{VRRFAKEDISPLAY_WORKERS} // 2, }, |