File Coverage

blib/lib/Power/Outlet/Common.pm
Criterion Covered Total %
statement 38 40 95.0
branch 17 22 77.2
condition n/a
subroutine 12 13 92.3
pod 8 8 100.0
total 75 83 90.3


line stmt bran cond sub pod time code
1             package Power::Outlet::Common;
2 15     15   2254 use strict;
  15         33  
  15         431  
3 15     15   79 use warnings;
  15         33  
  15         379  
4 15     15   104 use Time::HiRes qw{};
  15         32  
  15         242  
5 15     15   106 use base qw{Package::New};
  15         31  
  15         7597  
6              
7             our $VERSION = '0.47';
8             our $STATE = 'OFF';
9              
10             =head1 NAME
11              
12             Power::Outlet::Common - Power::Outlet base class for all power outlets
13              
14             =head1 SYNOPSIS
15              
16             use base qw{Power::Outlet::Common};
17              
18             =head1 DESCRIPTION
19            
20             Power::Outlet::Common is a base class for controlling and querying a power outlets.
21              
22             =head1 USAGE
23              
24             use base qw{Power::Outlet::Common};
25              
26             =head1 CONSTRUCTOR
27              
28             =head2 new
29              
30             =head1 METHODS
31              
32             =head2 action
33              
34             Smart case insensitive text-based wrapper around methods 0|ON => on, 1|OFF => off, SWITCH|TOGGLE => switch, CYCLE => cycle, QUERY => query
35              
36             my $state = $outlet->action("on");
37             my $state = $outlet->action("1");
38             my $state = $outlet->action("off");
39             my $state = $outlet->action("0");
40             my $state = $outlet->action("switch");
41             my $state = $outlet->action("toggle");
42              
43             =cut
44              
45             sub action {
46 35     35 1 84 my $self = shift;
47 35         80 my $action = shift;
48 35 100       361 if ($action =~ m/\A(?:1|ON)\Z/i) {
    100          
    100          
    100          
    50          
49 2         6 $self->on;
50             } elsif ($action =~ m/\A(?:0|OFF)\Z/i) {
51 3         7 $self->off;
52             } elsif ($action =~ m/\A(?:SWITCH|TOGGLE)\Z/i) {
53 4         9 $self->switch;
54             } elsif ($action =~ m/\A(?:CYCLE)\Z/i) {
55 2         13 $self->cycle;
56             } elsif ($action =~ m/\A(?:QUERY)\Z/i) {
57 24         63 $self->query;
58             } else {
59 0         0 die(qq{Error: action "$action" not supported});
60             }
61             }
62              
63             =head2 query
64              
65             The query method must be overridden in the sub class.
66              
67             my $state = $outlet->query; #returns ON|OFF Note: may return other values for edge case
68              
69             =cut
70              
71 32     32 1 225 sub query {return $STATE};
72              
73             =head2 on
74              
75             The on method must be overridden in the sub class.
76              
77             my $state = $outlet->on; #turns the outlet on reguardless of current state and returns ON.
78              
79             Note: This should cancel any non-blocking cycle requests
80              
81             =cut
82              
83 7     7 1 933 sub on {return $STATE = 'ON'};
84              
85             =head2 off
86              
87             The off method must be overridden in the sub class.
88              
89             my $state = $outlet->off; #turns the outlet off reguardless of current state and returns OFF.
90              
91             Note: This should cancel any non-blocking cycle requests
92              
93             =cut
94              
95 7     7 1 83 sub off {return $STATE = 'OFF'};
96              
97             =head2 switch
98              
99             Only override the switch method if your hardware natively supports this capability. However, it should still be documented.
100              
101             my $state = $outlet->switch; #turns the outlet off if on and on if off and returns the final state ON|OFF.
102              
103             Note: The default implementations does not cancel non-blocking cycle requests
104              
105             =cut
106              
107             sub switch {
108 28     28 1 16833 my $self = shift;
109 28         139 my $query = $self->query;
110 28 50       6777 return $query eq 'OFF' ? $self->on :
    100          
111             $query eq 'ON' ? $self->off :
112             $query; #e.g. CYCLE, BUSY, etc.
113             }
114              
115             =head2 cycle
116              
117             Only override the cycle method if your hardware natively supports this capability. However, it should still be documented.
118              
119             my $state = $outlet->cycle; #turns the outlet off-on-off or on-off-on with a delay and returns the final state ON|OFF.
120              
121             Note: Implementations may be blocking or non-blocking.
122              
123             =cut
124              
125             sub cycle {
126 7     7 1 8060 my $self = shift;
127 7         29 $self->switch;
128 7         47 Time::HiRes::sleep $self->cycle_duration; #blocking. Maybe we should be non-blocking somehow.
129 7         255 return $self->switch;
130             }
131              
132             =head2 cycle_duration
133              
134             Override the cycle_duration method if you want.
135              
136             Default; 10 seconds (floating point number)
137              
138             =cut
139              
140             sub cycle_duration {
141 7     7 1 19 my $self = shift;
142 7 50       42 $self->{'cycle_duration'} = shift if @_;
143 7 100       41 $self->{'cycle_duration'} = 10 unless defined $self->{'cycle_duration'};
144 7         70002194 return $self->{'cycle_duration'};
145             }
146              
147             =head2 name
148              
149             User friendly name for an outlet.
150              
151             =cut
152              
153             sub name {
154 2     2 1 6 my $self = shift;
155 2 50       8 $self->{'name'} = shift if @_;
156 2 50       8 $self->{'name'} = $self->_name_default unless defined $self->{'name'};
157 2         8 return $self->{'name'};
158             }
159              
160 0     0     sub _name_default {''};
161              
162             =head1 BUGS
163              
164             Please log on RT and send an email to the author.
165              
166             =head1 SUPPORT
167              
168             DavisNetworks.com supports all Perl applications including this package.
169              
170             =head1 AUTHOR
171              
172             Michael R. Davis
173             CPAN ID: MRDVT
174             DavisNetworks.com
175              
176             =head1 COPYRIGHT
177              
178             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
179              
180             The full text of the license can be found in the LICENSE file included with this module.
181              
182             =head1 SEE ALSO
183              
184             =cut
185              
186             1;