| 1 | package Travel::Status::DE::DBRIS::Journey; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use 5.020; | ||
| 6 | |||
| 7 | use parent 'Class::Accessor'; | ||
| 8 | |||
| 9 | use Travel::Status::DE::DBRIS::Location; | ||
| 10 | use Travel::Status::DE::DBRIS::Operators; | ||
| 11 | |||
| 12 | our $VERSION = '0.40'; | ||
| 13 | |||
| 14 | # ->number is deprecated | ||
| 15 | Travel::Status::DE::DBRIS::Journey->mk_ro_accessors( | ||
| 16 | qw(admin_id day id trip trip_no line_no type number operator is_cancelled)); | ||
| 17 | |||
| 18 | sub new { | ||
| 19 | my ( $obj, %opt ) = @_; | ||
| 20 | |||
| 21 | my $json = $opt{json}; | ||
| 22 | my $strpdate = $opt{strpdate_obj}; | ||
| 23 | my $strptime = $opt{strptime_obj}; | ||
| 24 | |||
| 25 | my $ref = { | ||
| 26 | id => $opt{id}, | ||
| 27 | day => $strpdate->parse_datetime( $json->{reisetag} ), | ||
| 28 | trip => $json->{zugName}, | ||
| 29 | is_cancelled => $json->{cancelled}, | ||
| 30 | raw_route => $json->{halte}, | ||
| 31 | raw_polyline => $json->{polylineGroup}{polylineDescriptions}, | ||
| 32 | strptime_obj => $strptime, | ||
| 33 | }; | ||
| 34 | |||
| 35 | if ( $json->{halte} and @{ $json->{halte} } ) { | ||
| 36 | my %admin_id_ml; | ||
| 37 | my %trip_no_ml; | ||
| 38 | my %type_ml; | ||
| 39 | |||
| 40 | for my $stop ( @{ $json->{halte} } ) { | ||
| 41 | if ( defined $stop->{adminID} ) { | ||
| 42 | $admin_id_ml{ $stop->{adminID} } += 1; | ||
| 43 | } | ||
| 44 | if ( defined $stop->{nummer} ) { | ||
| 45 | $trip_no_ml{ $stop->{nummer} } += 1; | ||
| 46 | } | ||
| 47 | if ( defined $stop->{kategorie} ) { | ||
| 48 | $type_ml{ $stop->{kategorie} } += 1; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | if (%admin_id_ml) { | ||
| 53 | my @admin_id_argmax | ||
| 54 | = reverse sort { $admin_id_ml{$a} <=> $admin_id_ml{$b} } | ||
| 55 | keys %admin_id_ml; | ||
| 56 | $ref->{admin_id} = $admin_id_argmax[0]; | ||
| 57 | if ( | ||
| 58 | my $op | ||
| 59 | = Travel::Status::DE::DBRIS::Operators::get_operator_name( | ||
| 60 | $ref->{admin_id} | ||
| 61 | ) | ||
| 62 | ) | ||
| 63 | { | ||
| 64 | $ref->{operator} = $op; | ||
| 65 | } | ||
| 66 | |||
| 67 | # return most frequent admin ID first | ||
| 68 | $ref->{admin_ids} = \@admin_id_argmax; | ||
| 69 | $ref->{operators} = [ | ||
| 70 | map { | ||
| 71 | Travel::Status::DE::DBRIS::Operators::get_operator_name($_) | ||
| 72 | // $_ | ||
| 73 | } @admin_id_argmax | ||
| 74 | ]; | ||
| 75 | } | ||
| 76 | |||
| 77 | if (%type_ml) { | ||
| 78 | my @type_argmax | ||
| 79 | = reverse sort { $type_ml{$a} <=> $type_ml{$b} } keys %type_ml; | ||
| 80 | $ref->{type} = $type_argmax[0]; | ||
| 81 | $ref->{types} = \@type_argmax; | ||
| 82 | } | ||
| 83 | |||
| 84 | if (%trip_no_ml) { | ||
| 85 | my @trip_no_argmax | ||
| 86 | = reverse sort { $trip_no_ml{$a} <=> $trip_no_ml{$b} } | ||
| 87 | keys %trip_no_ml; | ||
| 88 | $ref->{trip_no} = $trip_no_argmax[0]; | ||
| 89 | $ref->{trip_numbers} = \@trip_no_argmax; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | # Number is either train no (ICE, RE) or line no (S, U, Bus, ...) | ||
| 94 | # with no way of distinguishing between those | ||
| 95 | if ( $ref->{trip} ) { | ||
| 96 | my @trip_parts = split( qr{\s+}, $ref->{trip} ); | ||
| 97 | if ( not defined $ref->{type} ) { | ||
| 98 | $ref->{type} = $trip_parts[0]; | ||
| 99 | } | ||
| 100 | $ref->{number} = $trip_parts[-1]; | ||
| 101 | if ( not defined $ref->{trip_no} ) { | ||
| 102 | $ref->{trip_no} = $ref->{number}; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | # For some trips, the type also contains the trip number like "MEX19161" | ||
| 107 | # If we can detect this, remove the number from the trip type | ||
| 108 | if ( $ref->{trip_no} | ||
| 109 | and $ref->{type} | ||
| 110 | and $ref->{type} =~ m{ (?<actualtype> [^\d]+ ) $ref->{trip_no} $ }x ) | ||
| 111 | { | ||
| 112 | $ref->{type} = $+{actualtype}; | ||
| 113 | } | ||
| 114 | |||
| 115 | # The line number seems to be encoded in the trip ID | ||
| 116 | if ( not defined $ref->{number} | ||
| 117 | and $opt{id} =~ m{ [#] ZE [#] (?<line> [^#]+ ) [#] ZB [#] }x ) | ||
| 118 | { | ||
| 119 | $ref->{number} = $+{line}; | ||
| 120 | } | ||
| 121 | |||
| 122 | if ( defined $ref->{number} | ||
| 123 | and defined $ref->{trip_no} | ||
| 124 | and $ref->{number} ne $ref->{trip_no} ) | ||
| 125 | { | ||
| 126 | $ref->{line_no} = $ref->{number}; | ||
| 127 | } | ||
| 128 | |||
| 129 | bless( $ref, $obj ); | ||
| 130 | |||
| 131 | for my $message ( @{ $json->{himMeldungen} // [] } ) { | ||
| 132 | push( @{ $ref->{messages} }, $message ); | ||
| 133 | } | ||
| 134 | |||
| 135 | for my $message ( @{ $json->{priorisierteMeldungen} // [] } ) { | ||
| 136 | push( @{ $ref->{messages} }, $message ); | ||
| 137 | } | ||
| 138 | |||
| 139 | for my $attr ( @{ $json->{zugattribute} // [] } ) { | ||
| 140 | push( @{ $ref->{attributes} }, $attr ); | ||
| 141 | } | ||
| 142 | |||
| 143 | return $ref; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub polyline { | ||
| 147 | my ($self) = @_; | ||
| 148 | |||
| 149 | if ( not $self->{raw_polyline} ) { | ||
| 150 | return; | ||
| 151 | } | ||
| 152 | |||
| 153 | if ( $self->{polyline} ) { | ||
| 154 | return @{ $self->{polyline} }; | ||
| 155 | } | ||
| 156 | |||
| 157 | my $distance; | ||
| 158 | my $polyline = [ map { { lon => $_->{lng}, lat => $_->{lat} } } | ||
| 159 | @{ $self->{raw_polyline}[0]{coordinates} } ]; | ||
| 160 | |||
| 161 | eval { | ||
| 162 | require GIS::Distance; | ||
| 163 | $distance = GIS::Distance->new; | ||
| 164 | }; | ||
| 165 | |||
| 166 | if ($distance) { | ||
| 167 | my %min_dist; | ||
| 168 | for my $stop ( $self->route ) { | ||
| 169 | for my $polyline_index ( 0 .. $#{$polyline} ) { | ||
| 170 | my $pl = $polyline->[$polyline_index]; | ||
| 171 | my $dist | ||
| 172 | = $distance->distance_metal( $stop->{lat}, $stop->{lon}, | ||
| 173 | $pl->{lat}, $pl->{lon} ); | ||
| 174 | if ( not $min_dist{ $stop->{eva} } | ||
| 175 | or $min_dist{ $stop->{eva} }{dist} > $dist ) | ||
| 176 | { | ||
| 177 | $min_dist{ $stop->{eva} } = { | ||
| 178 | dist => $dist, | ||
| 179 | index => $polyline_index, | ||
| 180 | }; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | } | ||
| 184 | for my $stop ( $self->route ) { | ||
| 185 | if ( $min_dist{ $stop->{eva} } ) { | ||
| 186 | $polyline->[ $min_dist{ $stop->{eva} }{index} ]{stop} | ||
| 187 | = $stop; | ||
| 188 | } | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | $self->{polyline} = $polyline; | ||
| 193 | |||
| 194 | return @{ $self->{polyline} }; | ||
| 195 | } | ||
| 196 | |||
| 197 | sub route { | ||
| 198 | my ($self) = @_; | ||
| 199 | |||
| 200 | if ( $self->{route} ) { | ||
| 201 | return @{ $self->{route} }; | ||
| 202 | } | ||
| 203 | |||
| 204 | @{ $self->{route} } | ||
| 205 | = map { | ||
| 206 | Travel::Status::DE::DBRIS::Location->new( | ||
| 207 | json => $_, | ||
| 208 | operator => | ||
| 209 | scalar Travel::Status::DE::DBRIS::Operators::get_operator_name( | ||
| 210 | $_->{adminID} | ||
| 211 | ), | ||
| 212 | strptime_obj => $self->{strptime_obj} | ||
| 213 | ) | ||
| 214 | } ( @{ $self->{raw_route} // [] }, | ||
| 215 | @{ $self->{raw_cancelled_route} // [] } ); | ||
| 216 | |||
| 217 | return @{ $self->{route} }; | ||
| 218 | } | ||
| 219 | |||
| 220 | sub attributes { | ||
| 221 | my ($self) = @_; | ||
| 222 | |||
| 223 | return @{ $self->{attributes} // [] }; | ||
| 224 | } | ||
| 225 | |||
| 226 | sub messages { | ||
| 227 | my ($self) = @_; | ||
| 228 | |||
| 229 | return @{ $self->{messages} // [] }; | ||
| 230 | } | ||
| 231 | |||
| 232 | sub admin_ids { | ||
| 233 | my ($self) = @_; | ||
| 234 | |||
| 235 | return @{ $self->{admin_ids} // [] }; | ||
| 236 | } | ||
| 237 | |||
| 238 | sub operators { | ||
| 239 | my ($self) = @_; | ||
| 240 | |||
| 241 | return @{ $self->{operators} // [] }; | ||
| 242 | } | ||
| 243 | |||
| 244 | sub types { | ||
| 245 | my ($self) = @_; | ||
| 246 | |||
| 247 | return @{ $self->{types} // [] }; | ||
| 248 | } | ||
| 249 | |||
| 250 | sub trip_numbers { | ||
| 251 | my ($self) = @_; | ||
| 252 | |||
| 253 | return @{ $self->{trip_numbers} // [] }; | ||
| 254 | } | ||
| 255 | |||
| 256 | sub type_at { | ||
| 257 | my ( $self, $loc, $ts ) = @_; | ||
| 258 | for my $stop ( $self->route ) { | ||
| 259 | if ( $stop->name eq $loc or $stop->eva eq $loc ) { | ||
| 260 | if ( not defined $ts | ||
| 261 | or not( $stop->sched_dep // $stop->sched_arr ) | ||
| 262 | or ( $stop->sched_dep // $stop->sched_arr )->epoch == $ts ) | ||
| 263 | { | ||
| 264 | return $stop->type; | ||
| 265 | } | ||
| 266 | } | ||
| 267 | } | ||
| 268 | return; | ||
| 269 | } | ||
| 270 | |||
| 271 | sub trip_no_at { | ||
| 272 | my ( $self, $loc, $ts ) = @_; | ||
| 273 | for my $stop ( $self->route ) { | ||
| 274 | if ( $stop->name eq $loc or $stop->eva eq $loc ) { | ||
| 275 | if ( not defined $ts | ||
| 276 | or not( $stop->sched_dep // $stop->sched_arr ) | ||
| 277 | or ( $stop->sched_dep // $stop->sched_arr )->epoch == $ts ) | ||
| 278 | { | ||
| 279 | return $stop->trip_no; | ||
| 280 | } | ||
| 281 | } | ||
| 282 | } | ||
| 283 | return; | ||
| 284 | } | ||
| 285 | |||
| 286 | sub train { | ||
| 287 | my ($self) = @_; | ||
| 288 | |||
| 289 | warn('$journey->train accessor is deprecated; use ->trip instead'); | ||
| 290 | |||
| 291 | return $self->trip; | ||
| 292 | } | ||
| 293 | |||
| 294 | sub train_no { | ||
| 295 | my ($self) = @_; | ||
| 296 | |||
| 297 | warn('$journey->train_no accessor is deprecated; use ->trip_no instead'); | ||
| 298 | |||
| 299 | return $self->trip_no; | ||
| 300 | } | ||
| 301 | |||
| 302 | sub TO_JSON { | ||
| 303 | my ($self) = @_; | ||
| 304 | |||
| 305 | # transform raw_route into route (lazy accessor) | ||
| 306 | $self->route; | ||
| 307 | |||
| 308 | # transform raw_polyline into polyline (lazy accessor) | ||
| 309 | $self->polyline; | ||
| 310 | |||
| 311 | my $ret = { %{$self} }; | ||
| 312 | |||
| 313 | delete $ret->{strptime_obj}; | ||
| 314 | |||
| 315 | for my $k (qw(day)) { | ||
| 316 | if ( $ret->{$k} ) { | ||
| 317 | $ret->{$k} = $ret->{$k}->epoch; | ||
| 318 | } | ||
| 319 | } | ||
| 320 | |||
| 321 | return $ret; | ||
| 322 | } | ||
| 323 | |||
| 324 | 1; | ||
| 325 | |||
| 326 | __END__ |