DBRIS/Location.pm untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 200 CC 20 SCAR 54.7
CC
20
Cov
0%
new
342.0
parse_datetime
6.0
TO_JSON
6.0
CRAP 238.0
1 package Travel::Status::DE::DBRIS::Location;
2  
3 use strict;
4 use warnings;
5 use 5.020;
6  
7 use parent 'Class::Accessor';
8  
9 our $VERSION = '0.40';
10  
11 Travel::Status::DE::DBRIS::Location->mk_ro_accessors(
12 qw(eva id lat lon name admin_id operator products type is_cancelled is_additional is_separation display_priority
13 trip_no trip_type dep arr sched_dep sched_arr rt_dep rt_arr arr_delay dep_delay delay
14 platform sched_platform rt_platform
15 occupancy_first occupancy_second occupancy
16 )
17 );
18  
19 sub new {
20 my ( $obj, %opt ) = @_;
21  
22 my $json = $opt{json};
23  
24 # station search results include lat/lon keys in JSON; route entries do not
25 my ( $lon, $lat );
26 if ( $json->{id} =~ m{ [@]X= (?<lon> -? \d+) [@]Y= (?<lat> -? \d+) }x ) {
27 $lat = $+{lat} / 1e6;
28 $lon = $+{lon} / 1e6;
29 }
30  
31 my $ref = {
32 eva => $json->{extId} // $json->{evaNumber},
33 id => $json->{id},
34 lat => $json->{lat} // $lat,
35 lon => $json->{lon} // $lon,
36 name => $json->{name},
37 products => $json->{products},
38 type => $json->{type},
39 trip_type => $json->{kategorie},
40 is_cancelled => $json->{canceled},
41 is_additional => $json->{additional},
42 sched_platform => $json->{gleis},
43 rt_platform => $json->{ezGleis},
44 operator => $opt{operator},
45 };
46  
47 bless( $ref, $obj );
48  
49 if ( $json->{abfahrt}{sollzeit} ) {
50 $ref->{sched_dep} = $ref->parse_datetime( $opt{strptime_obj},
51 $json->{abfahrt}{sollzeit} );
52 }
53 if ( $json->{abfahrt}{echtzeit} ) {
54 $ref->{rt_dep} = $ref->parse_datetime( $opt{strptime_obj},
55 $json->{abfahrt}{echtzeit} );
56 }
57 if ( $json->{ankunft}{sollzeit} ) {
58 $ref->{sched_arr} = $ref->parse_datetime( $opt{strptime_obj},
59 $json->{ankunft}{sollzeit} );
60 }
61 if ( $json->{ankunft}{echtzeit} ) {
62 $ref->{rt_arr} = $ref->parse_datetime( $opt{strptime_obj},
63 $json->{ankunft}{echtzeit} );
64 }
65  
66 if ( $ref->{sched_dep} and $ref->{rt_dep} ) {
67 $ref->{dep_delay}
68 = $ref->{rt_dep}->subtract_datetime( $ref->{sched_dep} )
69 ->in_units('minutes');
70 }
71  
72 if ( $ref->{sched_arr} and $ref->{rt_arr} ) {
73 $ref->{arr_delay}
74 = $ref->{rt_arr}->subtract_datetime( $ref->{sched_arr} )
75 ->in_units('minutes');
76 }
77  
78 $ref->{delay} = $ref->{arr_delay} // $ref->{dep_delay};
79  
80 if ( $json->{adminID} ) {
81 $ref->{admin_id} = $json->{adminID};
82 }
83 if ( $json->{nummer} ) {
84 $ref->{trip_no} = $json->{nummer};
85 }
86  
87 for my $occupancy ( @{ $json->{auslastungsmeldungen} // [] } ) {
88 if ( $occupancy->{klasse} eq 'KLASSE_1' ) {
89 $ref->{occupancy_first} = $occupancy->{stufe};
90 }
91 if ( $occupancy->{klasse} eq 'KLASSE_2' ) {
92 $ref->{occupancy_second} = $occupancy->{stufe};
93 }
94 }
95  
96 if ( $ref->{occupancy_first} and $ref->{occupancy_second} ) {
97 $ref->{occupancy}
98 = ( $ref->{occupancy_first} + $ref->{occupancy_second} ) / 2;
99 }
100 elsif ( $ref->{occupancy_first} ) {
101 $ref->{occupancy} = $ref->{occupancy_first};
102 }
103 elsif ( $ref->{occupancy_second} ) {
104 $ref->{occupancy} = $ref->{occupancy_second};
105 }
106  
107 for my $message ( @{ $json->{priorisierteMeldungen} // [] } ) {
108 if ( $message->{type} and $message->{type} eq 'HALT_AUSFALL' ) {
109 $ref->{is_cancelled} = 1;
110 }
111 elsif ( $message->{text} and $message->{text} eq 'Zusatzhalt' ) {
112 $ref->{is_additional} = 1;
113 }
114 push( @{ $ref->{messages} }, $message );
115 }
116  
117 for my $message ( @{ $json->{risMeldungen} // [] } ) {
118 if ( $message->{key}
119 and $message->{key} eq 'text.realtime.stop.cancelled' )
120 {
121 $ref->{is_cancelled} = 1;
122 }
123 $ref->{ris_messages}{ $message->{key} } = $message->{value};
124 }
125  
126 $ref->{arr} = $ref->{rt_arr} // $ref->{sched_arr};
127 $ref->{dep} = $ref->{rt_dep} // $ref->{sched_dep};
128 $ref->{platform} = $ref->{rt_platform} // $ref->{sched_platform};
129  
130 return $ref;
131 }
132  
133 sub parse_datetime {
134 my ( $self, $strp, $dt_str ) = @_;
135 my $ret;
136 eval { $ret = $strp->parse_datetime($dt_str); };
137 if ($@) {
138 warn("Cannot parse datetime $dt_str: $@");
139 }
140 return $ret;
141 }
142  
143 sub TO_JSON {
144 my ($self) = @_;
145  
146 my $ret = { %{$self} };
147  
148 for my $k (qw(sched_dep rt_dep dep sched_arr rt_arr arr)) {
149 if ( $ret->{$k} ) {
150 $ret->{$k} = $ret->{$k}->epoch;
151 }
152 }
153  
154 return $ret;
155 }
156  
157 1;