Synchronize/CameraOffsets.pm

filter: Statementstmt 63.0% 138 / 219 Branchbran 41.6% 50 / 120 Conditioncond 53.6% 22 / 41 MC/DCmcdc 37.0% 10 / 27 Subroutinesub 66.6% 18 / 27 Podpod 57.8% 11 / 19 totaltotal 54.9% 249 / 453 CC 86 SCAR 39.1
CC
86
Cov
55%
relevant_part
210.0
set
83.9
identify
20.0
CRAP 49.9
« Synchronize/GroupedInfo.pm ↑ Summary Synchronize/GpsPositionCollection.pm »
1 package Image::Synchronize::CameraOffsets;
2
3 =head1 NAME
4
5 Image::Synchronize::CameraOffsets - Manage time offsets for a range of
6 times
7
8 =head1 METHODS
9
10 The module provides the following methods:
11
12 =cut
13
14 2 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
15
16 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Clone;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Image::Synchronize::Timestamp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Scalar::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
20 looks_like_number
21 2 );
22 2 ▶use Time::Local qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
23 timegm
24 2 );
25 2 ▶use YAML::Any qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
26 Load
27 Dump
28 2 );
29
30 use overload
31 2 ▶ '""' => \&stringify
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ;
33
34 =head2 new
35
36 $co = Image::Synchronize::CameraOffsets->new;
37 $co2 = $co->new;
38
39 Creates and returns a new instance of this class.
40
41 =cut
42
43 21 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod documented
44 21 my ( $invocant, %options ) = @_;
45
46 # all three hash references have structure
47 # $camera_id => { $time => $offset }
48 bless {
49 base => {},
50 added => {},
51 effective => {},
52 synchronized => 0,
53 exists( $options{log_callback} )
54 ? ( log_callback => $options{log_callback} )
55 21 ▶ : (),
Branch: 1 branch
BranchTrueFalseTotal
exists $options{'log_callback'} ? : 19 2 21
Condition66%2/3
ref $invocant || $invocant
l!l&&r!l&&!r
1200
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant $invocant
Truth table66%2/3
A || B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
56 }, (ref($invocant) || $invocant);
57 }
58
59 =head2 set
60
61 $co = $co->set($camera_id, $time, $offset);
62 $co = $co->set($camera_id, $time, $offset, $file);
63
64 Declares a time offset for a particular time for a particular camera.
65
66 C<$camera_id> is the ID of the camera that took the image for which an
67 offset is specified.
68
69 C<$time> is the time (either in seconds since the epoch, as for
70 L<gmtime>, or else as an L<Image::Synchronize::Timestamp>) of the
71 image for which the offset is specified.
72
73 C<$offset> is the time offset that is valid at the specified C<$time>.
74 It must be either a number, or else an
75 L<Image::Synchronize::Timestamp>.
76
77 If it is a number, then that is the time offset in seconds.
78
79 If it is an L<Image::Synchronize::Timestamp>, then the timezone part
80 provides the nominal timezone that the camera clock was set to, and
81 the clock time part provides the time offset relative to the nominal
82 timezone.
83
84 C<$file> is the optional file name. If it is defined, then it is used
85 in any generated messages.
86
87 In scalar context, returns the object itself. In list context,
88 returns the object and also a true or false value that says whether an
89 offset for that camera and time already existed and was replaced.
90 That second element has a false value if there was no offset yet, and
91 also if there was already an offset but it wasn't changed.
92
93 =cut
94
95 44 ▶sub set {
Subroutine set: called CC=23 SCAR=44.3
Pod documented
96 44 my ( $self, $camera_id, $time, $offset, $file ) = @_;
97 44 ▶ croak "Need camera ID" unless defined $camera_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $camera_id 0 44 44
98 44 ▶ if ( Image::Synchronize::Timestamp->istypeof($time) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('Image::Synchronize::Timestamp'->istypeof($time)) { } 44 0 44
elsif (not &looks_like_number($time)) { } 0 0 0
99 # we prefer UTC but if the camera doesn't provide UTC then we
100 # accept local time.
101 44 ▶ $time = $time->time_utc // $time->time_local;
Condition33%1/3
$time->time_utc // $time->time_local
l!l&&r!l&&!r
0440
MC/DC0%0/2
$time->time_utc // $time->time_local
$time->time_utc $time->time_local
Truth table33%1/3
A // B
A= $time->time_utcB= $time->time_local
ABresult
1X1
011
000
102 } elsif (not looks_like_number($time)) {
103 0 ▶ croak "Time must be a number or an Image::Synchronize::Timestamp"
Branch: 1 branch
BranchTrueFalseTotal
$file ? : 0 0 0
104 . ($file? " for $file": '') . "\n";
105 }
106 44 ▶ if (ref($offset)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $offset) { } 1 43 44
107 1 ▶ if (not(Image::Synchronize::Timestamp->istypeof($offset))) {
Branch: 2 branches
BranchTrueFalseTotal
if (not 'Image::Synchronize::Timestamp'->istypeof($offset)) { } 0 1 1
elsif ($offset->has_date) { } 0 1 1
108 0 ▶ croak 'Offset must be a scalar or an Image::Synchronize::Timestamp '
Branch: 1 branch
BranchTrueFalseTotal
$file ? : 0 0 0
109 . 'with no date but was a ' . identify($offset)
110 . ( $file ? " for $file" : '' ) . "\n";
111 } elsif ($offset->has_date) {
112 0 ▶ croak 'Offset cannot be an Image::Synchronize::Timestamp '
Branch: 1 branch
BranchTrueFalseTotal
$file ? : 0 0 0
113 . 'with a date '. ( $file ? "for $file" : '' ) . "\n";
114 }
115 } else {
116 43 $offset = Image::Synchronize::Timestamp->new($offset)
117 ->adjust_nodate;
118 }
119 44 my $changed = 0;
120 44 ▶ if ( not defined $self->{added}->{$camera_id}->{$time} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $self->{'added'}{$camera_id}{$time}) { } 44 0 44
elsif ($self->{'added'}{$camera_id}{$time}{'offset'} != $offset) { } 0 0 0
121 44 $changed = 1;
122 }
123 elsif ( $self->{added}->{$camera_id}->{$time}->{offset} != $offset ) {
124 0 ▶ if ( not wantarray ) { # print message here
Branch: 1 branch
BranchTrueFalseTotal
unless (wantarray) 0 0 0
125 0 my $t = Image::Synchronize::Timestamp->new($time);
126 0 ▶ my $message =
Branch: 1 branch
BranchTrueFalseTotal
$file ? : 0 0 0
127 "Replacing offset $self->{added}->{$camera_id}->{$time}->{offset} "
128 . "of camera $camera_id "
129 . ( $file ? "for $file " : '' ) . "at "
130 . $t->clone->adjust_to_local_timezone
131 . " with $offset\n";
132 0 ▶ if ( $self->{log_callback} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'log_callback'}) { } 0 0 0
133 0 $self->{log_callback}->($message);
134 }
135 else {
136 0 print $message;
137 }
138 } # otherwise we assume that the caller prints a message if needed
139 0 $changed = 2;
140 }
141 44 ▶ if ($changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 44 0 44
142 44 ▶ $self->{added}->{$camera_id}->{$time} = {
Branch: 1 branch
BranchTrueFalseTotal
$file ? : 0 44 44
143 offset => $offset,
144 $file ? ( file => $file ) : ()
145 };
146 }
147 44 $self->{synchronized} = 0;
148 $self->{min_added_time} = $time
149 if not( defined $self->{min_added_time} )
150 44 ▶ || $time < $self->{min_added_time};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->{"min_added_time"} or $time < $self->{"min_added_time"} 30 14 44
Condition100%3/3
not defined $self->{"min_added_time"} or $time < $self->{"min_added_time"}
l!l&&r!l&&!r
21914
MC/DC100%2/2
not defined $self->{"min_added_time"} or $time < $self->{"min_added_time"}
not defined $self->{"min_added_time"} $time < $self->{"min_added_time"}
Truth table100%3/3
not(A) or B
A= not defined $self->{"min_added_time"}B= $time < $self->{"min_added_time"}
ABresult
1X1
011
000
151 $self->{max_added_time} = $time
152 if not( defined $self->{max_added_time} )
153 44 ▶ || $time > $self->{max_added_time};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->{"max_added_time"} or $time > $self->{"max_added_time"} 35 9 44
Condition100%3/3
not defined $self->{"max_added_time"} or $time > $self->{"max_added_time"}
l!l&&r!l&&!r
21149
MC/DC100%2/2
not defined $self->{"max_added_time"} or $time > $self->{"max_added_time"}
not defined $self->{"max_added_time"} $time > $self->{"max_added_time"}
Truth table100%3/3
not(A) or B
A= not defined $self->{"max_added_time"}B= $time > $self->{"max_added_time"}
ABresult
1X1
011
000
154 44 $self->{accessed_cameras}->{$camera_id} = 1;
155 44 ▶ return wantarray ? ( $self, ( $changed == 2 ) ) : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 44 44
156 }
157
158 # identify the type of the value in a non-empty string, either the
159 # name of the package (if an object), or the reference type (if a
160 # reference), or '-undef-' (if undef), or '-scalar-'.
161 0 ▶sub identify {
Subroutine identify: not called CC=4 SCAR=30.0
Pod undocumented
162 0 my ($value) = @_;
163 0 ▶ if ( ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 0 0 0
164 0 ▶ return ( blessed($value) or ref($value) );
Condition0%0/3
blessed($value) || ref $value
l!l&&r!l&&!r
000
MC/DC0%0/2
blessed($value) || ref $value
blessed($value) ref $value
Truth table0%0/3
A || B
A= blessed($value)B= ref $value
ABresult
1X1
011
000
165 }
166 0 ▶ return defined($value)? '-scalar-': '-undef-';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 0 0 0
167 }
168
169 =head2 cameras
170
171 @cameras = $co->cameras;
172
173 Returns the IDs of the cameras for which time offsets have been
174 registered.
175
176 =cut
177
178 0 ▶sub cameras {
Subroutine cameras: not called CC=3 SCAR=24.8
Pod documented
179 0 my ($self) = @_;
180 0 $self->make_effective;
181 0 my %cameras;
182 0 for my $key ( keys %{ $self->{effective} } ) {
183 0 ▶ $cameras{$key} = 1 if keys %{ $self->{effective}->{$key} };
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"effective"}{$key};} 0 0 0
184 }
185 0 return sort keys %cameras;
186 }
187
188 =head2 camera_count
189
190 $count = $co->camera_count;
191
192 Returns the count of cameras represented in C<$co>.
193
194 =cut
195
196 0 ▶sub camera_count {
Subroutine camera_count: not called CC=1 SCAR=6.9
Pod documented
197 0 my ($self) = @_;
198 0 my @cameras = $self->cameras;
199 0 return scalar @cameras;
200 }
201
202 =head2 added_time_range
203
204 ($min_added_time, $max_added_time) = $co->added_time_range;
205
206 Returns the oldest and youngest time that have been added to C<$co>.
207 The timestamps are returned as L<Image::Synchronize::Timestamp>s.
208
209 =cut
210
211 24 ▶sub added_time_range {
Subroutine added_time_range: called CC=1 SCAR=0.0
Pod documented
212 24 my ($self) = @_;
213 24 return ( $self->{min_added_time}, $self->{max_added_time} );
214 }
215
216 81 ▶sub identical_offsets {
Subroutine identical_offsets: called CC=3 SCAR=12.0
Pod undocumented
217 81 my ($o1, $o2) = @_;
218 81 ▶ return if ref($o1) ne ref($o2);
Branch: 1 branch
BranchTrueFalseTotal
if ref $o1 ne ref $o2 0 81 81
219 81 ▶ if (ref $o1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $o1) 81 0 81
220 # assume Image::Synchronize::Timestamp
221 81 return $o1->identical($o2);
222 }
223 0 return $o1 == $o2;
224 }
225
226 # merge 'added' into 'base' to produce 'effective'
227 35 ▶sub make_effective {
Subroutine make_effective: called CC=14 SCAR=26.4
Pod undocumented
228 35 my ($self) = @_;
229 35 ▶ if ( not $self->{synchronized} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"synchronized"}) 24 11 35
230 24 my ( $min_time, $max_time ) = $self->added_time_range;
231 24 ▶ if ( defined($max_time) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $max_time) 23 1 24
232 23 my $sa = $self->{added};
233
234 # tried to use "while (my ($camera_id, $r) = each %{$sa}" here,
235 # but it seems to restart if a sibling hash element (such as
236 # $self->{effective}) is modified
237 23 foreach my $camera_id ( keys %{$sa} ) {
238 23 my $r = $sa->{$camera_id};
239 23 ▶ my %effective = %{ $self->{base}->{$camera_id} // {} };
Condition100%2/2
$self->{"base"}{$camera_id} // {}
l!l
194
MC/DC100%1/1
$self->{"base"}{$camera_id} // {}
$self->{"base"}{$camera_id}
Truth table100%2/2
A
A= $self->{"base"}{$camera_id}
Aresult
11
00
240
241 # Add the new entries for the period of interest
242 23 foreach ( keys %{$r} ) {
243 47 ▶ if ( defined( $effective{$_} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $effective{$_} and not identical_offsets($effective{$_}, $r->{$_}{"offset"})) 1 46 47
Condition100%3/3
defined $effective{$_} and not identical_offsets($effective{$_}, $r->{$_}{"offset"})
!ll&&!rl&&r
4061
MC/DC100%2/2
defined $effective{$_} and not identical_offsets($effective{$_}, $r->{$_}{"offset"})
defined $effective{$_} not identical_offsets($effective{$_}, $r->{$_}{"offset"})
Truth table100%3/3
A and not(B)
A= defined $effective{$_}B= not identical_offsets($effective{$_}, $r->{$_}{"offset"})
ABresult
0X0
100
111
244 && !identical_offsets($effective{$_}, $r->{$_}->{offset}) )
245 {
246 1 my $message =
247 "Replacing offset $effective{$_} "
248 . "of camera $camera_id at "
249 . Image::Synchronize::Timestamp->new($_)
250 ->adjust_to_local_timezone->display_iso
251 . " with $r->{$_}->{offset}\n";
252 1 ▶ if ( exists $self->{log_callback} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'log_callback'}) { } 1 0 1
253 1 $self->{log_callback}->($message);
254 }
255 else {
256 0 print $message;
257 }
258 }
259 47 $effective{$_} = $r->{$_}->{offset};
260 }
261
262 # Then normalize: remove all but the first of sequences of
263 # entries with the same offset -- except for the very last
264 # offset, which should remain if it is a duplicate so we can
265 # tell the range of times for which offsets are known.
266 23 my $offset;
267 23 my @times = sort { $a <=> $b } keys %effective;
268 23 foreach my $i ( 0 .. $#times ) {
269 97 ▶ if ( defined($offset)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $offset and identical_offsets($effective{$times[$i]}, $offset)) { } 39 58 97
Condition100%3/3
defined $offset and identical_offsets($effective{$times[$i]}, $offset)
!ll&&!rl&&r
233539
MC/DC100%2/2
defined $offset and identical_offsets($effective{$times[$i]}, $offset)
defined $offset identical_offsets($effective{$times[$i]}, $offset)
Truth table100%3/3
A and B
A= defined $offsetB= identical_offsets($effective{$times[$i]}, $offset)
ABresult
0X0
100
111
270 and identical_offsets($effective{ $times[$i] }, $offset) ) {
271 39 ▶ delete $effective{ $times[$i] } unless $i == $#times && $i > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $i == $#times and $i > 0 23 16 39
Condition66%2/3
$i == $#times and $i > 0
!ll&&!rl&&r
23016
MC/DC50%1/2
$i == $#times and $i > 0
$i == $#times $i > 0
Truth table66%2/3
A and B
A= $i == $#timesB= $i > 0
ABresult
0X0
100
111
272 }
273 else {
274 58 $offset = $effective{ $times[$i] };
275 }
276 }
277
278 23 $self->{effective}->{$camera_id} = \%effective;
279 }
280 }
281 24 $self->{synchronized} = 1;
282 }
283 35 return $self;
284 }
285
286 =head2 get
287
288 $offset = $co->get($camera_id, $time);
289 ($offset, $file) = $co->get($camera_id, $time);
290
291 Returns a camera offset, interpreting the registered camera offsets as
292 defining a piecewise constant function.
293
294 Camera offsets for a camera ID are registered through C</set> for
295 specific times. The current method assumes that a camera offset is
296 valid from that time until the next later time for which a camera
297 offset was registered for the same camera ID, and that the oldest
298 camera ofset is valid also for any earlier time.
299
300 C<$camera_id> is the camera ID for which to return a camera offset.
301
302 C<$time> is the time for which to return a camera offset. The time
303 must be specified either in seconds since the epoch, as for L<gmtime>,
304 or else as an L<Image::Synchronize::Timestamp>.
305
306 In scalar context, returns the requested camera offset, or C<undef> if
307 no offsets at all have yet been registered for that C<$camera_id>.
308
309 In list context, returns the C<$offset> as for the scalar case, and
310 also returns the name of the file for which that offset was
311 registered, if known, or C<undef> otherwise. A file name (different
312 from C<undef>) is only returned if an offset was registered for
313 I<exactly> that C<$time>.
314
315 =cut
316
317 16 ▶sub get {
Subroutine get: called CC=12 SCAR=26.6
Pod documented
318 16 my ( $self, $camera_id, $time ) = @_;
319 16 ▶ return unless defined($camera_id) and defined($time);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $camera_id and defined $time 0 16 16
Condition33%1/3
defined $camera_id and defined $time
!ll&&!rl&&r
0016
MC/DC0%0/2
defined $camera_id and defined $time
defined $camera_id defined $time
Truth table33%1/3
A and B
A= defined $camera_idB= defined $time
ABresult
0X0
100
111
320 16 ▶ if ( Image::Synchronize::Timestamp->istypeof($time) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Image::Synchronize::Timestamp"->istypeof($time)) 15 1 16
321 15 $time = $time->time_local;
322 }
323 16 $self->make_effective;
324 16 $self->{accessed_cameras}->{$camera_id} = 1;
325 16 my $c = $self->{effective}->{$camera_id};
326 16 my @times = sort { $a <=> $b } keys %{$c};
327 16 ▶ return unless @times;
Branch: 1 branch
BranchTrueFalseTotal
unless @times 2 14 16
328 14 my $prev;
329
330 my $file;
331 14 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 14 14
332
333 # get the file for which the offset was registered, if any
334 0 my $r = $self->{added}->{$camera_id};
335 0 ▶ $file = $r->{file} if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 0 0
336 }
337 14 foreach (@times) {
338 25 ▶ if ( $_ > $time ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ > $time) 8 17 25
339 8 ▶ my $offset = $prev // $c->{$_};
Condition66%2/3
$prev // $c->{$_}
l!l&&r!l&&!r
440
MC/DC0%0/2
$prev // $c->{$_}
$prev $c->{$_}
Truth table66%2/3
A // B
A= $prevB= $c->{$_}
ABresult
1X1
011
000
340 8 ▶ return wantarray ? ( $offset, $file ) : $offset;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 8 8
341 }
342 17 $prev = $c->{$_};
343 }
344 6 my $offset = $c->{ $times[-1] };
345 6 ▶ return wantarray ? ( $offset, $file ) : $offset;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 6 6
346 }
347
348 =head2 get_exact
349
350 $offset = $co->get_exact($camera_id, $time);
351
352 Returns the camera offset for the specified C<$time> for the specified
353 C<$camera_id>, if a camera offset was set (through L</set>) for
354 I<exactly> that time and camera ID. Returns C<undef> if not found.
355
356 C<$time> is the time either in seconds since the epoch, as for
357 L<gmtime>, or else as an L<Image::Synchronize::Timestamp>.
358
359 =cut
360
361 0 ▶sub get_exact {
Subroutine get_exact: not called CC=4 SCAR=30.0
Pod documented
362 0 my ( $self, $camera_id, $time ) = @_;
363 0 ▶ if ( Image::Synchronize::Timestamp->istypeof($time) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Image::Synchronize::Timestamp"->istypeof($time)) 0 0 0
364 0 $time = $time->time_local;
365 }
366 0 my $r = $self->{added}->{$camera_id};
367 0 ▶ $r = $r->{$time} if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 0 0
368 0 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 0 0
369 0 return ( $r->{offset}, $r->{file} );
370 }
371 else {
372 0 return ();
373 }
374 }
375
376 19 ▶sub export_internal {
Subroutine export_internal: called CC=3 SCAR=11.0
Pod undocumented
377 19 my ($e) = @_;
378 19 my %export;
379 19 ▶ while ( my ( $camera_id, $c ) = each %{$e} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$e;}) 19 19 38
380 19 my %items;
381 19 foreach my $time ( sort { $a <=> $b } keys %{$c} ) {
382 67 $items{ display_time($time) } = display_offset( $c->{$time} );
383 }
384 19 $export{$camera_id} = \%items;
385 }
386 19 return \%export;
387 }
388
389 =head2 export_data
390
391 $data = $co->export_data;
392
393 Returns a version of Image::Synchronize::CameraOffsets C<$co> suitable
394 for exporting. The return value is similar to
395
396 {
397 'cameraid1' =>
398 {
399 '2019-06-04T14:33:12' => '+1:03:15',
400 '2019-06-05T08:11:43' => '+1:03:15'
401 },
402 'cameraid2' =>
403 {
404 '2019-01-01T00:00:00' => '+2:00',
405 '2019-06-05T08:11:43' => '+2:00'
406 },
407 }
408
409 =cut
410
411 0 ▶sub export_data {
Subroutine export_data: not called CC=1 SCAR=6.9
Pod documented
412 0 my ($self) = @_;
413 0 $self->make_effective;
414 0 return export_internal($self->{effective});
415 }
416
417 =head2 parse
418
419 $co = $co->parse($data);
420
421 Parses camera offsets information from text C<$data> into
422 Image::Synchronize::CameraOffsets C<$co>, replacing any earlier
423 contents of C<$co>. The text is expected to be in the format that
424 L</stringify> produces. Returns the object.
425
426 =cut
427
428 19 ▶sub parse {
Subroutine parse: called CC=7 SCAR=19.7
Pod documented
429 19 my ( $self, $data ) = @_;
430 19 my %import;
431 19 ▶ while ( my ( $camera_id, $c ) = each %{$data} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$data;}) 19 19 38
432 19 my $e = $import{$camera_id} = {};
433 19 foreach my $key ( sort keys %{$c} ) {
434
435 # the key is assumed to be a timestamp in text format, without
436 # timezone offset
437 57 my $time = Image::Synchronize::Timestamp->new($key);
438 57 ▶ croak "Invalid time: $key\n" unless defined $time;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 0 57 57
439
440 # the value is assumed to be a timezone offset, in one of the
441 # following three formats: '-1805', '-03:11', '-03:11:17'. The
442 # first one specifies the optionally signed number of seconds.
443 # The second one specifies the optionally signed number of hours
444 # and number of minutes. The third one is like the second but
445 # adds the number of seconds.
446
447 57 my $offset = $c->{$key};
448 57 ▶ croak "Undefined offset\n" unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 0 57 57
449 57 my $o;
450 57 ▶ if (looks_like_number($offset)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($offset)) { } 0 57 57
451 0 $o = $offset;
452 } else {
453 57 $o = Image::Synchronize::Timestamp->new($offset);
454 }
455 57 ▶ croak "Invalid offset: $offset\n" unless defined $o;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o 0 57 57
456 57 $e->{ $time->time_local } = $o;
457 }
458 }
459 19 $self->{base} = \%import;
460
461 # drop previous contents
462 19 $self->{added} = {};
463 19 delete $self->{min_added_time};
464 19 delete $self->{max_added_time};
465
466 # store the "base" contents also as the "effective" contents. It
467 # must be a deep copy, not a shallow one, otherwise processing of
468 # "effective" may modify "base", too.
469 19 $self->{effective} = Clone::clone($self->{base});
470 19 $self->{synchronized} = 1;
471 19 return $self;
472 }
473
474 # returns a version of timestamp C<$time> (acceptable to
475 # L<Image::Synchronize::Timestamp>) that is ready for display. For
476 # use by the L</stringify> method.
477 67 ▶sub display_time {
Subroutine display_time: called CC=2 SCAR=7.0
Pod undocumented
478 67 ▶ my ($time) = (@_ == 1? $_[0]: $_[1]);;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 67 0 67
479 67 my $t = Image::Synchronize::Timestamp->new($time);
480 67 $t->remove_timezone;
481 67 return $t->display_iso;
482 }
483
484 # returns a version of timezone offset C<$offset> (a scalar in
485 # seconds, or an L<Image::Synchronize::Timestamp>) that is ready for
486 # display. For use by the L</stringify> method.
487 67 ▶sub display_offset {
Subroutine display_offset: called CC=3 SCAR=11.7
Pod undocumented
488 67 ▶ my ($offset) = (@_ == 1? $_[0]: $_[1]);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 67 0 67
489 67 ▶ $offset = Image::Synchronize::Timestamp->new($offset)
Branch: 1 branch
BranchTrueFalseTotal
unless "Image::Synchronize::Timestamp"->istypeof($offset) 0 67 67
490 unless Image::Synchronize::Timestamp->istypeof($offset);
491 67 return $offset->display_time;
492 }
493
494 =head2 relevant_part
495
496 $relevant = $co->relevant_part;
497
498 Extracts the part of the camera offsets C<$co> that was accessed. The
499 results include only those camera IDs that were accessed through
500 L</set> or L</get> since the last call to L</parse>, and for each of
501 those camera IDs only the smallest time range that was accessed for that
502 camera ID.
503
504 The return value is a HASH reference, similar to
505
506 {
507 'CameraID1' => {
508 '2018-03-10T17:44:22' => 76,
509 '2018-05-02T12:55:52' => -15,
510 '2018-06-22T09:22:15' => -15
511 },
512 }
513
514 The keys are camera IDs and their
515
516 =cut
517
518 0 ▶sub relevant_part {
Subroutine relevant_part: not called CC=14 SCAR=53.5
Pod documented
519 0 my ($self) = @_;
520 0 $self->make_effective;
521 0 my %relevant;
522 0 ▶ if (defined $self->{min_added_time}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"min_added_time"}) 0 0 0
523 0 ▶ while ( my ( $camera_id, $c ) = each %{ $self->{effective} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"effective"};}) 0 0 0
524 0 ▶ next unless $self->{accessed_cameras}->{$camera_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"accessed_cameras"}{$camera_id} 0 0 0
525 0 my $e = $relevant{$camera_id} = {};
526 0 my ( $first, $last );
527 0 my @times = sort { $a <=> $b } keys %{$c};
528 0 foreach my $i ( 0 .. $#times ) {
529 $first = $i
530 if not( defined $first )
531 0 ▶ && $times[$i] >= $self->{min_added_time};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $first and $times[$i] >= $self->{"min_added_time"} 0 0 0
Condition0%0/3
not defined $first and $times[$i] >= $self->{"min_added_time"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $first and $times[$i] >= $self->{"min_added_time"}
not defined $first $times[$i] >= $self->{"min_added_time"}
Truth table0%0/3
not(A) and B
A= not defined $firstB= $times[$i] >= $self->{"min_added_time"}
ABresult
0X0
100
111
532 0 ▶ $last = $i if $times[$i] <= $self->{max_added_time};
Branch: 1 branch
BranchTrueFalseTotal
if $times[$i] <= $self->{"max_added_time"} 0 0 0
533 }
534 0 ▶ if ( defined $first ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $first) 0 0 0
535
536 # expand range so it includes at least the entire relevant range
537 0 ▶ --$first if $first > 0 && $times[$first] > $self->{min_added_time};
Branch: 1 branch
BranchTrueFalseTotal
if $first > 0 and $times[$first] > $self->{"min_added_time"} 0 0 0
Condition0%0/3
$first > 0 and $times[$first] > $self->{"min_added_time"}
!ll&&!rl&&r
000
MC/DC0%0/2
$first > 0 and $times[$first] > $self->{"min_added_time"}
$first > 0 $times[$first] > $self->{"min_added_time"}
Truth table0%0/3
A and B
A= $first > 0B= $times[$first] > $self->{"min_added_time"}
ABresult
0X0
100
111
538 0 ▶ ++$last if $last < $#times && $times[$last] < $self->{max_added_time};
Branch: 1 branch
BranchTrueFalseTotal
if $last < $#times and $times[$last] < $self->{"max_added_time"} 0 0 0
Condition0%0/3
$last < $#times and $times[$last] < $self->{"max_added_time"}
!ll&&!rl&&r
000
MC/DC0%0/2
$last < $#times and $times[$last] < $self->{"max_added_time"}
$last < $#times $times[$last] < $self->{"max_added_time"}
Truth table0%0/3
A and B
A= $last < $#timesB= $times[$last] < $self->{"max_added_time"}
ABresult
0X0
100
111
539
540 0 foreach my $i ( $first .. $last ) {
541 $relevant{$camera_id}->{ display_time( $times[$i] ) } =
542 0 display_offset( $c->{ $times[$i] } );
543 }
544 }
545 }
546 }
547 0 return \%relevant;
548 }
549
550 =head2 stringify
551
552 $text = $co->stringify;
553
554 Returns the exportable contents of Image::Synchronize::CameraOffsets
555 C<$co> in YAML format. The return value is similar to
556
557 cameraid1:
558 2019-06-04T14:33:12: +1:03:15
559 2019-06-05T08:11:43: +1:03:15
560 cameraid2:
561 2019-01-01T00:00:00: +2:00
562 2019-06-05T08:11:43: +2:00
563
564 =cut
565
566 0 ▶sub stringify {
Subroutine stringify: not called CC=1 SCAR=6.9
Pod documented
567 0 my ($self) = @_;
568 0 return Dump($self->export_data);
569 }
570
571 0 ▶sub to_display {
Subroutine to_display: not called CC=3 SCAR=24.8
Pod undocumented
572 0 my ($self) = @_;
573 0 $self->make_effective;
574 0 my %relevant;
575 0 ▶ while ( my ( $camera_id, $c ) = each %{ $self->{effective} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"effective"};}) 0 0 0
576 0 my $e = $relevant{$camera_id} = {};
577 0 my @times = sort { $a <=> $b } keys %{$c};
578 0 foreach my $i ( 0 .. $#times ) {
579 $relevant{$camera_id}->{ display_time( $times[$i] ) } =
580 0 display_offset( $c->{ $times[$i] } );
581 }
582 }
583 0 return \%relevant;
584 }
585
586 0 ▶sub for_camera {
Subroutine for_camera: not called CC=2 SCAR=17.9
Pod undocumented
587 0 my ( $self, $camera_id ) = @_;
588 0 my %out;
589 0 foreach my $key (qw(base added effective)) {
590 0 $out{$key} = $self->{$key}->{$camera_id};
591 }
592 0 return \%out;
593 }
594
595 =head1 DEPENDENCIES
596
597 This module uses the following non-core Perl module:
598
599 =over
600
601 =item
602
603 Image::Synchronize::Timestamp
604
605 =item
606
607 namespace::clean
608
609 =item
610
611 YAML::Any
612
613 =back
614
615 =head1 AUTHOR
616
617 Louis Strous E<lt>LS@quae.nlE<gt>
618
619 =head1 LICENSE AND COPYRIGHT
620
621 Copyright (c) 2018 - 2020 Louis Strous.
622
623 This program is free software; you can redistribute it and/or modify
624 it under the same terms as Perl itself.
625
626 =cut
627
628 2 1;
« Synchronize/GroupedInfo.pm ↑ Summary Synchronize/GpsPositionCollection.pm »