diff options
-rwxr-xr-x | bin/dbwagenreihung | 66 | ||||
-rw-r--r-- | lib/Travel/Status/DE/DBWagenreihung.pm | 70 | ||||
-rw-r--r-- | lib/Travel/Status/DE/DBWagenreihung/Section.pm | 2 | ||||
-rw-r--r-- | lib/Travel/Status/DE/DBWagenreihung/Wagon.pm | 39 |
4 files changed, 95 insertions, 82 deletions
diff --git a/bin/dbwagenreihung b/bin/dbwagenreihung index dafefe3..fe0961a 100755 --- a/bin/dbwagenreihung +++ b/bin/dbwagenreihung @@ -10,90 +10,94 @@ use List::Util qw(min); use Travel::Status::DE::IRIS; use Travel::Status::DE::DBWagenreihung; -my ($station, $train_number) = @ARGV; +my ( $station, $train_number ) = @ARGV; -my $res = Travel::Status::DE::IRIS->new(station => $station, with_related => 1); +my $res = Travel::Status::DE::IRIS->new( + station => $station, + with_related => 1 +); -if ($res->errstr) { +if ( $res->errstr ) { say STDERR $res->errstr; exit 1; } my @trains = grep { $_->train_no eq $train_number } $res->results; -if (@trains != 1) { +if ( @trains != 1 ) { say STDERR "Unable to find train in reported departures"; exit 1; } my $wr = Travel::Status::DE::DBWagenreihung->new( - departure => $trains[0]->sched_departure, + departure => $trains[0]->sched_departure, developer_mode => 1, - train_number => $train_number, + train_number => $train_number, ); -for my $section ($wr->sections) { +for my $section ( $wr->sections ) { my $section_length = $section->length_percent; - my $spacing_left = int(($section_length - 2) / 2) - 1; - my $spacing_right = int(($section_length - 2) / 2); + my $spacing_left = int( ( $section_length - 2 ) / 2 ) - 1; + my $spacing_right = int( ( $section_length - 2 ) / 2 ); - if ($section_length % 2) { + if ( $section_length % 2 ) { $spacing_left++; } - printf("|%s%s%s|", + printf( "|%s%s%s|", ' ' x $spacing_left, $section->name, - ' ' x $spacing_right - ); + ' ' x $spacing_right ); } print "\n"; my @start_percentages = map { $_->{position}{start_percent} } $wr->wagons; -print ' ' x ((min @start_percentages) - 1); +print ' ' x ( ( min @start_percentages ) - 1 ); print '['; -for my $wagon ($wr->wagons) { - my $wagon_length = $wagon->{position}->{end_percent} - $wagon->{position}->{start_percent}; - my $spacing_left = int($wagon_length / 2) - 2; - my $spacing_right = int($wagon_length / 2) - 1; +for my $wagon ( $wr->wagons ) { + my $wagon_length + = $wagon->{position}->{end_percent} - $wagon->{position}->{start_percent}; + my $spacing_left = int( $wagon_length / 2 ) - 2; + my $spacing_right = int( $wagon_length / 2 ) - 1; - if ($wagon_length % 2) { + if ( $wagon_length % 2 ) { $spacing_left++; } my $wagon_desc = $wagon->number || '?'; - if ($wagon->is_locomotive or $wagon->is_powercar) { + if ( $wagon->is_locomotive or $wagon->is_powercar ) { $wagon_desc = '<->'; } - printf("%s%3s%s", ' ' x $spacing_left, $wagon_desc, ' ' x $spacing_right); + printf( "%s%3s%s", ' ' x $spacing_left, $wagon_desc, ' ' x $spacing_right ); } print "]\n"; -print ' ' x (min @start_percentages); -for my $wagon ($wr->wagons) { - my $wagon_length = $wagon->{position}->{end_percent} - $wagon->{position}->{start_percent}; - my $spacing_left = int($wagon_length / 2) - 2; - my $spacing_right = int($wagon_length / 2) - 1; +print ' ' x ( min @start_percentages ); +for my $wagon ( $wr->wagons ) { + my $wagon_length + = $wagon->{position}->{end_percent} - $wagon->{position}->{start_percent}; + my $spacing_left = int( $wagon_length / 2 ) - 2; + my $spacing_right = int( $wagon_length / 2 ) - 1; - if ($wagon_length % 2) { + if ( $wagon_length % 2 ) { $spacing_left++; } my $class = ''; - if ($wagon->class_type == 1) { + if ( $wagon->class_type == 1 ) { $class = ' 1 '; } - elsif ($wagon->class_type == 2) { + elsif ( $wagon->class_type == 2 ) { $class = ' 2 '; } - elsif ($wagon->class_type == 12) { + elsif ( $wagon->class_type == 12 ) { $class = '1/2'; } - printf("%s%3s%s", ' ' x $spacing_left, $class, ' ' x $spacing_right); + printf( "%s%3s%s", ' ' x $spacing_left, $class, ' ' x $spacing_right ); } print "\n"; diff --git a/lib/Travel/Status/DE/DBWagenreihung.pm b/lib/Travel/Status/DE/DBWagenreihung.pm index 608702a..c879d14 100644 --- a/lib/Travel/Status/DE/DBWagenreihung.pm +++ b/lib/Travel/Status/DE/DBWagenreihung.pm @@ -24,15 +24,15 @@ sub new { } my $self = { - api_base => $opt{api_base} + api_base => $opt{api_base} // 'https://www.apps-bahn.de/wr/wagenreihung/1.0', developer_mode => $opt{developer_mode}, - cache => $opt{cache}, - departure => $opt{departure}, - json => JSON->new, - serializable => $opt{serializable}, - train_number => $opt{train_number}, - user_agent => $opt{user_agent}, + cache => $opt{cache}, + departure => $opt{departure}, + json => JSON->new, + serializable => $opt{serializable}, + train_number => $opt{train_number}, + user_agent => $opt{user_agent}, }; bless( $self, $class ); @@ -51,17 +51,19 @@ sub new { sub get_wagonorder { my ($self) = @_; - my $api_base = $self->{api_base}; - my $cache = $self->{cache}; + my $api_base = $self->{api_base}; + my $cache = $self->{cache}; my $train_number = $self->{train_number}; my $datetime = $self->{departure}; - if (ref($datetime) eq 'DateTime') { + if ( ref($datetime) eq 'DateTime' ) { $datetime = $datetime->strftime('%Y%m%d%H%M'); } - my ($content, $err) = $self->get_with_cache($cache, "${api_base}/${train_number}/${datetime}"); + my ( $content, $err ) + = $self->get_with_cache( $cache, + "${api_base}/${train_number}/${datetime}" ); if ($err) { $self->{errstr} = "Failed to fetch station data: $err"; @@ -69,7 +71,7 @@ sub get_wagonorder { } my $json = $self->{json}->decode($content); - if (exists $json->{error}) { + if ( exists $json->{error} ) { $self->{errstr} = 'Backend error: ' . $json->{error}{msg}; return; } @@ -87,38 +89,46 @@ sub error { sub sections { my ($self) = @_; - if (exists $self->{sections}) { - return @{$self->{sections}}; + if ( exists $self->{sections} ) { + return @{ $self->{sections} }; } - for my $section (@{$self->{data}{istformation}{halt}{allSektor}}) { + for my $section ( @{ $self->{data}{istformation}{halt}{allSektor} } ) { my $pos = $section->{positionamgleis}; - push(@{$self->{sections}}, Travel::Status::DE::DBWagenreihung::Section->new( - name => $section->{sektorbezeichnung}, - start_percent => $pos->{startprozent}, - end_percent => $pos->{endeprozent}, - start_meters => $pos->{startmeter}, - end_meters => $pos->{endemeter}, - )); + push( + @{ $self->{sections} }, + Travel::Status::DE::DBWagenreihung::Section->new( + name => $section->{sektorbezeichnung}, + start_percent => $pos->{startprozent}, + end_percent => $pos->{endeprozent}, + start_meters => $pos->{startmeter}, + end_meters => $pos->{endemeter}, + ) + ); } - return @{$self->{sections} // []}; + return @{ $self->{sections} // [] }; } sub wagons { my ($self) = @_; - if (exists $self->{wagons}) { - return @{$self->{wagons}}; + if ( exists $self->{wagons} ) { + return @{ $self->{wagons} }; } - for my $group (@{$self->{data}{istformation}{allFahrzeuggruppe}}) { - for my $wagon (@{$group->{allFahrzeug}}) { - push(@{$self->{wagons}}, Travel::Status::DE::DBWagenreihung::Wagon->new(%{$wagon})); + for my $group ( @{ $self->{data}{istformation}{allFahrzeuggruppe} } ) { + for my $wagon ( @{ $group->{allFahrzeug} } ) { + push( + @{ $self->{wagons} }, + Travel::Status::DE::DBWagenreihung::Wagon->new( %{$wagon} ) + ); } } - @{$self->{wagons}} = sort { $a->{position}->{start_percent} <=> $b->{position}->{start_percent} } @{$self->{wagons}}; - return @{$self->{wagons} // []}; + @{ $self->{wagons} } = sort { + $a->{position}->{start_percent} <=> $b->{position}->{start_percent} + } @{ $self->{wagons} }; + return @{ $self->{wagons} // [] }; } sub get_with_cache { diff --git a/lib/Travel/Status/DE/DBWagenreihung/Section.pm b/lib/Travel/Status/DE/DBWagenreihung/Section.pm index 780031c..1c3bdf8 100644 --- a/lib/Travel/Status/DE/DBWagenreihung/Section.pm +++ b/lib/Travel/Status/DE/DBWagenreihung/Section.pm @@ -17,7 +17,7 @@ sub new { my ( $obj, %opt ) = @_; my $ref = \%opt; - $ref->{length_meters} = $ref->{end_meters} - $ref->{start_meters}; + $ref->{length_meters} = $ref->{end_meters} - $ref->{start_meters}; $ref->{length_percent} = $ref->{end_percent} - $ref->{start_percent}; return bless( $ref, $obj ); diff --git a/lib/Travel/Status/DE/DBWagenreihung/Wagon.pm b/lib/Travel/Status/DE/DBWagenreihung/Wagon.pm index ed1b87a..850bc84 100644 --- a/lib/Travel/Status/DE/DBWagenreihung/Wagon.pm +++ b/lib/Travel/Status/DE/DBWagenreihung/Wagon.pm @@ -11,47 +11,46 @@ use Carp qw(cluck); our $VERSION = '0.00'; Travel::Status::DE::DBWagenreihung::Wagon->mk_ro_accessors( - qw(class_type has_bistro is_locomotive is_powercar number section) -); + qw(class_type has_bistro is_locomotive is_powercar number section)); sub new { my ( $obj, %opt ) = @_; my $ref = {}; - $ref->{class_type} = 0; - $ref->{has_bistro} = 0; + $ref->{class_type} = 0; + $ref->{has_bistro} = 0; $ref->{is_locomotive} = 0; - $ref->{is_powercar} = 0; - $ref->{number} = $opt{wagenordnungsnummer}; - $ref->{section} = $opt{fahrzeugsektor}; - $ref->{type} = $opt{fahrzeugtyp}; + $ref->{is_powercar} = 0; + $ref->{number} = $opt{wagenordnungsnummer}; + $ref->{section} = $opt{fahrzeugsektor}; + $ref->{type} = $opt{fahrzeugtyp}; - if ($opt{kategorie} =~ m{SPEISEWAGEN}) { + if ( $opt{kategorie} =~ m{SPEISEWAGEN} ) { $ref->{has_bistro} = 1; } - elsif ($opt{kategorie} eq 'LOK') { + elsif ( $opt{kategorie} eq 'LOK' ) { $ref->{is_locomotive} = 1; } - elsif ($opt{kategorie} eq 'TRIEBKOPF') { + elsif ( $opt{kategorie} eq 'TRIEBKOPF' ) { $ref->{is_powercar} = 1; } - if ($opt{fahrzeugtyp} =~ m{AB}) { + if ( $opt{fahrzeugtyp} =~ m{AB} ) { $ref->{class_type} = 12; } - elsif ($opt{fahrzeugtyp} =~ m{A}) { + elsif ( $opt{fahrzeugtyp} =~ m{A} ) { $ref->{class_type} = 1; } - elsif ($opt{fahrzeugtyp} =~ m{B|WR}) { + elsif ( $opt{fahrzeugtyp} =~ m{B|WR} ) { $ref->{class_type} = 2; } my $pos = $opt{positionamhalt}; $ref->{position}{start_percent} = $pos->{startprozent}; - $ref->{position}{end_percent} = $pos->{endeprozent}; - $ref->{position}{start_meters} = $pos->{startmeter}; - $ref->{position}{end_meters} = $pos->{endemeter}; + $ref->{position}{end_percent} = $pos->{endeprozent}; + $ref->{position}{start_meters} = $pos->{startmeter}; + $ref->{position}{end_meters} = $pos->{endemeter}; return bless( $ref, $obj ); } @@ -59,7 +58,7 @@ sub new { sub is_first_class { my ($self) = @_; - if ($self->{type} =~ m{^A}) { + if ( $self->{type} =~ m{^A} ) { return 1; } return 0; @@ -68,7 +67,7 @@ sub is_first_class { sub is_second_class { my ($self) = @_; - if ($self->{type} =~ m{^A?B}) { + if ( $self->{type} =~ m{^A?B} ) { return 1; } return 0; @@ -77,7 +76,7 @@ sub is_second_class { sub sections { my ($self) = @_; - return @{$self->{sections}}; + return @{ $self->{sections} }; } sub TO_JSON { |