WebService-FindMyiPhone

 view release on metacpan or  search on metacpan

lib/WebService/FindMyiPhone/Device.pm  view on Meta::CPAN

package WebService::FindMyiPhone::Device;

use strict;
use warnings;

use 5.010_001;
our $VERSION = '0.02';

use Carp;

# use Data::Dumper;    # TODO: remove

sub new {
    my ( $class, $parent, $data ) = @_;
    my $self = { _parent => $parent, %$data };
    return bless $self, $class;
}

sub _update_self {
    my ( $self, $new_data ) = @_;
    $self->{$_} = $new_data->{$_} for keys %$new_data;
}

sub send_message {
    my ( $self, $sound, $message, $subject ) = @_;
    $sound = $sound ? 'true' : 'false';
    $subject ||= 'Important Message';
    my $post
        = sprintf(
        '{"clientContext":{"appName":"FindMyiPhone","appVersion":"1.4","buildVersion":"145","deviceUDID":"0000000000000000000000000000000000000000","inactiveTime":5911,"osVersion":"3.2","productType":"iPad1,1","selectedDevice":"%s","shouldLocate":fal...
        $self->{id}, $self->{id}, $sound, $subject, $message );
    return $self->{_parent}->_post( '/sendMessage', $post )->json;
}

sub remote_lock {
    my ( $self, $passcode ) = @_;
    my $post
        = sprintf(
        '{"clientContext":{"appName":"FindMyiPhone","appVersion":"1.4","buildVersion":"145","deviceUDID":"0000000000000000000000000000000000000000","inactiveTime":5911,"osVersion":"3.2","productType":"iPad1,1","selectedDevice":"%s","shouldLocate":fal...
        $self->{id}, $self->{id}, $passcode );
    return $self->{_parent}->_post( '/remoteLock', $post );
}

sub location {
    my ($self) = @_;
    my $count = 0;
    while ( !$self->{location}{locationFinished} ) {
        # print Dumper( $self->{location} );
        sleep 2;
        $self->{_parent}->update_devices;
        last if ++$count >= 3;
        # warn "Sleeping and checking again";

    }
    return $self->{location};
}

1;
__END__

=encoding utf-8

=head1 NAME

WebService::FindMyiPhone::Device - Device object for WebService::FindMyiPhone

=head1 SYNOPSIS

  use WebService::FindMyiPhone;
  my $fmiphone = WebService::FindMyiPhone->new(
      username => 'email@address',
      password => 'YaakovLOVE',
  );
  my $iphone = $fmiphone->get_device_by( name => 'mmm cake');
  my $location = $iphone->location();
  $iphone->send_message(1, 'Where did I leave you?');

=head1 DESCRIPTION

WebService::FindMyiPhone::Device is the class used for
L<WebService::FindMyiPhone> devices. See the documentation there for more
information.

Device objects are stored as a blessed hashref, the C<_parent> key is a
reference to the L<WebService::FindMyiPhone> object that created it.  The rest
of the keys are directly from Apple.  You are incouraged to inspect the data
there and make use of anything interesting to you.

=head1 METHODS

=head2 send_message( $sound, $message, $subject )

Send a message to the device.  C<$sound> determines if a sound should be
played with the message, a true value will cause a sound even if the phone or
iPad is in silent mode.  C<$message> is the message to display.  C<$subject> is
optional and defaults to 'Important Message'.

=head2 remote_lock($passcode)



( run in 0.992 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )