DBRIS/Connection/Segment.pm untested

filter: Statementstmt 0.0% 0 / 141 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 58 MC/DCmcdc 0.0% 0 / 34 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 299 CC 22 SCAR 53.1
CC
22
Cov
0%
new
380.0
TO_JSON
12.0
parse_datetime
6.0
CRAP 202.6
1 package Travel::Routing::DE::DBRIS::Connection::Segment;
2  
3 use strict;
4 use warnings;
5 use 5.020;
6  
7 use parent 'Class::Accessor';
8  
9 use DateTime::Duration;
10 use Travel::Status::DE::DBRIS::Location;
11  
12 our $VERSION = '0.18';
13  
14 Travel::Routing::DE::DBRIS::Connection::Segment->mk_ro_accessors(
15 qw(
16 dep_name dep_eva arr_name arr_eva
17 train train_long train_mid train_short direction
18 sched_dep rt_dep dep dep_platform dep_stop
19 sched_arr rt_arr arr arr_platform arr_stop
20 sched_duration rt_duration duration duration_percent
21 arr_delay dep_delay delay feasibility is_unlikely transfer_duration
22 journey_id
23 occupancy occupancy_first occupancy_second
24 is_cancelled is_transfer is_walk walk_name distance_m
25 )
26 );
27  
28 sub new {
29 my ( $obj, %opt ) = @_;
30  
31 my $json = $opt{json};
32 my $strptime = $opt{strptime_obj};
33  
34 my $ref = {
35 arr_eva => $json->{ankunftsOrtExtId},
36 arr_name => $json->{ankunftsOrt},
37 dep_eva => $json->{abfahrtsOrtExtId},
38 dep_name => $json->{abfahrtsOrt},
39 train => $json->{verkehrsmittel}{name},
40 train_short => $json->{verkehrsmittel}{kurzText},
41 train_mid => $json->{verkehrsmittel}{mittelText},
42 train_long => $json->{verkehrsmittel}{langText},
43 direction => $json->{verkehrsmittel}{richtung},
44 distance_m => $json->{distanz},
45 feasibility => $json->{anschlussBewertungCode},
46 journey_id => $json->{journeyId},
47 is_cancelled => $json->{is_cancelled},
48 };
49  
50 bless( $ref, $obj );
51  
52 if ( my $ts = $json->{abfahrt}{sollzeit} ) {
53 $ref->{sched_dep} = $ref->parse_datetime( $strptime, $ts );
54 }
55 if ( my $ts = $json->{abfahrt}{echtzeit} ) {
56 $ref->{rt_dep} = $ref->parse_datetime( $strptime, $ts );
57 }
58 $ref->{dep} = $ref->{rt_dep} // $ref->{sched_dep};
59  
60 if ( my $ts = $json->{ankunft}{sollzeit} ) {
61 $ref->{sched_arr} = $ref->parse_datetime( $strptime, $ts );
62 }
63 if ( my $ts = $json->{ankunft}{echtzeit} ) {
64 $ref->{rt_arr} = $ref->parse_datetime( $strptime, $ts );
65 }
66 $ref->{arr} = $ref->{rt_arr} // $ref->{sched_arr};
67  
68 if ( $ref->{sched_dep} and $ref->{rt_dep} ) {
69 $ref->{dep_delay}
70 = $ref->{rt_dep}->subtract_datetime( $ref->{sched_dep} )
71 ->in_units('minutes');
72 }
73  
74 if ( $ref->{sched_arr} and $ref->{rt_arr} ) {
75 $ref->{arr_delay}
76 = $ref->{rt_arr}->subtract_datetime( $ref->{sched_arr} )
77 ->in_units('minutes');
78 }
79  
80 $ref->{delay} = $ref->{arr_delay} // $ref->{dep_delay};
81  
82 # PUBLICTRANSPORT uses abschnittsDauerInSeconds; WALK uses abschnittsDauer
83 if ( my $d = $json->{abschnittsDauerInSeconds} // $json->{abschnittsDauer} )
84 {
85 $ref->{sched_duration} = DateTime::Duration->new(
86 hours => int( $d / 3600 ),
87 minutes => int( ( $d % 3600 ) / 60 ),
88 seconds => $d % 60,
89 );
90 }
91 if ( my $d = $json->{ezAbschnittsDauerInSeconds}
92 // $json->{ezAbschnittsDauer} )
93 {
94 $ref->{rt_duration} = DateTime::Duration->new(
95 hours => int( $d / 3600 ),
96 minutes => int( ( $d % 3600 ) / 60 ),
97 seconds => $d % 60,
98 );
99 }
100 $ref->{duration} = $ref->{rt_duration} // $ref->{sched_duration};
101  
102 for my $occupancy ( @{ $json->{auslastungsmeldungen} // [] } ) {
103 if ( $occupancy->{klasse} eq 'KLASSE_1' ) {
104 $ref->{occupancy_first} = $occupancy->{stufe};
105 }
106 if ( $occupancy->{klasse} eq 'KLASSE_2' ) {
107 $ref->{occupancy_second} = $occupancy->{stufe};
108 }
109 }
110  
111 if ( $ref->{occupancy_first} and $ref->{occupancy_second} ) {
112 $ref->{occupancy}
113 = ( $ref->{occupancy_first} + $ref->{occupancy_second} ) / 2;
114 }
115 elsif ( $ref->{occupancy_first} ) {
116 $ref->{occupancy} = $ref->{occupancy_first};
117 }
118 elsif ( $ref->{occupancy_second} ) {
119 $ref->{occupancy} = $ref->{occupancy_second};
120 }
121  
122 for my $stop ( @{ $json->{halte} // [] } ) {
123 push(
124 @{ $ref->{route} },
125 Travel::Status::DE::DBRIS::Location->new(
126 json => $stop,
127 strptime_obj => $strptime
128 )
129 );
130 }
131  
132 for my $attr ( @{ $json->{verkehrsmittel}{zugattribute} // [] } ) {
133 push( @{ $ref->{attributes} }, $attr );
134 }
135  
136 for my $message ( @{ $json->{himMeldungen} // [] } ) {
137 push( @{ $ref->{messages_him} }, $message );
138 }
139  
140 for my $message ( @{ $json->{risNotizen} // [] } ) {
141 push( @{ $ref->{messages_ris} }, $message );
142 if ( $message->{key} eq 'text.realtime.journey.missed.connection' ) {
143 $ref->{is_unlikely} = 1;
144 }
145 }
146  
147 for my $message ( @{ $json->{priorisierteMeldungen} // [] } ) {
148 push( @{ $ref->{messages_prio} }, $message );
149 }
150  
151 if ( $json->{verkehrsmittel}{typ} eq 'WALK' ) {
152 $ref->{is_walk} = 1;
153 $ref->{walk_name} = $json->{verkehrsmittel}{name};
154 }
155 if ( $json->{verkehrsmittel}{typ} eq 'TRANSFER' ) {
156 $ref->{is_transfer} = 1;
157 $ref->{transfer_notes}
158 = [ map { $_->{value} } @{ $json->{transferNotes} // [] } ];
159 }
160  
161 if ( @{ $ref->{route} // [] } ) {
162 $ref->{dep_stop} = $ref->{route}[0];
163 $ref->{arr_stop} = $ref->{route}[-1];
164 $ref->{dep_platform} = $ref->{route}[0]->platform;
165 $ref->{arr_platform} = $ref->{route}[-1]->platform;
166 if ( $ref->{dep_stop}{is_cancelled} or $ref->{arr_stop}{is_cancelled} )
167 {
168 $ref->{is_cancelled} = 1;
169 }
170 }
171  
172 return $ref;
173 }
174  
175 sub parse_datetime {
176 my ( $self, $strp, $dt_str ) = @_;
177 my $ret;
178 eval { $ret = $strp->parse_datetime($dt_str); };
179 if ($@) {
180 warn("Cannot parse datetime $dt_str: $@");
181 }
182 return $ret;
183 }
184  
185 sub attributes {
186 my ($self) = @_;
187  
188 return @{ $self->{attributes} // [] };
189 }
190  
191 sub messages_him {
192 my ($self) = @_;
193  
194 return @{ $self->{messages_him} // [] };
195 }
196  
197 sub messages_ris {
198 my ($self) = @_;
199  
200 return @{ $self->{messages_ris} // [] };
201 }
202  
203 sub messages_prio {
204 my ($self) = @_;
205  
206 return @{ $self->{messages_prio} // [] };
207 }
208  
209 sub route {
210 my ($self) = @_;
211  
212 return @{ $self->{route} // [] }[ 1 .. $#{ $self->{route} } - 1 ];
213 }
214  
215 sub transfer_notes {
216 my ($self) = @_;
217  
218 return @{ $self->{transfer_notes} // [] };
219 }
220  
221 sub TO_JSON {
222 my ($self) = @_;
223  
224 my $ret = { %{$self} };
225  
226 for my $k (qw(sched_dep rt_dep dep sched_arr rt_arr arr)) {
227 if ( $ret->{$k} ) {
228 $ret->{$k} = $ret->{$k}->epoch;
229 }
230 }
231  
232 for my $k (qw(sched_duration rt_duration duration transfer_duration)) {
233 if ( $ret->{$k} ) {
234 $ret->{$k} = $ret->{$k}->in_units('minutes');
235 }
236 }
237  
238 return $ret;
239 }
240  
241 1;