HAFAS/Connection/Section.pm untested

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 179 CC 10 SCAR 31.3
CC
10
Cov
0%
new
56.0
TO_JSON
12.0
messages
6.0
CRAP 22.8
1 package Travel::Routing::DE::HAFAS::Connection::Section;
2  
3 # vim:foldmethod=marker
4  
5 use strict;
6 use warnings;
7 use 5.014;
8  
9 use parent 'Class::Accessor';
10 use DateTime::Duration;
11 use Travel::Routing::DE::HAFAS::Utils;
12 use Travel::Status::DE::HAFAS::Journey;
13  
14 our $VERSION = '0.11';
15  
16 Travel::Routing::DE::HAFAS::Connection::Section->mk_ro_accessors(
17 qw(type schep_dep rt_dep sched_arr rt_arr dep arr arr_delay dep_delay journey distance duration transfer_duration dep_loc arr_loc
18 dep_platform arr_platform dep_cancelled arr_cancelled
19 load)
20 );
21  
22 # {{{ Constructor
23  
24 sub new {
25 my ( $obj, %opt ) = @_;
26  
27 my $hafas = $opt{hafas};
28 my $sec = $opt{sec};
29 my $date = $opt{date};
30 my $locL = $opt{locL};
31 my $prodL = $opt{prodL};
32  
33 # himL may only be present in departure monitor mode
34 my @remL = @{ $opt{common}{remL} // [] };
35 my @himL = @{ $opt{common}{himL} // [] };
36  
37 my @msgL = (
38 @{ $sec->{dep}{msgL} // [] },
39 @{ $sec->{arr}{msgL} // [] },
40 @{ $sec->{jny}{msgL} // [] }
41 );
42  
43 my @messages;
44 for my $msg (@msgL) {
45 if ( $msg->{type} eq 'REM' and defined $msg->{remX} ) {
46 push( @messages, $hafas->add_message( $remL[ $msg->{remX} ] ) );
47 }
48 elsif ( $msg->{type} eq 'HIM' and defined $msg->{himX} ) {
49 push( @messages, $hafas->add_message( $himL[ $msg->{himX} ], 1 ) );
50 }
51 else {
52 say "Unknown message type $msg->{type}";
53 }
54 }
55  
56 my $strptime = DateTime::Format::Strptime->new(
57 pattern => '%Y%m%dT%H%M%S',
58 time_zone => 'Europe/Berlin'
59 );
60  
61 my $sched_dep = handle_day_change(
62 date => $date,
63 time => $sec->{dep}{dTimeS},
64 offset => $sec->{dep}{dTZOffset},
65 strp_obj => $strptime,
66 );
67 my $rt_dep = handle_day_change(
68 date => $date,
69 time => $sec->{dep}{dTimeR},
70 offset => $sec->{dep}{dTZOffset},
71 strp_obj => $strptime,
72 );
73 my $sched_arr = handle_day_change(
74 date => $date,
75 time => $sec->{arr}{aTimeS},
76 offset => $sec->{arr}{aTZOffset},
77 strp_obj => $strptime,
78 );
79 my $rt_arr = handle_day_change(
80 date => $date,
81 time => $sec->{arr}{aTimeR},
82 offset => $sec->{arr}{aTZOffset},
83 strp_obj => $strptime,
84 );
85  
86 my $tco = {};
87 for my $tco_id ( @{ $sec->{jny}{dTrnCmpSX}{tcocX} // [] } ) {
88 my $tco_kv = $opt{common}{tcocL}[$tco_id];
89 $tco->{ $tco_kv->{c} } = $tco_kv->{r};
90 }
91  
92 my $ref = {
93 type => $sec->{type},
94 sched_dep => $sched_dep,
95 rt_dep => $rt_dep,
96 sched_arr => $sched_arr,
97 rt_arr => $rt_arr,
98 dep => $rt_dep // $sched_dep,
99 arr => $rt_arr // $sched_arr,
100 dep_loc => $locL->[ $sec->{dep}{locX} ],
101 arr_loc => $locL->[ $sec->{arr}{locX} ],
102 dep_platform => $sec->{dep}{dplatfR} // $sec->{dep}{dPlatfS},
103 arr_platform => $sec->{arr}{aplatfR} // $sec->{arr}{aPlatfS},
104 dep_cancelled => $sec->{dep}{dCncl},
105 arr_cancelled => $sec->{arr}{aCncl},
106 load => $tco,
107 messages => \@messages,
108 };
109  
110 if ( $sched_dep and $rt_dep ) {
111 $ref->{dep_delay} = ( $rt_dep->epoch - $sched_dep->epoch ) / 60;
112 }
113  
114 if ( $sched_arr and $rt_arr ) {
115 $ref->{arr_delay} = ( $rt_arr->epoch - $sched_arr->epoch ) / 60;
116 }
117  
118 # known types according to 2015 DB Navigator:
119 # HIDE, JNY, WALK, BIKE, KISS, PARK, TAXI, TRSF, DEVI
120 if ( $sec->{type} eq 'JNY' ) {
121  
122 $ref->{journey} = Travel::Status::DE::HAFAS::Journey->new(
123 common => $opt{common},
124 date => $date,
125 locL => $locL,
126 prodL => $prodL,
127 journey => $sec->{jny},
128 hafas => $hafas,
129 );
130 }
131 elsif ( $sec->{type} eq 'TRSF' or $sec->{type} eq 'WALK' ) {
132 $ref->{distance} = $sec->{gis}{dist};
133 my $duration = $sec->{gis}{durS};
134 $ref->{duration} = DateTime::Duration->new(
135 hours => substr( $duration, 0, 2 ),
136 minutes => substr( $duration, 2, 2 ),
137 seconds => substr( $duration, 4, 2 ),
138 );
139 }
140  
141 bless( $ref, $obj );
142  
143 return $ref;
144 }
145  
146 # }}}
147  
148 # {{{ Private
149  
150 sub set_transfer_from_previous_section {
151 my ( $self, $prev_sec ) = @_;
152  
153 my $delta = $self->dep - $prev_sec->arr;
154 $self->{transfer_duration} = $delta;
155 }
156  
157 # }}}
158  
159 # {{{ Accessors
160  
161 sub messages {
162 my ($self) = @_;
163  
164 if ( $self->{messages} ) {
165 return @{ $self->{messages} };
166 }
167 return;
168 }
169  
170 sub TO_JSON {
171 my ($self) = @_;
172  
173 my $ret = { %{$self} };
174  
175 for my $k ( keys %{$ret} ) {
176 if ( ref( $ret->{$k} ) eq 'DateTime' ) {
177 $ret->{$k} = $ret->{$k}->epoch;
178 }
179 if ( ref( $ret->{$k} ) eq 'DateTime::Duration' ) {
180 $ret->{$k} = [ $ret->{$k}->in_units( 'days', 'hours', 'minutes' ) ];
181 }
182 }
183  
184 return $ret;
185 }
186  
187 # }}}
188  
189 1;
190  
191 __END__