File Coverage

blib/lib/App/AquariumHive/Plugin/GemBird.pm
Criterion Covered Total %
statement 16 57 28.0
branch 0 20 0.0
condition n/a
subroutine 6 9 66.6
pod 0 1 0.0
total 22 87 25.2


line stmt bran cond sub pod time code
1             package App::AquariumHive::Plugin::GemBird;
2             BEGIN {
3 1     1   1143 $App::AquariumHive::Plugin::GemBird::AUTHORITY = 'cpan:GETTY';
4             }
5             $App::AquariumHive::Plugin::GemBird::VERSION = '0.003';
6 1     1   9 use Moo;
  1         2  
  1         6  
7 1     1   258 use App::AquariumHive::Tile;
  1         1  
  1         15  
8 1     1   3 use JSON::MaybeXS;
  1         6  
  1         48  
9 1     1   493 use String::Trim;
  1         560  
  1         43  
10 1     1   365 use App::AquariumHive::Plugin::GemBird::Socket;
  1         3  
  1         563  
11              
12             with qw(
13             App::AquariumHive::Role
14             );
15              
16             has sockets => (
17             is => 'lazy',
18             );
19              
20             sub _build_sockets {
21 0     0     my ( $self ) = @_;
22 0           my @lines = $self->run_cmd('sudo sispmctl -s');
23 0           my @sockets;
24             my $current;
25 0           for (@lines) {
26 0 0         unless ($current) {
27 0 0         if ($_ =~ m/^Gembird #\d+$/) {
28 0           $current = {
29             app => $self->app,
30             };
31             }
32             } else {
33 0 0         if ($_ =~ m/^device type:(.+)$/) {
    0          
    0          
34 0           $current->{device_type} = trim($1);
35             } elsif ($_ =~ m/^serial number:(.+)$/) {
36 0           $current->{serial_number} = trim($1);
37             } elsif ($_ =~ m/^$/) {
38 0           push @sockets, App::AquariumHive::Plugin::GemBird::Socket->new(%{$current});
  0            
39 0           $current = undef;
40             }
41             }
42             }
43 0           return \@sockets;
44             }
45              
46             sub BUILD {
47 0     0 0   my ( $self ) = @_;
48              
49 0           $self->sockets;
50              
51             $self->on_socketio( set_power => sub {
52 0     0     my ( $app, $data ) = @_;
53 0           my %new_states;
54 0           for my $key (keys %{$data}) {
  0            
55 0           my $new_state = $data->{$key};
56 0 0         if ($key =~ m/^([0-9a-f]+)_(\d+)$/) {
57 0           my $socket_id = $1;
58 0           my $no = $2;
59 0           my ( $socket ) = grep { $_->socket_id eq $socket_id } @{$self->sockets};
  0            
  0            
60 0 0         if ($socket) {
61 0           $new_states{$socket_id.'_'.$no} = $new_state;
62 0           $socket->set($no,$new_state);
63             }
64             }
65             }
66 0 0         if (%new_states) {
67 0           $self->send( power => \%new_states );
68             }
69 0           });
70              
71 0           for my $socket (@{$self->sockets}) {
  0            
72 0           my $socket_id = $socket->socket_id;
73 0           my $state = $socket->state;
74 0           for my $no (1..4) {
75 0           my $id = $socket_id.'_'.$no;
76 0 0         my $onoff = $state->{$no} ? 'ON' : 'OFF';
77 0 0         $self->add_tile( 'power_'.$id, App::AquariumHive::Tile->new(
78             id => 'power_'.$id,
79             bgcolor => $state->{$no} ? 'green' : 'red',
80             content => <<"__HTML__",
81              
82             <div class="large">
83             <div class="fattext">$no</div>
84             <div class="fattext" id="power_val_$id">$onoff</div>
85             <small>$socket_id</small>
86             </div>
87              
88             __HTML__
89             js => <<"__JS__",
90              
91             \$('#power_$id').click(function(){
92             var current = \$('#power_val_$id').text();
93             var set_state;
94             if (current == 'ON') {
95             set_state = 0;
96             } else {
97             set_state = 1;
98             }
99             socket.emit('set_power',{ '$id': set_state });
100             });
101              
102             socket.on('power', function(power){
103             if ('$id' in power) {
104             var onoff;
105             \$('#power_$id').removeClass('bg-red').removeClass('bg-green');
106             if (power['$id']) {
107             onoff = 'ON';
108             \$('#power_$id').addClass('bg-green');
109             } else {
110             onoff = 'OFF';
111             \$('#power_$id').addClass('bg-red');
112             }
113             \$('#power_val_$id').text(onoff);
114             }
115             });
116              
117             __JS__
118             ));
119             }
120             }
121              
122             }
123              
124             1;
125              
126             __END__
127              
128             =pod
129              
130             =head1 NAME
131              
132             App::AquariumHive::Plugin::GemBird
133              
134             =head1 VERSION
135              
136             version 0.003
137              
138             =head1 DESCRIPTION
139              
140             B<IN DEVELOPMENT, DO NOT USE YET>
141              
142             See L<http://aquariumhive.com/> for now.
143              
144             =head1 SUPPORT
145              
146             IRC
147              
148             Join #AquariumHive on irc.freenode.net. Highlight Getty for fast reaction :).
149              
150             Repository
151              
152             https://github.com/homehivelab/aquariumhive
153             Pull request and additional contributors are welcome
154              
155             Issue Tracker
156              
157             https://github.com/homehivelab/aquariumhive/issues
158              
159             =head1 AUTHOR
160              
161             Torsten Raudssus <torsten@raudss.us>
162              
163             =head1 COPYRIGHT AND LICENSE
164              
165             This software is copyright (c) 2014 by Torsten Raudssus.
166              
167             This is free software; you can redistribute it and/or modify it under
168             the same terms as the Perl 5 programming language system itself.
169              
170             =cut