summaryrefslogtreecommitdiff
path: root/lib/DBInfoscreen/Controller
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2020-04-16 09:50:37 +0200
committerDaniel Friesel <derf@finalrewind.org>2020-04-16 09:50:37 +0200
commit375f02ad634c10030f47af94f0530dda0d2d044d (patch)
treec5acdc82e8b60bd00c5725f541895538c42f533a /lib/DBInfoscreen/Controller
parenteeec5faaf0a94e3d238049d34eaa24d08e57b01e (diff)
Fix occasionally incorrect TripIDs (and thus incorrect polylines)3.0.3
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.
Diffstat (limited to 'lib/DBInfoscreen/Controller')
-rw-r--r--lib/DBInfoscreen/Controller/Stationboard.pm51
1 files 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 ) = @_;