X10/SchedEvent.pm

filter: Statementstmt 15.4% 13 / 84 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 36.3% 4 / 11 Podpod 0.0% 0 / 7 totaltotal 9.7% 17 / 175 CC 29 SCAR 44.9
CC
29
Cov
9%
reschedule
156.0
time
132.0
new
20.0
CRAP 89.0
« X10/Event.pm ↑ Summary X10/Controller.pm »
1  
2 # Copyright (c) 1999-2017 Rob Fugina <robf@fugina.com>
3 # Distributed under the terms of the GNU Public License, Version 3.0
4  
5 package X10::SchedEvent;
6  
7 1 ▶use Time::ParseDate;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Astro::SunTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub new
15 0 ▶{
Subroutine new: not called CC=4 SCAR=30.0
Pod undocumented
16 0 my $type = shift;
17  
18 0 my $self = bless { @_ }, $type;
19  
20 0 ▶ unless ($self->{macro} && $self->{macro}->isa('X10::Macro'))
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"macro"} and $self->{"macro"}->isa("X10::Macro")) 0 0 0
Condition0%0/3
$self->{"macro"} and $self->{"macro"}->isa("X10::Macro")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"macro"} and $self->{"macro"}->isa("X10::Macro")
$self->{"macro"} $self->{"macro"}->isa("X10::Macro")
Truth table0%0/3
A and B
A= $self->{"macro"}B= $self->{"macro"}->isa("X10::Macro")
ABresult
0X0
100
111
21 {
22 0 warn "No macro sent to create SchedEvent";
23 0 return undef;
24 }
25  
26 0 ▶ $self->{verbose} = 1 if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
27  
28 0 $self->{next_time} = int(time);
29 0 $self->reschedule;
30 0 $self->{last_time} = 0;
31  
32 0 return $self;
33 }
34  
35  
36  
37 sub run
38 0 ▶{
Subroutine run: not called CC=3 SCAR=24.8
Pod undocumented
39 0 my $self = shift;
40  
41 $self->{logger}->('info', "Running %s",
42 $self->{description} || 'unnamed event',
43 0 ▶ ) if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
Condition0%0/2
$self->{'description'} || 'unnamed event'
l!l
00
MC/DC0%0/1
$self->{'description'} || 'unnamed event'
$self->{'description'}
Truth table0%0/2
A
A= $self->{'description'}
Aresult
11
00
44  
45 0 $self->{macro}->run;
46 }
47  
48 sub reschedule
49 0 ▶{
Subroutine reschedule: not called CC=12 SCAR=50.5
Pod undocumented
50 0 my $self = shift;
51  
52 0 ▶ $self->{last_time} = $self->{next_time} || 0;
Condition0%0/2
$self->{'next_time'} || 0
l!l
00
MC/DC0%0/1
$self->{'next_time'} || 0
$self->{'next_time'}
Truth table0%0/2
A
A= $self->{'next_time'}
Aresult
11
00
53  
54 0 ▶ my $current = $self->{last_time} || time;
Condition0%0/2
$self->{'last_time'} || CORE::time
l!l
00
MC/DC0%0/1
$self->{'last_time'} || CORE::time
$self->{'last_time'}
Truth table0%0/2
A
A= $self->{'last_time'}
Aresult
11
00
55 0 my $next;
56  
57 0 ▶ if (!exists $self->{repeat_type} || $self->{repeat_type} eq 'none')
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $self->{'repeat_type'} or $self->{'repeat_type'} eq 'none') { } 0 0 0
elsif ($self->{'repeat_type'} eq 'day') { } 0 0 0
Condition0%0/3
not exists $self->{"repeat_type"} or $self->{"repeat_type"} eq "none"
l!l&&r!l&&!r
000
MC/DC0%0/2
not exists $self->{"repeat_type"} or $self->{"repeat_type"} eq "none"
not exists $self->{"repeat_type"} $self->{"repeat_type"} eq "none"
Truth table0%0/3
not(A) or B
A= not exists $self->{"repeat_type"}B= $self->{"repeat_type"} eq "none"
ABresult
1X1
011
000
58 {
59 0 return 0;
60 }
61 elsif ($self->{repeat_type} eq 'day')
62 {
63 0 my $new = parsedate(sprintf("today %s", $self->time('today')));
64  
65 0 ▶ if ($new <= time || $new <= $current)
Branch: 1 branch
BranchTrueFalseTotal
if ($new <= CORE::time or $new <= $current) 0 0 0
Condition0%0/3
$new <= CORE::time or $new <= $current
l!l&&r!l&&!r
000
MC/DC0%0/2
$new <= CORE::time or $new <= $current
$new <= CORE::time $new <= $current
Truth table0%0/3
A or B
A= $new <= CORE::timeB= $new <= $current
ABresult
1X1
011
000
66 {
67 0 $new = parsedate(sprintf("tomorrow %s", $self->time('tomorrow')));
68 }
69  
70 # check DOW loop here...
71 0 ▶ if (exists $self->{dowmask} && $self->{dowmask} > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"dowmask"} and $self->{"dowmask"} > 0) 0 0 0
Condition0%0/3
exists $self->{"dowmask"} and $self->{"dowmask"} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"dowmask"} and $self->{"dowmask"} > 0
exists $self->{"dowmask"} $self->{"dowmask"} > 0
Truth table0%0/3
A and B
A= exists $self->{"dowmask"}B= $self->{"dowmask"} > 0
ABresult
0X0
100
111
72 {
73 0 my @newarray = localtime($new);
74  
75 0 ▶ while ( ! ( (1 << $newarray[6]) & $self->{dowmask} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (1 << $newarray[6] & $self->{"dowmask"}) 0 0 0
76 {
77 $self->{logger}->('info', "Skipping %s...", strftime("%a %b %e %Y", @newarray))
78 0 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
79 0 $newarray[3] += 1; # add one day
80  
81 0 @newarray = localtime(mktime(@newarray)); # normalize
82  
83 # find new time on that day...
84  
85 0 my $datestr = strftime("%a %b %e %Y", @newarray);
86  
87 0 my $time = $self->time($datestr);
88  
89 0 $new = parsedate(sprintf("%s %s", $time, $datestr), WHOLE => 1);
90 0 @newarray = localtime($new);
91 }
92  
93 }
94  
95 0 $next = $new;
96 }
97 else
98 {
99 0 $self->{logger}->('info', "Unsupported repeat type: %s", $self->{repeat_type});
100 }
101  
102  
103 0 $self->{next_time} = $next;
104  
105 0 return 1;
106 }
107  
108 sub next_time
109 0 ▶{
Subroutine next_time: not called CC=1 SCAR=6.9
Pod undocumented
110 0 my $self = shift;
111  
112 0 return $self->{next_time};
113 }
114  
115 sub controller
116 0 ▶{
Subroutine controller: not called CC=2 SCAR=17.9
Pod undocumented
117 0 my $self = shift;
118  
119 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
120 {
121 0 $self->{macro}->controller(shift);
122 }
123  
124 0 return $self->{macro}->controller;
125 }
126  
127 sub description
128 0 ▶{
Subroutine description: not called CC=2 SCAR=17.9
Pod undocumented
129 0 my $self = shift;
130  
131 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
132 {
133 0 $self->{description} = shift;
134 }
135  
136 0 return $self->{description};
137 }
138  
139 sub time
140 0 ▶{
Subroutine time: not called CC=11 SCAR=48.8
Pod undocumented
141 0 my $self = shift;
142 0 my $date = shift;
143  
144 # offsets assumed not to force time across day boundaries...
145  
146 0 my $time;
147 my $sign;
148 0 my $offset;
149  
150 0 ▶ if ($self->{time} =~ /^\d?\d:\d\d$/)
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'time'} =~ /^\d?\d:\d\d$/) { } 0 0 0
elsif ($self->{'time'} =~ /^sunrise(\s*([+-])\s*(\d+))?$/) { } 0 0 0
elsif ($self->{'time'} =~ /^sunset(\s*([+-])\s*(\d+))?$/) { } 0 0 0
151 {
152 0 $time = $self->{time};
153 0 $offset = 0;
154 }
155 elsif ($self->{time} =~ /^sunrise(\s*([+-])\s*(\d+))?$/)
156 {
157 0 ▶ $sign = ($2 eq '-') ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$2 eq '-' ? : 0 0 0
158 0 ▶ $offset = $3 || 0;
Condition0%0/2
$3 || 0
l!l
00
MC/DC0%0/1
$3 || 0
$3
Truth table0%0/2
A
A= $3
Aresult
11
00
159 $time = sun_time(type => 'rise', date => $date,
160 latitude => $self->{latitude},
161 longitude => $self->{longitude},
162 0 );
163 }
164 elsif ($self->{time} =~ /^sunset(\s*([+-])\s*(\d+))?$/)
165 {
166 0 ▶ $sign = ($2 eq '-') ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$2 eq '-' ? : 0 0 0
167 0 ▶ $offset = $3 || 0;
Condition0%0/2
$3 || 0
l!l
00
MC/DC0%0/1
$3 || 0
$3
Truth table0%0/2
A
A= $3
Aresult
11
00
168 $time = sun_time(type => 'set', date => $date,
169 latitude => $self->{latitude},
170 longitude => $self->{longitude},
171 0 );
172 }
173 else
174 {
175 0 $self->{logger}->('info', "Unknown time string: %s", $self->{time});
176 0 return undef;
177 }
178  
179 0 ▶ return $time unless $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless $offset 0 0 0
180  
181 0 my ($hour, $minute) = $time =~ /^(\d?\d):(\d\d)$/;
182  
183 0 $minute += $sign * $offset;
184  
185 0 ▶ while ($minute >= 60)
Branch: 1 branch
BranchTrueFalseTotal
if ($minute >= 60) 0 0 0
186 {
187 0 $minute -= 60;
188 0 $hour++;
189 }
190  
191 0 ▶ while ($minute < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($minute < 0) 0 0 0
192 {
193 0 $minute += 60;
194 0 $hour--;
195 }
196  
197 0 return sprintf("%s:%02s", $hour, $minute);
198 }
199  
200  
201 1 1;
202  
« X10/Event.pm ↑ Summary X10/Controller.pm »