summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rwxr-xr-xlib/Travelynx.pm146
-rw-r--r--lib/Travelynx/Controller/Account.pm3
-rwxr-xr-xlib/Travelynx/Controller/Traveling.pm30
-rw-r--r--lib/Travelynx/Helper/DBRIS.pm1
-rw-r--r--lib/Travelynx/Helper/EFA.pm51
-rw-r--r--lib/Travelynx/Model/InTransit.pm75
-rw-r--r--lib/Travelynx/Model/Stations.pm58
7 files changed, 334 insertions, 30 deletions
diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm
index 75c9b57..6ae0135 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,136 @@ 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,
+ 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 +1027,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 +1285,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/Controller/Account.pm b/lib/Travelynx/Controller/Account.pm
index 74c272f..bf1eac2 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->{association} = 1;
if (
$s->{coverage}{area}
diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm
index cc3e1ac..9826211 100755
--- a/lib/Travelynx/Controller/Traveling.pm
+++ b/lib/Travelynx/Controller/Traveling.pm
@@ -841,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},
@@ -877,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};
}
@@ -916,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};
}
@@ -974,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/'
@@ -1004,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},
@@ -1165,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,
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 847755e..ba11764 100644
--- a/lib/Travelynx/Helper/EFA.pm
+++ b/lib/Travelynx/Helper/EFA.pm
@@ -48,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..662df54 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 => $journey->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');
}
}
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(