| 1 | package Travel::Status::MOTIS::Trip; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use 5.020; | ||
| 6 | |||
| 7 | use parent 'Class::Accessor'; | ||
| 8 | |||
| 9 | use DateTime::Format::ISO8601; | ||
| 10 | |||
| 11 | use Travel::Status::MOTIS::Stop; | ||
| 12 | use Travel::Status::MOTIS::Polyline qw(decode_polyline); | ||
| 13 | |||
| 14 | our $VERSION = '0.07'; | ||
| 15 | |||
| 16 | Travel::Status::MOTIS::Trip->mk_ro_accessors( | ||
| 17 | qw( | ||
| 18 | id | ||
| 19 | mode | ||
| 20 | agency | ||
| 21 | display_name | ||
| 22 | route_color | ||
| 23 | route_text_color | ||
| 24 | trip_short_name | ||
| 25 | headsign | ||
| 26 | |||
| 27 | is_realtime | ||
| 28 | is_cancelled | ||
| 29 | |||
| 30 | arrival | ||
| 31 | scheduled_arrival | ||
| 32 | realtime_arrival | ||
| 33 | |||
| 34 | departure | ||
| 35 | scheduled_departure | ||
| 36 | realtime_departure | ||
| 37 | ) | ||
| 38 | ); | ||
| 39 | |||
| 40 | sub new { | ||
| 41 | my ( $obj, %opt ) = @_; | ||
| 42 | |||
| 43 | my $json = $opt{json}{legs}[0]; | ||
| 44 | my $time_zone = $opt{time_zone}; | ||
| 45 | |||
| 46 | my $ref = { | ||
| 47 | id => $json->{tripId}, | ||
| 48 | mode => $json->{mode}, | ||
| 49 | agency => $json->{agencyName}, | ||
| 50 | display_name => $json->{displayName}, | ||
| 51 | trip_short_name => $json->{tripShortName}, | ||
| 52 | route_color => $json->{routeColor}, | ||
| 53 | route_text_color => $json->{routeTextColor}, | ||
| 54 | headsign => $json->{headsign}, | ||
| 55 | |||
| 56 | is_cancelled => $json->{cancelled}, | ||
| 57 | is_realtime => $json->{realTime}, | ||
| 58 | |||
| 59 | raw_stopovers => | ||
| 60 | [ $json->{from}, @{ $json->{intermediateStops} }, $json->{to} ], | ||
| 61 | raw_polyline => $json->{legGeometry}, | ||
| 62 | |||
| 63 | time_zone => $time_zone, | ||
| 64 | }; | ||
| 65 | |||
| 66 | # some trips encode the trip number both as tripShortName and as | ||
| 67 | # "line (trip number number)" in displayName | ||
| 68 | if ( $ref->{display_name} and $ref->{trip_short_name} ) { | ||
| 69 | if ( $ref->{display_name} eq $ref->{trip_short_name} ) { | ||
| 70 | $ref->{trip_short_name} = undef; | ||
| 71 | } | ||
| 72 | else { | ||
| 73 | my $trip_no = $ref->{trip_short_name} =~ s{ ^ 0 + }{}rx; | ||
| 74 | $ref->{display_name} =~ s{ [(] \Q$trip_no\E [)] }{}x; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | $ref->{scheduled_departure} = DateTime::Format::ISO8601->parse_datetime( | ||
| 79 | $json->{scheduledStartTime} ); | ||
| 80 | $ref->{scheduled_departure}->set_time_zone($time_zone); | ||
| 81 | |||
| 82 | if ( $json->{realTime} ) { | ||
| 83 | $ref->{realtime_departure} | ||
| 84 | = DateTime::Format::ISO8601->parse_datetime( $json->{startTime} ); | ||
| 85 | $ref->{realtime_departure}->set_time_zone($time_zone); | ||
| 86 | } | ||
| 87 | |||
| 88 | $ref->{departure} = $ref->{realtime_departure} | ||
| 89 | // $ref->{scheduled_departure}; | ||
| 90 | |||
| 91 | $ref->{scheduled_arrival} | ||
| 92 | = DateTime::Format::ISO8601->parse_datetime( $json->{scheduledEndTime} ); | ||
| 93 | $ref->{scheduled_arrival}->set_time_zone($time_zone); | ||
| 94 | |||
| 95 | if ( $json->{realTime} ) { | ||
| 96 | $ref->{realtime_arrival} | ||
| 97 | = DateTime::Format::ISO8601->parse_datetime( $json->{endTime} ); | ||
| 98 | $ref->{realtime_arrival}->set_time_zone($time_zone); | ||
| 99 | } | ||
| 100 | |||
| 101 | $ref->{arrival} = $ref->{realtime_arrival} // $ref->{scheduled_arrival}; | ||
| 102 | |||
| 103 | bless( $ref, $obj ); | ||
| 104 | |||
| 105 | return $ref; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub polyline { | ||
| 109 | my ($self) = @_; | ||
| 110 | |||
| 111 | if ( not $self->{raw_polyline} ) { | ||
| 112 | return; | ||
| 113 | } | ||
| 114 | |||
| 115 | if ( $self->{polyline} ) { | ||
| 116 | return @{ $self->{polyline} }; | ||
| 117 | } | ||
| 118 | |||
| 119 | my $polyline = [ decode_polyline( $self->{raw_polyline} ) ]; | ||
| 120 | |||
| 121 | my $gis_distance; | ||
| 122 | |||
| 123 | eval { | ||
| 124 | require GIS::Distance; | ||
| 125 | $gis_distance = GIS::Distance->new; | ||
| 126 | }; | ||
| 127 | |||
| 128 | if ($gis_distance) { | ||
| 129 | my %minimum_distances; | ||
| 130 | |||
| 131 | for my $stopover ( $self->stopovers ) { | ||
| 132 | my $stop = $stopover->stop; | ||
| 133 | |||
| 134 | for my $polyline_index ( 0 .. $#{$polyline} ) { | ||
| 135 | my $coordinate = $polyline->[$polyline_index]; | ||
| 136 | my $distance = $gis_distance->distance_metal( | ||
| 137 | $stop->{lat}, $stop->{lon}, | ||
| 138 | $coordinate->{lat}, $coordinate->{lon}, | ||
| 139 | ); | ||
| 140 | |||
| 141 | if ( not $minimum_distances{ $stop->id } | ||
| 142 | or $minimum_distances{ $stop->id }{distance} > $distance ) | ||
| 143 | { | ||
| 144 | $minimum_distances{ $stop->id } = { | ||
| 145 | distance => $distance, | ||
| 146 | index => $polyline_index, | ||
| 147 | }; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | for my $stopover ( $self->stopovers ) { | ||
| 153 | my $stop = $stopover->stop; | ||
| 154 | |||
| 155 | if ( $minimum_distances{ $stop->id } ) { | ||
| 156 | $polyline->[ $minimum_distances{ $stop->id }{index} ]{stop} | ||
| 157 | = $stop; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | $self->{polyline} = $polyline; | ||
| 163 | |||
| 164 | return @{ $self->{polyline} }; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub stopovers { | ||
| 168 | my ($self) = @_; | ||
| 169 | |||
| 170 | if ( $self->{stopovers} ) { | ||
| 171 | return @{ $self->{stopovers} }; | ||
| 172 | } | ||
| 173 | |||
| 174 | @{ $self->{stopovers} } = map { | ||
| 175 | Travel::Status::MOTIS::Stopover->new( | ||
| 176 | json => $_, | ||
| 177 | realtime => $self->{is_realtime}, | ||
| 178 | time_zone => $self->{time_zone}, | ||
| 179 | ) | ||
| 180 | } ( @{ $self->{raw_stopovers} // [] } ); | ||
| 181 | |||
| 182 | return @{ $self->{stopovers} }; | ||
| 183 | } | ||
| 184 | |||
| 185 | sub TO_JSON { | ||
| 186 | my ($self) = @_; | ||
| 187 | |||
| 188 | # transform raw_stopovers into stopovers (lazy accessor) | ||
| 189 | $self->stopovers; | ||
| 190 | |||
| 191 | # transform raw_polyline into polyline (lazy accessor) | ||
| 192 | $self->polyline; | ||
| 193 | |||
| 194 | my $ret = { %{$self} }; | ||
| 195 | |||
| 196 | for my $timestamp_key ( | ||
| 197 | qw( | ||
| 198 | arrival | ||
| 199 | scheduled_arrival | ||
| 200 | realtime_arrival | ||
| 201 | |||
| 202 | departure | ||
| 203 | scheduled_departure | ||
| 204 | realtime_departure | ||
| 205 | ) | ||
| 206 | ) | ||
| 207 | { | ||
| 208 | if ( $ret->{$timestamp_key} ) { | ||
| 209 | $ret->{$timestamp_key} = $ret->{$timestamp_key}->epoch; | ||
| 210 | } | ||
| 211 | } | ||
| 212 | |||
| 213 | return $ret; | ||
| 214 | } | ||
| 215 | |||
| 216 | 1; |