Created
May 9, 2016 17:28
-
-
Save jameswhite/16e3017a79971b69459b9375110b1238 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use LWP::UserAgent; | |
use Net::MQTT::Simple; | |
use JSON; | |
use Data::Dumper; | |
my $mqtt = Net::MQTT::Simple->new("127.0.0.1"); | |
my $color = { | |
'red' => hex("0x0000"), | |
'orange' => hex("0x1800"), | |
'yellow' => hex("0x4000"), | |
'green' => hex("0x639C"), | |
'cyan' => hex("0x9000"), | |
'blue' => hex("0xB748"), | |
'indigo' => hex("0xC000"), | |
'lavender' => hex("0xD000"), | |
'pink' => hex("0xE000"), | |
}; | |
my $lights = { | |
'all' => [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ], | |
'one' => [ 1 ], # lamp next to the keg | |
'two' => [ 2 ], # lamp between fryman and rick | |
'three' => [ 3 ], # lamp between james and aziz | |
'four' => [ 4 ], # window_0 (in front of rick) | |
'five' => [ 5 ], # window_2 (to jameswhite's left) | |
'six' => [ 6 ], # window_4 (in front of aziz) | |
'seven' => [ 7 ], # window_1 (in front of jfryman) | |
'eight' => [ 8 ], # window_5 (in makerbot) | |
'nine' => [ 9 ], # window_3 (in front of jameswhite) | |
'jameswhite' => [ 3, 5, 9 ], # (around jameswhite) | |
'aziz' => [ 3, 6, 8 ],# (around aziz) | |
'rick' => [ 2, 4 ],# (around rick) | |
'jfryman' => [ 2, 7 ],# (around jfryman) | |
'lamps' => [ 1, 2, 3 ],# standing lamps | |
'windows' => [ 4, 5, 6, 7, 8, 9 ] # windows | |
}; | |
sub callbacks { | |
$mqtt->run( | |
"bikeshed/hue" => sub { | |
my $saturation=255; | |
my $ua = LWP::UserAgent->new; | |
$ua->timeout(10); | |
$ua->env_proxy; | |
my ($topic, $message) = @_; | |
my $decoded = decode_json($message); | |
$action = $decoded->{'action'}; | |
$hue = $color->{$decoded->{'color'}}; | |
$light = $decoded->{'lights'}; | |
$saturation = 0 unless defined($hue); | |
if($action == 'color'){ | |
foreach $light (@{$lights->{$light}}){ | |
my $json = JSON->new->allow_nonref; | |
my $req = HTTP::Request->new( 'PUT', "http://philips-hue/api/jameswhite/lights/$light/state"); | |
$req->header( 'Content-Type' => 'application/json' ); | |
$req->content( $json->encode( { 'on' => $JSON::true, 'sat' => int($saturation), 'bri' => 255, 'hue' => int($hue) }) ); | |
my $lwp = LWP::UserAgent->new; | |
my $response = $lwp->request( $req ); | |
# print STDERR Data::Dumper->Dump([$response->{'_content'}]); | |
} | |
} | |
if($decoded->{'respond'}){ | |
($respond_topic, $respond_host) = split(/@/,$decoded->{'respond'}); | |
print "respond to $respond_topic at $respond_host\n"; | |
$respond_host='127.0.0.1' unless defined($respond_host); | |
my $response = Net::MQTT::Simple->new($respond_host); | |
$mqtt->publish("$respond_topic" => "lights changed to $decoded->{'color'}"); | |
} | |
}, | |
"#" => sub { | |
my ($topic, $message) = @_; | |
print "[$topic] $message\n"; | |
}, | |
); | |
} | |
callbacks |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment