Skip to content

Commit

Permalink
Merge pull request #581 from hollie/fix_inactivity_timer
Browse files Browse the repository at this point in the history
Merged proposed change in #559 by @jduda after perltidy change.
  • Loading branch information
hplato authored Jul 10, 2016
2 parents 611ad53 + 63ef668 commit b5792fe
Showing 1 changed file with 56 additions and 29 deletions.
85 changes: 56 additions & 29 deletions lib/Motion_Item.pm
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,16 @@ sub initialize {
$$self{m_timeout}->set( 2 * 60, $self );
$$self{m_timerCheck} = new Timer() unless $$self{m_timerCheck};

# $$self{m_timerCheck}->set(24*60*60,$self);
# Default to a print_log message after 24 hours of inactivity
$$self{'inactivity_time'} = 24 * 3600;

# initialize states array
@{ $$self{states} } = ( 'motion', 'still' );
}

sub set {
my ( $self, $p_state, $p_setby ) = @_;

# Ignore the dark/light and normal states
if ( ( $p_state eq 'dark' )
or ( $p_state eq 'light' )
or ( $p_state =~ /^normal/i ) )
{
return;
}
my $name = $$self{object_name};
&::print_log("Motion_Item::set name: $name state: $p_state")
if $main::Debug{occupancy};

if ( ref $p_setby and $p_setby->can('get_set_by') ) {
&::print_log(
Expand All @@ -111,21 +103,13 @@ sub set {
if $main::Debug{occupancy};
}

# Hawkeye (MS13) motion detector and security sensors
if ( ( $p_state eq 'on' ) or ( $p_state =~ /^alert/i ) ) {
$p_state = 'motion';
}
elsif ( ( $p_state eq 'off' ) ) { #or ($p_state =~ /^normal/i)) {
$p_state = 'still';
}

if ( $p_state eq 'motion' ) { # Received ON

# $main::DBI->prepare("insert into Events (Object,ObjectType,State) values ('$$self{object_name}','motion','$p_state');")->execute();
$$self{m_timeout}->set( 2 * 60, $self );
$$self{m_timerCheck}->set( $$self{'inactivity_time'}, $self );
}
elsif ( $p_setby eq $$self{m_timerCheck} ) { # Check timer expired
if ( $p_setby eq $$self{m_timerCheck} ) { # Check timer expired
&::print_log("Motion_Item:: name: $name m_timerCheck")
if $main::Debug{occupancy};
my $hours = $$self{'inactivity_time'} / ( 60 * 60 );
&::print_log(
"$name->Has not received motion in $hours hours (battery dead?) action->$$self{'inactivity_action'}"
) if $main::Debug{occupancy};
if ( $$self{'inactivity_action'} ) {

package main;
Expand All @@ -135,15 +119,55 @@ sub set {
}
else {
&::print_log(
"$$self{object_name}->Has not received motion in 24hrs");
"$$self{object_name}->Has not received motion in $hours hours")
if $main::Debug{occupancy};
}
if ( $$self{'inactivity_time'} ) {
$$self{m_timerCheck}->set( $$self{'inactivity_time'}, $self );
}
$p_state = 'check';
}
elsif ( $p_setby eq $$self{m_timeout} ) { # Timer expired
&::print_log("Motion_Item:: name: $name m_timeout")
if $main::Debug{occupancy};
$p_state = 'still';
}
elsif ( $p_state eq 'still' ) { # Motion OFF
$$self{m_timeout}->unset() if defined $$self{m_timeout};
else {
# Hawkeye (MS13) motion detector and security sensors
if ( ( $p_state eq 'on' ) or ( $p_state =~ /^alert/i ) ) {
$p_state = 'motion';
}
elsif ( ( $p_state eq 'off' ) ) {
$p_state = 'still';
}

# Ignore the dark/light/normal states
if ( ( $p_state eq 'dark' )
or ( $p_state eq 'light' )
or ( $p_state =~ /^normal/i ) )
{
# Reset watchdog timer and return on normal event
if ( $$self{'inactivity_time'} ) {
$$self{m_timerCheck}->set( $$self{'inactivity_time'}, $self );
}
return;
}
elsif ( $p_state eq 'motion' ) { # Received ON
&::print_log("Motion_Item:: name: $name state: $p_state")
if $main::Debug{occupancy};
$$self{m_timeout}->set( 2 * 60, $self );
if ( $$self{'inactivity_time'} ) {
$$self{m_timerCheck}->set( $$self{'inactivity_time'}, $self );
}
}
elsif ( $p_state eq 'still' ) { # Motion OFF
&::print_log("Motion_Item:: name: $name state: $p_state")
if $main::Debug{occupancy};
$$self{m_timeout}->unset();
if ( $$self{'inactivity_time'} ) {
$$self{m_timerCheck}->set( $$self{'inactivity_time'}, $self );
}
}
}
$self->SUPER::set( $p_state, $p_setby );
}
Expand All @@ -165,6 +189,9 @@ sub set_inactivity_alarm($$$) {
$$self{'inactivity_action'} = $action;
$$self{'inactivity_time'} = $time * 3600;
$$self{m_timerCheck}->set( $time * 3600, $self );
my $name = $$self{object_name};
&::print_log("$name->set_inactivity_alarm for $time hours action->$action")
if $main::Debug{occupancy};
}

1;
Expand Down

0 comments on commit b5792fe

Please sign in to comment.