lib/Power/Outlet/iBoot.pm

filter: Statementstmt 58.5% 24 / 41 Branchbran 25.0% 3 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 61.5% 8 / 13 Podpod 100% 5 / 5 totaltotal 56.3% 40 / 71 CC 7 SCAR 22.1
CC
7
Cov
51%
_send
30.0
pass
3.0
query
2.0
CRAP 9.2
« lib/Power/Outlet/TuyaAPI.pm ↑ Summary lib/Power/Outlet/HomeAssistantAPI.pm »
1 package Power::Outlet::iBoot;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use base qw{Power::Outlet::Common::IP};
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Time::HiRes qw{sleep};
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '0.54';
9 3 our $PACKAGE = __PACKAGE__;
10  
11 =head1 NAME
12  
13 Power::Outlet::iBoot - Control and query a Dataprobe iBoot power outlet
14  
15 =head1 SYNOPSIS
16  
17 my $outlet=Power::Outlet::iBoot->new(
18 host => "mylamp",
19 port => 80, #sane default from manufacture spec
20 auth => "PASS", #sane default from manufacture spec
21 );
22 print $outlet->query, "\n";
23 print $outlet->on, "\n";
24 print $outlet->off, "\n";
25  
26 =head1 DESCRIPTION
27
28 Power::Outlet::iBoot is a package for controlling and querying a Dataprobe iBoot network attached power outlet.
29  
30 iBoot Protocol: The iBoot uses the TCP (Transport Communication Protocol) to communicate with the client system. To communicate with iBoot, establish a TCP connection using the Port as assigned in iBoot Setup. Once connected use the Send() function to send the commands to the iBoot and the Recv() function to receive the response. After sending a response iBoot will close the connection. The following outlines the commands and their responses.
31  
32 Source: http://dataprobe.com/files/power/iboot_tcp.pdf
33  
34 =head1 USAGE
35  
36 use Power::Outlet::iBoot;
37 use DateTime;
38 my $lamp=Power::Outlet::iBoot->new(host=>"lamp");
39 my $hour=DateTime->now->hour;
40 my $night=$hour > 20 ? 1 : $hour < 06 ? 1 : 0;
41 if ($night) {
42 print $lamp->on, "\n";
43 } else {
44 print $lamp->off, "\n";
45 }
46  
47 =head1 CONSTRUCTOR
48  
49 =head2 new
50  
51 my $outlet=Power::Outlet->new(type=>"iBoot", "host=>"mylamp");
52 my $outlet=Power::Outlet::iBoot->new(host=>"mylamp");
53  
54  
55 =head1 PROPERTIES
56  
57 =head2 host
58  
59 Sets and returns the hostname or IP address.
60  
61 Manufacturer Default: 192.168.1.254
62  
63 =cut
64  
65 2 ▶sub _host_default {"192.168.1.254"};
Subroutine _host_default: called CC=1 SCAR=0.0
66  
67 =head2 port
68  
69 Sets and returns the TCP port
70  
71 Manufacturer Default: 80
72  
73 =cut
74  
75 2 ▶sub _port_default {"80"};
Subroutine _port_default: called CC=1 SCAR=0.0
76  
77 =head2 pass
78  
79 Sets and returns the case sensitive password
80  
81 Manufacturer Default: PASS
82  
83 =cut
84  
85 4 ▶sub pass {
Subroutine pass: called CC=3 SCAR=11.0
Pod documented
86 4 my $self=shift;
87 4 ▶ $self->{"pass"}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 4 4
88 4 ▶ $self->{"pass"}="PASS" unless defined $self->{"pass"}; #MFG Default
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"pass"} 2 2 4
89 4 return $self->{"pass"};
90 }
91  
92 =head2 name
93  
94 =cut
95  
96 =head1 METHODS
97  
98 =head2 query
99  
100 Sends a TCP/IP message to the iBoot device to query the current state
101  
102 =cut
103  
104 0 ▶sub _send {
Subroutine _send: not called CC=5 SCAR=34.0
105 0 my $self=shift;
106 0 ▶ my $cmd=shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $cmd = shift() or die 0 0 0
107 0 my $msg=sprintf("\e%s\e%s\r", $self->pass, $cmd);
108 #from docs "After sending a response iBoot will close the connection"
109 0 my $sock=IO::Socket::INET->new(PeerAddr => $self->host,
110 PeerPort => $self->port,
111 Proto => 'tcp');
112 0 ▶ die(sprintf(qq{Error: $PACKAGE could not connect to host "%s" on port "%s".\n},
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sock 0 0 0
113 $self->host, $self->port)) unless defined $sock;
114 0 $sock->send($msg);
115 0 sleep 0.1; #the manufacturer example uses a full second
116 0 my $stat="";
117 0 $sock->recv($stat, 5);
118 0 ▶ die(sprintf(qq{Error: $PACKAGE TCP/IP receive error with host "%s" on port "%s".\n},
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stat 0 0 0
119 $self->host, $self->port)) unless defined $stat;
120 0 ▶ die(sprintf(qq{Error: $PACKAGE receive error with host "%s" on port "%s". Verify password.\n},
Branch: 1 branch
BranchTrueFalseTotal
unless $stat 0 0 0
121 $self->host, $self->port)) unless $stat;
122 0 $sock->close;
123 0 return $stat;
124 }
125  
126 0 ▶sub query {shift->_send("q")};
Subroutine query: not called CC=1 SCAR=6.9
Pod documented
127  
128 =head2 on
129  
130 Sends a TCP/IP message to the iBoot device to Turn Power ON
131  
132 =cut
133  
134 0 ▶sub on {shift->_send("n")};
Subroutine on: not called CC=1 SCAR=6.9
Pod documented
135  
136 =head2 off
137  
138 Sends a TCP/IP message to the iBoot device to Turn Power OFF
139  
140 =cut
141  
142 0 ▶sub off {shift->_send("f")};
Subroutine off: not called CC=1 SCAR=6.9
Pod documented
143  
144 =head2 switch
145  
146 Queries the device for the current status and then requests the opposite.
147  
148 =cut
149  
150 #see Power::Outlet::Common->switch
151  
152 =head2 cycle
153  
154 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.
155  
156 Manufacturer Default Cycle Period: 10 seconds
157  
158 =cut
159  
160 0 ▶sub cycle {shift->_send("c")};
Subroutine cycle: not called CC=1 SCAR=6.9
Pod documented
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 3 1;
« lib/Power/Outlet/TuyaAPI.pm ↑ Summary lib/Power/Outlet/HomeAssistantAPI.pm »