From 63ef6680a7867777d3c6b15ed246eea6efb082ef Mon Sep 17 00:00:00 2001 From: Lieven Hollevoet Date: Sun, 20 Mar 2016 22:30:20 +0100 Subject: [PATCH] Merged proposed change in #559 by @jduda after perltidy change. --- lib/Motion_Item.pm | 85 ++++++++++++++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 29 deletions(-) diff --git a/lib/Motion_Item.pm b/lib/Motion_Item.pm index e2680bc35..2a3a2d21e 100644 --- a/lib/Motion_Item.pm +++ b/lib/Motion_Item.pm @@ -80,10 +80,6 @@ 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' ); } @@ -91,13 +87,9 @@ sub initialize { 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( @@ -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; @@ -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 ); } @@ -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;