From 375f02ad634c10030f47af94f0530dda0d2d044d Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Thu, 16 Apr 2020 09:50:37 +0200 Subject: Fix occasionally incorrect TripIDs (and thus incorrect polylines) Until now, tripIDs were cached based on station and departure timestamp. These are identical for any two trains departing at the same time at the same station, leading to one of those getting being assigned a wrong tripID. From now on, only the JSON reported by transport.rest is cached -- tripIDs are always recomputed based on it. --- lib/DBInfoscreen/Controller/Stationboard.pm | 51 +++++++++++++++++------------ 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/lib/DBInfoscreen/Controller/Stationboard.pm b/lib/DBInfoscreen/Controller/Stationboard.pm index e40fda3..8ac8101 100644 --- a/lib/DBInfoscreen/Controller/Stationboard.pm +++ b/lib/DBInfoscreen/Controller/Stationboard.pm @@ -137,27 +137,10 @@ sub get_hafas_trip_id { = "https://2.db.transport.rest/stations/${eva}/arrivals?duration=5&when=$dep_ts"; } - if ( my $content = $cache->get($url) ) { - return $content; - } - - $ua->request_timeout(2); - my $res = eval { - $ua->get( - $url => { 'User-Agent' => "dbf.finalrewind.org/${dbf_version}" } ) - ->result; - }; - if ($@) { - return; - } - if ( $res->is_error ) { - return; - } + my $json = hafas_rest_req( $ua, $cache, $url ); - my $json = decode_json( $res->body ); - - #say "looking for " . $train->train_no; - for my $result ( @{$json} ) { + #say "looking for " . $train->train_no . " in $url"; + for my $result ( @{ $json // [] } ) { my $trip_id = $result->{tripId}; my $fahrt = $result->{line}{fahrtNr}; @@ -165,7 +148,6 @@ sub get_hafas_trip_id { if ( $result->{line} and $result->{line}{fahrtNr} == $train->train_no ) { #say "Trip ID is $trip_id"; - $cache->set( $url, $trip_id ); return $trip_id; } else { @@ -201,6 +183,33 @@ sub check_wagonorder { } } +sub hafas_rest_req { + my ( $ua, $cache, $url ) = @_; + + if ( my $content = $cache->thaw($url) ) { + return $content; + } + + my $res = eval { + $ua->get( + $url => { 'User-Agent' => "dbf.finalrewind.org/${dbf_version}" } ) + ->result; + }; + + if ($@) { + return; + } + if ( $res->is_error ) { + return; + } + + my $json = decode_json( $res->body ); + + $cache->freeze( $url, $json ); + + return $json; +} + sub hafas_json_req { my ( $ua, $cache, $url ) = @_; -- cgit v1.2.3