Skip to content

Commit

Permalink
Merge branch 'timer_states'
Browse files Browse the repository at this point in the history
  • Loading branch information
hplato committed Sep 28, 2018
2 parents 839442f + 6873d21 commit 2a5fb3c
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions lib/Timer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ sub check_for_timer_actions {
my $ref;
while ( $ref = shift @sets_from_previous_pass ) {
&set_from_last_pass($ref);
# &update_state($ref);
}
for $ref (&expired_timers_with_actions) {
&run_action($ref);
Expand Down Expand Up @@ -110,6 +111,31 @@ sub delete_timer_with_action {
}
}

sub update_state {
my ($self) = @_;

return unless ($::New_Second);
if ($self->active() ) {
my $repeat = 0;
$repeat = $self->{repeat} if (defined $self->{repeat});
my $seconds = $self->seconds_remaining;
my $hours = int($seconds / 3600);
my $minutes = int (($seconds % 3600) / 60);
my $seconds = int($seconds % 60);
my $state = sprintf("%d:%02d:%02d",$hours, $minutes, $seconds);
#&::print_log("****** state=$state");
$state .= ",$repeat" if ($repeat);
$self->{state} = $state;
$self->{set_time} = $::Time;
} else {
unless ($self->{state} eq "inactive") {
$self->{state} = "inactive" ;
$self->{set_time} = $::Time;
}
}

}

=item C<new>
Used to create the object.
Expand All @@ -122,6 +148,17 @@ sub new {

# Not sure why this gives an error without || Timer
bless $self, $class || 'Timer';
# $id isn't actually used? Going to use that as a flag to enable/disable state setting
$self->{state_enable} = 1;
$self->{state_enable} = $::config_parms{enable_timer_state_updates} if (defined $::config_parms{enable_timer_state_updates});
$self->{state_enable} = $id if (defined $id);
if ($self->{state_enable}) {
$self->{state} = "inactive";
} else {
$self->{state} = undef; #restore the previous default behavior
}
$self->{state_updating} = 0; #&::MainLoop_pre_add_hook doesn't seem to be available yet, so add it to the set

return $self;
}

Expand Down Expand Up @@ -178,6 +215,11 @@ sub state_log {
return @{ $$self{state_log} } if $$self{state_log};
}

sub get_idle_time {
return undef unless $_[0]->{set_time};
return $main::Time - $_[0]->{set_time};
}

=item C<set($period, $action, $cycles)>
$period is the timer period in seconds
Expand All @@ -195,6 +237,10 @@ sub set {
# print "db1 $main::Time_Date running set s=$self s=$state a=$action t=$self->{text} c=@c\n";
return if &main::check_for_tied_filters( $self, $state );

if (($self->{state_updating} == 0) and ($self->{state_enable})) {
&::MainLoop_pre_add_hook( \&Timer::update_state, 0, $self);
}

# Set states for NEXT pass, so expired, active, etc,
# checks are consistent for one pass.
push @sets_from_previous_pass, $self;
Expand Down

0 comments on commit 2a5fb3c

Please sign in to comment.