summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2020-07-27 13:44:03 +0200
committerDaniel Friesel <derf@finalrewind.org>2020-07-27 13:44:03 +0200
commit7b47072af2cdc7f0fc5a88f8f1db1e2f361e8b12 (patch)
treec7e6bbffd4321626f8137163f2a4fa7243955ed0
parent3d2106af3850b50553522d5a68469e1c8e9836b7 (diff)
marrying two checkin services really isn't trivialtraewelling
-rwxr-xr-xlib/Travelynx.pm20
-rw-r--r--lib/Travelynx/Command/work.pm30
2 files changed, 42 insertions, 8 deletions
diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm
index 4bbbe56..2d03f34 100755
--- a/lib/Travelynx.pm
+++ b/lib/Travelynx.pm
@@ -1583,24 +1583,26 @@ sub startup {
$user->{dep_name}, $user->{arr_name},
$user->{extra_data}{trip_id}
);
- $self->mark_trwl_error( $uid,
- "travelynx → Traewelling: Checkin-Fehler bei $train: $error"
+ $self->trwl_log( $uid,
+ "travelynx → Traewelling: Checkin-Fehler bei $train: $error", 1
);
}
);
$self->helper(
- 'mark_trwl_error' => sub {
- my ( $self, $uid, $error ) = @_;
+ 'trwl_log' => sub {
+ my ( $self, $uid, $message, $is_error ) = @_;
my $res_h = $self->pg->db->select( 'traewelling', 'data',
{ user_id => $uid } )->expand->hash;
splice( @{ $res_h->{data}{log} // [] }, 9 );
- push( @{ $res_h->{data}{log} }, [ $self->now->epoch, $error ] );
- $res_h->{data}{error} = $error;
+ push( @{ $res_h->{data}{log} }, [ $self->now->epoch, $message ] );
+ if ($is_error) {
+ $res_h->{data}{error} = $message;
+ }
$self->pg->db->update(
'traewelling',
{
- errored => 1,
+ errored => $is_error ? 1 : 0,
latest_run => $self->now,
data => JSON->new->encode( $res_h->{data} )
},
@@ -1682,6 +1684,7 @@ sub startup {
time_zone => 'Europe/Berlin',
);
my $status_id = $status->{id};
+ my $message = $status->{body};
my $checkin_at
= $strp->parse_datetime( $status->{created_at} );
@@ -1700,6 +1703,7 @@ sub startup {
my $arr_name
= $status->{train_checkin}{destination}{name};
+ my $category = $status->{train_checkin}{hafas_trip}{category};
my $trip_id
= $status->{train_checkin}{hafas_trip}{trip_id};
my $linename
@@ -1709,6 +1713,7 @@ sub startup {
$promise->resolve(
{
status_id => $status_id,
+ message => $message,
checkin => $checkin_at,
dep_dt => $dep_dt,
dep_eva => $dep_eva,
@@ -1720,6 +1725,7 @@ sub startup {
train_type => $train_type,
line => $linename,
line_no => $train_line,
+ category => $category,
}
);
}
diff --git a/lib/Travelynx/Command/work.pm b/lib/Travelynx/Command/work.pm
index 8117cba..4e69309 100644
--- a/lib/Travelynx/Command/work.pm
+++ b/lib/Travelynx/Command/work.pm
@@ -226,13 +226,21 @@ sub run {
{
return;
}
+ # TODO Protokollieren: Letzter behandelter Status ist ID X -> muss nicht nochmal angeschaut werden
$self->app->log->debug("... user is checked in:");
while ( my ( $k, $v ) = each %{$status} ) {
$self->app->log->debug("$k = $v");
}
+ my $user = $self->app->get_user_status($traewelling->{user_id});
+ if ($user->{dep_eva} == $status->{dep_eva} and $user->{sched_departure}->epoch == $status->{dep_dt}->epoch) {
+ $self->app->log->debug("... user is also checked in via travelynx. nothing to do.");
+ continue;
+ }
my $dep
= $self->app->get_departures( $status->{dep_eva}, 60, 40, 0 );
if ( $dep->{errstr} ) {
+ $self->app->trwl_log($traewelling->{user_id},
+ "Fehler bei Traewelling-Status $status->{status_id} ($status->{line} nach $status->{arr_name}): $dep->{errstr}", 1);
return;
}
my $train_id;
@@ -240,7 +248,7 @@ sub run {
if ( $train->line ne $status->{line} ) {
next;
}
- if ( $train->sched_departure->epoch
+ if ( not $train->sched_departure or $train->sched_departure->epoch
!= $status->{dep_dt}->epoch )
{
next;
@@ -255,6 +263,9 @@ sub run {
$train_id = $train->train_id;
}
if ($train_id) {
+ # TODO Transaktion -> nur committen, wenn checkin und checkout erfolgreich
+ # my $db = $self->app->pg->db;
+ # my $tx = $db->begin;
my ( undef, $err )
= $self->app->checkin( $status->{dep_eva}, $train_id,
$traewelling->{user_id} );
@@ -262,7 +273,24 @@ sub run {
( undef, $err )
= $self->app->checkout( $status->{arr_eva}, 0,
$traewelling->{user_id} );
+ if (not $err) {
+ $self->app->trwl_log($traewelling->{user_id},
+ "Traewelling-Status $status->{status_id} übernommen: Eingecheckt in $status->{line} nach $status->{arr_name} (Zug-ID $train_id)", 0);
+ #$tx->commit;
+ }
+ }
+ if ($err) {
+ $self->app->trwl_log($traewelling->{user_id},
+ "Fehler bei Traewelling-Status $status->{status_id} ($status->{line} nach $status->{arr_name}, Zug-ID $train_id): $err", 1);
+ }
+ }
+ else {
+ my $is_error = 0;
+ if ($status->{category} =~ m{^ (?: nationalExpress | regional | suburban ) $ }x) {
+ $is_error = 1;
}
+ $self->app->trwl_log($traewelling->{user_id},
+ "Traewelling-Status $status->{status_id} ($status->{line} nach $status->{arr_name}): Kein Zug gefunden", $is_error);
}
}
)->catch(