diff options
Diffstat (limited to 'lib/DBInfoscreen.pm')
-rw-r--r-- | lib/DBInfoscreen.pm | 156 |
1 files changed, 31 insertions, 125 deletions
diff --git a/lib/DBInfoscreen.pm b/lib/DBInfoscreen.pm index 12da336..c784e96 100644 --- a/lib/DBInfoscreen.pm +++ b/lib/DBInfoscreen.pm @@ -1,32 +1,27 @@ package DBInfoscreen; -# Copyright (C) 2011-2020 Daniel Friesel +# Copyright (C) 2011-2020 Birte Kristina Friesel # # SPDX-License-Identifier: AGPL-3.0-or-later 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; -use Travel::Status::DE::IRIS::Stations; use utf8; -no if $] >= 5.018, warnings => 'experimental::smartmatch'; - sub startup { my ($self) = @_; $self->config( - hafas_rest_api => $ENV{DBFAKEDISPLAY_HAFAS_API} - // 'https://v5.db.transport.rest', hypnotoad => { - accepts => $ENV{DBFAKEDISPLAY_ACCEPTS} // 100, - clients => $ENV{DBFAKEDISPLAY_CLIENTS} // 10, + accepts => $ENV{DBFAKEDISPLAY_ACCEPTS} // 100, + clients => $ENV{DBFAKEDISPLAY_CLIENTS} // 10, listen => [ $ENV{DBFAKEDISPLAY_LISTEN} // 'http://*:8092' ], pid_file => $ENV{DBFAKEDISPLAY_PID_FILE} // '/tmp/db-fakedisplay.pid', @@ -41,20 +36,25 @@ sub startup { ); chomp $self->config->{version}; + $self->defaults( version => $self->config->{version} // 'UNKNOWN' ); + + # Generally, the reverse proxy handles compression. + # Also, Mojolicious compression breaks legacy callback-based JSON endpoints + # for some clients. + $self->renderer->compress(0); $self->hook( before_dispatch => sub { my ($self) = @_; - # The "theme" cookie is set client-side if the theme we delivered was - # changed by dark mode detection or by using the theme switcher. It's - # not part of Mojolicious' session data (and can't be, due to - # signing and HTTPOnly), so we need to add it here. + # The "theme" cookie is set client-side if the theme we delivered was + # changed by dark mode detection or by using the theme switcher. It's + # not part of Mojolicious' session data (and can't be, due to + # signing and HTTPOnly), so we need to add it here. for my $cookie ( @{ $self->req->cookies } ) { if ( $cookie->name eq 'theme' ) { $self->session( theme => $cookie->value ); - return; } } } @@ -85,36 +85,6 @@ sub startup { ); $self->attr( - ice_type_map => sub { - if ( -r 'share/zugbildungsplan.json' ) { - my $ice_type_map = JSON->new->utf8->decode( - scalar read_file('share/zugbildungsplan.json') ); - my $ret; - while ( my ( $k, $v ) = each %{ $ice_type_map->{train} } ) { - if ( $v->{type} ) { - $ret->{$k} = [ - $v->{type}, $v->{shortType}, - exists $v->{wagons} ? 1 : 0 - ]; - } - } - return $ret; - } - return {}; - } - ); - - $self->attr( - train_details_db => sub { - if ( -r 'share/zugbildungsplan.json' ) { - return JSON->new->utf8->decode( - scalar read_file('share/zugbildungsplan.json') )->{train}; - } - return {}; - } - ); - - $self->attr( dbdb_wagon => sub { return JSON->new->utf8->decode( scalar read_file('share/dbdb_wagen.json') ); @@ -122,10 +92,9 @@ sub startup { ); $self->helper( - hafas => sub { + efa => sub { my ($self) = @_; - state $hafas = DBInfoscreen::Helper::HAFAS->new( - api => $self->config->{hafas_rest_api}, + state $efa = DBInfoscreen::Helper::EFA->new( log => $self->app->log, main_cache => $self->app->cache_iris_main, realtime_cache => $self->app->cache_iris_rt, @@ -137,9 +106,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, @@ -214,71 +183,6 @@ sub startup { ); $self->helper( - 'json_route_diff' => sub { - my ( $self, $route, $sched_route ) = @_; - my @json_route; - my @route = @{$route}; - my @sched_route = @{$sched_route}; - - my $route_idx = 0; - my $sched_idx = 0; - - while ( $route_idx <= $#route and $sched_idx <= $#sched_route ) { - if ( $route[$route_idx] eq $sched_route[$sched_idx] ) { - push( @json_route, { name => $route[$route_idx] } ); - $route_idx++; - $sched_idx++; - } - - # this branch is inefficient, but won't be taken frequently - elsif ( not( $route[$route_idx] ~~ \@sched_route ) ) { - push( - @json_route, - { - name => $route[$route_idx], - isAdditional => 1 - } - ); - $route_idx++; - } - else { - push( - @json_route, - { - name => $sched_route[$sched_idx], - isCancelled => 1 - } - ); - $sched_idx++; - } - } - while ( $route_idx <= $#route ) { - push( - @json_route, - { - name => $route[$route_idx], - isAdditional => 1, - isCancelled => 0 - } - ); - $route_idx++; - } - while ( $sched_idx <= $#sched_route ) { - push( - @json_route, - { - name => $sched_route[$sched_idx], - isAdditional => 0, - isCancelled => 1 - } - ); - $sched_idx++; - } - return @json_route; - } - ); - - $self->helper( 'occupancy_icon' => sub { my ( $self, $occupancy ) = @_; @@ -303,7 +207,9 @@ sub startup { $self->helper( 'utilization_icon' => sub { my ( $self, $utilization ) = @_; - my ( $first, $second ) = @{ $utilization // [ 0, 0 ] }; + my ( $first, $second ) = @{ $utilization // [] }; + $first //= 0; + $second //= 0; my $sum = ( $first + $second ) / 2; my @symbols @@ -351,13 +257,12 @@ sub startup { my $r = $self->routes; - $r->get('/_redirect')->to('static#redirect'); + $r->get('/_redirect')->to('stationboard#redirect_to_station'); # legacy entry point $r->get('/_auto')->to('static#geostop'); $r->get('/_autostop')->to('static#geostop'); - $r->get('/_autotrain')->to('static#geotrain'); $r->get('/_datenschutz')->to('static#privacy'); @@ -367,24 +272,25 @@ sub startup { $r->get('/_impressum')->to('static#imprint'); + $r->get('/dyn/:av/autocomplete.js')->to('stationboard#autocomplete'); + $r->get('/_wr/:train/:departure')->to('wagenreihung#wagenreihung'); - $r->get('/wr/:train')->to('wagenreihung#zugbildung_db'); $r->get('/w/*wagon')->to('wagenreihung#wagen'); $r->get('/_ajax_mapinfo/:tripid/:lineno')->to('map#ajax_route'); $r->get('/map/:tripid/:lineno')->to('map#route'); - $r->get('/intersection/:trips')->to('map#intersection'); - $r->get('/z/:train/*station')->to('stationboard#station_train_details'); - $r->get('/z/:train')->to('stationboard#train_details'); - - $r->get('/map')->to('map#search_form'); - $r->get('/_trainsearch')->to('map#search'); + $r->get( '/z/:train/*station' => [ format => [ 'html', 'json' ] ] ) + ->to( 'stationboard#station_train_details', format => undef ) + ->name('train_at_station'); + $r->get( '/z/:train' => [ format => [ 'html', 'json' ] ] ) + ->to( 'stationboard#train_details', format => undef )->name('train'); $self->defaults( layout => 'app' ); $r->get('/')->to('stationboard#handle_request'); $r->get('/multi/*station')->to('stationboard#handle_request'); - $r->get('/*station')->to('stationboard#handle_request'); + $r->get( '/*station' => [ format => [ 'html', 'json' ] ] ) + ->to( 'stationboard#handle_request', format => undef ); $self->types->type( json => 'application/json; charset=utf-8' ); |