From 4dbbedaf6bd953e5d57023eefc3acf742e91f511 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 30 Oct 2022 17:14:21 +0100 Subject: Move EFA code to separate helper class --- lib/DBInfoscreen.pm | 10 +-- lib/DBInfoscreen/Controller/Stationboard.pm | 2 +- lib/DBInfoscreen/Helper/EFA.pm | 119 ++++++++++++++++++++++++++++ lib/DBInfoscreen/Helper/HAFAS.pm | 2 +- lib/DBInfoscreen/Helper/Marudor.pm | 119 ---------------------------- 5 files changed, 126 insertions(+), 126 deletions(-) create mode 100644 lib/DBInfoscreen/Helper/EFA.pm delete mode 100644 lib/DBInfoscreen/Helper/Marudor.pm diff --git a/lib/DBInfoscreen.pm b/lib/DBInfoscreen.pm index 23823f1..83cb77a 100644 --- a/lib/DBInfoscreen.pm +++ b/lib/DBInfoscreen.pm @@ -7,8 +7,8 @@ package DBInfoscreen; use Mojo::Base 'Mojolicious'; use Cache::File; +use DBInfoscreen::Helper::EFA; use DBInfoscreen::Helper::HAFAS; -use DBInfoscreen::Helper::Marudor; use DBInfoscreen::Helper::Wagonorder; use File::Slurp qw(read_file); use JSON; @@ -125,9 +125,9 @@ sub startup { ); $self->helper( - hafas => sub { + efa => sub { my ($self) = @_; - state $hafas = DBInfoscreen::Helper::HAFAS->new( + state $efa = DBInfoscreen::Helper::EFA->new( log => $self->app->log, main_cache => $self->app->cache_iris_main, realtime_cache => $self->app->cache_iris_rt, @@ -139,9 +139,9 @@ sub startup { ); $self->helper( - marudor => sub { + hafas => sub { my ($self) = @_; - state $hafas = DBInfoscreen::Helper::Marudor->new( + state $hafas = DBInfoscreen::Helper::HAFAS->new( log => $self->app->log, main_cache => $self->app->cache_iris_main, realtime_cache => $self->app->cache_iris_rt, diff --git a/lib/DBInfoscreen/Controller/Stationboard.pm b/lib/DBInfoscreen/Controller/Stationboard.pm index 5a8c44e..735cbe7 100644 --- a/lib/DBInfoscreen/Controller/Stationboard.pm +++ b/lib/DBInfoscreen/Controller/Stationboard.pm @@ -597,7 +597,7 @@ sub render_train { $wagonorder_req->resolve; } - $self->marudor->get_efa_occupancy( + $self->efa->get_efa_occupancy( eva => $result->station_uic, train_no => $result->train_no )->then( diff --git a/lib/DBInfoscreen/Helper/EFA.pm b/lib/DBInfoscreen/Helper/EFA.pm new file mode 100644 index 0000000..2386ebb --- /dev/null +++ b/lib/DBInfoscreen/Helper/EFA.pm @@ -0,0 +1,119 @@ +package DBInfoscreen::Helper::EFA; + +# Copyright (C) 2020-2022 Daniel Friesel +# +# SPDX-License-Identifier: AGPL-3.0-or-later + +use strict; +use warnings; +use 5.020; + +use DateTime; +use Encode qw(decode encode); +use Mojo::JSON qw(decode_json); +use Mojo::Promise; +use Mojo::Util qw(url_escape); +use XML::LibXML; + +sub new { + my ( $class, %opt ) = @_; + + my $version = $opt{version}; + + $opt{header} + = { 'User-Agent' => +"dbf/${version} on $opt{root_url} +https://finalrewind.org/projects/db-fakedisplay" + }; + + return bless( \%opt, $class ); + +} + +sub get_json_p { + my ( $self, $cache, $url ) = @_; + + my $promise = Mojo::Promise->new; + + if ( my $content = $cache->thaw($url) ) { + $self->{log}->debug("efa->get_json_p($url): cached"); + if ( $content->{error} ) { + return $promise->reject( $content->{error} ); + } + return $promise->resolve($content); + } + + $self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} ) + ->then( + sub { + my ($tx) = @_; + + if ( my $err = $tx->error ) { + $self->{log}->debug( +"efa->get_json_p($url): HTTP $err->{code} $err->{message}" + ); + $cache->freeze( $url, { error => $err->{message} } ); + $promise->reject( + "GET $url returned HTTP $err->{code} $err->{message}"); + return; + } + + my $res = $tx->res->json; + + if ( not $res ) { + $self->{log} + ->debug("efa->get_json_p($url): empty response"); + $promise->reject("GET $url returned empty response"); + return; + } + + $cache->freeze( $url, $res ); + + $promise->resolve($res); + + return; + } + )->catch( + sub { + my ($err) = @_; + $self->{log}->debug("efa->get_json_p($url): $err"); + $cache->freeze( $url, { error => $err } ); + $promise->reject($err); + return; + } + )->wait; + + return $promise; +} + +sub get_efa_occupancy { + my ( $self, %opt ) = @_; + + my $eva = $opt{eva}; + my $train_no = $opt{train_no}; + my $promise = Mojo::Promise->new; + + $self->get_json_p( $self->{realtime_cache}, + "https://vrrf.finalrewind.org/_eva/occupancy-by-eva/${eva}.json" ) + ->then( + sub { + my ($utilization_json) = @_; + + if ( $utilization_json->{train}{$train_no}{occupancy} ) { + $promise->resolve( + $utilization_json->{train}{$train_no}{occupancy} ); + return; + } + $promise->reject; + return; + } + )->catch( + sub { + $promise->reject; + return; + } + )->wait; + + return $promise; +} + +1; diff --git a/lib/DBInfoscreen/Helper/HAFAS.pm b/lib/DBInfoscreen/Helper/HAFAS.pm index 2fb088a..28f5f02 100644 --- a/lib/DBInfoscreen/Helper/HAFAS.pm +++ b/lib/DBInfoscreen/Helper/HAFAS.pm @@ -1,6 +1,6 @@ package DBInfoscreen::Helper::HAFAS; -# Copyright (C) 2011-2020 Daniel Friesel +# Copyright (C) 2011-2022 Daniel Friesel # # SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/lib/DBInfoscreen/Helper/Marudor.pm b/lib/DBInfoscreen/Helper/Marudor.pm deleted file mode 100644 index 352829b..0000000 --- a/lib/DBInfoscreen/Helper/Marudor.pm +++ /dev/null @@ -1,119 +0,0 @@ -package DBInfoscreen::Helper::Marudor; - -# Copyright (C) 2020 Daniel Friesel -# -# SPDX-License-Identifier: AGPL-3.0-or-later - -use strict; -use warnings; -use 5.020; - -use DateTime; -use Encode qw(decode encode); -use Mojo::JSON qw(decode_json); -use Mojo::Promise; -use Mojo::Util qw(url_escape); -use XML::LibXML; - -sub new { - my ( $class, %opt ) = @_; - - my $version = $opt{version}; - - $opt{header} - = { 'User-Agent' => -"dbf/${version} on $opt{root_url} +https://finalrewind.org/projects/db-fakedisplay" - }; - - return bless( \%opt, $class ); - -} - -sub get_json_p { - my ( $self, $cache, $url ) = @_; - - my $promise = Mojo::Promise->new; - - if ( my $content = $cache->thaw($url) ) { - $self->{log}->debug("marudor->get_json_p($url): cached"); - if ( $content->{error} ) { - return $promise->reject( $content->{error} ); - } - return $promise->resolve($content); - } - - $self->{user_agent}->request_timeout(5)->get_p( $url => $self->{header} ) - ->then( - sub { - my ($tx) = @_; - - if ( my $err = $tx->error ) { - $self->{log}->debug( -"marudor->get_json_p($url): HTTP $err->{code} $err->{message}" - ); - $cache->freeze( $url, { error => $err->{message} } ); - $promise->reject( - "GET $url returned HTTP $err->{code} $err->{message}"); - return; - } - - my $res = $tx->res->json; - - if ( not $res ) { - $self->{log} - ->debug("marudor->get_json_p($url): empty response"); - $promise->reject("GET $url returned empty response"); - return; - } - - $cache->freeze( $url, $res ); - - $promise->resolve($res); - - return; - } - )->catch( - sub { - my ($err) = @_; - $self->{log}->debug("marudor->get_json_p($url): $err"); - $cache->freeze( $url, { error => $err } ); - $promise->reject($err); - return; - } - )->wait; - - return $promise; -} - -sub get_efa_occupancy { - my ( $self, %opt ) = @_; - - my $eva = $opt{eva}; - my $train_no = $opt{train_no}; - my $promise = Mojo::Promise->new; - - $self->get_json_p( $self->{realtime_cache}, - "https://vrrf.finalrewind.org/_eva/occupancy-by-eva/${eva}.json" ) - ->then( - sub { - my ($utilization_json) = @_; - - if ( $utilization_json->{train}{$train_no}{occupancy} ) { - $promise->resolve( - $utilization_json->{train}{$train_no}{occupancy} ); - return; - } - $promise->reject; - return; - } - )->catch( - sub { - $promise->reject; - return; - } - )->wait; - - return $promise; -} - -1; -- cgit v1.2.3