Device-Hue
view release on metacpan or search on metacpan
lib/Device/Hue.pm view on Meta::CPAN
package Device::Hue;
{
$Device::Hue::VERSION = '0.4';
}
use warnings;
use common::sense;
use Moo;
has 'bridge' => ( is => 'rw' );
has 'key' => ( is => 'rw' );
has 'agent' => ( is => 'rw' );
has 'debug' => ( is => 'rw' );
use Device::Hue::UPnP;
use Device::Hue::Light;
use LWP::UserAgent;
use LWP::Protocol::https;
use JSON::XS;
use Data::Dumper;
use Carp;
sub BUILD
{
my ($self) = @_;
$self->agent(new LWP::UserAgent);
$self->init;
}
sub init
{
my ($self) = @_;
$self->bridge($ENV{'HUE_BRIDGE'})
if defined $ENV{'HUE_BRIDGE'};
$self->key($ENV{'HUE_KEY'})
if defined $ENV{'HUE_KEY'};
croak "missing hue bridge"
unless defined $self->bridge;
croak "missing hue key"
unless defined $self->key;
}
sub process
{
my ($self, $res) = @_;
if ($res->is_success) {
say $res->status_line
if $self->debug;
say Dumper(decode_json($res->decoded_content))
if $self->debug;
return decode_json($res->decoded_content);
} else {
say "Request failed: " . $res->status_line if $self->debug;
}
return;
}
sub get
{
my ($self, $uri) = @_;
say "GET $uri" if $self->debug;
my $req = HTTP::Request->new('GET', $uri);
$req->content_type('application/json');
return $self->process($self->agent->request($req));
}
sub put
{
my ($self, $uri, $data) = @_;
my $req = HTTP::Request->new('PUT', $uri);
$req->content_type('application/json');
$req->content(encode_json($data));
return $self->process($self->agent->request($req));
}
sub config
{
my ($self) = @_;
return $self->get($self->path_to(''));
}
sub schedules
{
my ($self) = @_;
return $self->get($self->path_to('schedules'));
}
sub lights
{
my ($self) = @_;
my $config = $self->config
or return undef;
my @lights = ();
foreach my $key (sort keys %{$config->{'lights'}}) {
my $light = $config->{'lights'}{$key};
push @lights, Device::Hue::Light->new({ 'hue' => $self, 'id' => $key, 'data' => $light });
( run in 1.622 second using v1.01-cache-2.11-cpan-aadc1410aed )