| 1 | |||
| 2 | # Copyright (c) 1999-2017 Rob Fugina <robf@fugina.com> | ||
| 3 | # Distributed under the terms of the GNU Public License, Version 3.0 | ||
| 4 | |||
| 5 | package X10::Controller; | ||
| 6 | |||
| 7 | use X10::Device; | ||
| 8 | use X10::Event; | ||
| 9 | |||
| 10 | sub unique (&@); | ||
| 11 | |||
| 12 | sub new | ||
| 13 | { | ||
| 14 | my $type = shift; | ||
| 15 | |||
| 16 | my $self = bless { @_ }; | ||
| 17 | bless $self, $type; | ||
| 18 | |||
| 19 | $self->{logger} ||= sub { | ||
| 20 | shift; | ||
| 21 | printf(@_); | ||
| 22 | print "\n"; | ||
| 23 | }; | ||
| 24 | |||
| 25 | $self->{verbose} = 1 if $self->{debug}; | ||
| 26 | |||
| 27 | $self->{house_code} ||= 'A'; | ||
| 28 | $self->{house_code} = uc($self->{house_code}); | ||
| 29 | |||
| 30 | $self->{listeners} = []; | ||
| 31 | $self->{output_word_queue} = []; | ||
| 32 | $self->{states} = {}; | ||
| 33 | $self->{working} = 0; | ||
| 34 | |||
| 35 | return $self; | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | ### creating controlled devices... | ||
| 40 | |||
| 41 | sub Appliance | ||
| 42 | { | ||
| 43 | my $self = shift; | ||
| 44 | return new X10::Device( | ||
| 45 | controller => $self, | ||
| 46 | @_, | ||
| 47 | ); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub Lamp | ||
| 51 | { | ||
| 52 | my $self = shift; | ||
| 53 | return new X10::Device( | ||
| 54 | controller => $self, | ||
| 55 | @_, | ||
| 56 | ); | ||
| 57 | } | ||
| 58 | |||
| 59 | ### directing input/output | ||
| 60 | |||
| 61 | sub register_listener | ||
| 62 | { | ||
| 63 | my $self = shift; | ||
| 64 | my $coderef = shift; | ||
| 65 | |||
| 66 | push @{$self->{listeners}}, $coderef; | ||
| 67 | } | ||
| 68 | |||
| 69 | # this is what the main program will use to find what to select() on | ||
| 70 | sub select_fds | ||
| 71 | { | ||
| 72 | my $self = shift; | ||
| 73 | |||
| 74 | # nothing, in this abstract class | ||
| 75 | return (); | ||
| 76 | } | ||
| 77 | |||
| 78 | # this is what the main program will call when my FD is readable | ||
| 79 | sub handle_input | ||
| 80 | { | ||
| 81 | my $self = shift; | ||
| 82 | # there will be no input in this abstract class... | ||
| 83 | } | ||
| 84 | |||
| 85 | ### manipulating controlled devices | ||
| 86 | |||
| 87 | sub on | ||
| 88 | { | ||
| 89 | my $self = shift; | ||
| 90 | |||
| 91 | $self->send( map { new X10::Event(sprintf("%s ON", $_->address)) } @_ ); | ||
| 92 | } | ||
| 93 | |||
| 94 | sub off | ||
| 95 | { | ||
| 96 | my $self = shift; | ||
| 97 | |||
| 98 | $self->send( map { new X10::Event(sprintf("%s OFF", $_->address)) } @_ ); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub dim | ||
| 102 | { | ||
| 103 | my $self = shift; | ||
| 104 | $self->send( map { new X10::Event(sprintf("%s DIM", $_->address)) } @_ ); | ||
| 105 | } | ||
| 106 | |||
| 107 | sub bright | ||
| 108 | { | ||
| 109 | my $self = shift; | ||
| 110 | $self->send( map { new X10::Event(sprintf("%s BRIGHT", $_->address)) } @_ ); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub lights_on | ||
| 114 | { | ||
| 115 | my $self = shift; | ||
| 116 | my %params = @_; | ||
| 117 | $self->send( new X10::Event( | ||
| 118 | sprintf("%s LIGHTS ON", $params{house_code} || $self->{house_code} ) | ||
| 119 | )); | ||
| 120 | } | ||
| 121 | |||
| 122 | sub all_off | ||
| 123 | { | ||
| 124 | my $self = shift; | ||
| 125 | my %params = @_; | ||
| 126 | $self->send( new X10::Event( | ||
| 127 | sprintf("%s ALL OFF", $params{house_code} || $self->{house_code} ) | ||
| 128 | )); | ||
| 129 | } | ||
| 130 | |||
| 131 | ### semi-private methods | ||
| 132 | |||
| 133 | sub send | ||
| 134 | { | ||
| 135 | my $self = shift; | ||
| 136 | |||
| 137 | $self->queue_words(map {$_->compile} @_); | ||
| 138 | } | ||
| 139 | |||
| 140 | sub send_one | ||
| 141 | { | ||
| 142 | my $self = shift; | ||
| 143 | my $event = shift; # this sub ONLY SENDS ONE | ||
| 144 | $self->queue_words($event->compile); | ||
| 145 | } | ||
| 146 | |||
| 147 | ### private stuff -- only to be called from me & derived modules | ||
| 148 | |||
| 149 | sub got_events | ||
| 150 | { | ||
| 151 | my $self = shift; | ||
| 152 | |||
| 153 | foreach (@_) | ||
| 154 | { | ||
| 155 | $self->got_event($_); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | sub got_event | ||
| 160 | { | ||
| 161 | my $self = shift; | ||
| 162 | my $event = shift; | ||
| 163 | |||
| 164 | foreach (@{$self->{listeners}}) | ||
| 165 | { | ||
| 166 | $_->($event); | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | sub optimize_wordlist | ||
| 171 | { | ||
| 172 | my $self = shift; | ||
| 173 | |||
| 174 | my @words; | ||
| 175 | |||
| 176 | OPTPASS: | ||
| 177 | while (@_) | ||
| 178 | { | ||
| 179 | if (scalar @_ >= 4) | ||
| 180 | { | ||
| 181 | my ($hc0, $cm0) = $_[0] =~ /^(.)(..)$/; | ||
| 182 | my ($hc1, $cm1) = $_[1] =~ /^(.)(..)$/; | ||
| 183 | my ($hc2, $cm2) = $_[2] =~ /^(.)(..)$/; | ||
| 184 | my ($hc3, $cm3) = $_[3] =~ /^(.)(..)$/; | ||
| 185 | |||
| 186 | if ( | ||
| 187 | $_[1] eq $_[3] | ||
| 188 | && ( $cm1 eq 'OF' || $cm1 eq 'ON' ) | ||
| 189 | && $hc0 eq $hc1 | ||
| 190 | && $hc0 eq $hc2 | ||
| 191 | && $cm0 =~ /^\d\d$/ | ||
| 192 | && $cm2 =~ /^\d\d$/ | ||
| 193 | ) | ||
| 194 | { | ||
| 195 | $self->{logger}->('info', "Opt 1: %s\n", join(" ", @_[0..3])) if $self->{debug}; | ||
| 196 | |||
| 197 | @_[0,1] = @_[1,0]; | ||
| 198 | shift; | ||
| 199 | |||
| 200 | next OPTPASS; | ||
| 201 | } | ||
| 202 | elsif ( | ||
| 203 | $_[0] eq $_[2] | ||
| 204 | && $cm0 =~ /^\d\d$/ | ||
| 205 | && $cm2 =~ /^\d\d$/ # 0 & 2 are identical addresses | ||
| 206 | && ( $cm1 eq 'OF' || $cm1 eq 'ON' | ||
| 207 | || $cm1 eq 'BR' || $cm1 eq 'DI' ) | ||
| 208 | ) | ||
| 209 | { | ||
| 210 | $self->{logger}->('info', "Opt 2: %s\n", join(" ", @_[0..3])) if $self->{debug}; | ||
| 211 | shift; | ||
| 212 | @_[0,1] = @_[1,0]; | ||
| 213 | |||
| 214 | next OPTPASS; | ||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | if (scalar @_ >= 3) | ||
| 219 | { | ||
| 220 | } | ||
| 221 | |||
| 222 | if (scalar @_ >= 2) | ||
| 223 | { | ||
| 224 | if ($_[0] eq $_[1]) | ||
| 225 | { | ||
| 226 | $self->{logger}->('info', "Opt 3: %s\n", join(" ", @_[0..1])) if $self->{debug}; | ||
| 227 | shift; | ||
| 228 | next OPTPASS; | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | push @words, shift; | ||
| 233 | } | ||
| 234 | |||
| 235 | return @words; | ||
| 236 | } | ||
| 237 | |||
| 238 | sub queue_words | ||
| 239 | { | ||
| 240 | my $self = shift; | ||
| 241 | |||
| 242 | push @{$self->{output_word_queue}}, @_; | ||
| 243 | |||
| 244 | @{$self->{output_word_queue}} = $self->optimize_wordlist(@{$self->{output_word_queue}}); | ||
| 245 | |||
| 246 | $self->work; | ||
| 247 | } | ||
| 248 | |||
| 249 | sub work | ||
| 250 | { | ||
| 251 | my $self = shift; | ||
| 252 | |||
| 253 | return if $self->{working}; | ||
| 254 | |||
| 255 | $self->{working} = 1; | ||
| 256 | while (my $word = shift @{$self->{output_word_queue}}) | ||
| 257 | { | ||
| 258 | $self->send_word($word); | ||
| 259 | } | ||
| 260 | $self->{working} = 0; | ||
| 261 | } | ||
| 262 | |||
| 263 | sub send_word | ||
| 264 | { | ||
| 265 | my $self = shift; | ||
| 266 | my $word = shift; | ||
| 267 | $self->{logger}->('info', "Sending word: %s", $word) if $self->{verbose}; | ||
| 268 | $self->got_words($word); # here's the fake-out since we're abstract | ||
| 269 | } | ||
| 270 | |||
| 271 | # this does all the state-machine stuff to keep track of what's going on... | ||
| 272 | # does NOT keep track of what devices are on/off | ||
| 273 | sub got_words | ||
| 274 | { | ||
| 275 | my $self = shift; | ||
| 276 | |||
| 277 | while ( my $word = shift ) | ||
| 278 | { | ||
| 279 | |||
| 280 | if ($word =~ /^([a-p])(\d\d)$/i) # got an address | ||
| 281 | { | ||
| 282 | my $hc = uc($1); | ||
| 283 | my $uc = $2 * 1; | ||
| 284 | |||
| 285 | unless (exists $self->{states}->{$hc}) | ||
| 286 | { | ||
| 287 | $self->{states}->{$hc}->{mode} = 'addr'; | ||
| 288 | $self->{states}->{$hc}->{selected} = []; | ||
| 289 | } | ||
| 290 | |||
| 291 | if ($self->{states}->{$hc}->{mode} eq 'addr') | ||
| 292 | { | ||
| 293 | push @{$self->{states}->{$hc}->{selected}}, $uc; | ||
| 294 | } | ||
| 295 | elsif ($self->{states}->{$hc}->{mode} eq 'cmd') | ||
| 296 | { | ||
| 297 | $self->{states}->{$hc}->{mode} = 'addr'; | ||
| 298 | $self->{states}->{$hc}->{selected} = [ $uc ]; | ||
| 299 | } | ||
| 300 | else | ||
| 301 | { | ||
| 302 | warn "Bleah: ", Dumper($self->{states}); | ||
| 303 | } | ||
| 304 | } | ||
| 305 | elsif ($word =~ /^([a-p])(on|of|di|br)$/i) # got a cmd | ||
| 306 | { | ||
| 307 | my $hc = uc($1); | ||
| 308 | my $cmd = uc($2); | ||
| 309 | |||
| 310 | unless (exists $self->{states}->{$hc}) | ||
| 311 | { | ||
| 312 | $self->{states}->{$hc}->{mode} = 'addr'; | ||
| 313 | $self->{states}->{$hc}->{selected} = []; | ||
| 314 | } | ||
| 315 | |||
| 316 | # use long versions | ||
| 317 | $cmd = 'off' if $cmd eq 'OF'; | ||
| 318 | $cmd = 'dim' if $cmd eq 'DI'; | ||
| 319 | $cmd = 'bright' if $cmd eq 'BR'; | ||
| 320 | |||
| 321 | $self->{states}->{$hc}->{mode} = 'cmd'; | ||
| 322 | |||
| 323 | $self->got_events( | ||
| 324 | map { new X10::Event(sprintf("%s%02s %s", $hc, $_, $cmd)) } | ||
| 325 | unique { $_ } | ||
| 326 | @{$self->{states}->{$hc}->{selected}} | ||
| 327 | ); | ||
| 328 | } | ||
| 329 | elsif ($word =~ /^([a-p])(a0|l1)$/i) # got an 'all' cmd | ||
| 330 | { | ||
| 331 | my $hc = uc($1); | ||
| 332 | my $cmd = uc($2); | ||
| 333 | |||
| 334 | unless (exists $self->{states}->{$hc}) | ||
| 335 | { | ||
| 336 | $self->{states}->{$hc}->{mode} = 'addr'; | ||
| 337 | $self->{states}->{$hc}->{selected} = []; | ||
| 338 | } | ||
| 339 | |||
| 340 | # use long versions | ||
| 341 | $cmd = 'all off' if $cmd eq 'A0'; | ||
| 342 | $cmd = 'lights on' if $cmd eq 'L1'; | ||
| 343 | |||
| 344 | $self->{states}->{$hc}->{mode} = 'cmd'; | ||
| 345 | $self->{states}->{$hc}->{selected} = []; | ||
| 346 | |||
| 347 | $self->got_events( new X10::Event(sprintf("%s %s", $hc, $cmd)) ); | ||
| 348 | } | ||
| 349 | else | ||
| 350 | { | ||
| 351 | warn "Unknown word: ", $word; | ||
| 352 | } | ||
| 353 | } | ||
| 354 | |||
| 355 | } | ||
| 356 | |||
| 357 | ### | ||
| 358 | |||
| 359 | sub unique (&@) | ||
| 360 | { | ||
| 361 | my($c,%hash) = shift; | ||
| 362 | grep { not $hash{&$c}++ } @_; | ||
| 363 | } | ||
| 364 | |||
| 365 | |||
| 366 | |||
| 367 | 1; | ||
| 368 |