blib/lib/LightWaveRF.pm

filter: Statementstmt 65.3% 34 / 52 Branchbran 21.4% 3 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.9% 10 / 11 Podpod 83.3% 5 / 6 totaltotal 62.6% 52 / 83 CC 8 SCAR 22.4
CC
8
Cov
56%
get_kwh
30.0
get_next_msg_id
2.0
_send_status
2.0
CRAP 9.4
↑ Summary
1 package LightWaveRF;
2  
3 =head1 NAME
4
5 LightWaveRF - Integration with LightWaveRF modules
6
7 =head1 SYNOPSIS
8
9 use LightWaveRF;
10 my $lw = new LightWaveRF;
11 $lw->register('D1', 'R1', "LivingRoom");
12 $lw->on('LivingRoom');
13
14 =head1 DESCRIPTION
15
16 Provides an interface to LightWaveRF modules via the LightWaveRF Wifi Link.
17  
18 =cut
19  
20 =head2 Methods
21  
22 =head3 new
23  
24 my $lf = new LightWaveRF;
25  
26 Instantiates a LightWaveRF object ready to register devices against.
27  
28 =cut
29  
30  
31 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 2 our $VERSION = 0.04;
36  
37 2 ▶has '_devices' => (is => 'rw', default => sub{{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
38 2 has '_current_msg_id' => (is => 'rw', default => 0 );
39 2 has '_port' => (is => 'ro', default => 9760);
40  
41 =head3 register
42  
43 $lw->register(<NODE ID>, <DEVICE ID>, <NAME>);
44 $lw->register('R1', 'LivingRoomLight');
45  
46 =cut
47 2 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod documented
48 2 my ( $self, $node_id, $device_id, $name ) = @_;
49
50 2 $self->_devices->{$name} = {node => $node_id, id => $device_id};
51 }
52  
53 =head3 on
54
55 $lw->on('LivingRoomLight'); # $lw->send_device_status('LivingRoomlight', 'F1');
56  
57 =cut
58 1 ▶sub on {
Subroutine on: called CC=1 SCAR=0.0
Pod documented
59 1 my ( $self, $name ) = @_;
60 1 return $self->send_device_status($name, 'F1');
61 }
62  
63 =head3 off
64
65 $lw->on('LivingRoomLight'); # $lw->send_device_status('LivingRoomlight', 'F0');
66  
67 =cut
68 1 ▶sub off {
Subroutine off: called CC=1 SCAR=0.0
Pod documented
69 1 my ( $self, $name ) = @_;
70 1 return $self->send_device_status($name, 'F0');
71 }
72  
73  
74 =head3 send_device_status
75  
76 $lw->send_device_status('LivingRoomlight', 'F1');
77  
78 =cut
79 2 ▶sub send_device_status {
Subroutine send_device_status: called CC=2 SCAR=7.0
Pod documented
80 2 my ( $self, $name, $status ) = @_;
81 2 my $device = $self->_devices->{$name};
82  
83 2 ▶ return undef unless $device;
Branch: 1 branch
BranchTrueFalseTotal
unless $device 0 2 2
84  
85 2 $self->_send_status($device->{'node'}, $device->{'id'}, $status);
86  
87 2 return 1;
88 }
89  
90 2 ▶sub get_next_msg_id {
Subroutine get_next_msg_id: called CC=2 SCAR=7.0
Pod undocumented
91 2 my $self = shift;
92  
93 2 ▶ $self->_current_msg_id(0) if($self->_current_msg_id) >= 999;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_current_msg_id >= 999 0 2 2
94 2 my $msg_id = $self->_current_msg_id($self->_current_msg_id+1);
95 2 return $msg_id;
96 }
97  
98 #Broadcast a status.
99 2 ▶sub _send_status {
Subroutine _send_status: called CC=2 SCAR=7.0
100 2 my ( $self, $node, $device_id, $status ) = @_;
101  
102 2 my $broadcast_string = sprintf("%03d", $self->get_next_msg_id ). ",!$device_id$node$status|";
103  
104 2 ▶ my $sock = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
my $sock = "IO::Socket::INET"->new("PeerPort", $self->_port, "PeerAddr", inet_ntoa("\377\377\377\377"), "Proto", "udp", "Broadcast", 1) or die "Can't bind : $@\n" 2 0 2
105 PeerPort => $self->_port,
106 PeerAddr => inet_ntoa(INADDR_BROADCAST),
107 Proto => 'udp',
108 Broadcast => 1 )
109 or die "Can't bind : $@\n";
110  
111 2 $sock->send($broadcast_string);
112 }
113  
114 =head2 get_kwh
115  
116 returns the current wattage from the power meter
117  
118 =cut
119 0 ▶sub get_kwh {
Subroutine get_kwh: not called CC=5 SCAR=34.0
Pod documented
120 #This routine is horrible and completely needs refactoring
121 0 my $self = shift;
122  
123  
124 0 my $sel = IO::Select->new;
125 0 ▶ my $in_sock = IO::Socket::INET -> new (LocalPort => 9761,
Branch: 1 branch
BranchTrueFalseTotal
my $in_sock = "IO::Socket::INET"->new("LocalPort", 9761, "Broadcast", 1, "Proto", "udp") or die "Failed to bind to socket: $@" 0 0 0
126 Broadcast => 1,
127 Proto => 'udp')
128 or die "Failed to bind to socket: $@";
129  
130 0 $sel->add($in_sock);
131 0 my $timeout = 2;
132 0 my $mess;
133  
134  
135 0 ▶ my $sock = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
my $sock = "IO::Socket::INET"->new("PeerPort", $self->_port, "PeerAddr", inet_ntoa("\377\377\377\377"), "Proto", "udp", "Broadcast", 1) or die "Can't bind : $@\n" 0 0 0
136 PeerPort => $self->_port,
137 PeerAddr => inet_ntoa(INADDR_BROADCAST),
138 Proto => 'udp',
139 Broadcast => 1 )
140 or die "Can't bind : $@\n";
141  
142 0 $sock->send("@?W|EcoQuery");
143  
144  
145 0 while (1) {
146 0 my @r = $sel->can_read($timeout);
147 0 ▶ unless (@r) { last; } #Tiemout
Branch: 1 branch
BranchTrueFalseTotal
unless (@r) 0 0 0
148 0 $in_sock -> recv ($mess, 1024);
149 0 last;
150 }
151  
152 0 ▶ return unless($mess);
Branch: 1 branch
BranchTrueFalseTotal
unless $mess 0 0 0
153 0 $mess =~ /W=(\d*)\,(\d*)\,(\d*),(\d*)/;
154 0 my $watts = $1;
155  
156 0 return $watts/1000;
157  
158 }
159  
160  
161 =head1 AUTHOR
162 Graeme Lawton <graeme@per.ly>
163 =cut
164  
165 =head2 Development
166  
167 This module is very much under development and not really production ready yet
168 and there is lots of functinality still to be added to it. Patches welcome git
169 repo is at https://github.com/grim8634/LightWaveRF.git
170  
171 =cut
172 2 1;
↑ Summary