2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-03-10 03:06:37 +00:00

98_RandomTimer.pm: Add perl syntax check to disableCond attribute; remove unused function; perltidy patch

git-svn-id: https://svn.fhem.de/fhem/trunk@21983 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
Beta-User 2020-05-21 04:12:35 +00:00
parent a885bfe86d
commit cef7607065
2 changed files with 529 additions and 448 deletions

View File

@ -1,5 +1,6 @@
# Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Add changes at the top of the list. Keep it in ASCII, and 80-char wide.
# Do not insert empty lines here, update check depends on it. # Do not insert empty lines here, update check depends on it.
- feature: 98_RandomTimer: Add perlSyntaxCheck to condition attribute
- bugfix: dashboard.js: Dashboard no longer requires dummy readingsgroup - bugfix: dashboard.js: Dashboard no longer requires dummy readingsgroup
- feature: 72_XiaomiDevice: new device SmartFan1X, S5 Max vacuum water box - feature: 72_XiaomiDevice: new device SmartFan1X, S5 Max vacuum water box
- feature: 48_BlinkCamera: Support for BlinkMini - en/disable + thumbnail - feature: 48_BlinkCamera: Support for BlinkMini - en/disable + thumbnail

View File

@ -30,7 +30,7 @@ use strict;
use warnings; use warnings;
use utf8; use utf8;
use Time::HiRes qw(gettimeofday); use Time::HiRes qw(gettimeofday);
use Time::Local 'timelocal_nocheck'; use Time::Local qw(timelocal_nocheck);
use List::Util qw(max); use List::Util qw(max);
use GPUtils qw(GP_Import GP_Export); use GPUtils qw(GP_Import GP_Export);
@ -60,6 +60,7 @@ BEGIN {
CommandDeleteAttr CommandDeleteAttr
AnalyzeCommandChain AnalyzeCommandChain
AnalyzePerlCommand AnalyzePerlCommand
perlSyntaxCheck
SemicolonEscape SemicolonEscape
FmtDateTime FmtDateTime
strftime strftime
@ -68,25 +69,19 @@ BEGIN {
); );
} }
#-- Export to main context with different name sub main::RandomTimer_Initialize { goto &Initialize }
# use this feature with care!
GP_Export(
qw(
Initialize
)
);
# initialize ################################################################## # initialize ##################################################################
sub Initialize { sub Initialize {
my $hash = shift; my $hash = shift // return;
$hash->{DefFn} = \&Define; $hash->{DefFn} = \&Define;
$hash->{UndefFn} = \&Undef; $hash->{UndefFn} = \&Undef;
$hash->{SetFn} = \&Set; $hash->{SetFn} = \&Set;
$hash->{AttrFn} = \&Attr; $hash->{AttrFn} = \&Attr;
$hash->{AttrList} = "onCmd offCmd switchmode disable:0,1 disableCond disableCondCmd:none,offCmd,onCmd offState ". $hash->{AttrList} = "onCmd offCmd switchmode disable:0,1 disableCond disableCondCmd:none,offCmd,onCmd offState "
"runonce:0,1 keepDeviceAlive:0,1 forceStoptimeSameDay:0,1 disabledForIntervals ". . "runonce:0,1 keepDeviceAlive:0,1 forceStoptimeSameDay:0,1 disabledForIntervals "
$readingFnAttributes; . $readingFnAttributes;
return; return;
} }
@ -96,8 +91,9 @@ sub Define {
my $def = shift // return; my $def = shift // return;
RemoveInternalTimer($hash); RemoveInternalTimer($hash);
my ($name, $type, $timespec_start, $device, $timespec_stop, $timeToSwitch, $variation) = my ( $name, $type, $timespec_start, $device, $timespec_stop, $timeToSwitch,
split m{\s+}xms, $def; $variation )
= split m{\s+}xms, $def;
return "wrong syntax: define <name> RandomTimer <timespec_start> <device> <timespec_stop> <timeToSwitch> [<variations>]" return "wrong syntax: define <name> RandomTimer <timespec_start> <device> <timespec_stop> <timeToSwitch> [<variations>]"
if ( !defined $timeToSwitch ); if ( !defined $timeToSwitch );
@ -107,7 +103,8 @@ sub Define {
$rel = $1; $rel = $1;
$rep = $2; $rep = $2;
$tspec = $3; $tspec = $3;
} else { }
else {
return qq{ "Wrong timespec_start <$timespec_start>, use "[+][*]<time or func>" }; return qq{ "Wrong timespec_start <$timespec_start>, use "[+][*]<time or func>" };
} }
@ -122,8 +119,10 @@ sub Define {
$srel = $1; $srel = $1;
$srep = $2; $srep = $2;
$stspec = $3; $stspec = $3;
} else { }
return qq{"Wrong timespec_stop <$timespec_stop>, use "[+][*]<time or func>"}; else {
return
qq{"Wrong timespec_stop <$timespec_stop>, use "[+][*]<time or func>"};
} }
my ( $e, $h, $m, $s, $f ) = GetTimeSpec($stspec); my ( $e, $h, $m, $s, $f ) = GetTimeSpec($stspec);
@ -138,7 +137,8 @@ sub Define {
$variation =~ m{^([\d]+)}xms ? $varDuration = $1 : undef; $variation =~ m{^([\d]+)}xms ? $varDuration = $1 : undef;
$variation =~ m{[:]([\d]+)}xms ? $varStart = $1 : undef; $variation =~ m{[:]([\d]+)}xms ? $varStart = $1 : undef;
} }
setSwitchmode ($hash, "800/200") if (!defined $hash->{helper}{SWITCHMODE}); setSwitchmode( $hash, "800/200" )
if ( !defined $hash->{helper}{SWITCHMODE} );
$hash->{NAME} = $name; $hash->{NAME} = $name;
$hash->{DEVICE} = $device; $hash->{DEVICE} = $device;
@ -158,9 +158,9 @@ sub Define {
$hash->{helper}{offRegex} = "off"; $hash->{helper}{offRegex} = "off";
$hash->{helper}{offReading} = "state"; $hash->{helper}{offReading} = "state";
} }
#$attr{$name}{verbose} = 4;
readingsSingleUpdate ($hash, "TimeToSwitch", $hash->{helper}{TIMETOSWITCH}, 1); readingsSingleUpdate( $hash, "TimeToSwitch", $hash->{helper}{TIMETOSWITCH},
1 );
RmInternalTimer( "SetTimer", $hash ); RmInternalTimer( "SetTimer", $hash );
MkInternalTimer( "SetTimer", time(), \&SetTimer, $hash, 0 ); MkInternalTimer( "SetTimer", time(), \&SetTimer, $hash, 0 );
@ -188,6 +188,7 @@ sub Attr {
} }
if ( $attrName =~ m{\A disable(Cond)? \z}xms ) { if ( $attrName =~ m{\A disable(Cond)? \z}xms ) {
# Immediately execute next switch check # Immediately execute next switch check
RmInternalTimer( "Exec", $hash ); RmInternalTimer( "Exec", $hash );
MkInternalTimer( "Exec", time() + 1, \&Exec, $hash, 0 ); MkInternalTimer( "Exec", time() + 1, \&Exec, $hash, 0 );
@ -199,15 +200,22 @@ sub Attr {
$hash->{helper}{offReading} = $offReading // 'state'; $hash->{helper}{offReading} = $offReading // 'state';
} }
return; if ( $attrName eq 'disableCond' ) {
if( $cmd eq "set" ) {
my $err = perlSyntaxCheck($attrVal);
return $err if ( $err );
}
} }
return;
}
sub Set { sub Set {
my ( $hash, @a ) = @_; my ( $hash, @a ) = @_;
return "no set value specified" if ( int(@a) < 2 ); return "no set value specified" if ( int(@a) < 2 );
return "Unknown argument $a[1], choose one of execNow:noArg active:noArg inactive:noArg" if($a[1] eq "?"); return "Unknown argument $a[1], choose one of execNow:noArg active:noArg inactive:noArg"
if ( $a[1] eq "?" );
my $name = shift @a; my $name = shift @a;
my $v = join( " ", @a ); my $v = join( " ", @a );
@ -216,7 +224,8 @@ sub Set {
Log3( $hash, 3, "[$name] set $name $v" ); Log3( $hash, 3, "[$name] set $name $v" );
if ( AttrVal( $name, "disable", 0 ) ) { if ( AttrVal( $name, "disable", 0 ) ) {
Log3( $hash, 3, "[$name] is disabled, set execNow not possible" ); Log3( $hash, 3, "[$name] is disabled, set execNow not possible" );
} else { }
else {
RmInternalTimer( "Exec", $hash ); RmInternalTimer( "Exec", $hash );
MkInternalTimer( "Exec", time() + 1, \&Exec, $hash, 0 ); MkInternalTimer( "Exec", time() + 1, \&Exec, $hash, 0 );
} }
@ -236,7 +245,6 @@ sub Set {
return; return;
} }
# module Fn ################################################################### # module Fn ###################################################################
sub addDays { sub addDays {
my $now = shift; my $now = shift;
@ -256,16 +264,18 @@ sub device_switch {
my $command = "set @ $hash->{COMMAND}"; my $command = "set @ $hash->{COMMAND}";
if ( $hash->{COMMAND} eq "on" ) { if ( $hash->{COMMAND} eq "on" ) {
$command = AttrVal( $hash->{NAME}, "onCmd", $command ); $command = AttrVal( $hash->{NAME}, "onCmd", $command );
} else { }
else {
$command = AttrVal( $hash->{NAME}, "offCmd", $command ); $command = AttrVal( $hash->{NAME}, "offCmd", $command );
} }
$command =~ s/@/$hash->{DEVICE}/gxms; $command =~ s/@/$hash->{DEVICE}/gxms;
$command = SemicolonEscape($command); $command = SemicolonEscape($command);
readingsSingleUpdate( $hash, 'LastCommand', $command, 1 ); readingsSingleUpdate( $hash, 'LastCommand', $command, 1 );
Log3 $hash, 4, "[".$hash->{NAME}. "]"." command: $command"; Log3( $hash, 4, "[" . $hash->{NAME} . "]" . " command: $command" );
my $ret = AnalyzeCommandChain( undef, $command ); my $ret = AnalyzeCommandChain( undef, $command );
Log3 ($hash, 3, "[$hash->{NAME}] ERROR: " . $ret . " SENDING " . $command) if($ret); Log3( $hash, 3, "[$hash->{NAME}] ERROR: " . $ret . " SENDING " . $command )
if ($ret);
return; return;
} }
@ -273,29 +283,34 @@ sub device_switch {
sub device_toggle { sub device_toggle {
my $hash = shift // return; my $hash = shift // return;
my $name = $hash->{NAME}; my $name = $hash->{NAME};
#my $attrOffState = AttrVal($name,"offState",undef); #my $attrOffState = AttrVal($name,"offState",undef);
my $status = Value( $hash->{DEVICE} ); my $status = Value( $hash->{DEVICE} );
if ( defined $hash->{helper}{offRegex} ) { if ( defined $hash->{helper}{offRegex} ) {
$status = ReadingsVal($hash->{DEVICE},$hash->{helper}{offReading},"off"); $status =
ReadingsVal( $hash->{DEVICE}, $hash->{helper}{offReading}, "off" );
my $attrOffState = $hash->{helper}{offRegex}; my $attrOffState = $hash->{helper}{offRegex};
$status = $status =~ m{^$attrOffState$}xms ? "off" : lc($status); $status = $status =~ m{^$attrOffState$}xms ? "off" : lc($status);
$status = $status =~ m{off}xms ? "off" : "on"; $status = $status =~ m{off}xms ? "off" : "on";
} }
if ( $status ne "on" && $status ne "off" ) { if ( $status ne "on" && $status ne "off" ) {
if ( $hash->{helper}{offRegex} ) { 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"; 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'";; 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}{SIGMAWHENON}
: $hash->{helper}{SIGMAWHENOFF}; : $hash->{helper}{SIGMAWHENOFF};
my $zufall = int( rand(1000) ); my $zufall = int( rand(1000) );
Log3 $hash, 4, "[$name] IstZustand:$status sigmaWhen-$status:$sigma random:$zufall<$sigma=>" . (($zufall < $sigma)?"true":"false"); Log3( $hash, 4, "[$name] IstZustand:$status sigmaWhen-$status:$sigma random:$zufall<$sigma=>"
. ( ( $zufall < $sigma ) ? "true" : "false" ) );
if ( $zufall < $sigma ) { if ( $zufall < $sigma ) {
$hash->{COMMAND} = ( $status eq "on" ) ? "off" : "on"; $hash->{COMMAND} = ( $status eq "on" ) ? "off" : "on";
@ -309,24 +324,37 @@ sub disableDown {
my $disableCondCmd = AttrVal( $hash->{NAME}, "disableCondCmd", 0 ); my $disableCondCmd = AttrVal( $hash->{NAME}, "disableCondCmd", 0 );
if ( $disableCondCmd ne "none" ) { if ( $disableCondCmd ne "none" ) {
Log3 $hash, 4, "[".$hash->{NAME}."]"." setting requested disableCondCmd on $hash->{DEVICE}: "; Log3( $hash, 4,
$hash->{COMMAND} = AttrVal($hash->{NAME}, "disableCondCmd", 0) eq "onCmd" ? "on" : "off"; "["
. $hash->{NAME} . "]"
. " setting requested disableCondCmd on $hash->{DEVICE}: " );
$hash->{COMMAND} =
AttrVal( $hash->{NAME}, "disableCondCmd", 0 ) eq "onCmd"
? "on"
: "off";
device_switch($hash); device_switch($hash);
} else { }
Log3 $hash, 4, "[".$hash->{NAME}."]"." no action requested on $hash->{DEVICE}: "; else {
Log3( $hash, 4,
"["
. $hash->{NAME} . "]"
. " no action requested on $hash->{DEVICE}: " );
} }
return; return;
} }
sub down { sub down {
my $hash = shift // return; my $hash = shift // return;
Log3 $hash, 4, "[".$hash->{NAME}."]"." setting requested keepDeviceAlive on $hash->{DEVICE}: "; Log3( $hash, 4,
$hash->{COMMAND} = AttrVal($hash->{NAME}, "keepDeviceAlive", 0) ? "on" : "off"; "["
. $hash->{NAME} . "]"
. " setting requested keepDeviceAlive on $hash->{DEVICE}: " );
$hash->{COMMAND} =
AttrVal( $hash->{NAME}, "keepDeviceAlive", 0 ) ? "on" : "off";
device_switch($hash); device_switch($hash);
return; return;
} }
sub Exec { sub Exec {
my $myHash = shift // return; my $myHash = shift // return;
@ -341,61 +369,95 @@ sub Exec {
my $stopTimeReached = stopTimeReached($hash); my $stopTimeReached = stopTimeReached($hash);
if ($active) { if ($active) {
# wenn temporär ausgeschaltet # wenn temporär ausgeschaltet
if ($disabled) { if ($disabled) {
Log3 $hash, 3, "[".$hash->{NAME}."]"." disabled before stop-time , ending RandomTimer on $hash->{DEVICE}: " Log3( $hash, 3,
. strftime("%H:%M:%S(%d)",localtime($hash->{helper}{startTime})) . " - " "["
. strftime("%H:%M:%S(%d)",localtime($hash->{helper}{stopTime})); . $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); disableDown($hash);
setActive( $hash, 0 ); setActive( $hash, 0 );
setState($hash); 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) { if ($stopTimeReached) {
Log3 $hash, 3, "[".$hash->{NAME}."]"." stop-time reached, ending RandomTimer on $hash->{DEVICE}: " Log3( $hash, 3,
. strftime("%H:%M:%S(%d)",localtime($hash->{helper}{startTime})) . " - " "["
. strftime("%H:%M:%S(%d)",localtime($hash->{helper}{stopTime})); . $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); down($hash);
setActive( $hash, 0 ); setActive( $hash, 0 );
if ( AttrVal( $hash->{NAME}, "runonce", -1 ) eq "1" ) { if ( AttrVal( $hash->{NAME}, "runonce", -1 ) eq "1" ) {
Log3 $hash, 3, "[".$hash->{NAME}. "]" ."runonceMode"; Log3( $hash, 3, "[" . $hash->{NAME} . "]" . "runonceMode" );
fhem("delete $hash->{NAME}"); fhem("delete $hash->{NAME}");
} }
setState($hash); setState($hash);
return; return;
} }
} else { # !active }
else { # !active
if ($disabled) { if ($disabled) {
Log3 $hash, 4, "[".$hash->{NAME}. "] RandomTimer on $hash->{DEVICE} timer disabled - no switch"; Log3( $hash, 4,
"["
. $hash->{NAME}
. "] RandomTimer on $hash->{DEVICE} timer disabled - no switch" );
setState($hash); setState($hash);
setActive( $hash, 0 ); setActive( $hash, 0 );
} }
if ($stopTimeReached) { if ($stopTimeReached) {
Log3 $hash, 4, "[".$hash->{NAME}."]"." definition RandomTimer on $hash->{DEVICE}: " Log3( $hash, 4,
. strftime("%H:%M:%S(%d)",localtime($hash->{helper}{startTime})) . " - " "["
. strftime("%H:%M:%S(%d)",localtime($hash->{helper}{stopTime})); . $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); setState($hash);
setActive( $hash, 0 ); setActive( $hash, 0 );
return; return;
} }
if ( !$disabled ) { if ( !$disabled ) {
if ($now>$hash->{helper}{startTime} && $now<$hash->{helper}{stopTime}) { if ( $now > $hash->{helper}{startTime}
Log3 $hash, 3, "[".$hash->{NAME}."]"." starting RandomTimer on $hash->{DEVICE}: " && $now < $hash->{helper}{stopTime} )
. strftime("%H:%M:%S(%d)",localtime($hash->{helper}{startTime})) . " - " {
. strftime("%H:%M:%S(%d)",localtime($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 ); setActive( $hash, 1 );
} }
} }
} }
setState($hash); setState($hash);
if ($now>$hash->{helper}{startTime} && $now<$hash->{helper}{stopTime}) { if ( $now > $hash->{helper}{startTime} && $now < $hash->{helper}{stopTime} )
{
device_toggle($hash) if ( !$disabled ); device_toggle($hash) if ( !$disabled );
} }
my $nextSwitch = time() + getSecsToNextAbschaltTest($hash); my $nextSwitch = time() + getSecsToNextAbschaltTest($hash);
RmInternalTimer( "Exec", $hash ); RmInternalTimer( "Exec", $hash );
$hash->{helper}{NEXT_CHECK} = strftime("%d.%m.%Y %H:%M:%S",localtime($nextSwitch)); $hash->{helper}{NEXT_CHECK} =
strftime( "%d.%m.%Y %H:%M:%S", localtime($nextSwitch) );
MkInternalTimer( "Exec", $nextSwitch, \&Exec, $hash, 0 ); MkInternalTimer( "Exec", $nextSwitch, \&Exec, $hash, 0 );
return; return;
} }
@ -421,7 +483,8 @@ sub isAktive {
sub isDisabled { sub isDisabled {
my $hash = shift // return; my $hash = shift // return;
my $disable = IsDisabled($hash->{NAME}); #AttrVal($hash->{NAME}, "disable", 0 ); my $disable =
IsDisabled( $hash->{NAME} ); #AttrVal($hash->{NAME}, "disable", 0 );
return $disable if ($disable); return $disable if ($disable);
my $disableCond = AttrVal( $hash->{NAME}, "disableCond", "nf" ); my $disableCond = AttrVal( $hash->{NAME}, "disableCond", "nf" );
@ -438,10 +501,13 @@ sub schaltZeitenErmitteln {
stopZeitErmitteln( $hash, $now ); stopZeitErmitteln( $hash, $now );
readingsBeginUpdate($hash); readingsBeginUpdate($hash);
# readingsBulkUpdate ($hash, "Startzeit", FmtDateTime($hash->{helper}{startTime})); # readingsBulkUpdate ($hash, "Startzeit", FmtDateTime($hash->{helper}{startTime}));
# readingsBulkUpdate ($hash, "Stoppzeit", FmtDateTime($hash->{helper}{stopTime})); # readingsBulkUpdate ($hash, "Stoppzeit", FmtDateTime($hash->{helper}{stopTime}));
readingsBulkUpdate ($hash, "StartTime", FmtDateTime($hash->{helper}{startTime})); readingsBulkUpdate( $hash, "StartTime",
readingsBulkUpdate ($hash, "StopTime", FmtDateTime($hash->{helper}{stopTime})); FmtDateTime( $hash->{helper}{startTime} ) );
readingsBulkUpdate( $hash, "StopTime",
FmtDateTime( $hash->{helper}{stopTime} ) );
readingsEndUpdate( $hash, defined( $hash->{LOCAL} ? 0 : 1 ) ); readingsEndUpdate( $hash, defined( $hash->{LOCAL} ? 0 : 1 ) );
return; return;
} }
@ -460,10 +526,14 @@ sub setState {
if ( isDisabled($hash) ) { if ( isDisabled($hash) ) {
if ( ReadingsVal( $hash->{NAME}, "state", "" ) ne "inactive" ) { if ( ReadingsVal( $hash->{NAME}, "state", "" ) ne "inactive" ) {
my $dotrigger = ReadingsVal($hash->{NAME},"state","none") ne "disabled" ? 1 : 0; my $dotrigger =
ReadingsVal( $hash->{NAME}, "state", "none" ) ne "disabled"
? 1
: 0;
readingsSingleUpdate( $hash, "state", "disabled", $dotrigger ); readingsSingleUpdate( $hash, "state", "disabled", $dotrigger );
} }
} else { }
else {
my $state = $hash->{helper}{active} ? "on" : "off"; my $state = $hash->{helper}{active} ? "on" : "off";
readingsSingleUpdate( $hash, "state", $state, 1 ); readingsSingleUpdate( $hash, "state", $state, 1 );
} }
@ -476,10 +546,10 @@ sub setSwitchmode {
my $attrVal = shift // return; my $attrVal = shift // return;
my $mod = "[" . $hash->{NAME} . "] "; my $mod = "[" . $hash->{NAME} . "] ";
if ( !( $attrVal =~ m/^([0-9]{1,3})\/([0-9]{1,3})$/ixms ) ) { if ( !( $attrVal =~ m/^([0-9]{1,3})\/([0-9]{1,3})$/ixms ) ) {
Log3 undef, 3, $mod . "invalid switchMode <$attrVal>, use 999/999"; Log3( undef, 3, $mod . "invalid switchMode <$attrVal>, use 999/999");
} else { }
else {
my ( $sigmaWhenOff, $sigmaWhenOn ) = ( $1, $2 ); my ( $sigmaWhenOff, $sigmaWhenOn ) = ( $1, $2 );
$hash->{helper}{SWITCHMODE} = $attrVal; $hash->{helper}{SWITCHMODE} = $attrVal;
$hash->{helper}{SIGMAWHENON} = $sigmaWhenOn; $hash->{helper}{SIGMAWHENON} = $sigmaWhenOn;
@ -495,15 +565,20 @@ sub SetTimer {
return if ( !defined($hash) ); return if ( !defined($hash) );
my $now = time(); 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);
setActive( $hash, 0 ); setActive( $hash, 0 );
schaltZeitenErmitteln( $hash, $now ); schaltZeitenErmitteln( $hash, $now );
setState($hash); setState($hash);
Log3 $hash, 4, "[".$hash->{NAME}."]" . " timings RandomTimer on $hash->{DEVICE}: " Log3( $hash, 4,
. strftime("%H:%M:%S(%d)",localtime($hash->{helper}{startTime})) . " - " "["
. strftime("%H:%M:%S(%d)",localtime($hash->{helper}{stopTime})); . $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 );
@ -512,8 +587,9 @@ sub SetTimer {
MkInternalTimer( "Exec", $setExecTime, \&Exec, $hash, 0 ); MkInternalTimer( "Exec", $setExecTime, \&Exec, $hash, 0 );
if ( $hash->{helper}{REP} gt "" ) { if ( $hash->{helper}{REP} gt "" ) {
my $setTimerTime = max($now+$secToMidnight + 15, my $setTimerTime =
$hash->{helper}{stopTime}) + $hash->{helper}{TIMETOSWITCH}+15; max( $now + $secToMidnight + 15, $hash->{helper}{stopTime} ) +
$hash->{helper}{TIMETOSWITCH} + 15;
RmInternalTimer( "SetTimer", $hash ); RmInternalTimer( "SetTimer", $hash );
MkInternalTimer( "SetTimer", $setTimerTime, \&SetTimer, $hash, 0 ); MkInternalTimer( "SetTimer", $setTimerTime, \&SetTimer, $hash, 0 );
} }
@ -531,8 +607,10 @@ sub startZeitErmitteln {
$rel = $1; $rel = $1;
$rep = $2; $rep = $2;
$tspec = $3; $tspec = $3;
} else { }
return "Wrong timespec_start <$timespec_start>, use \"[+][*]<time or func>\""; else {
return
"Wrong timespec_start <$timespec_start>, use \"[+][*]<time or func>\"";
} }
my ( $err, $hour, $min, $sec, $fn ) = GetTimeSpec($tspec); my ( $err, $hour, $min, $sec, $fn ) = GetTimeSpec($tspec);
@ -541,14 +619,16 @@ sub startZeitErmitteln {
my $startTime; my $startTime;
if ($rel) { if ($rel) {
$startTime = $now + 3600 * $hour + 60 * $min + $sec; $startTime = $now + 3600 * $hour + 60 * $min + $sec;
} else { }
else {
$startTime = zeitBerechnen( $now, $hour, $min, $sec ); $startTime = zeitBerechnen( $now, $hour, $min, $sec );
} }
my $varStart = $hash->{helper}{VAR_START}; my $varStart = $hash->{helper}{VAR_START};
$startTime += int( rand($varStart) ); $startTime += int( rand($varStart) );
$hash->{helper}{startTime} = $startTime; $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) );
return; return;
} }
@ -568,8 +648,10 @@ sub stopZeitErmitteln {
$rel = $1; $rel = $1;
$rep = $2; $rep = $2;
$tspec = $3; $tspec = $3;
} else { }
return "Wrong timespec_stop <$timespec_stop>, use \"[+][*]<time or func>\""; else {
return
"Wrong timespec_stop <$timespec_stop>, use \"[+][*]<time or func>\"";
} }
my ( $err, $hour, $min, $sec, $fn ) = GetTimeSpec($tspec); my ( $err, $hour, $min, $sec, $fn ) = GetTimeSpec($tspec);
@ -577,8 +659,10 @@ sub stopZeitErmitteln {
my $stopTime; my $stopTime;
if ($rel) { if ($rel) {
$stopTime = $hash->{helper}{startTime} + 3600* $hour + 60* $min + $sec; $stopTime =
} else { $hash->{helper}{startTime} + 3600 * $hour + 60 * $min + $sec;
}
else {
$stopTime = zeitBerechnen( $now, $hour, $min, $sec ); $stopTime = zeitBerechnen( $now, $hour, $min, $sec );
} }
@ -588,21 +672,8 @@ sub stopZeitErmitteln {
} }
} }
$hash->{helper}{stopTime} = $stopTime; $hash->{helper}{stopTime} = $stopTime;
$hash->{helper}{STOPTIME} = strftime("%d.%m.%Y %H:%M:%S",localtime($stopTime)); $hash->{helper}{STOPTIME} =
return; strftime( "%d.%m.%Y %H:%M:%S", localtime($stopTime) );
}
sub Wakeup {
for my $hc ( sort keys %{$modules{RandomTimer}{defptr}} ) {
my $hash = $modules{RandomTimer}{defptr}{$hc};
my $myHash->{HASH}=$hash;
SetTimer($myHash);
Log3 undef, 3, "RandomTimer::Wakeup() for $hash->{NAME} done!";
}
Log3 undef, 3, "RandomTimer::Wakeup() done!";
return; return;
} }
@ -610,8 +681,11 @@ sub zeitBerechnen {
my ( $now, $hour, $min, $sec ) = @_; my ( $now, $hour, $min, $sec ) = @_;
my @jetzt_arr = localtime($now); my @jetzt_arr = localtime($now);
#Stunden Minuten Sekunden #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); my $next = timelocal_nocheck(@jetzt_arr);
return $next; return $next;
} }
@ -620,15 +694,21 @@ sub MkInternalTimer {
my ( $modifier, $tim, $callback, $hash, $waitIfInitNotDone ) = @_; my ( $modifier, $tim, $callback, $hash, $waitIfInitNotDone ) = @_;
my $timerName = "$hash->{NAME}_$modifier"; my $timerName = "$hash->{NAME}_$modifier";
my $mHash = { HASH=>$hash, NAME=>"$hash->{NAME}_$modifier", MODIFIER=>$modifier}; my $mHash = {
HASH => $hash,
NAME => "$hash->{NAME}_$modifier",
MODIFIER => $modifier
};
if ( defined( $hash->{TIMER}{$timerName} ) ) { if ( defined( $hash->{TIMER}{$timerName} ) ) {
Log3 $hash, 1, "[$hash->{NAME}] possible overwriting of timer $timerName - please delete first"; Log3( $hash, 1, "[$hash->{NAME}] possible overwriting of timer $timerName - please delete first" );
stacktrace(); stacktrace();
} else { }
else {
$hash->{TIMER}{$timerName} = $mHash; $hash->{TIMER}{$timerName} = $mHash;
} }
Log3 $hash, 5, "[$hash->{NAME}] setting Timer: $timerName " . FmtDateTime($tim); Log3( $hash, 5,
"[$hash->{NAME}] setting Timer: $timerName " . FmtDateTime($tim) );
InternalTimer( $tim, $callback, $mHash, $waitIfInitNotDone ); InternalTimer( $tim, $callback, $mHash, $waitIfInitNotDone );
return $mHash; return $mHash;
} }
@ -641,7 +721,7 @@ sub RmInternalTimer {
my $myHash = $hash->{TIMER}{$timerName}; my $myHash = $hash->{TIMER}{$timerName};
if ( defined($myHash) ) { if ( defined($myHash) ) {
delete $hash->{TIMER}{$timerName}; delete $hash->{TIMER}{$timerName};
Log3 $hash, 5, "[$hash->{NAME}] removing Timer: $timerName"; Log3( $hash, 5, "[$hash->{NAME}] removing Timer: $timerName" );
RemoveInternalTimer($myHash); RemoveInternalTimer($myHash);
} }
return; return;
@ -652,9 +732,9 @@ sub GetHashIndirekt {
my $function = shift // return; my $function = shift // return;
if ( !defined( $myHash->{HASH} ) ) { if ( !defined( $myHash->{HASH} ) ) {
Log3 $myHash, 3, "[$function] myHash not valid"; Log3( $myHash, 3, "[$function] myHash not valid" );
return; return;
}; }
return $myHash->{HASH}; return $myHash->{HASH};
} }