diff options
-rwxr-xr-x | bin/aseag-m | 83 | ||||
-rw-r--r-- | lib/Travel/Status/DE/URA.pm | 65 | ||||
-rw-r--r-- | lib/Travel/Status/DE/URA/Result.pm | 10 | ||||
-rw-r--r-- | t/22-results-via.t | 79 |
4 files changed, 130 insertions, 107 deletions
diff --git a/bin/aseag-m b/bin/aseag-m index 21d56d4..928e78d 100755 --- a/bin/aseag-m +++ b/bin/aseag-m @@ -21,7 +21,7 @@ my $relative_times = 0; my $strftime_format = '%H:%M:%S'; my $strfrel_format = '%M min'; my ( %edata, @edata_pre ); -my $full_route; +my $calculate_routes = 0; my $via; GetOptions( @@ -47,21 +47,12 @@ if ( @ARGV != 1 ) { for my $efield (@edata_pre) { given ($efield) { - when ('a') { $edata{route_after} = 1 } - when ('b') { $edata{route_before} = 1 } - when ('f') { $edata{route_full} = 1 } + when ('a') { $edata{route_after} = 1; $calculate_routes = 1 } + when ('b') { $edata{route_before} = 1; $calculate_routes = 1 } + when ('f') { $edata{route_full} = 1; $calculate_routes = 1 } default { $edata{$efield} = 1 } } } -if ( $edata{route_full} ) { - $full_route = 1; -} -elsif ( $edata{route_after} ) { - $full_route = 'after'; -} -elsif ( $edata{route_before} ) { - $full_route = 'before'; -} my ($stop_name) = @ARGV; @@ -104,7 +95,7 @@ sub display_result { printf( join( q{ }, @format ) . "\n", @{$line}[ 0 .. 2 ] ); - if ($full_route) { + if ( @{ $line->[3] } ) { for my $route ( @{ $line->[3] } ) { printf( join( q{ }, @format ) . "\n", @{$route} ); } @@ -136,6 +127,29 @@ sub get_exact_stop_name { } } +sub show_route { + my ( $dt_now, $dt_format, @routes ) = @_; + my @res; + + if ($relative_times) { + @res = map { + [ + $dt_format->format_duration( + $_->[0]->subtract_datetime($dt_now) + ), + q{}, + $_->[1] + ] + } @routes; + } + else { + @res = map { [ $_->[0]->strftime($strftime_format), q{}, $_->[1] ] } + @routes; + } + + return @res; +} + sub show_results { my @output; @@ -145,10 +159,10 @@ sub show_results { for my $d ( $status->results( - full_routes => $full_route, - hide_past => $hide_past, - stop => $stop_name, - via => $via, + calculate_routes => $calculate_routes, + hide_past => $hide_past, + stop => $stop_name, + via => $via, ) ) { @@ -156,7 +170,17 @@ sub show_results { if ( ( @grep_lines and not( $d->line ~~ \@grep_lines ) ) ) { next; } - my @line; + my ( @line, @route ); + + if ( $edata{route_full} ) { + @route = ( $d->route_pre, $d->route_post ); + } + elsif ( $edata{route_after} ) { + @route = $d->route_post; + } + elsif ( $edata{route_before} ) { + @route = $d->route_pre; + } if ($relative_times) { @line = ( @@ -165,29 +189,14 @@ sub show_results { ), $d->line, $d->destination, - [ - map { - [ - $dt_format->format_duration( - $_->[0]->subtract_datetime($dt_now) - ), - q{}, - $_->[1] - ] - } $d->route_timetable - ], + [ show_route( $dt_now, $dt_format, @route ) ], ); } else { @line = ( $d->datetime->strftime($strftime_format), - $d->line, - $d->destination, - [ - map { - [ $_->[0]->strftime($strftime_format), q{}, $_->[1] ] - } $d->route_timetable - ], + $d->line, $d->destination, + [ show_route( $dt_now, $dt_format, @route ) ], ); } diff --git a/lib/Travel/Status/DE/URA.pm b/lib/Travel/Status/DE/URA.pm index ab02936..00b2a40 100644 --- a/lib/Travel/Status/DE/URA.pm +++ b/lib/Travel/Status/DE/URA.pm @@ -33,7 +33,7 @@ sub new { // DateTime->now( time_zone => 'Europe/Berlin' ), ura_base => $opt{ura_base}, ura_version => $opt{ura_version}, - full_routes => $opt{full_routes} // 0, + full_routes => $opt{calculate_routes} // 0, hide_past => $opt{hide_past} // 1, stop => $opt{stop}, via => $opt{via}, @@ -122,16 +122,16 @@ sub results { my ( $self, %opt ) = @_; my @results; - my $full_routes = $opt{full_routes} // $self->{full_routes} // 0; - my $hide_past = $opt{hide_past} // $self->{hide_past} // 1; - my $stop = $opt{stop} // $self->{stop}; - my $via = $opt{via} // $self->{via}; + my $full_routes = $opt{calculate_routes} // $self->{full_routes} // 0; + my $hide_past = $opt{hide_past} // $self->{hide_past} // 1; + my $stop = $opt{stop} // $self->{stop}; + my $via = $opt{via} // $self->{via}; my $dt_now = $self->{datetime}; my $ts_now = $dt_now->epoch; if ($via) { - $full_routes ||= 'after'; + $full_routes = 1; } for my $dep ( @{ $self->{raw_list} } ) { @@ -140,7 +140,7 @@ sub results { $u1, $stopname, $stopid, $lineid, $linename, $u2, $dest, $vehicleid, $tripid, $timestamp ) = @{$dep}; - my @route; + my ( @route_pre, @route_post ); if ( $stop and not( $stopname eq $stop ) ) { next; @@ -164,31 +164,39 @@ sub results { my $ts_dep = $dt_dep->epoch; if ($full_routes) { - @route = map { [ $_->[9] / 1000, $_->[1] ] } + my @route = map { [ $_->[9] / 1000, $_->[1] ] } grep { $_->[8] == $tripid } @{ $self->{raw_list} }; - if ( $full_routes eq 'before' ) { - @route = grep { $_->[0] < $ts_dep } @route; - } - elsif ( $full_routes eq 'after' ) { - @route = grep { $_->[0] > $ts_dep } @route; - } + @route_pre = grep { $_->[0] < $ts_dep } @route; + @route_post = grep { $_->[0] > $ts_dep } @route; if ( $via - and none { $_->[1] eq $via } @route ) + and none { $_->[1] eq $via } @route_post ) { next; } if ($hide_past) { - @route = grep { $_->[0] >= $ts_now } @route; + @route_pre = grep { $_->[0] >= $ts_now } @route_pre; } - @route = map { $_->[0] } + @route_pre = map { $_->[0] } + sort { $a->[1] <=> $b->[1] } + map { [ $_, $_->[0] ] } @route_pre; + @route_post = map { $_->[0] } sort { $a->[1] <=> $b->[1] } - map { [ $_, $_->[0] ] } @route; + map { [ $_, $_->[0] ] } @route_post; - @route = map { + @route_pre = map { + [ + DateTime->from_epoch( + epoch => $_->[0], + time_zone => 'Europe/Berlin' + ), + decode( 'UTF-8', $_->[1] ) + ] + } @route_pre; + @route_post = map { [ DateTime->from_epoch( epoch => $_->[0], @@ -196,20 +204,21 @@ sub results { ), decode( 'UTF-8', $_->[1] ) ] - } @route; + } @route_post; } push( @results, Travel::Status::DE::URA::Result->new( - datetime => $dt_dep, - dt_now => $dt_now, - line => $linename, - line_id => $lineid, - destination => decode( 'UTF-8', $dest ), - route_timetable => [@route], - stop => $stopname, - stop_id => $stopid, + datetime => $dt_dep, + dt_now => $dt_now, + line => $linename, + line_id => $lineid, + destination => decode( 'UTF-8', $dest ), + route_pre => [@route_pre], + route_post => [@route_post], + stop => $stopname, + stop_id => $stopid, ) ); } diff --git a/lib/Travel/Status/DE/URA/Result.pm b/lib/Travel/Status/DE/URA/Result.pm index c59d9e1..fe6b75e 100644 --- a/lib/Travel/Status/DE/URA/Result.pm +++ b/lib/Travel/Status/DE/URA/Result.pm @@ -106,10 +106,16 @@ sub route_interesting { return @via_show; } -sub route_timetable { +sub route_pre { my ($self) = @_; - return @{ $self->{route_timetable} }; + return @{ $self->{route_pre} }; +} + +sub route_post { + my ($self) = @_; + + return @{ $self->{route_post} }; } sub TO_JSON { diff --git a/t/22-results-via.t b/t/22-results-via.t index 1960228..6366527 100644 --- a/t/22-results-via.t +++ b/t/22-results-via.t @@ -5,7 +5,7 @@ use 5.010; use utf8; use List::Util qw(first); -use Test::More tests => 23; +use Test::More tests => 16; BEGIN { use_ok('Travel::Status::DE::URA'); @@ -59,7 +59,7 @@ is( '"Aachen Bushof" via_after "Brand" does not contain other dests' ); -# via filter in ->results, explicit route_after +# via filter in ->results, explicit route calculation $s = Travel::Status::DE::URA->new( ura_base => 'file:t/in', @@ -69,7 +69,7 @@ $s = Travel::Status::DE::URA->new( ); @results = $s->results( via => 'Finkensief', - full_routes => 'after' + calculate_routes => 1, ); is( @results, 5, '"Aachen Bushof" via_after Finkensief' ); @@ -101,40 +101,39 @@ is( # via filter in ->results, explicit route_before -$s = Travel::Status::DE::URA->new( - ura_base => 'file:t/in', - ura_version => 1, - hide_past => 0, - stop => 'Aachen Bushof', -); -@results = $s->results( - via => 'Finkensief', - full_routes => 'before' -); - -is( @results, 5, '"Aachen Bushof" via_before Finkensief' ); -ok( ( first { $_->line == 25 } @results ), - '"Aachen Bushof" via_after "Brand" contains line 25' ); -ok( - ( first { $_->destination eq 'Vaals Heuvel' } @results ), - '"Aachen Bushof" via_after "Brand" contains dest Vaals Heuvel' -); -ok( ( first { $_->line == 1 } @results ), - '"Aachen Bushof" via_after "Brand" contains line 1' ); -ok( - ( first { $_->destination eq 'Lintert Friedhof' } @results ), - '"Aachen Bushof" via_after "Brand" contains dest Lintert Friedhof' -); -is( ( first { $_->line != 1 and $_->line != 25 } @results ), - undef, '"Aachen Bushof" via_after "Brand" does not contain anything else' ); -is( - ( - first { - $_->destination ne 'Vaals Heuvel' - and $_->destination ne 'Lintert Friedhof'; - } - @results - ), - undef, - '"Aachen Bushof" via_after "Brand" does not contain other dests' -); +#$s = Travel::Status::DE::URA->new( +# ura_base => 'file:t/in', +# ura_version => 1, +# hide_past => 0, +# stop => 'Aachen Bushof', +#); +#@results = $s->results( +# via => 'Finkensief', +#); +# +#is( @results, 5, '"Aachen Bushof" via_before Finkensief' ); +#ok( ( first { $_->line == 25 } @results ), +# '"Aachen Bushof" via_after "Brand" contains line 25' ); +#ok( +# ( first { $_->destination eq 'Vaals Heuvel' } @results ), +# '"Aachen Bushof" via_after "Brand" contains dest Vaals Heuvel' +#); +#ok( ( first { $_->line == 1 } @results ), +# '"Aachen Bushof" via_after "Brand" contains line 1' ); +#ok( +# ( first { $_->destination eq 'Lintert Friedhof' } @results ), +# '"Aachen Bushof" via_after "Brand" contains dest Lintert Friedhof' +#); +#is( ( first { $_->line != 1 and $_->line != 25 } @results ), +# undef, '"Aachen Bushof" via_after "Brand" does not contain anything else' ); +#is( +# ( +# first { +# $_->destination ne 'Vaals Heuvel' +# and $_->destination ne 'Lintert Friedhof'; +# } +# @results +# ), +# undef, +# '"Aachen Bushof" via_after "Brand" does not contain other dests' +#); |