From 34edf620bce916d71c8413041dea447a83acb76c Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 9 Oct 2022 08:35:16 +0200 Subject: move journey parser to Result object --- lib/Travel/Status/DE/HAFAS/Result.pm | 108 ++++++++++++++++++++++++++++++++++- 1 file changed, 106 insertions(+), 2 deletions(-) (limited to 'lib/Travel/Status/DE/HAFAS') diff --git a/lib/Travel/Status/DE/HAFAS/Result.pm b/lib/Travel/Status/DE/HAFAS/Result.pm index b217473..f1e376b 100644 --- a/lib/Travel/Status/DE/HAFAS/Result.pm +++ b/lib/Travel/Status/DE/HAFAS/Result.pm @@ -16,9 +16,113 @@ Travel::Status::DE::HAFAS::Result->mk_ro_accessors( ); sub new { - my ( $obj, %conf ) = @_; + my ( $obj, %opt ) = @_; + + my @locL = @{ $opt{common}{locL} // [] }; + my @prodL = @{ $opt{common}{prodL} // [] }; + my @opL = @{ $opt{common}{opL} // [] }; + my @icoL = @{ $opt{common}{icoL} // [] }; + my @remL = @{ $opt{common}{remL} // [] }; + my @himL = @{ $opt{common}{himL} // [] }; + + my $hafas = $opt{hafas}; + my $journey = $opt{journey}; + + my $date = $journey->{date}; + my $time_s + = $journey->{stbStop}{ $hafas->{arrivals} ? 'aTimeS' : 'dTimeS' }; + my $time_r + = $journey->{stbStop}{ $hafas->{arrivals} ? 'aTimeR' : 'dTimeR' }; + my $datetime_s + = $hafas->{strptime_obj}->parse_datetime("${date}T${time_s}"); + my $datetime_r + = $time_r + ? $hafas->{strptime_obj}->parse_datetime("${date}T${time_r}") + : undef; + my $delay + = $datetime_r + ? ( $datetime_r->epoch - $datetime_s->epoch ) / 60 + : undef; + + my $destination = $journey->{dirTxt}; + my $is_cancelled = $journey->{isCncl}; + my $jid = $journey->{jid}; + my $platform = $journey->{stbStop}{dPlatfS}; + my $new_platform = $journey->{stbStop}{dPlatfR}; + + my $product = $prodL[ $journey->{prodX} ]; + my $train = $product->{prodCtx}{name}; + my $train_type = $product->{prodCtx}{catOutS}; + my $line_no = $product->{prodCtx}{line}; + + my $operator; + if ( defined $product->{oprX} ) { + if ( my $opref = $opL[ $product->{oprX} ] ) { + $operator = $opref->{name}; + } + } + + my @messages; + for my $msg ( @{ $journey->{msgL} // [] } ) { + if ( $msg->{type} eq 'REM' and defined $msg->{remX} ) { + push( @messages, $hafas->add_message( $remL[ $msg->{remX} ] ) ); + } + elsif ( $msg->{type} eq 'HIM' and defined $msg->{himX} ) { + push( @messages, $hafas->add_message( $himL[ $msg->{himX} ], 1 ) ); + } + else { + say "Unknown message type $msg->{type}"; + } + } + + my @stops; + for my $stop ( @{ $journey->{stopL} // [] } ) { + my $loc = $locL[ $stop->{locX} ]; + my $arr = $stop->{aTimeS}; + my $arr_dt; + if ($arr) { + if ( length($arr) == 8 ) { + + # arrival time includes a day offset + my $offset_date = $hafas->{now}->clone; + $offset_date->add( days => substr( $arr, 0, 2, q{} ) ); + $offset_date = $offset_date->strftime('%Y%m%d'); + $arr_dt = $hafas->{strptime_obj} + ->parse_datetime("${offset_date}T${arr}"); + } + else { + $arr_dt + = $hafas->{strptime_obj}->parse_datetime("${date}T${arr}"); + } + } + push( + @stops, + { + name => $loc->{name}, + eva => $loc->{extId} + 0, + arrival => $arr_dt, + } + ); + } + + shift @stops; + + my $ref = { + sched_datetime => $datetime_s, + rt_datetime => $datetime_r, + datetime => $datetime_r // $datetime_s, + datetime_now => $hafas->{now}, + delay => $delay, + is_cancelled => $is_cancelled, + train => $train, + operator => $operator, + route_end => $destination, + platform => $platform, + new_platform => $new_platform, + messages => \@messages, + route => \@stops, + }; - my $ref = \%conf; bless( $ref, $obj ); if ( $ref->{delay} ) { -- cgit v1.2.3