-
Notifications
You must be signed in to change notification settings - Fork 1
/
Helper.pm
86 lines (65 loc) · 2.23 KB
/
Helper.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# $Id: Helper.pm 22299 2020-06-28 20:31:30Z Sidey $
package FHEM::Core::Timer::Helper;
use strict;
use warnings;
use utf8;
use Carp qw(croak carp);
use Time::HiRes;
use version; our $VERSION = qv('1.0.0');
my %LOT; # Hash for ListOfTimers
use Exporter qw(import);
our @EXPORT_OK = qw(addTimer removeTimer renewTimer optimizeLOT);
sub addTimer {
my $defName = shift // carp 'No definition name' && return;
my $time = shift // carp q[No time specified] && return;
my $func = shift // carp q[No function specified] && return;
my $arg = shift // q{};
my $initFlag = shift // 0;
my %h = (
arg => $arg,
func => $func,
calltime => $time,
);
::InternalTimer($time, $func, $arg , $initFlag);
return push @{$LOT{$defName}} , \%h;
}
sub removeTimer {
my $defName = shift // carp q[No definition name];
my $func = shift // undef;
my $arg = shift // q{};
return 0 if ( !exists $LOT{$defName} );
my $numRemoved = 0;
for my $index (0 .. scalar @{$LOT{$defName}}-1 ) {
if ( ref $LOT{$defName}[$index] eq 'HASH' && exists $LOT{$defName}[$index]->{func}
&& (!defined $func || $LOT{$defName}[$index]->{func} == $func )
&& ( $arg eq q{} || $LOT{$defName}[$index]->{arg} eq $arg)
) {
::RemoveInternalTimer($LOT{$defName}[$index]->{arg},$LOT{$defName}[$index]->{func});
delete($LOT{$defName}[$index]);
$numRemoved++;
}
}
return $numRemoved;
}
sub renewTimer {
my $defName = shift // carp 'No definition name' && return;
my $time = shift // carp q[No time specified] && return;
my $func = shift // carp q[No function specified] && return;
my $arg = shift // q{};
my $initFlag = shift // 0;
my $getLOT = shift // 0;
removeTimer ($defName, $func, $arg);
addTimer ($defName, $func, $arg , $initFlag);
return push @{$LOT{$defName}} , \%h if !$getLOT;
optimizeLOT($defName);
return \%LOT;
}
sub optimizeLOT
{
my $defName = shift // carp q[No definition name];
return 0 if ( !exists $LOT{$defName} );
my $now= ::gettimeofday();
@{$LOT{$defName}} = grep { $_->{calltime} >= $now } @{$LOT{$defName}};
return scalar @{$LOT{$defName}};
}
1;