Device-TPLink

 view release on metacpan or  search on metacpan

lib/Device/TPLink/Kasa.pm  view on Meta::CPAN

);

has 'token' => (
  is => 'rw',
  isa => 'Str',
);

=head1 NAME

Device::TPLink::Kasa - Interact with TP-Link's Kasa cloud service API

=head1 VERSION

Version 0.02

=cut

our $VERSION = '0.02';


=head1 SYNOPSIS

A small collection of helper methods to make working with Device::TPLink::SmartHome::Kasa easier.

    use Device::TPLink::Kasa;

    my $kasa = Device::TPLink::Kasa->new(
        username => 'username', # username for Kasa cloud service
        password => 'password', # password for Kasa cloud service
        uuid => 'uuid', # Optional, v4 UUID. Will be auto generated if not set
    );

=head1 SUBROUTINES/METHODS

=head2 resetToken

Get a new token from the Kasa service
    
    $kasa->resetToken();

    my $token = $kasa->token;

=cut

sub resetToken {
  my $self = shift;

  # If we don't already have a UUID set, generate a new one.
  unless ($self->uuid) {
    my $ug = UUID::Generator::PurePerl->new();
    my $uuid = $ug->generate_v4();
    $self->uuid($uuid->as_string());
  }

  # If the username and/or password are not set, bail out with an error.
  unless ($self->username && $self->password) {
    Carp::croak "Username and/or password not set!";
  }

  # We have a username, password, and UUID - everything we need to get a new token
  my $credentials = {
    appType => 'Kasa_Android',
    cloudUserName => $self->username,
    cloudPassword => $self->password,
    terminalUUID => $self->uuid
  };

  my $request_object = {
     method => 'login',
     params => $credentials
  };

  my $user_agent = LWP::UserAgent::JSON->new;
  # Uncomment the next two lines if you need to debug...
  #$user_agent->add_handler("request_send",  sub { shift->dump; return });
  #$user_agent->add_handler("response_done", sub { shift->dump; return });

  my $request = HTTP::Request::JSON->new(POST => "https://wap.tplinkcloud.com");
  $request->header('Accept' => '*/*'); # Really, really annoying, but required by Kasa service...
  $request->json_content( $request_object );

  my $response = $user_agent->request($request);
  my $response_json_string =  $response->content;
  #print $response_json_string;

  my $json = JSON->new->allow_nonref;
  $json = $json->pretty(1);

  my $response_perl_scalar = $json->decode( $response_json_string );
  $self->token($response_perl_scalar->{result}{token});

  return 1;
}

=head2 getDevices

Returns an array of Device::TPLink::SmartHome::Kasa objects representing all devices associated with this account.

    use Device::TPLink::SmartHome::Kasa;

    my @devices = $kasa->getDevices();

=cut

sub getDevices {
  my $self = shift;

  # If we don't have a token, bail out with an error
  unless ($self->token) {
    Carp::croak "No token found!";
  }

  my $token = $self->token;
  my $user_agent = LWP::UserAgent::JSON->new;
  # Uncomment the next two lines if you need to debug...
  #$user_agent->add_handler("request_send",  sub { shift->dump; return });
  #$user_agent->add_handler("response_done", sub { shift->dump; return });

  my $json = JSON->new->allow_nonref;
  $json = $json->pretty(1);
  my $request_object = { method => "getDeviceList" };
  my $url = "https://wap.tplinkcloud.com?token=$token";
  my $request = HTTP::Request::JSON->new(POST => $url);
  $request->header('Accept' => '*/*');
  $request->json_content( $request_object );
  my $response = $user_agent->request($request);
  my $response_json_string =  $response->content;
  my $response_perl_scalar = $json->decode( $response_json_string );

  my @devices = ();



( run in 0.728 second using v1.01-cache-2.11-cpan-140bd7fdf52 )