From 98c8f19f1996ec3ef7b6bdf2f84a7f04e1982248 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Wed, 3 Jun 2015 08:51:33 +0200 Subject: implement experimental full-route feature --- bin/efa-m | 41 +++++++++++++++++++++++++++++++-- lib/Travel/Status/DE/EFA.pm | 46 ++++++++++++++++++++++++++++++++++++++ lib/Travel/Status/DE/EFA/Result.pm | 12 ++++++++++ 3 files changed, 97 insertions(+), 2 deletions(-) diff --git a/bin/efa-m b/bin/efa-m index bda2185..3956bdb 100755 --- a/bin/efa-m +++ b/bin/efa-m @@ -2,6 +2,7 @@ use strict; use warnings; use 5.010; +use utf8; no if $] >= 5.018, warnings => "experimental::smartmatch"; @@ -80,6 +81,31 @@ sub show_version { exit 0; } +sub format_route { + my (@route) = @_; + + my $output = q{}; + + for my $stop (@route) { + if ( not $stop ) { + say "BUG"; + next; + } + if ( $stop->{arr_time} eq $stop->{dep_time} ) { + $output .= sprintf( " %5s %40s %s\n", + $stop->{dep_time}, $stop->{stop}, $stop->{platform}, ); + } + else { + $output .= sprintf( + "%5s → %5s %40s %s\n", + $stop->{arr_time}, $stop->{dep_time}, + $stop->{stop}, $stop->{platform}, + ); + } + } + return $output; +} + sub display_result { my (@lines) = @_; @@ -108,6 +134,10 @@ sub display_result { join( q{ }, ( map { "%-${_}s" } @line_length ) ) . "\n", @{$line}[ 0 .. 3 ] ); + + if ( $line->[5] and $full_routes ) { + say $line->[5]; + } } return; @@ -136,6 +166,7 @@ sub show_results { for my $d ( $status->results ) { + my @output_line; my $platform = $d->platform; my $dtime = ( $relative_times ? sprintf( '%2d min', $d->countdown ) : $d->time ); @@ -166,8 +197,14 @@ sub show_results { $dtime .= ' (+' . $d->delay . ')'; } - push( @output, - [ $dtime, $platform, $d->line, $d->destination, $d->info ] ); + @output_line + = ( $dtime, $platform, $d->line, $d->destination, $d->info ); + + if ($full_routes) { + $output_line[5] = format_route( $d->route_post ); + } + + push( @output, \@output_line ); } display_result(@output); diff --git a/lib/Travel/Status/DE/EFA.pm b/lib/Travel/Status/DE/EFA.pm index 76b87f2..38a4fa8 100644 --- a/lib/Travel/Status/DE/EFA.pm +++ b/lib/Travel/Status/DE/EFA.pm @@ -110,6 +110,7 @@ sub new { if ( $opt{full_routes} ) { $self->{post}->{depType} = 'stopEvents'; $self->{post}->{includeCompleteStopSeq} = 1; + $self->{want_full_routes} = 1; } bless( $self, $class ); @@ -292,6 +293,35 @@ sub lines { return @lines; } +sub parse_route { + my ( $self, @nodes ) = @_; + my $xp_routepoint_date + = XML::LibXML::XPathExpression->new('./itdDateTime/itdDate'); + my $xp_routepoint_time + = XML::LibXML::XPathExpression->new('./itdDateTime/itdTime'); + + my @ret; + + for my $e (@nodes) { + my @dates = $e->findnodes($xp_routepoint_date); + my @times = $e->findnodes($xp_routepoint_time); + + push( + @ret, + { + arr_date => sprintf_date( $dates[0] ), + arr_time => sprintf_time( $times[0] ), + dep_date => sprintf_date( $dates[-1] ), + dep_time => sprintf_time( $times[-1] ), + stop => decode( 'UTF-8', $e->getAttribute('name') ), + platform => $e->getAttribute('platformName'), + } + ); + } + + return @ret; +} + sub results { my ($self) = @_; my @results; @@ -309,6 +339,10 @@ sub results { my $xp_line = XML::LibXML::XPathExpression->new('./itdServingLine'); my $xp_info = XML::LibXML::XPathExpression->new('./itdServingLine/itdNoTrain'); + my $xp_prev_route + = XML::LibXML::XPathExpression->new('./itdPrevStopSeq/itdPoint'); + my $xp_next_route + = XML::LibXML::XPathExpression->new('./itdOnwardStopSeq/itdPoint'); if ( $self->{results} ) { return @{ $self->{results} }; @@ -349,6 +383,16 @@ sub results { my $platform_is_db = 0; + my @prev_route; + my @next_route; + + if ( $self->{want_full_routes} ) { + @prev_route + = $self->parse_route( @{ [ $e->findnodes($xp_prev_route) ] } ); + @next_route + = $self->parse_route( @{ [ $e->findnodes($xp_next_route) ] } ); + } + my @line_obj = grep { $_->{identifier} eq $e_line->getAttribute('stateless') } @{ $self->{lines} }; @@ -395,6 +439,8 @@ sub results { sched_date => $date, sched_time => $time, type => $type, + prev_route => \@prev_route, + next_route => \@next_route, ) ); } diff --git a/lib/Travel/Status/DE/EFA/Result.pm b/lib/Travel/Status/DE/EFA/Result.pm index 95e35cd..f729034 100644 --- a/lib/Travel/Status/DE/EFA/Result.pm +++ b/lib/Travel/Status/DE/EFA/Result.pm @@ -29,6 +29,18 @@ sub new { return bless( $ref, $obj ); } +sub route_pre { + my ($self) = @_; + + return @{ $self->{prev_route} }; +} + +sub route_post { + my ($self) = @_; + + return @{ $self->{next_route} }; +} + sub TO_JSON { my ($self) = @_; -- cgit v1.2.3