lib/Power/Outlet/iBootBarGroup.pm untested

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 109 CC 9 SCAR 26.7
CC
9
Cov
0%
query
42.0
outlets
20.0
_host_default
2.0
CRAP 14.5
1 package Power::Outlet::iBootBarGroup;
2 use strict;
3 use warnings;
4 use base qw{Power::Outlet::Common::IP::SNMP};
5 use List::MoreUtils qw{any all};
6 use Time::HiRes qw{sleep};
7  
8 our $VERSION = '0.54';
9 our $_oid_outletEntry = '1.3.6.1.4.1.1418.4.3.1'; #enterprises.dataprobe.iBootBarAgent.outletTable.outletEntry
10  
11 =head1 NAME
12  
13 Power::Outlet::iBootBarGroup - Control and query multiple Dataprobe iBootBar power outlets together
14  
15 =head1 SYNOPSIS
16  
17 my $outlet=Power::Outlet::iBootBarGroup->new(
18 host => "mybar",
19 outlets => "1,2,3,4"
20 community => "private",
21 );
22 print $outlet->query, "\n"; #any on
23 print $outlet->on , "\n"; #all on
24 print $outlet->off , "\n"; #all off
25  
26 =head1 DESCRIPTION
27
28 Power::Outlet::iBootBar is a package for controlling and querying multiple outlets together on a Dataprobe iBootBar network attached power outlet.
29  
30 =head1 USAGE
31  
32 =head1 CONSTRUCTOR
33  
34 =head2 new
35  
36 my $outlet=Power::Outlet->new(type=>"iBootBarGroup", "host=>"mylamp", outlets=>"1,2,3,4");
37 my $outlet=Power::Outlet::iBootBarGroup->new(host=>"mylamp", outlets=>"1,2,3,4");
38  
39 =head1 PROPERTIES
40  
41 =head2 host
42  
43 Sets and returns the hostname or IP address.
44  
45 Manufacturer Default: 192.168.0.254
46  
47 Note: Set IP address via telnet User Name: admin, Password: admin then "help network"
48  
49 set ipmode dhcp
50  
51 OR
52  
53 set ipmode static
54 set ipaddress 192.168.0.254
55 set subnet 255.255.255.0
56 set gateway 192.168.0.1
57  
58 =cut
59  
60 sub _host_default {"192.168.0.254"};
61  
62 =head2 community
63  
64 Sets and returns the SNMP community.
65  
66 my $community = $outlet->community("private"); #read/write
67 my $community = $outlet->community("public"); #read only features
68  
69 Note: Set SNMP community via telnet User Name: admin, Password: admin then "help snmp"
70  
71 set snmp writecommunity private
72 set snmp readcommunity public
73 set snmp 1 enable yes
74  
75 =cut
76  
77 sub _community_default {"private"};
78  
79 =head2 outlets
80  
81 Sets and returns the outlets CSV list as labeled on the back of the device.
82  
83 Default: "1,2,3,4,5,6,7,8"
84  
85 =cut
86  
87 sub outlets {
88 my $self = shift;
89 $self->{"outlets"} = shift if @_;
90 $self->{"outlets"} = '1,2,3,4,5,6,7,8' unless $self->{"outlets"};
91 die("Error: outlets property invalid format") unless $self->{"outlets"} =~ m/\A([0-8]\,){0,7}[0-8]\Z/;
92 return $self->{"outlets"};
93 }
94  
95 sub _outlets_aref {[split /,/, shift->outlets]};
96  
97 sub _outletIndexes {[map {$_ - 1} @{shift->_outlets_aref}]};
98  
99 sub _oid_outletBuilder {
100 my $self = shift;
101 my $type = shift;
102 return [map {join(".", $_oid_outletEntry, $type, $_)} @{$self->_outletIndexes}]; #oids so MIB is not required
103 }
104  
105 sub _oid_outletStatus {shift->_oid_outletBuilder('3')};
106
107 sub _oid_outletCommand {shift->_oid_outletBuilder('4')};
108  
109 =head1 METHODS
110  
111 =head2 query
112  
113 Sends a TCP/IP message to the iBootBar device to query the current state
114  
115 =cut
116  
117 sub query {
118 my $self=shift;
119 if (defined wantarray) { #scalar and list context
120 #status = (0 => "OFF", 1 => "ON", 2 => "REBOOT", 3 => "CYCLE", 4 => "ONPENDING", 5 => "CYCLEPENDING");
121 my %return = $self->snmp_multiget($self->_oid_outletStatus);
122 my @values = values %return;
123 return 'ON' if any {$_ == 1} @values; #any ON
124 return 'OFF' if all {$_ == 0} @values; #all OFF
125 return 'CYCLE' if any {$_ == 3} @values; #any CYCLE
126 return 'CYCLE' if any {$_ == 5} @values; #any CYCLE
127 return 'UNKNOWN';
128 } else { #void context
129 return;
130 }
131 }
132  
133 =head2 on
134  
135 Sends a TCP/IP message to the iBoot device to Turn Power ON
136  
137 =cut
138  
139 sub on {
140 my $self = shift;
141 $self->snmp_multiset($self->_oid_outletCommand, 1);
142 sleep 0.4;
143 return $self->query;
144 }
145  
146 =head2 off
147  
148 Sends a TCP/IP message to the iBoot device to Turn Power OFF
149  
150 =cut
151  
152 sub off {
153 my $self = shift;
154 $self->snmp_multiset($self->_oid_outletCommand, 0);
155 sleep 0.4;
156 return $self->query;
157 }
158  
159 =head2 switch
160  
161 Queries the device for the current status and then requests the opposite.
162  
163 =cut
164  
165 #see Power::Outlet::Common->switch
166  
167 =head2 cycle
168  
169 Sends a TCP/IP message to the iBoot device to Cycle Power (ON-OFF-ON or OFF-ON-OFF). Cycle time is determined by Setup.
170  
171 Manufacturer Default Cycle Period: 10 seconds
172  
173 =cut
174  
175 sub cycle {
176 my $self = shift;
177 $self->snmp_multiset($self->_oid_outletCommand, 2);
178 sleep 0.4;
179 return $self->query;
180 }
181  
182 =head1 BUGS
183  
184 Please log on RT and send an email to the author.
185  
186 =head1 SUPPORT
187  
188 DavisNetworks.com supports all Perl applications including this package.
189  
190 =head1 AUTHOR
191  
192 Michael R. Davis
193 CPAN ID: MRDVT
194 DavisNetworks.com
195  
196 =head1 COPYRIGHT
197  
198 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
199  
200 The full text of the license can be found in the LICENSE file included with this module.
201  
202 =head1 SEE ALSO
203  
204 =cut
205  
206 1;