-
Notifications
You must be signed in to change notification settings - Fork 1
/
Helper2.pm
93 lines (77 loc) · 3.13 KB
/
Helper2.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
87
88
89
90
91
92
93
# $Id$
package FHEM::Core::Timer::Helper2;
use strict;
use warnings;
use utf8;
use Carp qw( carp );
use Time::HiRes;
use version; our $VERSION = qv('1.0.0');
use Exporter qw(import);
our @EXPORT_OK = qw(setRegIntTimer deleteSingleRegIntTimer resetRegIntTimer deleteAllRegIntTimer renameAllRegIntTimer);
sub setRegIntTimer {
my $modifier = shift // carp q[No modifier name] && return;
my $time = shift // carp q[No time specified] && return;
my $callback = shift // carp q[No function specified ] && return;
my $hash = shift // carp q[No hash to reference specified] && return;;
my $initFlag = shift // 0;
my $timerName = "$hash->{NAME}:$modifier";
my $fnHash = {
time => $tim,
callback => $callback
};
if ( defined( $hash->{TIMER}{$timerName} ) ) {
::Log3( $hash, 1, "[$hash->{NAME}] possible overwriting of timer $timerName - please delete it first" );
::stacktrace();
}
else {
$hash->{TIMER}{$timerName} = $fnHash;
}
::Log3( $hash, 5, "[$hash->{NAME}] setting Timer: $timerName " . ::FmtDateTime($tim) );
::InternalTimer( $tim, $callback, $timerName, $initFlag );
return $fnHash;
}
sub deleteSingleRegIntTimer {
my $modifier = shift // carp q[No modifier name] && return;
my $hash = shift // carp q[No hash to reference specified] && return;;
my $timerName = "$hash->{NAME}:$modifier";
my $fnHash = $hash->{TIMER}{$timerName};
if ( defined($fnHash) ) {
::Log3( $hash, 5, "[$hash->{NAME}] removing Timer: $timerName" );
::RemoveInternalTimer($timerName);
delete $hash->{TIMER}{$timerName};
}
return;
}
sub resetRegIntTimer {
my $modifier = shift // carp q[No modifier name] && return;
my $time = shift // carp q[No time specified] && return;
my $callback = shift // carp q[No function specified ] && return;
my $hash = shift // carp q[No hash to reference specified] && return;;
my $initFlag = shift // 0;
deleteSingleRegIntTimer( $modifier, $hash );
return setRegIntTimer ( $modifier, $time, $callback, $hash, $initFlag );
}
sub deleteAllRegIntTimer {
my $hash = shift // carp q[No hash to reference specified] && return;;
for ( keys %{ $hash->{TIMER} } ) {
my ($oname, $modifier) = split m{:}xms;
deleteSingleRegIntTimer( $modifier, $hash );
}
return;
}
sub renameAllRegIntTimer {
my $hash = shift // carp q[No hash to reference specified] && return;;
my $newName = shift // carp q[No new name specified] && return;
my $oldName = shift // carp q[No old name specified] && return;
for ( keys %{ $hash->{TIMER} } ) {
my $tim = $hash->{TIMER}{$_}->{time};
my $callback = $hash->{TIMER}{$_}->{callback};
::RemoveInternalTimer($_);
delete $hash->{TIMER}{$_};
my ($oname, $modifier) = split m{:}xms;
setRegisteredInternalTimer($modifier, $tim, $callback, $hash);
}
return;
}
1;
__END__