diff options
Diffstat (limited to 'index.pl')
-rw-r--r-- | index.pl | 166 |
1 files changed, 97 insertions, 69 deletions
@@ -46,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; @@ -130,9 +133,6 @@ sub get_results { } 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) } @@ -142,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 ) { @@ -192,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 ) { @@ -273,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}; @@ -303,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; @@ -323,10 +332,11 @@ 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; @@ -390,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'; } @@ -426,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) { @@ -470,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'; } } @@ -510,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 ) { @@ -567,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 ) { @@ -577,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' ); } @@ -637,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'; } @@ -681,14 +709,14 @@ 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, }, |