change Modul to packages
This commit is contained in:
parent
2ef6d60201
commit
d1638cf310
@ -24,92 +24,103 @@
|
||||
# along with FHEM. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
# packages ####################################################################
|
||||
package main;
|
||||
use strict;
|
||||
use warnings;
|
||||
no if $] >= 5.017011, warnings => 'experimental::smartmatch';
|
||||
use Time::HiRes qw(gettimeofday);
|
||||
use Time::Local 'timelocal_nocheck';
|
||||
## unserer packagename
|
||||
package FHEM::RandomTimer;
|
||||
|
||||
# forward declarations ########################################################
|
||||
sub RandomTimer_Initialize($);
|
||||
use strict;
|
||||
use warnings;
|
||||
no if $] >= 5.017011, warnings => 'experimental::smartmatch';
|
||||
use Time::HiRes qw(gettimeofday);
|
||||
use Time::Local 'timelocal_nocheck';
|
||||
|
||||
sub RandomTimer_Define($$);
|
||||
sub RandomTimer_Undef($$);
|
||||
sub RandomTimer_Set($@);
|
||||
sub RandomTimer_Attr($$$);
|
||||
## Import der FHEM Funktionen
|
||||
#-- Run before package compilation
|
||||
BEGIN {
|
||||
|
||||
sub RandomTimer_addDays ($$);
|
||||
sub RandomTimer_device_switch ($);
|
||||
sub RandomTimer_device_toggle ($);
|
||||
sub RandomTimer_disableDown($);
|
||||
sub RandomTimer_down($);
|
||||
sub RandomTimer_Exec($);
|
||||
sub RandomTimer_getSecsToNextAbschaltTest($);
|
||||
sub RandomTimer_isAktive ($);
|
||||
sub RandomTimer_isDisabled($);
|
||||
sub RandomTimer_schaltZeitenErmitteln ($$);
|
||||
sub RandomTimer_setActive($$);
|
||||
sub RandomTimer_setState($);
|
||||
sub RandomTimer_setSwitchmode ($$);
|
||||
sub RandomTimer_SetTimer($);
|
||||
sub RandomTimer_startZeitErmitteln ($$);
|
||||
sub RandomTimer_stopTimeReached($);
|
||||
sub RandomTimer_stopZeitErmitteln ($$);
|
||||
sub RandomTimer_Wakeup();
|
||||
sub RandomTimer_zeitBerechnen ($$$$);
|
||||
# Import from main context
|
||||
GP_Import(
|
||||
qw(
|
||||
readingsSingleUpdate
|
||||
readingsBulkUpdate
|
||||
readingsBeginUpdate
|
||||
readingsEndUpdate
|
||||
defs
|
||||
modules
|
||||
Log
|
||||
Log3
|
||||
attr
|
||||
readingFnAttributes
|
||||
AttrVal
|
||||
ReadingsVal
|
||||
IsDisabled)
|
||||
);
|
||||
}
|
||||
|
||||
#-- Export to main context with different name
|
||||
GP_Export(
|
||||
qw(
|
||||
Initialize
|
||||
Exec
|
||||
)
|
||||
);
|
||||
|
||||
# initialize ##################################################################
|
||||
sub RandomTimer_Initialize($) {
|
||||
sub Initialize {
|
||||
my ($hash) = @_;
|
||||
|
||||
$hash->{DefFn} = "RandomTimer_Define";
|
||||
$hash->{UndefFn} = "RandomTimer_Undef";
|
||||
$hash->{SetFn} = "RandomTimer_Set";
|
||||
$hash->{AttrFn} = "RandomTimer_Attr";
|
||||
$hash->{AttrList} = "onCmd offCmd switchmode disable:0,1 disableCond disableCondCmd:none,offCmd,onCmd offState ".
|
||||
"runonce:0,1 keepDeviceAlive:0,1 forceStoptimeSameDay:0,1 ".
|
||||
$readingFnAttributes;
|
||||
$hash->{DefFn} = 'FHEM::RandomTimer::Define';
|
||||
$hash->{UndefFn} = 'FHEM::RandomTimer::Undef';
|
||||
$hash->{SetFn} = 'FHEM::RandomTimer::Set';
|
||||
$hash->{AttrFn} = 'FHEM::RandomTimer::Attr';
|
||||
$hash->{AttrList} =
|
||||
'onCmd offCmd switchmode disable:0,1 disableCond disableCondCmd:none,offCmd,onCmd offState '
|
||||
. 'runonce:0,1 keepDeviceAlive:0,1 forceStoptimeSameDay:0,1 '
|
||||
. $readingFnAttributes;
|
||||
}
|
||||
|
||||
# regular Fn ##################################################################
|
||||
sub RandomTimer_Define($$) {
|
||||
my ($hash, $def) = @_;
|
||||
sub Define {
|
||||
my ( $hash, $def ) = @_;
|
||||
|
||||
RemoveInternalTimer($hash);
|
||||
my ($name, $type, $timespec_start, $device, $timespec_stop, $timeToSwitch, $variation) =
|
||||
split("[ \t][ \t]*", $def);
|
||||
my ( $name, $type, $timespec_start, $device, $timespec_stop, $timeToSwitch,
|
||||
$variation )
|
||||
= split( "[ \t][ \t]*", $def );
|
||||
|
||||
return "wrong syntax: define <name> RandomTimer <timespec_start> <device> <timespec_stop> <timeToSwitch> [<variations>]"
|
||||
if(!defined $timeToSwitch);
|
||||
return
|
||||
"wrong syntax: define <name> RandomTimer <timespec_start> <device> <timespec_stop> <timeToSwitch> [<variations>]"
|
||||
if ( !defined $timeToSwitch );
|
||||
|
||||
return "Wrong timespec_start <$timespec_start>, use \"[+][*]<time or func>\""
|
||||
if($timespec_start !~ m/^(\+)?(\*)?(.*)$/i);
|
||||
return
|
||||
"Wrong timespec_start <$timespec_start>, use \"[+][*]<time or func>\""
|
||||
if ( $timespec_start !~ m/^(\+)?(\*)?(.*)$/i );
|
||||
|
||||
my ($rel, $rep, $tspec) = ($1, $2, $3);
|
||||
my ( $rel, $rep, $tspec ) = ( $1, $2, $3 );
|
||||
|
||||
my ($err, $hr, $min, $sec, $fn) = GetTimeSpec($tspec);
|
||||
return $err if($err);
|
||||
my ( $err, $hr, $min, $sec, $fn ) = GetTimeSpec($tspec);
|
||||
return $err if ($err);
|
||||
|
||||
$rel = "" if(!defined($rel));
|
||||
$rep = "" if(!defined($rep));
|
||||
$rel = "" if ( !defined($rel) );
|
||||
$rep = "" if ( !defined($rep) );
|
||||
|
||||
return "Wrong timespec_stop <$timespec_stop>, use \"[+][*]<time or func>\""
|
||||
if($timespec_stop !~ m/^(\+)?(\*)?(.*)$/i);
|
||||
my ($srel, $srep, $stspec) = ($1, $2, $3);
|
||||
my ($e, $h, $m, $s, $f) = GetTimeSpec($stspec);
|
||||
return $e if($e);
|
||||
if ( $timespec_stop !~ m/^(\+)?(\*)?(.*)$/i );
|
||||
my ( $srel, $srep, $stspec ) = ( $1, $2, $3 );
|
||||
my ( $e, $h, $m, $s, $f ) = GetTimeSpec($stspec);
|
||||
return $e if ($e);
|
||||
|
||||
return "invalid timeToSwitch <$timeToSwitch>, use 9999"
|
||||
if(!($timeToSwitch =~ m/^[0-9]{2,4}$/i));
|
||||
my ($varDuration,$varStart);
|
||||
$varDuration=0;
|
||||
$varStart=0;
|
||||
if(defined $variation) {
|
||||
if ( !( $timeToSwitch =~ m/^[0-9]{2,4}$/i ) );
|
||||
my ( $varDuration, $varStart );
|
||||
$varDuration = 0;
|
||||
$varStart = 0;
|
||||
if ( defined $variation ) {
|
||||
$variation =~ /^([\d]+)/ ? $varDuration = $1 : undef;
|
||||
$variation =~ /[:]([\d]+)/ ? $varStart = $1 : undef;
|
||||
}
|
||||
RandomTimer_setSwitchmode ($hash, "800/200") if (!defined $hash->{helper}{SWITCHMODE});
|
||||
|
||||
SetSwitchmode( $hash, "800/200" )
|
||||
if ( !defined $hash->{helper}{SWITCHMODE} );
|
||||
|
||||
$hash->{NAME} = $name;
|
||||
$hash->{DEVICE} = $device;
|
||||
@ -123,80 +134,86 @@ sub RandomTimer_Define($$) {
|
||||
$hash->{helper}{S_REL} = $srel;
|
||||
$hash->{helper}{S_REL} = $srel;
|
||||
|
||||
$hash->{COMMAND} = Value($hash->{DEVICE}) if ($featurelevel < 6.1);
|
||||
if ($featurelevel > 6.0) {
|
||||
$hash->{COMMAND} = ReadingsVal($hash->{DEVICE},"state",undef) ;
|
||||
$hash->{COMMAND} = Value( $hash->{DEVICE} ) if ( $featurelevel < 6.1 );
|
||||
if ( $featurelevel > 6.0 ) {
|
||||
$hash->{COMMAND} = ReadingsVal( $hash->{DEVICE}, "state", undef );
|
||||
$hash->{helper}{offRegex} = "off";
|
||||
$hash->{helper}{offReading} = "state";
|
||||
}
|
||||
|
||||
#$attr{$name}{verbose} = 4;
|
||||
|
||||
readingsSingleUpdate ($hash, "TimeToSwitch", $hash->{helper}{TIMETOSWITCH}, 1);
|
||||
readingsSingleUpdate( $hash, "TimeToSwitch", $hash->{helper}{TIMETOSWITCH},
|
||||
1 );
|
||||
|
||||
RandomTimer_RemoveInternalTimer("SetTimer", $hash);
|
||||
RandomTimer_InternalTimer("SetTimer", time(), "RandomTimer_SetTimer", $hash, 0);
|
||||
RemoveInternalTimer( "FHEM::RandomTimer::SetTimer", $hash );
|
||||
InternalTimer( "SetTimer", time(), "FHEM::RandomTimer::SetTimer", $hash,
|
||||
0 );
|
||||
|
||||
return undef;
|
||||
}
|
||||
|
||||
sub RandomTimer_Undef($$) {
|
||||
sub Undef {
|
||||
|
||||
my ($hash, $arg) = @_;
|
||||
my ( $hash, $arg ) = @_;
|
||||
|
||||
RandomTimer_RemoveInternalTimer("SetTimer", $hash);
|
||||
RandomTimer_RemoveInternalTimer("Exec", $hash);
|
||||
delete $modules{RandomTimer}{defptr}{$hash->{NAME}};
|
||||
RemoveInternalTimer( "FHEM::RandomTimer::SetTimer", $hash );
|
||||
RemoveInternalTimer( "FHEM::RandomTimer::Exec", $hash );
|
||||
delete $modules{RandomTimer}{defptr}{ $hash->{NAME} };
|
||||
return undef;
|
||||
}
|
||||
|
||||
sub RandomTimer_Attr($$$) {
|
||||
my ($cmd, $name, $attrName, $attrVal) = @_;
|
||||
sub Attr {
|
||||
my ( $cmd, $name, $attrName, $attrVal ) = @_;
|
||||
|
||||
my $hash = $defs{$name};
|
||||
|
||||
if( $attrName ~~ ["switchmode"] ) {
|
||||
RandomTimer_setSwitchmode($hash, $attrVal);
|
||||
if ( $attrName ~~ ["switchmode"] ) {
|
||||
SetSwitchmode( $hash, $attrVal );
|
||||
}
|
||||
|
||||
if( $attrName ~~ ["disable","disableCond"] ) {
|
||||
if ( $attrName ~~ [ "disable", "disableCond" ] ) {
|
||||
|
||||
# Schaltung vorziehen, damit bei einem disable abgeschaltet wird.
|
||||
RandomTimer_RemoveInternalTimer("Exec", $hash);
|
||||
RandomTimer_InternalTimer("Exec", time()+1, "RandomTimer_Exec", $hash, 0);
|
||||
RemoveInternalTimer( "FHEM::RandomTimer::Exec", $hash );
|
||||
InternalTimer( "Exec", time() + 1, "FHEM::RandomTimer::Exec", $hash,
|
||||
0 );
|
||||
}
|
||||
if( $attrName ~~ ["offState"] ) {
|
||||
my @offState = split(' ',$attrVal);
|
||||
if ( $attrName ~~ ["offState"] ) {
|
||||
my @offState = split( ' ', $attrVal );
|
||||
$hash->{helper}{offRegex} = $offState[0];
|
||||
$hash->{helper}{offReading} = $offState[1]//"state";
|
||||
$hash->{helper}{offReading} = $offState[1] // "state";
|
||||
}
|
||||
return undef;
|
||||
}
|
||||
|
||||
sub RandomTimer_Set($@) {
|
||||
my ($hash, @a) = @_;
|
||||
sub Set {
|
||||
my ( $hash, @a ) = @_;
|
||||
|
||||
return "no set value specified" if(int(@a) < 2);
|
||||
return "Unknown argument $a[1], choose one of execNow:noArg" if($a[1] eq "?");
|
||||
return "no set value specified" if ( int(@a) < 2 );
|
||||
return "Unknown argument $a[1], choose one of execNow:noArg"
|
||||
if ( $a[1] eq "?" );
|
||||
|
||||
my $name = shift @a;
|
||||
my $v = join(" ", @a);
|
||||
my $v = join( " ", @a );
|
||||
|
||||
if ($v eq "execNow") {
|
||||
Log3 ($hash, 3, "[$name] set $name $v");
|
||||
if (AttrVal($name, "disable", 0)) {
|
||||
Log3 ($hash, 3, "[$name] is disabled, set execNow not possible");
|
||||
} else {
|
||||
RandomTimer_RemoveInternalTimer("Exec", $hash);
|
||||
RandomTimer_InternalTimer("Exec", time()+1, "RandomTimer_Exec", $hash, 0);
|
||||
if ( $v eq "execNow" ) {
|
||||
Log3( $hash, 3, "[$name] set $name $v" );
|
||||
if ( AttrVal( $name, "disable", 0 ) ) {
|
||||
Log3( $hash, 3, "[$name] is disabled, set execNow not possible" );
|
||||
}
|
||||
else {
|
||||
RemoveInternalTimer( "FHEM::RandomTimer::Exec", $hash );
|
||||
InternalTimer( "Exec", time() + 1,
|
||||
"FHEM::RandomTimer::Exec", $hash, 0 );
|
||||
}
|
||||
}
|
||||
return undef;
|
||||
}
|
||||
|
||||
|
||||
# module Fn ###################################################################
|
||||
sub RandomTimer_addDays ($$) {
|
||||
my ($now, $days) = @_;
|
||||
sub AddDays {
|
||||
my ( $now, $days ) = @_;
|
||||
|
||||
my @jetzt_arr = localtime($now);
|
||||
$jetzt_arr[3] += $days;
|
||||
@ -205,395 +222,487 @@ sub RandomTimer_addDays ($$) {
|
||||
|
||||
}
|
||||
|
||||
sub RandomTimer_device_switch ($) {
|
||||
sub Device_switch {
|
||||
my ($hash) = @_;
|
||||
|
||||
my $command = "set @ $hash->{COMMAND}";
|
||||
if ($hash->{COMMAND} eq "on") {
|
||||
$command = AttrVal($hash->{NAME}, "onCmd", $command);
|
||||
} else {
|
||||
$command = AttrVal($hash->{NAME}, "offCmd", $command);
|
||||
if ( $hash->{COMMAND} eq "on" ) {
|
||||
$command = AttrVal( $hash->{NAME}, "onCmd", $command );
|
||||
}
|
||||
else {
|
||||
$command = AttrVal( $hash->{NAME}, "offCmd", $command );
|
||||
}
|
||||
$command =~ s/@/$hash->{DEVICE}/g;
|
||||
$command = SemicolonEscape($command);
|
||||
readingsSingleUpdate($hash, 'LastCommand', $command, 1);
|
||||
Log3 $hash, 4, "[".$hash->{NAME}. "]"." command: $command";
|
||||
readingsSingleUpdate( $hash, 'LastCommand', $command, 1 );
|
||||
Log3 $hash, 4, "[" . $hash->{NAME} . "]" . " command: $command";
|
||||
|
||||
my $ret = AnalyzeCommandChain(undef, $command);
|
||||
Log3 ($hash, 3, "[$hash->{NAME}] ERROR: " . $ret . " SENDING " . $command) if($ret);
|
||||
#Log3 $hash, 3, "[$hash->{NAME}] Value($hash->{COMMAND})=".Value($hash->{DEVICE});
|
||||
#$hash->{"$hash->{COMMAND}Value"} = Value($hash->{DEVICE});
|
||||
my $ret = AnalyzeCommandChain( undef, $command );
|
||||
Log3( $hash, 3, "[$hash->{NAME}] ERROR: " . $ret . " SENDING " . $command )
|
||||
if ($ret);
|
||||
|
||||
#Log3 $hash, 3, "[$hash->{NAME}] Value($hash->{COMMAND})=".Value($hash->{DEVICE});
|
||||
#$hash->{"$hash->{COMMAND}Value"} = Value($hash->{DEVICE});
|
||||
}
|
||||
|
||||
sub RandomTimer_device_toggle ($) {
|
||||
sub Device_toggle {
|
||||
my ($hash) = @_;
|
||||
my $name = $hash->{NAME};
|
||||
|
||||
#my $attrOffState = AttrVal($name,"offState",undef);
|
||||
my $status = Value($hash->{DEVICE});
|
||||
my $status = Value( $hash->{DEVICE} );
|
||||
|
||||
if (defined $hash->{helper}{offRegex}) {
|
||||
$status = ReadingsVal($hash->{DEVICE},$hash->{helper}{offReading},"off");
|
||||
if ( defined $hash->{helper}{offRegex} ) {
|
||||
$status =
|
||||
ReadingsVal( $hash->{DEVICE}, $hash->{helper}{offReading}, "off" );
|
||||
my $attrOffState = $hash->{helper}{offRegex};
|
||||
$status = $status =~ /^$attrOffState$/ ? "off" : lc($status) ;
|
||||
$status = $status =~ /off/ ? "off" : "on" ;
|
||||
$status = $status =~ /^$attrOffState$/ ? "off" : lc($status);
|
||||
$status = $status =~ /off/ ? "off" : "on";
|
||||
}
|
||||
if ($status ne "on" && $status ne "off" ) {
|
||||
if ($hash->{helper}{offRegex}) {
|
||||
Log3 $hash, 3, "[$name] result of function ReadingsVal($hash->{DEVICE},\"<offReading>\",undef) must be 'on' or 'off' or set attribute offState accordingly";
|
||||
} else {
|
||||
Log3 $hash, 3, "[$name] result of function Value($hash->{DEVICE}) must be 'on' or 'off'";;
|
||||
if ( $status ne "on" && $status ne "off" ) {
|
||||
if ( $hash->{helper}{offRegex} ) {
|
||||
Log3 $hash, 3,
|
||||
"[$name] result of function ReadingsVal($hash->{DEVICE},\"<offReading>\",undef) must be 'on' or 'off' or set attribute offState accordingly";
|
||||
}
|
||||
else {
|
||||
Log3 $hash, 3,
|
||||
"[$name] result of function Value($hash->{DEVICE}) must be 'on' or 'off'";
|
||||
}
|
||||
}
|
||||
|
||||
my $sigma = ($status eq "on")
|
||||
my $sigma =
|
||||
( $status eq "on" )
|
||||
? $hash->{helper}{SIGMAWHENON}
|
||||
: $hash->{helper}{SIGMAWHENOFF};
|
||||
|
||||
my $zufall = int(rand(1000));
|
||||
Log3 $hash, 4, "[$name] IstZustand:$status sigmaWhen-$status:$sigma random:$zufall<$sigma=>" . (($zufall < $sigma)?"true":"false");
|
||||
my $zufall = int( rand(1000) );
|
||||
Log3 $hash, 4,
|
||||
"[$name] IstZustand:$status sigmaWhen-$status:$sigma random:$zufall<$sigma=>"
|
||||
. ( ( $zufall < $sigma ) ? "true" : "false" );
|
||||
|
||||
if ($zufall < $sigma ) {
|
||||
$hash->{COMMAND} = ($status eq "on") ? "off" : "on";
|
||||
RandomTimer_device_switch($hash);
|
||||
if ( $zufall < $sigma ) {
|
||||
$hash->{COMMAND} = ( $status eq "on" ) ? "off" : "on";
|
||||
Device_switch($hash);
|
||||
}
|
||||
}
|
||||
|
||||
sub RandomTimer_disableDown($) {
|
||||
sub DisableDown {
|
||||
my ($hash) = @_;
|
||||
my $disableCondCmd = AttrVal($hash->{NAME}, "disableCondCmd", 0);
|
||||
my $disableCondCmd = AttrVal( $hash->{NAME}, "disableCondCmd", 0 );
|
||||
|
||||
if ($disableCondCmd ne "none") {
|
||||
Log3 $hash, 4, "[".$hash->{NAME}."]"." setting requested disableCondCmd on $hash->{DEVICE}: ";
|
||||
$hash->{COMMAND} = AttrVal($hash->{NAME}, "disableCondCmd", 0) eq "onCmd" ? "on" : "off";
|
||||
RandomTimer_device_switch($hash);
|
||||
} else {
|
||||
Log3 $hash, 4, "[".$hash->{NAME}."]"." no action requested on $hash->{DEVICE}: ";
|
||||
if ( $disableCondCmd ne "none" ) {
|
||||
Log3 $hash, 4,
|
||||
"["
|
||||
. $hash->{NAME} . "]"
|
||||
. " setting requested disableCondCmd on $hash->{DEVICE}: ";
|
||||
$hash->{COMMAND} =
|
||||
AttrVal( $hash->{NAME}, "disableCondCmd", 0 ) eq "onCmd"
|
||||
? "on"
|
||||
: "off";
|
||||
Device_switch($hash);
|
||||
}
|
||||
else {
|
||||
Log3 $hash, 4,
|
||||
"["
|
||||
. $hash->{NAME} . "]"
|
||||
. " no action requested on $hash->{DEVICE}: ";
|
||||
}
|
||||
}
|
||||
|
||||
sub RandomTimer_down($) {
|
||||
sub Down {
|
||||
my ($hash) = @_;
|
||||
Log3 $hash, 4, "[".$hash->{NAME}."]"." setting requested keepDeviceAlive on $hash->{DEVICE}: ";
|
||||
$hash->{COMMAND} = AttrVal($hash->{NAME}, "keepDeviceAlive", 0) ? "on" : "off";
|
||||
RandomTimer_device_switch($hash);
|
||||
Log3 $hash, 4,
|
||||
"["
|
||||
. $hash->{NAME} . "]"
|
||||
. " setting requested keepDeviceAlive on $hash->{DEVICE}: ";
|
||||
$hash->{COMMAND} =
|
||||
AttrVal( $hash->{NAME}, "keepDeviceAlive", 0 ) ? "on" : "off";
|
||||
Device_switch($hash);
|
||||
}
|
||||
|
||||
|
||||
sub RandomTimer_Exec($) {
|
||||
sub Exec {
|
||||
my ($myHash) = @_;
|
||||
|
||||
my $hash = RandomTimer_GetHashIndirekt($myHash, (caller(0))[3]);
|
||||
return if (!defined($hash));
|
||||
my $hash = GetHashIndirekt( $myHash, ( caller(0) )[3] );
|
||||
return if ( !defined($hash) );
|
||||
|
||||
my $now = time();
|
||||
|
||||
# Wenn aktiv aber disabled, dann timer abschalten, Meldung ausgeben.
|
||||
my $active = RandomTimer_isAktive($hash);
|
||||
my $disabled = RandomTimer_isDisabled($hash);
|
||||
my $stopTimeReached = RandomTimer_stopTimeReached($hash);
|
||||
my $active = IsAktive($hash);
|
||||
my $disabled = IsDisabled($hash);
|
||||
my $stopTimeReached = StopTimeReached($hash);
|
||||
|
||||
if ($active) {
|
||||
|
||||
# wenn temporär ausgeschaltet
|
||||
if ($disabled) {
|
||||
Log3 $hash, 3, "[".$hash->{NAME}."]"." disabled before stop-time , ending RandomTimer on $hash->{DEVICE}: "
|
||||
. strftime("%H:%M:%S(%d)",localtime($hash->{helper}{startTime})) . " - "
|
||||
. strftime("%H:%M:%S(%d)",localtime($hash->{helper}{stopTime}));
|
||||
RandomTimer_disableDown($hash);
|
||||
RandomTimer_setActive($hash,0);
|
||||
RandomTimer_setState ($hash);
|
||||
Log3 $hash, 3,
|
||||
"["
|
||||
. $hash->{NAME} . "]"
|
||||
. " disabled before stop-time , ending RandomTimer on $hash->{DEVICE}: "
|
||||
. strftime( "%H:%M:%S(%d)",
|
||||
localtime( $hash->{helper}{startTime} ) )
|
||||
. " - "
|
||||
. strftime( "%H:%M:%S(%d)",
|
||||
localtime( $hash->{helper}{stopTime} ) );
|
||||
DisableDown($hash);
|
||||
SetActive( $hash, 0 );
|
||||
SetState($hash);
|
||||
}
|
||||
# Wenn aktiv und Abschaltzeit erreicht, dann Gerät ausschalten, Meldung ausgeben und Timer schließen
|
||||
|
||||
# Wenn aktiv und Abschaltzeit erreicht, dann Gerät ausschalten, Meldung ausgeben und Timer schließen
|
||||
if ($stopTimeReached) {
|
||||
Log3 $hash, 3, "[".$hash->{NAME}."]"." stop-time reached, ending RandomTimer on $hash->{DEVICE}: "
|
||||
. strftime("%H:%M:%S(%d)",localtime($hash->{helper}{startTime})) . " - "
|
||||
. strftime("%H:%M:%S(%d)",localtime($hash->{helper}{stopTime}));
|
||||
RandomTimer_down($hash);
|
||||
RandomTimer_setActive($hash, 0);
|
||||
if ( AttrVal($hash->{NAME}, "runonce", -1) eq 1 ) {
|
||||
Log 3, "[".$hash->{NAME}. "]" ."runonceMode";
|
||||
fhem ("delete $hash->{NAME}") ;
|
||||
Log3 $hash, 3,
|
||||
"["
|
||||
. $hash->{NAME} . "]"
|
||||
. " stop-time reached, ending RandomTimer on $hash->{DEVICE}: "
|
||||
. strftime( "%H:%M:%S(%d)",
|
||||
localtime( $hash->{helper}{startTime} ) )
|
||||
. " - "
|
||||
. strftime( "%H:%M:%S(%d)",
|
||||
localtime( $hash->{helper}{stopTime} ) );
|
||||
Down($hash);
|
||||
SetActive( $hash, 0 );
|
||||
if ( AttrVal( $hash->{NAME}, "runonce", -1 ) eq 1 ) {
|
||||
Log 3, "[" . $hash->{NAME} . "]" . "runonceMode";
|
||||
fhem("delete $hash->{NAME}");
|
||||
}
|
||||
RandomTimer_setState($hash);
|
||||
SetState($hash);
|
||||
return;
|
||||
}
|
||||
} else { # !active
|
||||
}
|
||||
else { # !active
|
||||
if ($disabled) {
|
||||
Log3 $hash, 4, "[".$hash->{NAME}. "] RandomTimer on $hash->{DEVICE} timer disabled - no switch";
|
||||
RandomTimer_setState($hash);
|
||||
RandomTimer_setActive($hash,0);
|
||||
Log3 $hash, 4,
|
||||
"["
|
||||
. $hash->{NAME}
|
||||
. "] RandomTimer on $hash->{DEVICE} timer disabled - no switch";
|
||||
SetState($hash);
|
||||
SetActive( $hash, 0 );
|
||||
}
|
||||
if ($stopTimeReached) {
|
||||
Log3 $hash, 4, "[".$hash->{NAME}."]"." definition RandomTimer on $hash->{DEVICE}: "
|
||||
. strftime("%H:%M:%S(%d)",localtime($hash->{helper}{startTime})) . " - "
|
||||
. strftime("%H:%M:%S(%d)",localtime($hash->{helper}{stopTime}));
|
||||
RandomTimer_setState ($hash);
|
||||
RandomTimer_setActive($hash,0);
|
||||
Log3 $hash, 4,
|
||||
"["
|
||||
. $hash->{NAME} . "]"
|
||||
. " definition RandomTimer on $hash->{DEVICE}: "
|
||||
. strftime( "%H:%M:%S(%d)",
|
||||
localtime( $hash->{helper}{startTime} ) )
|
||||
. " - "
|
||||
. strftime( "%H:%M:%S(%d)",
|
||||
localtime( $hash->{helper}{stopTime} ) );
|
||||
SetState($hash);
|
||||
SetActive( $hash, 0 );
|
||||
return;
|
||||
}
|
||||
if (!$disabled) {
|
||||
if ($now>$hash->{helper}{startTime} && $now<$hash->{helper}{stopTime}) {
|
||||
Log3 $hash, 3, "[".$hash->{NAME}."]"." starting RandomTimer on $hash->{DEVICE}: "
|
||||
. strftime("%H:%M:%S(%d)",localtime($hash->{helper}{startTime})) . " - "
|
||||
. strftime("%H:%M:%S(%d)",localtime($hash->{helper}{stopTime}));
|
||||
RandomTimer_setActive($hash,1);
|
||||
if ( !$disabled ) {
|
||||
if ( $now > $hash->{helper}{startTime}
|
||||
&& $now < $hash->{helper}{stopTime} )
|
||||
{
|
||||
Log3 $hash, 3,
|
||||
"["
|
||||
. $hash->{NAME} . "]"
|
||||
. " starting RandomTimer on $hash->{DEVICE}: "
|
||||
. strftime( "%H:%M:%S(%d)",
|
||||
localtime( $hash->{helper}{startTime} ) )
|
||||
. " - "
|
||||
. strftime( "%H:%M:%S(%d)",
|
||||
localtime( $hash->{helper}{stopTime} ) );
|
||||
SetActive( $hash, 1 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RandomTimer_setState($hash);
|
||||
if ($now>$hash->{helper}{startTime} && $now<$hash->{helper}{stopTime}) {
|
||||
RandomTimer_device_toggle($hash) if (!$disabled);
|
||||
SetState($hash);
|
||||
if ( $now > $hash->{helper}{startTime} && $now < $hash->{helper}{stopTime} )
|
||||
{
|
||||
Device_toggle($hash) if ( !$disabled );
|
||||
}
|
||||
|
||||
my $nextSwitch = time() + RandomTimer_getSecsToNextAbschaltTest($hash);
|
||||
RandomTimer_RemoveInternalTimer("Exec", $hash);
|
||||
$hash->{helper}{NEXT_CHECK} = strftime("%d.%m.%Y %H:%M:%S",localtime($nextSwitch));
|
||||
RandomTimer_InternalTimer("Exec", $nextSwitch, "RandomTimer_Exec", $hash, 0);
|
||||
my $nextSwitch = time() + GetSecsToNextAbschaltTest($hash);
|
||||
RemoveInternalTimer( "FHEM::RandomTimer::Exec", $hash );
|
||||
$hash->{helper}{NEXT_CHECK} =
|
||||
strftime( "%d.%m.%Y %H:%M:%S", localtime($nextSwitch) );
|
||||
InternalTimer( "Exec", $nextSwitch, "FHEM::RandomTimer::Exec", $hash, 0 );
|
||||
|
||||
}
|
||||
|
||||
sub RandomTimer_getSecsToNextAbschaltTest($) {
|
||||
sub GetSecsToNextAbschaltTest {
|
||||
my ($hash) = @_;
|
||||
my $intervall = $hash->{helper}{TIMETOSWITCH};
|
||||
my $varDuration = $hash->{helper}{VAR_DURATION};
|
||||
my $nextSecs = $intervall + int(rand($varDuration));
|
||||
my $nextSecs = $intervall + int( rand($varDuration) );
|
||||
unless ($varDuration) {
|
||||
my $proz = 10;
|
||||
my $delta = $intervall * $proz/100;
|
||||
$nextSecs = $intervall - $delta/2 + int(rand($delta));
|
||||
my $delta = $intervall * $proz / 100;
|
||||
$nextSecs = $intervall - $delta / 2 + int( rand($delta) );
|
||||
}
|
||||
return $nextSecs;
|
||||
}
|
||||
|
||||
sub RandomTimer_isAktive ($) {
|
||||
sub IsAktive {
|
||||
my ($hash) = @_;
|
||||
return defined ($hash->{helper}{active}) ? $hash->{helper}{active} : 0;
|
||||
return defined( $hash->{helper}{active} ) ? $hash->{helper}{active} : 0;
|
||||
}
|
||||
|
||||
sub RandomTimer_isDisabled($) {
|
||||
sub IsDisabled {
|
||||
my ($hash) = @_;
|
||||
|
||||
my $disable = AttrVal($hash->{NAME}, "disable", 0 );
|
||||
return $disable if($disable);
|
||||
my $disable = AttrVal( $hash->{NAME}, "disable", 0 );
|
||||
return $disable if ($disable);
|
||||
|
||||
my $disableCond = AttrVal($hash->{NAME}, "disableCond", "nf" );
|
||||
if ($disableCond eq "nf") {
|
||||
my $disableCond = AttrVal( $hash->{NAME}, "disableCond", "nf" );
|
||||
if ( $disableCond eq "nf" ) {
|
||||
return 0;
|
||||
} else {
|
||||
$disable = eval ($disableCond);
|
||||
}
|
||||
else {
|
||||
$disable = eval($disableCond);
|
||||
if ($@) {
|
||||
$@ =~ s/\n/ /g;
|
||||
Log3 ($hash, 3, "[$hash->{NAME}] ERROR: " . $@ . " EVALUATING " . $disableCond);
|
||||
Log3( $hash, 3,
|
||||
"[$hash->{NAME}] ERROR: "
|
||||
. $@
|
||||
. " EVALUATING "
|
||||
. $disableCond );
|
||||
}
|
||||
return $disable;
|
||||
}
|
||||
}
|
||||
|
||||
sub RandomTimer_schaltZeitenErmitteln ($$) {
|
||||
my ($hash,$now) = @_;
|
||||
sub SchaltZeitenErmitteln {
|
||||
my ( $hash, $now ) = @_;
|
||||
|
||||
RandomTimer_startZeitErmitteln($hash, $now);
|
||||
RandomTimer_stopZeitErmitteln ($hash, $now);
|
||||
StartZeitErmitteln( $hash, $now );
|
||||
StopZeitErmitteln( $hash, $now );
|
||||
|
||||
readingsBeginUpdate($hash);
|
||||
|
||||
# readingsBulkUpdate ($hash, "Startzeit", FmtDateTime($hash->{helper}{startTime}));
|
||||
# readingsBulkUpdate ($hash, "Stoppzeit", FmtDateTime($hash->{helper}{stopTime}));
|
||||
readingsBulkUpdate ($hash, "StartTime", FmtDateTime($hash->{helper}{startTime}));
|
||||
readingsBulkUpdate ($hash, "StopTime", FmtDateTime($hash->{helper}{stopTime}));
|
||||
readingsEndUpdate ($hash, defined($hash->{LOCAL} ? 0 : 1));
|
||||
readingsBulkUpdate( $hash, "StartTime",
|
||||
FmtDateTime( $hash->{helper}{startTime} ) );
|
||||
readingsBulkUpdate( $hash, "StopTime",
|
||||
FmtDateTime( $hash->{helper}{stopTime} ) );
|
||||
readingsEndUpdate( $hash, defined( $hash->{LOCAL} ? 0 : 1 ) );
|
||||
|
||||
}
|
||||
|
||||
sub RandomTimer_setActive($$) {
|
||||
my ($hash, $value) = @_;
|
||||
sub SetActive {
|
||||
my ( $hash, $value ) = @_;
|
||||
$hash->{helper}{active} = $value;
|
||||
my $trigger = (RandomTimer_isDisabled($hash)) ? 0 : 1;
|
||||
readingsSingleUpdate ($hash, "active", $value, $trigger);
|
||||
my $trigger = ( IsDisabled($hash) ) ? 0 : 1;
|
||||
readingsSingleUpdate( $hash, "active", $value, $trigger );
|
||||
}
|
||||
|
||||
sub RandomTimer_setState($) {
|
||||
sub SetState {
|
||||
my ($hash) = @_;
|
||||
|
||||
if (RandomTimer_isDisabled($hash)) {
|
||||
my $dotrigger = ReadingsVal($hash->{NAME},"state","none") ne "disabled" ? 1 : 0;
|
||||
readingsSingleUpdate ($hash, "state", "disabled", $dotrigger);
|
||||
} else {
|
||||
if ( IsDisabled($hash) ) {
|
||||
my $dotrigger =
|
||||
ReadingsVal( $hash->{NAME}, "state", "none" ) ne "disabled" ? 1 : 0;
|
||||
readingsSingleUpdate( $hash, "state", "disabled", $dotrigger );
|
||||
}
|
||||
else {
|
||||
my $state = $hash->{helper}{active} ? "on" : "off";
|
||||
readingsSingleUpdate ($hash, "state", $state, 1);
|
||||
readingsSingleUpdate( $hash, "state", $state, 1 );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
sub RandomTimer_setSwitchmode ($$) {
|
||||
sub SetSwitchmode {
|
||||
|
||||
my ($hash, $attrVal) = @_;
|
||||
my $mod = "[".$hash->{NAME} ."] ";
|
||||
my ( $hash, $attrVal ) = @_;
|
||||
my $mod = "[" . $hash->{NAME} . "] ";
|
||||
|
||||
|
||||
if(!($attrVal =~ m/^([0-9]{1,3})\/([0-9]{1,3})$/i)) {
|
||||
if ( !( $attrVal =~ m/^([0-9]{1,3})\/([0-9]{1,3})$/i ) ) {
|
||||
Log3 undef, 3, $mod . "invalid switchMode <$attrVal>, use 999/999";
|
||||
} else {
|
||||
my ($sigmaWhenOff, $sigmaWhenOn) = ($1, $2);
|
||||
}
|
||||
else {
|
||||
my ( $sigmaWhenOff, $sigmaWhenOn ) = ( $1, $2 );
|
||||
$hash->{helper}{SWITCHMODE} = $attrVal;
|
||||
$hash->{helper}{SIGMAWHENON} = $sigmaWhenOn;
|
||||
$hash->{helper}{SIGMAWHENOFF} = $sigmaWhenOff;
|
||||
$attr{$hash->{NAME}}{switchmode} = $attrVal;
|
||||
$attr{ $hash->{NAME} }{switchmode} = $attrVal;
|
||||
}
|
||||
}
|
||||
|
||||
sub RandomTimer_SetTimer($) {
|
||||
sub SetTimer {
|
||||
my ($myHash) = @_;
|
||||
my $hash = RandomTimer_GetHashIndirekt($myHash, (caller(0))[3]);
|
||||
return if (!defined($hash));
|
||||
my $hash = GetHashIndirekt( $myHash, ( caller(0) )[3] );
|
||||
return if ( !defined($hash) );
|
||||
|
||||
my $now = time();
|
||||
my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($now);
|
||||
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
|
||||
localtime($now);
|
||||
|
||||
RandomTimer_setActive($hash, 0);
|
||||
RandomTimer_schaltZeitenErmitteln($hash, $now);
|
||||
RandomTimer_setState($hash);
|
||||
SetActive( $hash, 0 );
|
||||
SchaltZeitenErmitteln( $hash, $now );
|
||||
SetState($hash);
|
||||
|
||||
Log3 $hash, 4, "[".$hash->{NAME}."]" . " timings RandomTimer on $hash->{DEVICE}: "
|
||||
. strftime("%H:%M:%S(%d)",localtime($hash->{helper}{startTime})) . " - "
|
||||
. strftime("%H:%M:%S(%d)",localtime($hash->{helper}{stopTime}));
|
||||
Log3 $hash, 4,
|
||||
"["
|
||||
. $hash->{NAME} . "]"
|
||||
. " timings RandomTimer on $hash->{DEVICE}: "
|
||||
. strftime( "%H:%M:%S(%d)", localtime( $hash->{helper}{startTime} ) )
|
||||
. " - "
|
||||
. strftime( "%H:%M:%S(%d)", localtime( $hash->{helper}{stopTime} ) );
|
||||
|
||||
my $secToMidnight = 24*3600 -(3600*$hour + 60*$min + $sec);
|
||||
my $secToMidnight = 24 * 3600 - ( 3600 * $hour + 60 * $min + $sec );
|
||||
|
||||
my $setExecTime = max($now, $hash->{helper}{startTime});
|
||||
RandomTimer_RemoveInternalTimer("Exec", $hash);
|
||||
RandomTimer_InternalTimer("Exec", $setExecTime, "RandomTimer_Exec", $hash, 0);
|
||||
my $setExecTime = max( $now, $hash->{helper}{startTime} );
|
||||
RemoveInternalTimer( "FHEM::RandomTimer::Exec", $hash );
|
||||
InternalTimer( "Exec", $setExecTime, "FHEM::RandomTimer::Exec", $hash, 0 );
|
||||
|
||||
if ($hash->{helper}{REP} gt "") {
|
||||
my $setTimerTime = max($now+$secToMidnight + 15,
|
||||
$hash->{helper}{stopTime}) + $hash->{helper}{TIMETOSWITCH}+15;
|
||||
RandomTimer_RemoveInternalTimer("SetTimer", $hash);
|
||||
RandomTimer_InternalTimer("SetTimer", $setTimerTime, "RandomTimer_SetTimer", $hash, 0);
|
||||
if ( $hash->{helper}{REP} gt "" ) {
|
||||
my $setTimerTime =
|
||||
max( $now + $secToMidnight + 15, $hash->{helper}{stopTime} ) +
|
||||
$hash->{helper}{TIMETOSWITCH} + 15;
|
||||
RemoveInternalTimer( "FHEM::RandomTimer::SetTimer", $hash );
|
||||
InternalTimer( "SetTimer", $setTimerTime,
|
||||
"FHEM::RandomTimer::SetTimer", $hash, 0 );
|
||||
}
|
||||
}
|
||||
|
||||
sub RandomTimer_startZeitErmitteln ($$) {
|
||||
my ($hash,$now) = @_;
|
||||
sub StartZeitErmitteln {
|
||||
my ( $hash, $now ) = @_;
|
||||
|
||||
my $timespec_start = $hash->{helper}{TIMESPEC_START};
|
||||
|
||||
return "Wrong timespec_start <$timespec_start>, use \"[+][*]<time or func>\""
|
||||
if($timespec_start !~ m/^(\+)?(\*)?(.*)$/i);
|
||||
my ($rel, $rep, $tspec) = ($1, $2, $3);
|
||||
return
|
||||
"Wrong timespec_start <$timespec_start>, use \"[+][*]<time or func>\""
|
||||
if ( $timespec_start !~ m/^(\+)?(\*)?(.*)$/i );
|
||||
my ( $rel, $rep, $tspec ) = ( $1, $2, $3 );
|
||||
|
||||
my ($err, $hour, $min, $sec, $fn) = GetTimeSpec($tspec);
|
||||
return $err if($err);
|
||||
my ( $err, $hour, $min, $sec, $fn ) = GetTimeSpec($tspec);
|
||||
return $err if ($err);
|
||||
|
||||
my $startTime;
|
||||
if($rel) {
|
||||
$startTime = $now + 3600* $hour + 60* $min + $sec;
|
||||
} else {
|
||||
$startTime = RandomTimer_zeitBerechnen($now, $hour, $min, $sec);
|
||||
if ($rel) {
|
||||
$startTime = $now + 3600 * $hour + 60 * $min + $sec;
|
||||
}
|
||||
else {
|
||||
$startTime = ZeitBerechnen( $now, $hour, $min, $sec );
|
||||
}
|
||||
my $varStart = $hash->{helper}{VAR_START};
|
||||
$startTime += int(rand($varStart));
|
||||
$startTime += int( rand($varStart) );
|
||||
|
||||
$hash->{helper}{startTime} = $startTime;
|
||||
$hash->{helper}{STARTTIME} = strftime("%d.%m.%Y %H:%M:%S",localtime($startTime));
|
||||
$hash->{helper}{STARTTIME} =
|
||||
strftime( "%d.%m.%Y %H:%M:%S", localtime($startTime) );
|
||||
}
|
||||
|
||||
sub RandomTimer_stopTimeReached($) {
|
||||
sub StopTimeReached {
|
||||
my ($hash) = @_;
|
||||
return ( time()>$hash->{helper}{stopTime} );
|
||||
return ( time() > $hash->{helper}{stopTime} );
|
||||
}
|
||||
|
||||
sub RandomTimer_stopZeitErmitteln ($$) {
|
||||
my ($hash,$now) = @_;
|
||||
sub StopZeitErmitteln {
|
||||
my ( $hash, $now ) = @_;
|
||||
|
||||
my $timespec_stop = $hash->{helper}{TIMESPEC_STOP};
|
||||
|
||||
return "Wrong timespec_stop <$timespec_stop>, use \"[+][*]<time or func>\""
|
||||
if($timespec_stop !~ m/^(\+)?(\*)?(.*)$/i);
|
||||
my ($rel, $rep, $tspec) = ($1, $2, $3);
|
||||
if ( $timespec_stop !~ m/^(\+)?(\*)?(.*)$/i );
|
||||
my ( $rel, $rep, $tspec ) = ( $1, $2, $3 );
|
||||
|
||||
my ($err, $hour, $min, $sec, $fn) = GetTimeSpec($tspec);
|
||||
return $err if($err);
|
||||
my ( $err, $hour, $min, $sec, $fn ) = GetTimeSpec($tspec);
|
||||
return $err if ($err);
|
||||
|
||||
my $stopTime;
|
||||
if($rel) {
|
||||
$stopTime = $hash->{helper}{startTime} + 3600* $hour + 60* $min + $sec;
|
||||
} else {
|
||||
$stopTime = RandomTimer_zeitBerechnen($now, $hour, $min, $sec);
|
||||
if ($rel) {
|
||||
$stopTime =
|
||||
$hash->{helper}{startTime} + 3600 * $hour + 60 * $min + $sec;
|
||||
}
|
||||
else {
|
||||
$stopTime = ZeitBerechnen( $now, $hour, $min, $sec );
|
||||
}
|
||||
|
||||
if (!AttrVal($hash->{NAME}, "forceStoptimeSameDay", 0)) {
|
||||
if ($hash->{helper}{startTime} > $stopTime) {
|
||||
$stopTime = RandomTimer_addDays($stopTime, 1);
|
||||
if ( !AttrVal( $hash->{NAME}, "forceStoptimeSameDay", 0 ) ) {
|
||||
if ( $hash->{helper}{startTime} > $stopTime ) {
|
||||
$stopTime = AddDays( $stopTime, 1 );
|
||||
}
|
||||
}
|
||||
$hash->{helper}{stopTime} = $stopTime;
|
||||
$hash->{helper}{STOPTIME} = strftime("%d.%m.%Y %H:%M:%S",localtime($stopTime));
|
||||
$hash->{helper}{STOPTIME} =
|
||||
strftime( "%d.%m.%Y %H:%M:%S", localtime($stopTime) );
|
||||
|
||||
}
|
||||
|
||||
sub RandomTimer_Wakeup() { # {RandomTimer_Wakeup()}
|
||||
sub Wakeup() { # {Wakeup()}
|
||||
|
||||
foreach my $hc ( sort keys %{$modules{RandomTimer}{defptr}} ) {
|
||||
foreach my $hc ( sort keys %{ $modules{RandomTimer}{defptr} } ) {
|
||||
my $hash = $modules{RandomTimer}{defptr}{$hc};
|
||||
|
||||
my $myHash->{HASH}=$hash;
|
||||
RandomTimer_SetTimer($myHash);
|
||||
my $myHash->{HASH} = $hash;
|
||||
SetTimer($myHash);
|
||||
Log3 undef, 3, "RandomTimer_Wakeup() for $hash->{NAME} done!";
|
||||
}
|
||||
Log3 undef, 3, "RandomTimer_Wakeup() done!";
|
||||
}
|
||||
|
||||
sub RandomTimer_zeitBerechnen ($$$$) {
|
||||
my ($now, $hour, $min, $sec) = @_;
|
||||
sub ZeitBerechnen {
|
||||
my ( $now, $hour, $min, $sec ) = @_;
|
||||
|
||||
my @jetzt_arr = localtime($now);
|
||||
|
||||
#Stunden Minuten Sekunden
|
||||
$jetzt_arr[2] = $hour; $jetzt_arr[1] = $min; $jetzt_arr[0] = $sec;
|
||||
$jetzt_arr[2] = $hour;
|
||||
$jetzt_arr[1] = $min;
|
||||
$jetzt_arr[0] = $sec;
|
||||
my $next = timelocal_nocheck(@jetzt_arr);
|
||||
return $next;
|
||||
}
|
||||
|
||||
sub RandomTimer_InternalTimer($$$$$) {
|
||||
my ($modifier, $tim, $callback, $hash, $waitIfInitNotDone) = @_;
|
||||
sub InternalTimer {
|
||||
my ( $modifier, $tim, $callback, $hash, $waitIfInitNotDone ) = @_;
|
||||
|
||||
my $timerName = "$hash->{NAME}_$modifier";
|
||||
my $mHash = { HASH=>$hash, NAME=>"$hash->{NAME}_$modifier", MODIFIER=>$modifier};
|
||||
if (defined($hash->{TIMER}{$timerName})) {
|
||||
Log3 $hash, 1, "[$hash->{NAME}] possible overwriting of timer $timerName - please delete first";
|
||||
my $mHash = {
|
||||
HASH => $hash,
|
||||
NAME => "$hash->{NAME}_$modifier",
|
||||
MODIFIER => $modifier
|
||||
};
|
||||
if ( defined( $hash->{TIMER}{$timerName} ) ) {
|
||||
Log3 $hash, 1,
|
||||
"[$hash->{NAME}] possible overwriting of timer $timerName - please delete first";
|
||||
stacktrace();
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
$hash->{TIMER}{$timerName} = $mHash;
|
||||
}
|
||||
|
||||
Log3 $hash, 5, "[$hash->{NAME}] setting Timer: $timerName " . FmtDateTime($tim);
|
||||
InternalTimer($tim, $callback, $mHash, $waitIfInitNotDone);
|
||||
Log3 $hash, 5,
|
||||
"[$hash->{NAME}] setting Timer: $timerName " . FmtDateTime($tim);
|
||||
main::InternalTimer( $tim, $callback, $mHash, $waitIfInitNotDone );
|
||||
return $mHash;
|
||||
}
|
||||
################################################################################
|
||||
sub RandomTimer_RemoveInternalTimer($$) {
|
||||
my ($modifier, $hash) = @_;
|
||||
sub RemoveInternalTimer {
|
||||
my ( $modifier, $hash ) = @_;
|
||||
|
||||
my $timerName = "$hash->{NAME}_$modifier";
|
||||
my $myHash = $hash->{TIMER}{$timerName};
|
||||
if (defined($myHash)) {
|
||||
if ( defined($myHash) ) {
|
||||
delete $hash->{TIMER}{$timerName};
|
||||
Log3 $hash, 5, "[$hash->{NAME}] removing Timer: $timerName";
|
||||
RemoveInternalTimer($myHash);
|
||||
main::RemoveInternalTimer($myHash);
|
||||
}
|
||||
}
|
||||
|
||||
sub RandomTimer_GetHashIndirekt ($$) {
|
||||
my ($myHash, $function) = @_;
|
||||
sub GetHashIndirekt ($$) {
|
||||
my ( $myHash, $function ) = @_;
|
||||
|
||||
if (!defined($myHash->{HASH})) {
|
||||
if ( !defined( $myHash->{HASH} ) ) {
|
||||
Log 3, "[$function] myHash not valid";
|
||||
return undef;
|
||||
};
|
||||
}
|
||||
return $myHash->{HASH};
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
# commandref ##################################################################
|
||||
|
||||
=pod
|
||||
=encoding utf8
|
||||
=item helper
|
||||
|
Loading…
x
Reference in New Issue
Block a user