Amazon-Dash-Button

 view release on metacpan or  search on metacpan

lib/Amazon/Dash/Button/Device.pm  view on Meta::CPAN


sub build {
    my $self = shift;

    # check the mac address
    die "mac address is undefined" unless defined $self->mac();

    return $self;
}

sub debug {
	warn join( ' ', map { $_ // 'undef' } @_ );
}

sub check {
    my ( $self, $mac2check ) = @_;

    return unless defined $mac2check;

    $mac2check =~ s{:}{}g;

    return if $self->mac() ne lc($mac2check);

    debug( "Find Button", $self->name() );
    my $now = time();
    return
      if $self->timeout > 0 && ( $now - $self->last_click ) <= $self->timeout;
    $self->last_click($now);
    debug( "perform onClick for", $self->name() );

    # we want to fork to run the onClick action
    #	we can disable it during unit tests or others
    return
      if $self->_fork_for_onClick()
      && fork();    # TODO protect the fork if it fails ?
     # if you are not forking you want the onClick function to return pretty fast
     #	as a click will generate several packets inside the timeout window
    $self->onClick->();

t/Device.t  view on Meta::CPAN

isa_ok $adb, 'Amazon::Dash::Button::Device';

ok $adb->_fork_for_onClick;

my $click = 0;

$adb = Amazon::Dash::Button::Device->new( mac => q{00:11:22:33:44:55}, onClick => sub { ++$click; note "this is a click" }, _fork_for_onClick => 0);
isa_ok $adb, 'Amazon::Dash::Button::Device';

no warnings qw{redefine once};
*Amazon::Dash::Button::Device::debug = sub { note @_ };

ok !$adb->_fork_for_onClick;

ok !$adb->check();
ok !$adb->check('foo');
ok !$adb->check('00:11:22:33:44:66');

ok $adb->check('00:11:22:33:44:55'), 'click';
is $click, 1;
ok !$adb->check('00:11:22:33:44:55'), 'do not click twice in the timeout window';



( run in 1.316 second using v1.01-cache-2.11-cpan-49f99fa48dc )