| 1 | package Travel::Status::DE::DBRIS::JourneyAtStop; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use 5.020; | ||
| 6 | |||
| 7 | use parent 'Class::Accessor'; | ||
| 8 | |||
| 9 | our $VERSION = '0.40'; | ||
| 10 | |||
| 11 | Travel::Status::DE::DBRIS::JourneyAtStop->mk_ro_accessors( | ||
| 12 | qw(type dep sched_dep rt_dep delay is_cancelled line stop_eva id platform rt_platform destination via_last | ||
| 13 | train_short train_mid train_long train maybe_train_no maybe_line_no | ||
| 14 | ) | ||
| 15 | ); | ||
| 16 | |||
| 17 | sub new { | ||
| 18 | my ( $obj, %opt ) = @_; | ||
| 19 | |||
| 20 | my $json = $opt{json}; | ||
| 21 | my $strptime = $opt{strptime_obj}; | ||
| 22 | |||
| 23 | my $ref = { | ||
| 24 | type => $json->{verkehrmittel}{kurzText}, | ||
| 25 | line => $json->{verkehrmittel}{mittelText}, | ||
| 26 | train => $json->{verkehrmittel}{name}, | ||
| 27 | train_short => $json->{verkehrmittel}{kurzText}, | ||
| 28 | train_mid => $json->{verkehrmittel}{mittelText}, | ||
| 29 | train_long => $json->{verkehrmittel}{langText}, | ||
| 30 | id => $json->{journeyId}, | ||
| 31 | stop_eva => $json->{bahnhofsId}, | ||
| 32 | destination => $json->{terminus}, | ||
| 33 | platform => $json->{gleis}, | ||
| 34 | rt_platform => $json->{ezGleis}, | ||
| 35 | via => $json->{ueber}, | ||
| 36 | via_last => ( $json->{ueber} // [] )->[-1], | ||
| 37 | }; | ||
| 38 | |||
| 39 | if ( $ref->{via} and @{ $ref->{via} } ) { | ||
| 40 | shift( @{ $ref->{via} } ); | ||
| 41 | } | ||
| 42 | |||
| 43 | if ( defined $ref->{train} ) { | ||
| 44 | $ref->{maybe_train_no} = $ref->{train} =~ s{^.* ++}{}r; | ||
| 45 | } | ||
| 46 | if ( defined $ref->{train_mid} ) { | ||
| 47 | $ref->{maybe_line_no} = $ref->{train_mid} =~ s{^.* ++}{}r; | ||
| 48 | } | ||
| 49 | |||
| 50 | bless( $ref, $obj ); | ||
| 51 | |||
| 52 | if ( $json->{zeit} ) { | ||
| 53 | eval { $ref->{sched_dep} = $strptime->parse_datetime( $json->{zeit} ); }; | ||
| 54 | if ($@) { | ||
| 55 | warn("Cannot parse sched_dep $json->{zeit}: $@"); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | if ( $json->{ezZeit} ) { | ||
| 59 | eval { $ref->{rt_dep} = $strptime->parse_datetime( $json->{ezZeit} ); }; | ||
| 60 | if ($@) { | ||
| 61 | warn("Cannot parse rt_dep $json->{zeit}: $@"); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | $ref->{dep} = $ref->{rt_dep} // $ref->{sched_dep}; | ||
| 65 | |||
| 66 | if ( $ref->{sched_dep} and $ref->{rt_dep} ) { | ||
| 67 | $ref->{delay} = $ref->{rt_dep}->subtract_datetime( $ref->{sched_dep} ) | ||
| 68 | ->in_units('minutes'); | ||
| 69 | } | ||
| 70 | |||
| 71 | for my $message ( @{ $json->{meldungen} // [] } ) { | ||
| 72 | if ( $message->{type} and $message->{type} eq 'HALT_AUSFALL' ) { | ||
| 73 | $ref->{is_cancelled} = 1; | ||
| 74 | } | ||
| 75 | push( @{ $ref->{messages} }, $message ); | ||
| 76 | } | ||
| 77 | |||
| 78 | return $ref; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub messages { | ||
| 82 | my ($self) = @_; | ||
| 83 | |||
| 84 | return @{ $self->{messages} // [] }; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub via { | ||
| 88 | my ($self) = @_; | ||
| 89 | |||
| 90 | return @{ $self->{via} // [] }; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub TO_JSON { | ||
| 94 | my ($self) = @_; | ||
| 95 | |||
| 96 | my $ret = { %{$self} }; | ||
| 97 | |||
| 98 | for my $k (qw(sched_dep rt_dep dep sched_arr rt_arr arr)) { | ||
| 99 | if ( $ret->{$k} ) { | ||
| 100 | $ret->{$k} = $ret->{$k}->epoch; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | return $ret; | ||
| 105 | } | ||
| 106 | |||
| 107 | 1; |