| 1 | use strict; | ||
| 2 | |||
| 3 | package Device::Hue; | ||
| 4 | { | ||
| 5 | $Device::Hue::VERSION = '0.4'; | ||
| 6 | } | ||
| 7 | |||
| 8 | use warnings; | ||
| 9 | use common::sense; | ||
| 10 | |||
| 11 | use Moo; | ||
| 12 | |||
| 13 | has 'bridge' => ( is => 'rw' ); | ||
| 14 | has 'key' => ( is => 'rw' ); | ||
| 15 | has 'agent' => ( is => 'rw' ); | ||
| 16 | has 'debug' => ( is => 'rw' ); | ||
| 17 | |||
| 18 | use Device::Hue::UPnP; | ||
| 19 | use Device::Hue::Light; | ||
| 20 | |||
| 21 | use LWP::UserAgent; | ||
| 22 | use LWP::Protocol::https; | ||
| 23 | |||
| 24 | use JSON::XS; | ||
| 25 | |||
| 26 | use Data::Dumper; | ||
| 27 | use Carp; | ||
| 28 | |||
| 29 | sub BUILD | ||
| 30 | { | ||
| 31 | my ($self) = @_; | ||
| 32 | |||
| 33 | $self->agent(new LWP::UserAgent); | ||
| 34 | $self->init; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub init | ||
| 38 | { | ||
| 39 | my ($self) = @_; | ||
| 40 | |||
| 41 | $self->bridge($ENV{'HUE_BRIDGE'}) | ||
| 42 | if defined $ENV{'HUE_BRIDGE'}; | ||
| 43 | |||
| 44 | $self->key($ENV{'HUE_KEY'}) | ||
| 45 | if defined $ENV{'HUE_KEY'}; | ||
| 46 | |||
| 47 | croak "missing hue bridge" | ||
| 48 | unless defined $self->bridge; | ||
| 49 | |||
| 50 | croak "missing hue key" | ||
| 51 | unless defined $self->key; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub process | ||
| 55 | { | ||
| 56 | my ($self, $res) = @_; | ||
| 57 | |||
| 58 | if ($res->is_success) { | ||
| 59 | |||
| 60 | say $res->status_line | ||
| 61 | if $self->debug; | ||
| 62 | |||
| 63 | say Dumper(decode_json($res->decoded_content)) | ||
| 64 | if $self->debug; | ||
| 65 | |||
| 66 | return decode_json($res->decoded_content); | ||
| 67 | } else { | ||
| 68 | say "Request failed: " . $res->status_line if $self->debug; | ||
| 69 | } | ||
| 70 | |||
| 71 | return; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub get | ||
| 75 | { | ||
| 76 | my ($self, $uri) = @_; | ||
| 77 | |||
| 78 | say "GET $uri" if $self->debug; | ||
| 79 | |||
| 80 | my $req = HTTP::Request->new('GET', $uri); | ||
| 81 | |||
| 82 | $req->content_type('application/json'); | ||
| 83 | |||
| 84 | return $self->process($self->agent->request($req)); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub put | ||
| 88 | { | ||
| 89 | my ($self, $uri, $data) = @_; | ||
| 90 | |||
| 91 | my $req = HTTP::Request->new('PUT', $uri); | ||
| 92 | |||
| 93 | $req->content_type('application/json'); | ||
| 94 | $req->content(encode_json($data)); | ||
| 95 | |||
| 96 | return $self->process($self->agent->request($req)); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub config | ||
| 100 | { | ||
| 101 | my ($self) = @_; | ||
| 102 | |||
| 103 | return $self->get($self->path_to('')); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub schedules | ||
| 107 | { | ||
| 108 | my ($self) = @_; | ||
| 109 | |||
| 110 | return $self->get($self->path_to('schedules')); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub lights | ||
| 114 | { | ||
| 115 | my ($self) = @_; | ||
| 116 | |||
| 117 | my $config = $self->config | ||
| 118 | or return undef; | ||
| 119 | |||
| 120 | my @lights = (); | ||
| 121 | |||
| 122 | foreach my $key (sort keys %{$config->{'lights'}}) { | ||
| 123 | |||
| 124 | my $light = $config->{'lights'}{$key}; | ||
| 125 | |||
| 126 | push @lights, Device::Hue::Light->new({ 'hue' => $self, 'id' => $key, 'data' => $light }); | ||
| 127 | } | ||
| 128 | |||
| 129 | return \@lights; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub discovery | ||
| 133 | { | ||
| 134 | my ($self) = @_; | ||
| 135 | |||
| 136 | my $devices = $self->nupnp; | ||
| 137 | |||
| 138 | return scalar @$devices ? $devices : $self->upnp; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub nupnp | ||
| 142 | { | ||
| 143 | my $data = (shift)->get('https://www.meethue.com/api/nupnp') | ||
| 144 | or return []; | ||
| 145 | |||
| 146 | return [ map { $_->{'internalipaddress'} } @$data ]; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub upnp | ||
| 150 | { | ||
| 151 | return Device::Hue::UPnP::upnp(); | ||
| 152 | } | ||
| 153 | |||
| 154 | sub path_to | ||
| 155 | { | ||
| 156 | my ($self, @endp) = @_; | ||
| 157 | |||
| 158 | my $uri = join('/', $self->bridge, 'api', $self->key, @endp); | ||
| 159 | |||
| 160 | say $uri | ||
| 161 | if $self->debug; | ||
| 162 | |||
| 163 | return $uri; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub light | ||
| 167 | { | ||
| 168 | my ($self, $id) = @_; | ||
| 169 | |||
| 170 | return Device::Hue::Light->new({ 'hue' => $self, 'id' => $id }); | ||
| 171 | } | ||
| 172 | |||
| 173 | 1; | ||
| 174 | |||
| 175 | # ABSTRACT: Perl module for the Philips Hue light system | ||
| 176 | |||
| 177 | __END__ |