Loading lib/Travel/Status/DE/DBWagenreihung.pm +180 −167 Original line number Original line Diff line number Diff line Loading @@ -19,6 +19,8 @@ our $VERSION = '0.12'; Travel::Status::DE::DBWagenreihung->mk_ro_accessors( Travel::Status::DE::DBWagenreihung->mk_ro_accessors( qw(direction platform station train_no train_type)); qw(direction platform station train_no train_type)); # {{{ Rolling Stock Models my %is_redesign = ( my %is_redesign = ( "02" => 1, "02" => 1, "03" => 1, "03" => 1, Loading Loading @@ -103,6 +105,9 @@ my %power_desc = ( 99 => 'Sonderfahrzeug', 99 => 'Sonderfahrzeug', ); ); # }}} # {{{ Constructors sub new { sub new { my ( $class, %opt ) = @_; my ( $class, %opt ) = @_; Loading Loading @@ -184,69 +189,94 @@ sub get_wagonorder { return $self->parse_wagonorder; return $self->parse_wagonorder; } } sub parse_wagonorder { # }}} my ($self) = @_; # {{{ Internal Helpers $self->{platform} = $self->{data}{istformation}{halt}{gleisbezeichnung}; sub get_with_cache { my ( $self, $cache, $url ) = @_; $self->{station} = { if ( $self->{developer_mode} ) { ds100 => $self->{data}{istformation}{halt}{rl100}, say "GET $url"; eva => $self->{data}{istformation}{halt}{evanummer}, } name => $self->{data}{istformation}{halt}{bahnhofsname}, }; $self->{train_type} = $self->{data}{istformation}{zuggattung}; if ($cache) { $self->{train_no} = $self->{data}{istformation}{zugnummer}; my $content = $cache->thaw($url); if ($content) { if ( $self->{developer_mode} ) { say ' cache hit'; } return ( ${$content}, undef ); } } $self->parse_wagons; if ( $self->{developer_mode} ) { $self->{origins} = $self->parse_wings('startbetriebsstellename'); say ' cache miss'; $self->{destinations} = $self->parse_wings('zielbetriebsstellename'); } } sub errstr { my $ua = $self->{user_agent}; my ($self) = @_; my $res = $ua->get($url); return $self->{errstr}; if ( $res->is_error ) { return ( undef, $res->status_line ); } my $content = $res->decoded_content; if ($cache) { $cache->freeze( $url, \$content ); } return ( $content, undef ); } } sub TO_JSON { sub wagongroup_powertype { my ($self) = @_; my ( $self, @wagons ) = @_; # ensure that all objects are available if ( not @wagons ) { $self->origins; @wagons = $self->wagons; $self->destinations; } $self->train_numbers; $self->train_descriptions; $self->sections; my %copy = %{$self}; my %ml = map { $_ => 0 } ( 90 .. 99 ); delete $copy{from_json}; for my $wagon (@wagons) { return {%copy}; if ( not $wagon->uic_id or length( $wagon->uic_id ) != 12 ) { } next; } sub has_bad_wagons { my $wagon_type = substr( $wagon->uic_id, 0, 2 ); my ($self) = @_; if ( $wagon_type < 90 ) { next; } if ( defined $self->{has_bad_wagons} ) { $ml{$wagon_type}++; return $self->{has_bad_wagons}; } } for my $group ( @{ $self->{data}{istformation}{allFahrzeuggruppe} } ) { my @likelihood = reverse sort { $ml{$a} <=> $ml{$b} } keys %ml; for my $wagon ( @{ $group->{allFahrzeug} } ) { my $pos = $wagon->{positionamhalt}; if ( $ml{ $likelihood[0] } == 0 ) { if ( $pos->{startprozent} eq '' return undef; or $pos->{endeprozent} eq '' or $pos->{startmeter} eq '' or $pos->{endemeter} eq '' ) { return $self->{has_bad_wagons} = 1; } } } } return $self->{has_bad_wagons} = 0; return $likelihood[0]; } sub parse_wagonorder { my ($self) = @_; $self->{platform} = $self->{data}{istformation}{halt}{gleisbezeichnung}; $self->{station} = { ds100 => $self->{data}{istformation}{halt}{rl100}, eva => $self->{data}{istformation}{halt}{evanummer}, name => $self->{data}{istformation}{halt}{bahnhofsname}, }; $self->{train_type} = $self->{data}{istformation}{zuggattung}; $self->{train_no} = $self->{data}{istformation}{zugnummer}; $self->parse_wagons; $self->{origins} = $self->parse_wings('startbetriebsstellename'); $self->{destinations} = $self->parse_wings('zielbetriebsstellename'); } } sub parse_wings { sub parse_wings { Loading @@ -270,6 +300,63 @@ sub parse_wings { return \@names; return \@names; } } sub parse_wagons { my ($self) = @_; my @wagon_groups; for my $group ( @{ $self->{data}{istformation}{allFahrzeuggruppe} } ) { my @group; for my $wagon ( @{ $group->{allFahrzeug} } ) { my $wagon_object = Travel::Status::DE::DBWagenreihung::Wagon->new( %{$wagon}, train_no => $group->{verkehrlichezugnummer} ); push( @{ $self->{wagons} }, $wagon_object ); push( @group, $wagon_object ); if ( not $wagon_object->{position}{valid} ) { $self->{has_bad_wagons} = 1; } } push( @wagon_groups, [@group] ); } if ( @{ $self->{wagons} // [] } > 1 and not $self->has_bad_wagons ) { if ( $self->{wagons}[0]->{position}{start_percent} > $self->{wagons}[-1]->{position}{start_percent} ) { $self->{direction} = 100; } else { $self->{direction} = 0; } } if ( not $self->has_bad_wagons ) { @{ $self->{wagons} } = sort { $a->{position}->{start_percent} <=> $b->{position}->{start_percent} } @{ $self->{wagons} }; } for my $i ( 0 .. $#wagon_groups ) { my $group = $wagon_groups[$i]; my $tt = $self->wagongroup_subtype( @{$group} ); if ($tt) { for my $wagon ( @{$group} ) { $wagon->set_traintype( $i, $tt ); } } } $self->{wagongroups} = [@wagon_groups]; } # }}} # {{{ Public Functions sub errstr { my ($self) = @_; return $self->{errstr}; } sub destinations { sub destinations { my ($self) = @_; my ($self) = @_; Loading Loading @@ -309,6 +396,30 @@ sub sections { return @{ $self->{sections} // [] }; return @{ $self->{sections} // [] }; } } sub train_descriptions { my ($self) = @_; if ( exists $self->{train_descriptions} ) { return @{ $self->{train_descriptions} }; } for my $wagons ( @{ $self->{wagongroups} } ) { my ( $short, $desc ) = $self->wagongroup_description( @{$wagons} ); my @sections = uniq map { $_->section } @{$wagons}; push( @{ $self->{train_descriptions} }, { sections => [@sections], short => $short, text => $desc, } ); } return @{ $self->{train_descriptions} }; } sub train_numbers { sub train_numbers { my ($self) = @_; my ($self) = @_; Loading @@ -329,60 +440,27 @@ sub train_numbers { return @numbers; return @numbers; } } sub wagongroup_powertype { sub has_bad_wagons { my ( $self, @wagons ) = @_; if ( not @wagons ) { @wagons = $self->wagons; } my %ml = map { $_ => 0 } ( 90 .. 99 ); for my $wagon (@wagons) { if ( not $wagon->uic_id or length( $wagon->uic_id ) != 12 ) { next; } my $wagon_type = substr( $wagon->uic_id, 0, 2 ); if ( $wagon_type < 90 ) { next; } $ml{$wagon_type}++; } my @likelihood = reverse sort { $ml{$a} <=> $ml{$b} } keys %ml; if ( $ml{ $likelihood[0] } == 0 ) { return undef; } return $likelihood[0]; } sub train_descriptions { my ($self) = @_; my ($self) = @_; if ( exists $self->{train_descriptions} ) { if ( defined $self->{has_bad_wagons} ) { return @{ $self->{train_descriptions} }; return $self->{has_bad_wagons}; } } for my $wagons ( @{ $self->{wagongroups} } ) { for my $group ( @{ $self->{data}{istformation}{allFahrzeuggruppe} } ) { my ( $short, $desc ) = $self->wagongroup_description( @{$wagons} ); for my $wagon ( @{ $group->{allFahrzeug} } ) { my @sections = uniq map { $_->section } @{$wagons}; my $pos = $wagon->{positionamhalt}; if ( $pos->{startprozent} eq '' push( or $pos->{endeprozent} eq '' @{ $self->{train_descriptions} }, or $pos->{startmeter} eq '' or $pos->{endemeter} eq '' ) { { sections => [@sections], return $self->{has_bad_wagons} = 1; short => $short, text => $desc, } } ); } } } return @{ $self->{train_descriptions} }; return $self->{has_bad_wagons} = 0; } } sub wagongroup_description { sub wagongroup_description { Loading Loading @@ -635,89 +713,24 @@ sub wagons { return @{ $self->{wagons} // [] }; return @{ $self->{wagons} // [] }; } } sub parse_wagons { sub TO_JSON { my ($self) = @_; my ($self) = @_; my @wagon_groups; # ensure that all objects are available $self->origins; $self->destinations; $self->train_numbers; $self->train_descriptions; $self->sections; for my $group ( @{ $self->{data}{istformation}{allFahrzeuggruppe} } ) { my %copy = %{$self}; my @group; for my $wagon ( @{ $group->{allFahrzeug} } ) { my $wagon_object = Travel::Status::DE::DBWagenreihung::Wagon->new( %{$wagon}, train_no => $group->{verkehrlichezugnummer} ); push( @{ $self->{wagons} }, $wagon_object ); push( @group, $wagon_object ); if ( not $wagon_object->{position}{valid} ) { $self->{has_bad_wagons} = 1; } } push( @wagon_groups, [@group] ); } if ( @{ $self->{wagons} // [] } > 1 and not $self->has_bad_wagons ) { if ( $self->{wagons}[0]->{position}{start_percent} > $self->{wagons}[-1]->{position}{start_percent} ) { $self->{direction} = 100; } else { $self->{direction} = 0; } } if ( not $self->has_bad_wagons ) { @{ $self->{wagons} } = sort { $a->{position}->{start_percent} <=> $b->{position}->{start_percent} } @{ $self->{wagons} }; } for my $i ( 0 .. $#wagon_groups ) { delete $copy{from_json}; my $group = $wagon_groups[$i]; my $tt = $self->wagongroup_subtype( @{$group} ); if ($tt) { for my $wagon ( @{$group} ) { $wagon->set_traintype( $i, $tt ); } } } $self->{wagongroups} = [@wagon_groups]; return {%copy}; } } sub get_with_cache { # }}} my ( $self, $cache, $url ) = @_; if ( $self->{developer_mode} ) { say "GET $url"; } if ($cache) { my $content = $cache->thaw($url); if ($content) { if ( $self->{developer_mode} ) { say ' cache hit'; } return ( ${$content}, undef ); } } if ( $self->{developer_mode} ) { say ' cache miss'; } my $ua = $self->{user_agent}; my $res = $ua->get($url); if ( $res->is_error ) { return ( undef, $res->status_line ); } my $content = $res->decoded_content; if ($cache) { $cache->freeze( $url, \$content ); } return ( $content, undef ); } 1; 1; Loading Loading
lib/Travel/Status/DE/DBWagenreihung.pm +180 −167 Original line number Original line Diff line number Diff line Loading @@ -19,6 +19,8 @@ our $VERSION = '0.12'; Travel::Status::DE::DBWagenreihung->mk_ro_accessors( Travel::Status::DE::DBWagenreihung->mk_ro_accessors( qw(direction platform station train_no train_type)); qw(direction platform station train_no train_type)); # {{{ Rolling Stock Models my %is_redesign = ( my %is_redesign = ( "02" => 1, "02" => 1, "03" => 1, "03" => 1, Loading Loading @@ -103,6 +105,9 @@ my %power_desc = ( 99 => 'Sonderfahrzeug', 99 => 'Sonderfahrzeug', ); ); # }}} # {{{ Constructors sub new { sub new { my ( $class, %opt ) = @_; my ( $class, %opt ) = @_; Loading Loading @@ -184,69 +189,94 @@ sub get_wagonorder { return $self->parse_wagonorder; return $self->parse_wagonorder; } } sub parse_wagonorder { # }}} my ($self) = @_; # {{{ Internal Helpers $self->{platform} = $self->{data}{istformation}{halt}{gleisbezeichnung}; sub get_with_cache { my ( $self, $cache, $url ) = @_; $self->{station} = { if ( $self->{developer_mode} ) { ds100 => $self->{data}{istformation}{halt}{rl100}, say "GET $url"; eva => $self->{data}{istformation}{halt}{evanummer}, } name => $self->{data}{istformation}{halt}{bahnhofsname}, }; $self->{train_type} = $self->{data}{istformation}{zuggattung}; if ($cache) { $self->{train_no} = $self->{data}{istformation}{zugnummer}; my $content = $cache->thaw($url); if ($content) { if ( $self->{developer_mode} ) { say ' cache hit'; } return ( ${$content}, undef ); } } $self->parse_wagons; if ( $self->{developer_mode} ) { $self->{origins} = $self->parse_wings('startbetriebsstellename'); say ' cache miss'; $self->{destinations} = $self->parse_wings('zielbetriebsstellename'); } } sub errstr { my $ua = $self->{user_agent}; my ($self) = @_; my $res = $ua->get($url); return $self->{errstr}; if ( $res->is_error ) { return ( undef, $res->status_line ); } my $content = $res->decoded_content; if ($cache) { $cache->freeze( $url, \$content ); } return ( $content, undef ); } } sub TO_JSON { sub wagongroup_powertype { my ($self) = @_; my ( $self, @wagons ) = @_; # ensure that all objects are available if ( not @wagons ) { $self->origins; @wagons = $self->wagons; $self->destinations; } $self->train_numbers; $self->train_descriptions; $self->sections; my %copy = %{$self}; my %ml = map { $_ => 0 } ( 90 .. 99 ); delete $copy{from_json}; for my $wagon (@wagons) { return {%copy}; if ( not $wagon->uic_id or length( $wagon->uic_id ) != 12 ) { } next; } sub has_bad_wagons { my $wagon_type = substr( $wagon->uic_id, 0, 2 ); my ($self) = @_; if ( $wagon_type < 90 ) { next; } if ( defined $self->{has_bad_wagons} ) { $ml{$wagon_type}++; return $self->{has_bad_wagons}; } } for my $group ( @{ $self->{data}{istformation}{allFahrzeuggruppe} } ) { my @likelihood = reverse sort { $ml{$a} <=> $ml{$b} } keys %ml; for my $wagon ( @{ $group->{allFahrzeug} } ) { my $pos = $wagon->{positionamhalt}; if ( $ml{ $likelihood[0] } == 0 ) { if ( $pos->{startprozent} eq '' return undef; or $pos->{endeprozent} eq '' or $pos->{startmeter} eq '' or $pos->{endemeter} eq '' ) { return $self->{has_bad_wagons} = 1; } } } } return $self->{has_bad_wagons} = 0; return $likelihood[0]; } sub parse_wagonorder { my ($self) = @_; $self->{platform} = $self->{data}{istformation}{halt}{gleisbezeichnung}; $self->{station} = { ds100 => $self->{data}{istformation}{halt}{rl100}, eva => $self->{data}{istformation}{halt}{evanummer}, name => $self->{data}{istformation}{halt}{bahnhofsname}, }; $self->{train_type} = $self->{data}{istformation}{zuggattung}; $self->{train_no} = $self->{data}{istformation}{zugnummer}; $self->parse_wagons; $self->{origins} = $self->parse_wings('startbetriebsstellename'); $self->{destinations} = $self->parse_wings('zielbetriebsstellename'); } } sub parse_wings { sub parse_wings { Loading @@ -270,6 +300,63 @@ sub parse_wings { return \@names; return \@names; } } sub parse_wagons { my ($self) = @_; my @wagon_groups; for my $group ( @{ $self->{data}{istformation}{allFahrzeuggruppe} } ) { my @group; for my $wagon ( @{ $group->{allFahrzeug} } ) { my $wagon_object = Travel::Status::DE::DBWagenreihung::Wagon->new( %{$wagon}, train_no => $group->{verkehrlichezugnummer} ); push( @{ $self->{wagons} }, $wagon_object ); push( @group, $wagon_object ); if ( not $wagon_object->{position}{valid} ) { $self->{has_bad_wagons} = 1; } } push( @wagon_groups, [@group] ); } if ( @{ $self->{wagons} // [] } > 1 and not $self->has_bad_wagons ) { if ( $self->{wagons}[0]->{position}{start_percent} > $self->{wagons}[-1]->{position}{start_percent} ) { $self->{direction} = 100; } else { $self->{direction} = 0; } } if ( not $self->has_bad_wagons ) { @{ $self->{wagons} } = sort { $a->{position}->{start_percent} <=> $b->{position}->{start_percent} } @{ $self->{wagons} }; } for my $i ( 0 .. $#wagon_groups ) { my $group = $wagon_groups[$i]; my $tt = $self->wagongroup_subtype( @{$group} ); if ($tt) { for my $wagon ( @{$group} ) { $wagon->set_traintype( $i, $tt ); } } } $self->{wagongroups} = [@wagon_groups]; } # }}} # {{{ Public Functions sub errstr { my ($self) = @_; return $self->{errstr}; } sub destinations { sub destinations { my ($self) = @_; my ($self) = @_; Loading Loading @@ -309,6 +396,30 @@ sub sections { return @{ $self->{sections} // [] }; return @{ $self->{sections} // [] }; } } sub train_descriptions { my ($self) = @_; if ( exists $self->{train_descriptions} ) { return @{ $self->{train_descriptions} }; } for my $wagons ( @{ $self->{wagongroups} } ) { my ( $short, $desc ) = $self->wagongroup_description( @{$wagons} ); my @sections = uniq map { $_->section } @{$wagons}; push( @{ $self->{train_descriptions} }, { sections => [@sections], short => $short, text => $desc, } ); } return @{ $self->{train_descriptions} }; } sub train_numbers { sub train_numbers { my ($self) = @_; my ($self) = @_; Loading @@ -329,60 +440,27 @@ sub train_numbers { return @numbers; return @numbers; } } sub wagongroup_powertype { sub has_bad_wagons { my ( $self, @wagons ) = @_; if ( not @wagons ) { @wagons = $self->wagons; } my %ml = map { $_ => 0 } ( 90 .. 99 ); for my $wagon (@wagons) { if ( not $wagon->uic_id or length( $wagon->uic_id ) != 12 ) { next; } my $wagon_type = substr( $wagon->uic_id, 0, 2 ); if ( $wagon_type < 90 ) { next; } $ml{$wagon_type}++; } my @likelihood = reverse sort { $ml{$a} <=> $ml{$b} } keys %ml; if ( $ml{ $likelihood[0] } == 0 ) { return undef; } return $likelihood[0]; } sub train_descriptions { my ($self) = @_; my ($self) = @_; if ( exists $self->{train_descriptions} ) { if ( defined $self->{has_bad_wagons} ) { return @{ $self->{train_descriptions} }; return $self->{has_bad_wagons}; } } for my $wagons ( @{ $self->{wagongroups} } ) { for my $group ( @{ $self->{data}{istformation}{allFahrzeuggruppe} } ) { my ( $short, $desc ) = $self->wagongroup_description( @{$wagons} ); for my $wagon ( @{ $group->{allFahrzeug} } ) { my @sections = uniq map { $_->section } @{$wagons}; my $pos = $wagon->{positionamhalt}; if ( $pos->{startprozent} eq '' push( or $pos->{endeprozent} eq '' @{ $self->{train_descriptions} }, or $pos->{startmeter} eq '' or $pos->{endemeter} eq '' ) { { sections => [@sections], return $self->{has_bad_wagons} = 1; short => $short, text => $desc, } } ); } } } return @{ $self->{train_descriptions} }; return $self->{has_bad_wagons} = 0; } } sub wagongroup_description { sub wagongroup_description { Loading Loading @@ -635,89 +713,24 @@ sub wagons { return @{ $self->{wagons} // [] }; return @{ $self->{wagons} // [] }; } } sub parse_wagons { sub TO_JSON { my ($self) = @_; my ($self) = @_; my @wagon_groups; # ensure that all objects are available $self->origins; $self->destinations; $self->train_numbers; $self->train_descriptions; $self->sections; for my $group ( @{ $self->{data}{istformation}{allFahrzeuggruppe} } ) { my %copy = %{$self}; my @group; for my $wagon ( @{ $group->{allFahrzeug} } ) { my $wagon_object = Travel::Status::DE::DBWagenreihung::Wagon->new( %{$wagon}, train_no => $group->{verkehrlichezugnummer} ); push( @{ $self->{wagons} }, $wagon_object ); push( @group, $wagon_object ); if ( not $wagon_object->{position}{valid} ) { $self->{has_bad_wagons} = 1; } } push( @wagon_groups, [@group] ); } if ( @{ $self->{wagons} // [] } > 1 and not $self->has_bad_wagons ) { if ( $self->{wagons}[0]->{position}{start_percent} > $self->{wagons}[-1]->{position}{start_percent} ) { $self->{direction} = 100; } else { $self->{direction} = 0; } } if ( not $self->has_bad_wagons ) { @{ $self->{wagons} } = sort { $a->{position}->{start_percent} <=> $b->{position}->{start_percent} } @{ $self->{wagons} }; } for my $i ( 0 .. $#wagon_groups ) { delete $copy{from_json}; my $group = $wagon_groups[$i]; my $tt = $self->wagongroup_subtype( @{$group} ); if ($tt) { for my $wagon ( @{$group} ) { $wagon->set_traintype( $i, $tt ); } } } $self->{wagongroups} = [@wagon_groups]; return {%copy}; } } sub get_with_cache { # }}} my ( $self, $cache, $url ) = @_; if ( $self->{developer_mode} ) { say "GET $url"; } if ($cache) { my $content = $cache->thaw($url); if ($content) { if ( $self->{developer_mode} ) { say ' cache hit'; } return ( ${$content}, undef ); } } if ( $self->{developer_mode} ) { say ' cache miss'; } my $ua = $self->{user_agent}; my $res = $ua->get($url); if ( $res->is_error ) { return ( undef, $res->status_line ); } my $content = $res->decoded_content; if ($cache) { $cache->freeze( $url, \$content ); } return ( $content, undef ); } 1; 1; Loading