File Coverage

blib/lib/X10/Event.pm
Criterion Covered Total %
statement 9 51 17.6
branch 0 26 0.0
condition 0 36 0.0
subroutine 3 10 30.0
pod 0 7 0.0
total 12 130 9.2


line stmt bran cond sub pod time code
1              
2             # Copyright (c) 1999-2017 Rob Fugina
3             # Distributed under the terms of the GNU Public License, Version 3.0
4              
5             package X10::Event;
6              
7 1     1   3 use vars qw(@ISA);
  1         2  
  1         50  
8              
9 1     1   836 use Storable;
  1         3622  
  1         90  
10              
11             @ISA = qw(Storable);
12              
13 1     1   6 use strict;
  1         1  
  1         810  
14              
15             sub new
16             {
17 0     0 0   my $type = shift;
18              
19 0           my $self;
20              
21 0 0         if (@_ == 1)
22             {
23 0           my ($hc, $uc, $func) = &parse_string(shift);
24              
25 0 0 0       return undef unless ($hc && $uc && $func);
      0        
26              
27 0           $self = {
28             house_code => $hc,
29             unit_code => $uc,
30             func => $func,
31             }
32             }
33              
34 0           bless $self, $type;
35              
36 0           return $self;
37             }
38              
39             sub house_code
40             {
41 0     0 0   my $self = shift;
42 0           return $self->{house_code};
43             }
44              
45             sub unit_code
46             {
47 0     0 0   my $self = shift;
48 0           return $self->{unit_code};
49             }
50              
51             sub func
52             {
53 0     0 0   my $self = shift;
54 0           return $self->{func};
55             }
56              
57             sub as_string
58             {
59 0     0 0   my $self = shift;
60              
61 0 0 0       if ($self->unit_code eq 'ALL' || $self->unit_code eq 'LIGHTS')
62             {
63 0           return join(' ', $self->house_code, $self->unit_code, $self->func);
64             }
65             else
66             {
67 0           return sprintf("%s%02s %s", $self->house_code, $self->unit_code, $self->func);
68             }
69             }
70              
71             ###
72              
73             # build a string of words that implement this event
74             sub compile
75             {
76 0     0 0   my $self = shift;
77              
78 0           my @words = ();
79              
80 0 0 0       if ($self->func eq 'ON' || $self->func eq 'OFF')
    0 0        
81             {
82 0 0 0       if ($self->unit_code eq 'LIGHTS' && $self->func eq 'ON')
    0 0        
    0 0        
83             {
84 0           push @words, sprintf("%sL1", $self->house_code);
85             }
86             elsif ($self->unit_code eq 'ALL' && $self->func eq 'OFF')
87             {
88 0           push @words, sprintf("%sA0", $self->house_code);
89             }
90             elsif ($self->unit_code > 0 && $self->unit_code <= 16)
91             {
92 0           push @words,
93             sprintf("%s%02s", $self->house_code, $self->unit_code),
94             sprintf("%s%2s", $self->house_code, substr($self->func, 0, 2));
95             }
96             else
97             {
98 0           warn sprintf "Unknown command: %s %s %s (%s)",
99             $self->house_code, $self->unit_code, $self->func, $_;
100             }
101             }
102             elsif ($self->func eq 'DIM' || $self->func eq 'BRIGHT')
103             {
104 0 0 0       if ($self->unit_code > 0 && $self->unit_code <= 16)
105             {
106 0           push @words,
107             sprintf("%s%02s", $self->house_code, $self->unit_code),
108             sprintf("%s%2s", $self->house_code, substr($self->func, 0, 2));
109             }
110             else
111             {
112 0           warn sprintf "Unknown command: %s %s %s (%s)",
113             $self->house_code, $self->unit_code, $self->func, $_;
114             }
115             }
116             else
117             {
118 0           warn sprintf "Unknown command: %s %s %s (%s)",
119             $self->house_code, $self->unit_code, $self->func, $_;
120             }
121              
122 0           return map {uc} @words;
  0            
123             }
124              
125              
126             ###
127              
128             sub parse_string
129             {
130 0     0 0   my $string = uc(shift);
131              
132 0 0         if ( $string =~ /^\s*([a-p])\s*(\d+|all|lights)\s*(on|off|dim|bright)\s*$/i )
133             {
134 0 0 0       if ( lc($2) eq 'all' && lc($3) eq 'off' )
    0 0        
    0 0        
135             {
136 0           return map {uc} ($1, $2, $3);
  0            
137             }
138             elsif ( lc($2) eq 'lights' && lc($3) eq 'on' )
139             {
140 0           return map {uc} ($1, $2, $3);
  0            
141             }
142             elsif ( $2 > 0 && $2 <= 16 )
143             {
144 0           return (uc($1), $2 * 1, uc($3));
145             }
146             else
147             {
148 0           return ();
149             }
150             }
151             else
152             {
153 0           return ();
154             }
155             }
156              
157              
158             1;
159