From a6fab60716a8aba9030d60bc821545e7c41f1a46 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 25 Nov 2018 19:49:43 +0100 Subject: add locomotive/powercar flag and positions --- bin/dbwagenreihung | 69 +++++++++++++++++++++++++--- lib/Travel/Status/DE/DBWagenreihung.pm | 25 ++++++++-- lib/Travel/Status/DE/DBWagenreihung/Wagon.pm | 31 +++++++++---- 3 files changed, 108 insertions(+), 17 deletions(-) diff --git a/bin/dbwagenreihung b/bin/dbwagenreihung index 1d842fa..0d75fd7 100644 --- a/bin/dbwagenreihung +++ b/bin/dbwagenreihung @@ -6,6 +6,7 @@ use utf8; our $VERSION = '0.00'; +use List::Util qw(min); use Travel::Status::DE::IRIS; use Travel::Status::DE::DBWagenreihung; @@ -27,16 +28,72 @@ if (@trains != 1) { my $wr = Travel::Status::DE::DBWagenreihung->new( departure => $trains[0]->sched_departure, + developer_mode => 1, train_number => $train_number, ); +for my $section ($wr->sections) { + my $section_length = $section->{end_percent} - $section->{start_percent}; + my $spacing_left = int(($section_length - 2) / 2) - 1; + my $spacing_right = int(($section_length - 2) / 2); + + if ($section_length % 2) { + $spacing_left++; + } + + printf("|%s%s%s|", + ' ' x $spacing_left, + $section->{name}, + ' ' x $spacing_right + ); +} +print "\n"; + +my @start_percentages = map { $_->{position}{start_percent} } $wr->wagons; +print ' ' x ((min @start_percentages) - 1); +print '['; + for my $wagon ($wr->wagons) { - printf("%s %2s", $wagon->section, $wagon->number || 'X'); - if ($wagon->class_type == 12) { - print(" 1/2"); + 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) { + $spacing_left++; } - elsif ($wagon->class_type) { - printf(" %d", $wagon->class_type); + + my $wagon_desc = $wagon->number || '?'; + + if ($wagon->is_locomotive or $wagon->is_powercar) { + $wagon_desc = '<->'; } - print("\n"); + + 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; + + if ($wagon_length % 2) { + $spacing_left++; + } + + my $class = ''; + + if ($wagon->class_type == 1) { + $class = ' 1 '; + } + elsif ($wagon->class_type == 2) { + $class = ' 2 '; + } + elsif ($wagon->class_type == 12) { + $class = '1/2'; + } + + 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 68dbd69..b97e2bd 100644 --- a/lib/Travel/Status/DE/DBWagenreihung.pm +++ b/lib/Travel/Status/DE/DBWagenreihung.pm @@ -67,9 +67,6 @@ sub get_wagonorder { return; } my $json = $self->{json}->decode($content); - if ($self->{developer_mode}) { - say $self->{json}->pretty->encode($json); - } if (exists $json->{error}) { $self->{errstr} = 'Backend error: ' . $json->{error}{msg}; @@ -86,6 +83,27 @@ sub error { return $self->{errstr}; } +sub sections { + my ($self) = @_; + + if (exists $self->{sections}) { + return @{$self->{sections}}; + } + + for my $section (@{$self->{data}{istformation}{halt}{allSektor}}) { + my $pos = $section->{positionamgleis}; + push(@{$self->{sections}}, { + name => $section->{sektorbezeichnung}, + start_percent => $pos->{startprozent}, + end_percent => $pos->{endeprozent}, + start_meters => $pos->{startmeter}, + end_meters => $pos->{endemeter}, + }); + } + + return @{$self->{sections} // []}; +} + sub wagons { my ($self) = @_; @@ -98,6 +116,7 @@ sub wagons { 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} // []}; } diff --git a/lib/Travel/Status/DE/DBWagenreihung/Wagon.pm b/lib/Travel/Status/DE/DBWagenreihung/Wagon.pm index a29d42a..0cda4f3 100644 --- a/lib/Travel/Status/DE/DBWagenreihung/Wagon.pm +++ b/lib/Travel/Status/DE/DBWagenreihung/Wagon.pm @@ -11,32 +11,47 @@ use Carp qw(cluck); our $VERSION = '0.00'; Travel::Status::DE::DBWagenreihung::Wagon->mk_ro_accessors( - qw(class_type has_bistro number section) + qw(class_type has_bistro is_locomotive is_powercar number section) ); sub new { my ( $obj, %opt ) = @_; - my $ref = \%opt; + my $ref = {}; $ref->{class_type} = 0; $ref->{has_bistro} = 0; - $ref->{number} = $ref->{wagenordnungsnummer}; - $ref->{section} = $ref->{fahrzeugsektor}; + $ref->{is_locomotive} = 0; + $ref->{is_powercar} = 0; + $ref->{number} = $opt{wagenordnungsnummer}; + $ref->{section} = $opt{fahrzeugsektor}; - if ($ref->{kategorie} =~ m{SPEISEWAGEN}) { + if ($opt{kategorie} =~ m{SPEISEWAGEN}) { $ref->{has_bistro} = 1; } + elsif ($opt{kategorie} eq 'LOK') { + $ref->{is_locomotive} = 1; + } + elsif ($opt{kategorie} eq 'TRIEBKOPF') { + $ref->{is_powercar} = 1; + } - if ($ref->{fahrzeugtyp} =~ m{^AB}) { + if ($opt{fahrzeugtyp} =~ m{AB}) { $ref->{class_type} = 12; } - elsif ($ref->{fahrzeugtyp} =~ m{^A}) { + elsif ($opt{fahrzeugtyp} =~ m{A}) { $ref->{class_type} = 1; } - elsif ($ref->{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}; + return bless( $ref, $obj ); } -- cgit v1.2.3