| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | # $Id: Device.pm,v 1.4 2013-12-31 03:29:22 ericblue76 Exp $ | ||
| 4 | # | ||
| 5 | # Author: Eric Blue - ericblue76@gmail.com | ||
| 6 | # Project: Belkin Wemo API | ||
| 7 | # Url: http://eric-blue.com/belkin-wemo-api/ | ||
| 8 | # | ||
| 9 | |||
| 10 | # ABSTRACT: Uses UPNP to control Belkin Wemo Switches | ||
| 11 | |||
| 12 | package WebService::Belkin::WeMo::Device; | ||
| 13 | { | ||
| 14 | $WebService::Belkin::WeMo::Device::VERSION = '1.0'; | ||
| 15 | } | ||
| 16 | |||
| 17 | use Data::Dumper; | ||
| 18 | use Net::UPnP::ControlPoint; | ||
| 19 | use Carp; | ||
| 20 | |||
| 21 | use strict; | ||
| 22 | |||
| 23 | sub new { | ||
| 24 | |||
| 25 | my $class = shift; | ||
| 26 | my $self = {}; | ||
| 27 | my %params = @_; | ||
| 28 | |||
| 29 | if ( ( !defined $params{'ip'} ) && ( !defined $params{'name'} ) ) { | ||
| 30 | croak("Insufficient parameters: ip or name are required!"); | ||
| 31 | } | ||
| 32 | |||
| 33 | $self->{'_ip'} = $params{'ip'}; | ||
| 34 | $self->{'_name'} = $params{'name'}; | ||
| 35 | $self->{'_db'} = $params{'db'}; | ||
| 36 | |||
| 37 | my $wemoDiscover = WebService::Belkin::WeMo::Discover->new(); | ||
| 38 | my $discovered; | ||
| 39 | if ( defined $params{'db'} ) { | ||
| 40 | $discovered = $wemoDiscover->load($params{'db'}); | ||
| 41 | } | ||
| 42 | else { | ||
| 43 | $discovered = $wemoDiscover->search(); | ||
| 44 | } | ||
| 45 | |||
| 46 | if ( defined( $params{'ip'} ) ) { | ||
| 47 | if ( !defined $discovered->{ $self->{'_ip'} } ) { | ||
| 48 | croak "IP not found - try running another discovery search!"; | ||
| 49 | } | ||
| 50 | |||
| 51 | $self->{_device} = $discovered->{ $self->{'_ip'} }->{'device'}; | ||
| 52 | $self->{_type} = $discovered->{ $self->{'_ip'} }->{'type'}; | ||
| 53 | |||
| 54 | } | ||
| 55 | |||
| 56 | if ( defined( $params{'name'} ) ) { | ||
| 57 | my $found = 0; | ||
| 58 | |||
| 59 | foreach ( keys( %{$discovered} ) ) { | ||
| 60 | |||
| 61 | if ( $discovered->{$_}->{'name'} eq $params{'name'} ) { | ||
| 62 | $found = 1; | ||
| 63 | $self->{_device} = $discovered->{$_}->{'device'}; | ||
| 64 | last; | ||
| 65 | } | ||
| 66 | |||
| 67 | } | ||
| 68 | |||
| 69 | if ( !$found ) { | ||
| 70 | croak "Name not found - try running another discovery search!"; | ||
| 71 | } | ||
| 72 | |||
| 73 | } | ||
| 74 | |||
| 75 | # Load service - only basic is here for now, others will be supported later | ||
| 76 | foreach my $service ( $self->{_device}->getservicelist() ) { | ||
| 77 | |||
| 78 | if ( $service->getservicetype() =~ /urn:Belkin:service:basicevent:1/ ) { | ||
| 79 | $self->{_basicService} = $service; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | bless $self, $class; | ||
| 84 | |||
| 85 | $self; | ||
| 86 | |||
| 87 | } | ||
| 88 | |||
| 89 | sub getType() { | ||
| 90 | |||
| 91 | my $self = shift; | ||
| 92 | |||
| 93 | if (defined($self->{_type})) { | ||
| 94 | return $self->{_type}; | ||
| 95 | } else { | ||
| 96 | return "undefined"; | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | } | ||
| 101 | |||
| 102 | sub getFriendlyName() { | ||
| 103 | |||
| 104 | my $self = shift; | ||
| 105 | |||
| 106 | my $resp = $self->{_basicService}->postaction("GetFriendlyName"); | ||
| 107 | if ( $resp->getstatuscode() == 200 ) { | ||
| 108 | return $resp->getargumentlist()->{'FriendlyName'}; | ||
| 109 | } | ||
| 110 | else { | ||
| 111 | croak "Got status code " . $resp->getstatuscode() . "!\n"; | ||
| 112 | } | ||
| 113 | |||
| 114 | } | ||
| 115 | |||
| 116 | sub isSwitchOn() { | ||
| 117 | |||
| 118 | my $self = shift; | ||
| 119 | |||
| 120 | my $resp = $self->{_basicService}->postaction("GetBinaryState"); | ||
| 121 | if ( $resp->getstatuscode() == 200 ) { | ||
| 122 | return $resp->getargumentlist()->{'BinaryState'}; | ||
| 123 | } | ||
| 124 | else { | ||
| 125 | croak "Got status code " . $resp->getstatuscode() . "!\n"; | ||
| 126 | } | ||
| 127 | |||
| 128 | } | ||
| 129 | |||
| 130 | sub toggleSwitch() { | ||
| 131 | |||
| 132 | my $self = shift; | ||
| 133 | |||
| 134 | if ($self->getType() eq "sensor") { | ||
| 135 | warn "Method only supported for switches, not sensors.\n"; | ||
| 136 | return; | ||
| 137 | } | ||
| 138 | |||
| 139 | my $state = $self->isSwitchOn(); | ||
| 140 | my $toggle = $state ^= 1; | ||
| 141 | |||
| 142 | $self->on() if $toggle == 1; | ||
| 143 | $self->off() if $toggle == 0; | ||
| 144 | |||
| 145 | } | ||
| 146 | |||
| 147 | sub getBinaryState() { | ||
| 148 | |||
| 149 | my $self = shift; | ||
| 150 | |||
| 151 | my $resp = | ||
| 152 | $self->{_basicService} | ||
| 153 | ->postaction( "GetBinaryState"); | ||
| 154 | if ( $resp->getstatuscode() == 200 ) { | ||
| 155 | |||
| 156 | my $state = $resp->getargumentlist()->{'BinaryState'}; | ||
| 157 | if ($state == 1) { | ||
| 158 | return "on"; | ||
| 159 | } elsif ($state == 0) { | ||
| 160 | return "off"; | ||
| 161 | } else { | ||
| 162 | return "unknown"; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | else { | ||
| 166 | croak "Got status code " . $resp->getstatuscode() . "!\n"; | ||
| 167 | } | ||
| 168 | |||
| 169 | } | ||
| 170 | |||
| 171 | sub on() { | ||
| 172 | |||
| 173 | my $self = shift; | ||
| 174 | |||
| 175 | if ($self->getType() eq "sensor") { | ||
| 176 | warn "Method only supported for switches, not sensors.\n"; | ||
| 177 | return; | ||
| 178 | } | ||
| 179 | |||
| 180 | my $resp = | ||
| 181 | $self->{_basicService} | ||
| 182 | ->postaction( "SetBinaryState", { BinaryState => 1 } ); | ||
| 183 | if ( $resp->getstatuscode() == 200 ) { | ||
| 184 | |||
| 185 | # Not this will be Error if the switch is already on | ||
| 186 | return $resp->getargumentlist()->{'BinaryState'}; | ||
| 187 | } | ||
| 188 | else { | ||
| 189 | croak "Got status code " . $resp->getstatuscode() . "!\n"; | ||
| 190 | } | ||
| 191 | |||
| 192 | } | ||
| 193 | |||
| 194 | sub off() { | ||
| 195 | |||
| 196 | my $self = shift; | ||
| 197 | |||
| 198 | if ($self->getType() eq "sensor") { | ||
| 199 | warn "Method only supported for switches, not sensors.\n"; | ||
| 200 | return; | ||
| 201 | } | ||
| 202 | |||
| 203 | my $resp = | ||
| 204 | $self->{_basicService} | ||
| 205 | ->postaction( "SetBinaryState", { BinaryState => 0 } ); | ||
| 206 | if ( $resp->getstatuscode() == 200 ) { | ||
| 207 | |||
| 208 | # Not this will be Error if the switch is already off | ||
| 209 | return $resp->getargumentlist()->{'BinaryState'}; | ||
| 210 | } | ||
| 211 | else { | ||
| 212 | croak "Got status code " . $resp->getstatuscode() . "!\n"; | ||
| 213 | } | ||
| 214 | |||
| 215 | } | ||
| 216 | |||
| 217 | 1; | ||
| 218 | |||
| 219 | __END__ |