diff options
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/Travelynx.pm | 147 | ||||
-rw-r--r-- | lib/Travelynx/Command/database.pm | 2 | ||||
-rw-r--r-- | lib/Travelynx/Command/work.pm | 79 | ||||
-rw-r--r-- | lib/Travelynx/Controller/Account.pm | 3 | ||||
-rwxr-xr-x | lib/Travelynx/Controller/Traveling.pm | 87 | ||||
-rw-r--r-- | lib/Travelynx/Helper/DBRIS.pm | 1 | ||||
-rw-r--r-- | lib/Travelynx/Helper/EFA.pm | 53 | ||||
-rw-r--r-- | lib/Travelynx/Model/InTransit.pm | 171 | ||||
-rw-r--r-- | lib/Travelynx/Model/Stations.pm | 58 |
9 files changed, 562 insertions, 39 deletions
diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index 75c9b57..3d892ec 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -507,15 +507,18 @@ sub startup { return Mojo::Promise->reject('You are already checked in'); } - if ( $opt{motis} ) { - return $self->_checkin_motis_p(%opt); - } if ( $opt{dbris} ) { return $self->_checkin_dbris_p(%opt); } + if ( $opt{efa} ) { + return $self->_checkin_efa_p(%opt); + } if ( $opt{hafas} ) { return $self->_checkin_hafas_p(%opt); } + if ( $opt{motis} ) { + return $self->_checkin_motis_p(%opt); + } my $promise = Mojo::Promise->new; @@ -886,6 +889,137 @@ sub startup { ); $self->helper( + '_checkin_efa_p' => sub { + my ( $self, %opt ) = @_; + my $station = $opt{station}; + my $trip_id = $opt{train_id}; + my $ts = $opt{ts}; + my $uid = $opt{uid} // $self->current_user->{id}; + my $db = $opt{db} // $self->pg->db; + + my $promise = Mojo::Promise->new; + $self->efa->get_journey_p( + service => $opt{efa}, + trip_id => $trip_id + )->then( + sub { + my ($journey) = @_; + + my $found; + for my $stop ( $journey->route ) { + if ( $stop->id_num == $station ) { + $found = $stop; + + # Lines may serve the same stop several times. + # Keep looking until the scheduled departure + # matches the one passed while checking in. + if ( $ts and $stop->sched_dep->epoch == $ts ) { + last; + } + } + } + if ( not $found ) { + $promise->reject( +"Did not find stop '$station' within journey '$trip_id'" + ); + return; + } + + for my $stop ( $journey->route ) { + $self->stations->add_or_update( + stop => $stop, + db => $db, + efa => $opt{efa}, + ); + } + + eval { + $self->in_transit->add( + uid => $uid, + db => $db, + journey => $journey, + stop => $found, + trip_id => $trip_id, + backend_id => $self->stations->get_backend_id( + efa => $opt{efa} + ), + ); + }; + if ($@) { + $self->app->log->error( + "Checkin($uid): INSERT failed: $@"); + $promise->reject( 'INSERT failed: ' . $@ ); + return; + } + + my $polyline; + if ( $journey->polyline ) { + my @station_list; + my @coordinate_list; + for my $coord ( $journey->polyline ) { + if ( $coord->{stop} ) { + push( + @coordinate_list, + [ + $coord->{lon}, $coord->{lat}, + $coord->{stop}->id_num + ] + ); + push( @station_list, + $coord->{stop}->full_name ); + } + else { + push( @coordinate_list, + [ $coord->{lon}, $coord->{lat} ] ); + } + } + + # equal length → polyline only consists of straight + # lines between stops. that's not helpful. + if ( @station_list == @coordinate_list ) { + $self->log->debug( 'Ignoring polyline for ' + . $journey->line + . ' as it only consists of straight lines between stops.' + ); + } + else { + $polyline = { + from_eva => ( $journey->route )[0]->id_num, + to_eva => ( $journey->route )[-1]->id_num, + coords => \@coordinate_list, + }; + } + } + + if ($polyline) { + $self->in_transit->set_polyline( + uid => $uid, + db => $db, + polyline => $polyline, + ); + } + + # mustn't be called during a transaction + if ( not $opt{in_transaction} ) { + $self->run_hook( $uid, 'checkin' ); + } + + $promise->resolve($journey); + + return; + } + )->catch( + sub { + my ($err) = @_; + $promise->reject($err); + return; + } + )->wait; + return $promise; + } + ); + + $self->helper( '_checkin_hafas_p' => sub { my ( $self, %opt ) = @_; @@ -894,7 +1028,6 @@ sub startup { my $ts = $opt{ts}; my $uid = $opt{uid} // $self->current_user->{id}; my $db = $opt{db} // $self->pg->db; - my $hafas; my $promise = Mojo::Promise->new; @@ -1153,7 +1286,11 @@ sub startup { return $promise->resolve( 0, 'race condition' ); } - if ( $user->{is_dbris} or $user->{is_hafas} or $user->{is_motis} ) { + if ( $user->{is_dbris} + or $user->{is_efa} + or $user->{is_hafas} + or $user->{is_motis} ) + { return $self->_checkout_journey_p(%opt); } diff --git a/lib/Travelynx/Command/database.pm b/lib/Travelynx/Command/database.pm index 460e05c..1385389 100644 --- a/lib/Travelynx/Command/database.pm +++ b/lib/Travelynx/Command/database.pm @@ -3032,7 +3032,7 @@ qq{select distinct checkout_station_id from in_transit where backend_id = 0;} $db->query( qq{ alter table schema_version add column efa varchar(12); - update schema_version set version = 59; + update schema_version set version = 63; update schema_version set efa = '0'; } ); diff --git a/lib/Travelynx/Command/work.pm b/lib/Travelynx/Command/work.pm index 2b01cb2..5ea1810 100644 --- a/lib/Travelynx/Command/work.pm +++ b/lib/Travelynx/Command/work.pm @@ -185,6 +185,85 @@ sub run { next; } + if ( $entry->{is_efa} ) { + eval { + $self->app->efa->get_journey_p( + trip_id => $train_id, + service => $entry->{backend_name} + )->then( + sub { + my ($journey) = @_; + + my $found_dep; + my $found_arr; + for my $stop ( $journey->route ) { + if ( $stop->id_num == $dep ) { + $found_dep = $stop; + } + if ( $arr and $stop->id_num == $arr ) { + $found_arr = $stop; + last; + } + } + if ( not $found_dep ) { + $self->app->log->debug( + "Did not find $dep within journey $train_id"); + return; + } + + if ( $found_dep->rt_dep ) { + $self->app->in_transit->update_departure_efa( + uid => $uid, + journey => $journey, + stop => $found_dep, + dep_eva => $dep, + arr_eva => $arr, + trip_id => $train_id, + ); + } + + if ( $found_arr and $found_arr->rt_arr ) { + $self->app->in_transit->update_arrival_efa( + uid => $uid, + journey => $journey, + stop => $found_arr, + dep_eva => $dep, + arr_eva => $arr, + trip_id => $train_id, + ); + } + } + )->catch( + sub { + my ($err) = @_; + $backend_issues += 1; + $self->app->log->error( +"work($uid) @ EFA $entry->{backend_name}: journey: $err" + ); + } + )->wait; + + if ( $arr + and $entry->{real_arr_ts} + and $now->epoch - $entry->{real_arr_ts} > 600 ) + { + $self->app->checkout_p( + station => $arr, + force => 2, + dep_eva => $dep, + arr_eva => $arr, + uid => $uid + )->wait; + } + }; + if ($@) { + $errors += 1; + $self->app->log->error( + "work($uid) @ EFA $entry->{backend_name}: $@"); + } + next; + } + if ( $entry->{is_motis} ) { eval { diff --git a/lib/Travelynx/Controller/Account.pm b/lib/Travelynx/Controller/Account.pm index 74c272f..0978c88 100644 --- a/lib/Travelynx/Controller/Account.pm +++ b/lib/Travelynx/Controller/Account.pm @@ -1084,7 +1084,8 @@ sub backend_form { $backend->{homepage} = $s->{homepage}; $backend->{regions} = [ map { $place_map{$_} // $_ } @{ $s->{coverage}{regions} // [] } ]; - $backend->{has_area} = $s->{coverage}{area} ? 1 : 0; + $backend->{has_area} = $s->{coverage}{area} ? 1 : 0; + $backend->{experimental} = 1; if ( $s->{coverage}{area} diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm index de1a547..9826211 100755 --- a/lib/Travelynx/Controller/Traveling.pm +++ b/lib/Travelynx/Controller/Traveling.pm @@ -562,11 +562,14 @@ sub geolocation { return; } - my ( $dbris_service, $hafas_service, $motis_service ); + my ( $dbris_service, $efa_service, $hafas_service, $motis_service ); my $backend = $self->stations->get_backend( backend_id => $backend_id ); if ( $backend->{dbris} ) { $dbris_service = $backend->{name}; } + if ( $backend->{efa} ) { + $efa_service = $backend->{name}; + } elsif ( $backend->{hafas} ) { $hafas_service = $backend->{name}; } @@ -617,6 +620,50 @@ sub geolocation { )->wait; return; } + elsif ($efa_service) { + $self->render_later; + + Travel::Status::DE::EFA->new_p( + promise => 'Mojo::Promise', + user_agent => Mojo::UserAgent->new, + service => $efa_service, + coord => { + lat => $lat, + lon => $lon + } + )->then( + sub { + my ($efa) = @_; + my @results = map { + { + name => $_->full_name, + eva => $_->id_code, + distance => 0, + efa => $efa_service, + } + } $efa->results; + if ( @results > 10 ) { + @results = @results[ 0 .. 9 ]; + } + $self->render( + json => { + candidates => [@results], + } + ); + } + )->catch( + sub { + my ($err) = @_; + $self->render( + json => { + candidates => [], + warning => $err, + } + ); + } + )->wait; + return; + } elsif ($hafas_service) { $self->render_later; @@ -726,8 +773,6 @@ sub geolocation { lon => $_->[0][3], lat => $_->[0][4], distance => $_->[1], - dbris => 0, - hafas => 0, } } Travel::Status::DE::IRIS::Stations::get_station_by_location( $lon, $lat, 10 ); @@ -796,6 +841,7 @@ sub travel_action { sub { return $self->checkin_p( dbris => $params->{dbris}, + efa => $params->{efa}, hafas => $params->{hafas}, motis => $params->{motis}, station => $params->{station}, @@ -832,6 +878,9 @@ sub travel_action { if ( $status->{is_dbris} ) { $station_link .= '?dbris=' . $status->{backend_name}; } + elsif ( $status->{is_efa} ) { + $station_link .= '?efa=' . $status->{backend_name}; + } elsif ( $status->{is_hafas} ) { $station_link .= '?hafas=' . $status->{backend_name}; } @@ -871,6 +920,9 @@ sub travel_action { if ( $status->{is_dbris} ) { $station_link .= '?dbris=' . $status->{backend_name}; } + elsif ( $status->{is_efa} ) { + $station_link .= '?efa=' . $status->{backend_name}; + } elsif ( $status->{is_hafas} ) { $station_link .= '?hafas=' . $status->{backend_name}; } @@ -929,6 +981,12 @@ sub travel_action { . '?dbris=' . $status->{backend_name}; } + elsif ( $status->{is_efa} ) { + $redir + = '/s/' + . $status->{dep_eva} . '?efa=' + . $status->{backend_name}; + } elsif ( $status->{is_hafas} ) { $redir = '/s/' @@ -959,6 +1017,7 @@ sub travel_action { $self->render_later; $self->checkin_p( dbris => $params->{dbris}, + efa => $params->{efa}, hafas => $params->{hafas}, motis => $params->{motis}, station => $params->{station}, @@ -1120,19 +1179,19 @@ sub station { lookbehind => 30, ); } - elsif ($hafas_service) { - $promise = $self->hafas->get_departures_p( - service => $hafas_service, - eva => $station, + elsif ($efa_service) { + $promise = $self->efa->get_departures_p( + service => $efa_service, + name => $station, timestamp => $timestamp, lookbehind => 30, lookahead => 30, ); } - elsif ($efa_service) { - $promise = $self->efa->get_departures_p( - service => $efa_service, - name => $station, + elsif ($hafas_service) { + $promise = $self->hafas->get_departures_p( + service => $hafas_service, + eva => $station, timestamp => $timestamp, lookbehind => 30, lookahead => 30, @@ -1225,8 +1284,8 @@ sub station { sort { $b->[1] <=> $a->[1] } map { [ $_, $_->datetime->epoch ] } $status->results; $status = { - station_eva => $status->stop->id, - station_name => $status->stop->full_name, + station_eva => $status->stop->id_num, + station_name => $status->stop->full_name, related_stations => [], }; } @@ -1310,7 +1369,7 @@ sub station { 'departures', user => $user, dbris => $dbris_service, - efa => $efa_service, + efa => $efa_service, hafas => $hafas_service, motis => $motis_service, eva => $status->{station_eva}, diff --git a/lib/Travelynx/Helper/DBRIS.pm b/lib/Travelynx/Helper/DBRIS.pm index 0a46758..9ddaa5f 100644 --- a/lib/Travelynx/Helper/DBRIS.pm +++ b/lib/Travelynx/Helper/DBRIS.pm @@ -94,7 +94,6 @@ sub get_journey_p { my ( $self, %opt ) = @_; my $promise = Mojo::Promise->new; - my $now = DateTime->now( time_zone => 'Europe/Berlin' ); my $agent = $self->{user_agent}; my $proxy; diff --git a/lib/Travelynx/Helper/EFA.pm b/lib/Travelynx/Helper/EFA.pm index 54c633d..ba11764 100644 --- a/lib/Travelynx/Helper/EFA.pm +++ b/lib/Travelynx/Helper/EFA.pm @@ -26,8 +26,6 @@ sub new { sub get_service { my ( $self, $service ) = @_; - say "get_service $service"; - return Travel::Status::DE::EFA::get_service($service); } @@ -50,4 +48,55 @@ sub get_departures_p { ); } +sub get_journey_p { + my ( $self, %opt ) = @_; + + my $promise = Mojo::Promise->new; + my $agent = $self->{user_agent}; + my $stopseq; + + if ( $opt{trip_id} =~ m{ ^ ([^@]*) @ ([^@]*) [(] ([^)]*) [)] (.*) $ }x ) { + $stopseq = { + stateless => $1, + stop_id => $2, + date => $3, + key => $4 + }; + } + else { + return $promise->reject("Invalid trip_id: $opt{trip_id}"); + } + + Travel::Status::DE::EFA->new_p( + service => $opt{service}, + stopseq => $stopseq, + cache => $self->{realtime_cache}, + promise => 'Mojo::Promise', + user_agent => $agent->request_timeout(10), + )->then( + sub { + my ($efa) = @_; + my $journey = $efa->result; + + if ($journey) { + $self->{log}->debug("get_journey_p($opt{trip_id}): success"); + $promise->resolve($journey); + return; + } + $self->{log}->debug("get_journey_p($opt{trip_id}): no journey"); + $promise->reject('no journey'); + return; + } + )->catch( + sub { + my ($err) = @_; + $self->{log}->debug("get_journey_p($opt{trip_id}): error $err"); + $promise->reject($err); + return; + } + )->wait; + + return $promise; +} + 1; diff --git a/lib/Travelynx/Model/InTransit.pm b/lib/Travelynx/Model/InTransit.pm index cc943b3..470b45d 100644 --- a/lib/Travelynx/Model/InTransit.pm +++ b/lib/Travelynx/Model/InTransit.pm @@ -110,8 +110,6 @@ sub add { my $now = DateTime->now( time_zone => 'Europe/Berlin' ); if ($train) { - - # IRIS $db->insert( 'in_transit', { @@ -142,9 +140,60 @@ sub add { } ); } - elsif ( $journey and $stop and $journey->can('product') ) { - - # HAFAS + elsif ( $journey + and $stop + and ref($journey) eq 'Travel::Status::DE::EFA::Trip' ) + { + my @route; + for my $j_stop ( $journey->route ) { + push( + @route, + [ + $j_stop->full_name, + $j_stop->id_num, + { + sched_arr => _epoch( $j_stop->sched_arr ), + sched_dep => _epoch( $j_stop->sched_dep ), + rt_arr => _epoch( $j_stop->rt_arr ), + rt_dep => _epoch( $j_stop->rt_dep ), + arr_delay => $j_stop->arr_delay, + dep_delay => $j_stop->dep_delay, + efa_load => $j_stop->occupancy, + lat => $j_stop->latlon->[0], + lon => $j_stop->latlon->[1], + } + ] + ); + } + $db->insert( + 'in_transit', + { + user_id => $uid, + cancelled => 0, # TODO + checkin_station_id => $stop->id_num, + checkin_time => $now, + dep_platform => $stop->platform, + train_type => $journey->type // q{}, + train_line => $journey->line, + train_no => $journey->number // q{}, + train_id => $opt{trip_id}, + sched_departure => $stop->sched_dep, + real_departure => $stop->rt_dep // $stop->sched_dep, + route => $json->encode( \@route ), + data => JSON->new->encode( + { + rt => $stop->rt_dep ? 1 : 0, + %{ $data // {} } + } + ), + backend_id => $backend_id, + } + ); + } + elsif ( $journey + and $stop + and ref($journey) eq 'Travel::Status::DE::HAFAS::Journey' ) + { my @route; my $product = $journey->product_at( $stop->loc->eva ) // $journey->product; @@ -198,9 +247,10 @@ sub add { } ); } - elsif ( $journey and $stop ) { - - # DBRIS + elsif ( $journey + and $stop + and ref($journey) eq 'Travel::Status::DE::DBRIS::Journey' ) + { my $number = $journey->train_no // $journey->number // $train_suffix; my $line; @@ -284,9 +334,10 @@ sub add { } ); } - elsif ( $journey and $stopover ) { - - # MOTIS + elsif ( $journey + and $stopover + and ref($journey) eq 'Travel::Status::MOTIS::Trip' ) + { my @route; for my $journey_stopover ( $journey->stopovers ) { push( @@ -340,7 +391,7 @@ sub add { ); } else { - die('neither train nor journey specified'); + die('invalid arguments / argument types passed to InTransit->add'); } } @@ -944,6 +995,41 @@ sub update_departure_dbris { ); } +sub update_departure_efa { + my ( $self, %opt ) = @_; + my $uid = $opt{uid}; + my $db = $opt{db} // $self->{pg}->db; + my $dep_eva = $opt{dep_eva}; + my $arr_eva = $opt{arr_eva}; + my $journey = $opt{journey}; + my $stop = $opt{stop}; + my $json = JSON->new; + + my $res_h = $db->select( 'in_transit', ['data'], { user_id => $uid } ) + ->expand->hash; + my $ephemeral_data = $res_h ? $res_h->{data} : {}; + if ( $stop->rt_dep ) { + $ephemeral_data->{rt} = 1; + } + + # selecting on user_id and train_no avoids a race condition if a user checks + # into a new train while we are fetching data for their previous journey. In + # this case, the new train would receive data from the previous journey. + $db->update( + 'in_transit', + { + data => $json->encode($ephemeral_data), + real_departure => $stop->rt_dep, + }, + { + user_id => $uid, + train_id => $opt{trip_id}, + checkin_station_id => $dep_eva, + checkout_station_id => $arr_eva, + } + ); +} + sub update_departure_motis { my ( $self, %opt ) = @_; my $uid = $opt{uid}; @@ -1137,6 +1223,67 @@ sub update_arrival_dbris { ); } +sub update_arrival_efa { + my ( $self, %opt ) = @_; + my $uid = $opt{uid}; + my $db = $opt{db} // $self->{pg}->db; + my $dep_eva = $opt{dep_eva}; + my $arr_eva = $opt{arr_eva}; + my $journey = $opt{journey}; + my $stop = $opt{stop}; + my $json = JSON->new; + + my $res_h + = $db->select( 'in_transit', [ 'data', 'route' ], { user_id => $uid } ) + ->expand->hash; + my $ephemeral_data = $res_h ? $res_h->{data} : {}; + my $old_route = $res_h ? $res_h->{route} : []; + + if ( $stop->rt_arr ) { + $ephemeral_data->{rt} = 1; + } + + my @route; + for my $j_stop ( $journey->route ) { + push( + @route, + [ + $j_stop->full_name, + $j_stop->id_num, + { + sched_arr => _epoch( $j_stop->sched_arr ), + sched_dep => _epoch( $j_stop->sched_dep ), + rt_arr => _epoch( $j_stop->rt_arr ), + rt_dep => _epoch( $j_stop->rt_dep ), + arr_delay => $j_stop->arr_delay, + dep_delay => $j_stop->dep_delay, + efa_load => $j_stop->occupancy, + lat => $j_stop->latlon->[0], + lon => $j_stop->latlon->[1], + } + ] + ); + } + + # selecting on user_id and train_no avoids a race condition if a user checks + # into a new train while we are fetching data for their previous journey. In + # this case, the new train would receive data from the previous journey. + $db->update( + 'in_transit', + { + data => $json->encode($ephemeral_data), + real_arrival => $stop->rt_arr, + route => $json->encode( [@route] ), + }, + { + user_id => $uid, + train_id => $opt{trip_id}, + checkin_station_id => $dep_eva, + checkout_station_id => $arr_eva, + } + ); +} + sub update_arrival_motis { my ( $self, %opt ) = @_; my $uid = $opt{uid}; diff --git a/lib/Travelynx/Model/Stations.pm b/lib/Travelynx/Model/Stations.pm index 44618ac..bf35d1a 100644 --- a/lib/Travelynx/Model/Stations.pm +++ b/lib/Travelynx/Model/Stations.pm @@ -23,12 +23,15 @@ sub get_backend_id { # special case return 0; } - if ( $opt{hafas} and $self->{backend_id}{hafas}{ $opt{hafas} } ) { - return $self->{backend_id}{hafas}{ $opt{hafas} }; - } if ( $opt{dbris} and $self->{backend_id}{dbris}{ $opt{dbris} } ) { return $self->{backend_id}{dbris}{ $opt{dbris} }; } + if ( $opt{efa} and $self->{backend_id}{efa}{ $opt{efa} } ) { + return $self->{backend_id}{efa}{ $opt{efa} }; + } + if ( $opt{hafas} and $self->{backend_id}{hafas}{ $opt{hafas} } ) { + return $self->{backend_id}{hafas}{ $opt{hafas} }; + } if ( $opt{motis} and $self->{backend_id}{motis}{ $opt{motis} } ) { return $self->{backend_id}{motis}{ $opt{motis} }; } @@ -47,6 +50,17 @@ sub get_backend_id { )->hash->{id}; $self->{backend_id}{dbris}{ $opt{dbris} } = $backend_id; } + elsif ( $opt{efa} ) { + $backend_id = $db->select( + 'backends', + ['id'], + { + efa => 1, + name => $opt{efa} + } + )->hash->{id}; + $self->{backend_id}{efa}{ $opt{efa} } = $backend_id; + } elsif ( $opt{hafas} ) { $backend_id = $db->select( 'backends', @@ -167,6 +181,44 @@ sub add_or_update { return; } + if ( $opt{efa} ) { + if ( + my $s = $self->get_by_eva( + $stop->id_num, + db => $opt{db}, + backend_id => $opt{backend_id} + ) + ) + { + $opt{db}->update( + 'stations', + { + name => $stop->full_name, + lat => $stop->latlon->[0], + lon => $stop->latlon->[1], + archived => 0 + }, + { + eva => $stop->id_num, + source => $opt{backend_id} + } + ); + return; + } + $opt{db}->insert( + 'stations', + { + eva => $stop->id_num, + name => $stop->full_name, + lat => $stop->latlon->[0], + lon => $stop->latlon->[1], + source => $opt{backend_id}, + archived => 0 + } + ); + return; + } + if ( $opt{motis} ) { if ( my $s = $self->get_by_external_id( |