WebService-Belkin-WeMo

 view release on metacpan or  search on metacpan

lib/WebService/Belkin/WeMo/Device.pm  view on Meta::CPAN

		croak "Got status code " . $resp->getstatuscode() . "!\n";
	}

}

sub isSwitchOn() {

	my $self = shift;

	my $resp = $self->{_basicService}->postaction("GetBinaryState");
	if ( $resp->getstatuscode() == 200 ) {
		return $resp->getargumentlist()->{'BinaryState'};
	}
	else {
		croak "Got status code " . $resp->getstatuscode() . "!\n";
	}

}

sub toggleSwitch() {

	my $self = shift;
	
	if ($self->getType() eq "sensor") {
        warn "Method only supported for switches, not sensors.\n";
	    return;
	}

	my $state  = $self->isSwitchOn();
	my $toggle = $state ^= 1;

	$self->on()  if $toggle == 1;
	$self->off() if $toggle == 0;

}

sub getBinaryState() {

	my $self = shift;

	my $resp =
	  $self->{_basicService}
	  ->postaction( "GetBinaryState");
	if ( $resp->getstatuscode() == 200 ) {

		my $state = $resp->getargumentlist()->{'BinaryState'};
		if ($state == 1) { 
		    return "on";
		} elsif ($state == 0) {
		    return "off";
		} else {
		    return "unknown";
		}
	}
	else {
		croak "Got status code " . $resp->getstatuscode() . "!\n";
	}

}

sub on() {

	my $self = shift;
	
    if ($self->getType() eq "sensor") {
        warn "Method only supported for switches, not sensors.\n";
	    return;
	}

	my $resp =
	  $self->{_basicService}
	  ->postaction( "SetBinaryState", { BinaryState => 1 } );
	if ( $resp->getstatuscode() == 200 ) {

		# Not this will be Error if the switch is already on
		return $resp->getargumentlist()->{'BinaryState'};
	}
	else {
		croak "Got status code " . $resp->getstatuscode() . "!\n";
	}

}

sub off() {

	my $self = shift;
	
	if ($self->getType() eq "sensor") {
	    warn "Method only supported for switches, not sensors.\n";
	    return;
	}

	my $resp =
	  $self->{_basicService}
	  ->postaction( "SetBinaryState", { BinaryState => 0 } );
	if ( $resp->getstatuscode() == 200 ) {

		# Not this will be Error if the switch is already off
		return $resp->getargumentlist()->{'BinaryState'};
	}
	else {
		croak "Got status code " . $resp->getstatuscode() . "!\n";
	}

}

1;

__END__


=head1 NAME

WebService::Belkin::Wemo::Device - Device class for controlling Wemo Switches
=head1 SYNOPSIS

Sample Usage:

my $wemo = WebService::Belkin::WeMo::Device->new(ip => '192.168.2.126', db => '/tmp/belkin.db');

OR



( run in 3.197 seconds using v1.01-cache-2.11-cpan-524268b4103 )