From 2671068e9f8dcebfb4527640e959b4e15790a20b Mon Sep 17 00:00:00 2001 From: Beta-User <> Date: Wed, 10 Feb 2021 06:37:52 +0000 Subject: [PATCH] 98_WeekdayTimer.pm: some perlcritics code modifications git-svn-id: https://svn.fhem.de/fhem/trunk@23710 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/98_WeekdayTimer.pm | 172 +++++++++++++++++------------------ 1 file changed, 83 insertions(+), 89 deletions(-) diff --git a/fhem/FHEM/98_WeekdayTimer.pm b/fhem/FHEM/98_WeekdayTimer.pm index 9943d4957..3bd1d6f2e 100644 --- a/fhem/FHEM/98_WeekdayTimer.pm +++ b/fhem/FHEM/98_WeekdayTimer.pm @@ -137,8 +137,8 @@ sub WeekdayTimer_Define { $hash->{helper}{daysRegExp} =~ s{\$}{\\\$}gxms; $hash->{helper}{daysRegExp} =~ s{\!}{\\\!}gxms; - $hash->{CONDITION} = ""; - $hash->{COMMAND} = ""; + $hash->{CONDITION} = q{}; + $hash->{COMMAND} = q{}; addToDevAttrList($name, "weekprofile") if $def =~ m{weekprofile}xms; @@ -178,7 +178,7 @@ sub WeekdayTimer_Start { WeekdayTimer_GlobalDaylistSpec ($hash, \@arr); my @switchingtimes = gatherSwitchingTimes ($hash, \@arr); - my $conditionOrCommand = join (" ", @arr); + my $conditionOrCommand = join q{ }, @arr; my @errors; # test if device is defined if(!$defs{$device} ) { @@ -292,7 +292,7 @@ sub WeekdayTimer_Get { ################################################################################ sub resetRegisteredInternalTimer { - my ( $modifier, $tim, $callback, $hash, $waitIfInitNotDone, $oldTime ) = @_; + my ( $modifier, $tim, $callback, $hash, $waitIfInitNotDone ) = @_; deleteSingleRegisteredInternalTimer( $modifier, $hash, $callback ); return setRegisteredInternalTimer ( $modifier, $tim, $callback, $hash, $waitIfInitNotDone ); } @@ -387,9 +387,9 @@ sub WeekdayTimer_Profile { $idx++; for my $d (@{$tage}) { my @listeDerTage = ($d); - push (@listeDerTage, WeekdayTimer_getListeDerTage($hash, $d, $time)) if ($d>=7); + push @listeDerTage, WeekdayTimer_getListeDerTage($hash, $d, $time) if $d >= 7; - map { my $day = $_; + for my $day (@listeDerTage) { my $dayOfEchteZeit = $day; ##### if ($day < 7) { @@ -404,7 +404,7 @@ sub WeekdayTimer_Profile { $hash->{profile} {$day}{$echtZeit} = $parameter; $hash->{profile_IDX}{$day}{$echtZeit} = $idx; } - } @listeDerTage; + } } } # ---- Zeitpunkte des aktuellen Tages mit EPOCH ermitteln -------------- @@ -451,12 +451,12 @@ sub WeekdayTimer_getListeDerTage { @hdays{(0..6)} = undef if ($d==8); # mo-fr (!$we) } my ($sec,$min,$hour,$mday,$mon,$year,$nowWday,$yday,$isdst) = localtime(time()); - for (my $i=0;$i<=6;$i++) { - my $relativeDay = $i - $nowWday; + for (0..6) { + my $relativeDay = $_ - $nowWday; $relativeDay = $relativeDay + 7 if $relativeDay < 0 ; if ($hash->{helper}{WEDAYS}{$relativeDay}) { - $hdays{$i} = undef if ($d==7); # $we Tag aufnehmen - delete $hdays{$i} if ($d==8); # !$we Tag herausnehmen + $hdays{$_} = undef if ($d==7); # $we Tag aufnehmen + delete $hdays{$_} if ($d==8); # !$we Tag herausnehmen } } @@ -557,10 +557,10 @@ sub WeekdayTimer_daylistAsArray { #avoid 78 settings: if ($daylist =~ m{[7]}xms && $daylist =~ m{[8]}xms) { Log3( $hash, 4, "[$name] useless double setting of \$we and !\$we found" ); - $daylist = "0123456"; + $daylist = '0123456'; } - @days = split("", $daylist); + @days = split m{}x, $daylist; @hdays{@days} = undef; } @@ -584,7 +584,7 @@ sub WeekdayTimer_EchteZeit { # Zeitangabe verarbeiten. $time = '"' . "$time" . '"' if($time !~ m{\A\{.*\}\z}xms); - my $date = $now+($d-$wday)*86400; + my $date = $now+($d-$wday)*86_400; my $timeString = '{ my $date='."$date;" .$time."}"; my $eTimeString = AnalyzePerlCommand( $hash, $timeString ); # must deliver HH:MM[:SS] @@ -596,7 +596,7 @@ sub WeekdayTimer_EchteZeit { if ($eTimeString =~ m{\A[0-2][0-9]:[0-5][0-9]\z}xms) { # HH:MM $eTimeString .= ":00"; # HH:MM:SS erzeugen - } elsif ($eTimeString !~ m{\A[0-2][0-9](:[0-5][0-9]){2,2}\z}xms) { # not HH:MM:SS + } elsif ($eTimeString !~ m{\A[0-2][0-9]:[0-5][0-9]:[0-5][0-9]\z}xms) { # not HH:MM:SS Log3( $hash, 1, "[$name] invalid time <$eTimeString> HH:MM[:SS]" ); $eTimeString = "00:00:00"; } @@ -625,14 +625,14 @@ sub gatherSwitchingTimes { my $conditionOrCommand; # switchingtime einsammeln - while (@$a > 0) { + while (@{ $a } > 0) { #pruefen auf Angabe eines Schaltpunktes my $element = ""; my @restoreElements = (); -E: while (@$a > 0) { +E: while (@{ $a } > 0) { - my $actualElement = shift @$a; + my $actualElement = shift @{ $a }; push @restoreElements, $actualElement; $element = $element . $actualElement . " "; Log3( $hash, 5, "[$name] $element - trying to accept as a switchtime" ); @@ -686,7 +686,7 @@ E: while (@$a > 0) { ($unused,$wp_profile) = split m{:}xms, getWeekprofileReadingTriplett($hash, $wp_name, 'default'),2 if !$wp_profile && $wprof[2] ne 'default'; return if !$wp_profile; - my $wp_sunaswe = $wprof[2]//0; + my $wp_sunaswe = $wprof[2] // 0; my $wp_profile_data = CommandGet(undef,"$wp_name profile_data $wp_profile 0"); if ($wp_profile_data =~ m{(profile.*not.found|usage..profile_data..name)}xms ) { Log3( $hash, 3, "[$name] weekprofile $wp_name: no profile named \"$wp_profile\" available" ); @@ -701,10 +701,10 @@ E: while (@$a > 0) { my $wp_times = $hash->{weekprofiles}{$wp_name}{PROFILE_DATA}{$wp_days}{time}; my $wp_temps = $hash->{weekprofiles}{$wp_name}{PROFILE_DATA}{$wp_days}{temp}; my $wp_shortDay = $wp_shortDays{$wp_days}; - for ( my $i = 0; $i < @{$wp_temps}; $i++ ) { + for ( 0..@{ $wp_temps }-1 ) { my $itime = "00:10"; - $itime = $hash->{weekprofiles}{$wp_name}{PROFILE_DATA}{$wp_days}{time}[$i-1] if $i; - my $itemp = $hash->{weekprofiles}{$wp_name}{PROFILE_DATA}{$wp_days}{temp}[$i]; + $itime = $hash->{weekprofiles}{$wp_name}{PROFILE_DATA}{$wp_days}{time}[$_-1] if $_; + my $itemp = $hash->{weekprofiles}{$wp_name}{PROFILE_DATA}{$wp_days}{temp}[$_]; my $wp_dayprofile = "$wp_shortDay"."|$itime" . "|$itemp"; $wp_dayprofile .= "|w" if $wp_sunaswe eq "true"; push(@switchingtimes, $wp_dayprofile); @@ -716,7 +716,7 @@ E: while (@$a > 0) { } } else { Log3( $hash, 4, "[$name] $element - NOT accepted, must be command or condition" ); - unshift @$a, @restoreElements; + unshift @{ $a }, @restoreElements; last; } } @@ -731,11 +731,11 @@ sub getWDTLanguage { # ggf. language optional Parameter my $langRegExp = "(" . join ("|", keys(%{$hash->{'.shortDays'}})) . ")"; - my $language = shift @$a; + my $language = shift @{ $a }; if ( $language !~ m{\A$langRegExp\z}xms ) { Log3( $hash, 3, "[$name] language: $language not recognized, use one of $langRegExp" ) if ( length($language) == 2 && $language !~ m{\A[0-9]+\z}gmx ); - unshift @$a, $language; + unshift @{ $a }, $language; $language = lc(AttrVal("global","language","en")); $language = $language =~ m{\A$langRegExp\z}xms ? $language : "en"; } @@ -748,7 +748,7 @@ sub getWDTLanguage { sub WeekdayTimer_GlobalDaylistSpec { my ($hash, $a) = @_; - my $daylist = shift @$a; + my $daylist = shift @{ $a }; my @tage = @{ WeekdayTimer_daylistAsArray( $hash, $daylist ) }; @@ -791,10 +791,10 @@ sub WeekdayTimer_SetTimerOfDay { $iswe = IsWe("tomorrow"); $wedays{(1)} = $iswe if $iswe; - for (my $i=2;$i<=6;$i++) { + for (2..6) { my $noWeekEnd = 0; my $ergebnis = 'none'; - my $izeit = time() + DAYSECONDS * $i; + my $izeit = time() + DAYSECONDS * $_; my ($isec,$imin,$ihour,$imday,$imon,$iyear,$iwday,$iyday,$iisdst) = localtime($izeit); for my $h2we (split m{,}xms, AttrVal('global', 'holiday2we', '')) { @@ -807,13 +807,13 @@ sub WeekdayTimer_SetTimerOfDay { } } if ($ergebnis ne 'none') { - $wedays{$i} = $ergebnis ; + $wedays{$_} = $ergebnis ; } else { if ($iwday == 0 || $iwday == 6) { - $wedays{$i} = 1; - delete $wedays{$i} if AttrVal('global', 'holiday2we', '') =~ m{\bweekEnd\b}xms; + $wedays{$_} = 1; + delete $wedays{$_} if AttrVal('global', 'holiday2we', '') =~ m{\bweekEnd\b}xms; } else { - delete $wedays{$i}; + delete $wedays{$_}; } } } @@ -842,10 +842,8 @@ sub WeekdayTimer_SetTimer { Log3( $hash, 4, "[$name] no switch in the yesterdays because of the devices type($hash->{DEVICE} is not recognized as heating) - use attr switchInThePast" ) if ( !$switchInThePast && !defined $hash->{SETTIMERATMIDNIGHT} ); my @switches = sort keys %{$hash->{profil}}; - if (@switches == 0) { - Log3( $hash, 3, "[$name] no switches to send, due to possible errors." ); - return; - } + + return Log3( $hash, 3, "[$name] no switches to send, due to possible errors." ) if !(@switches); readingsSingleUpdate ($hash, "state", "inactive", 1) if (!defined $hash->{SETTIMERATMIDNIGHT}); for(my $i=0; $i<=$#switches; $i++) { @@ -871,20 +869,14 @@ sub WeekdayTimer_SetTimer { Log3( $hash, 4, "[$name] setTimer - timer seems to be NOT active today: ".join("",@$tage)."|$time|$para ". $hash->{CONDITION} ); deleteSingleRegisteredInternalTimer("$idx", $hash); } - #WeekdayTimer_RemoveInternalTimer("$idx", $hash); - #WeekdayTimer_InternalTimer ("$idx", $timToSwitch, "$hash->{TYPE}_Update", $hash, 0); } } - if (defined $hash->{SETTIMERATMIDNIGHT}) { - return; - } + return if defined $hash->{SETTIMERATMIDNIGHT}; my ($aktIdx,$aktTime,$aktParameter,$nextTime,$nextParameter) = WeekdayTimer_searchAktNext($hash, time()+5); - if(!defined $aktTime) { - Log3( $hash, 3, "[$name] can not compute past switching time" ); - } + Log3( $hash, 3, "[$name] can not compute past switching time" ) if !defined $aktTime; readingsBeginUpdate($hash); readingsBulkUpdate ($hash, "nextUpdate", FmtDateTime($nextTime)); @@ -892,11 +884,10 @@ sub WeekdayTimer_SetTimer { readingsBulkUpdate ($hash, "currValue", $aktParameter); readingsEndUpdate ($hash, 1); - if ($switchInThePast && defined $aktTime) { + return if !$switchInThePast || !defined $aktTime || checkDelayedExecution($hash, $aktParameter, $aktIdx ); + #if ($switchInThePast && defined $aktTime) { # Fensterkontakte abfragen - wenn einer im Status closed, dann Schaltung um 60 Sekunden verzögern - if (checkDelayedExecution($hash, $aktParameter, $aktIdx)) { - return; - } + #return if (checkDelayedExecution($hash, $aktParameter, $aktIdx)); # alle in der Vergangenheit liegenden Schaltungen sammeln und # nach 5 Sekunden in der Reihenfolge der Schaltzeiten @@ -908,17 +899,16 @@ sub WeekdayTimer_SetTimer { my $parameter = $modules{WeekdayTimer}{timerInThePast}{$device}{$aktTime}; $parameter = [] if (!defined $parameter); - push (@$parameter,["$aktIdx", $aktTime, \&WeekdayTimer_Update, $hash, 0]); + push @$parameter,["$aktIdx", $aktTime, \&WeekdayTimer_Update, $hash, 0]; $modules{WeekdayTimer}{timerInThePast}{$device}{$aktTime} = $parameter; my $tipHash = $modules{WeekdayTimer}{timerInThePastHash}; $tipHash = $hash if (!defined $tipHash); $modules{WeekdayTimer}{timerInThePastHash} = $tipHash; - #WeekdayTimer_RemoveInternalTimer("delayed", $tipHash); resetRegisteredInternalTimer("delayed", time()+5, \&WeekdayTimer_delayedTimerInPast, $tipHash, 0); - } + #} return; } @@ -1152,10 +1142,12 @@ sub checkDelayedExecution { $nextRetry = $epoch + $nextDelay + AttrVal($name,'WDT_sendDelay',0); Log3( $hash, 4, "[$name] time=".$hash->{profil}{$time}{TIME}."/$epoch delay=$delay, nextDelay=$nextDelay, nextRetry=$nextRetry" ); - map { my $key = $_; $key =~ s{\$}{\\\$}gxms; - my $val = $specials{$_}; - $verzoegerteAusfuehrungCond =~ s{$key}{$val}gxms - } keys %specials; + for my $key (keys %specials) { + my $val = $specials{$key}; + $key =~ s{\$}{\\\$}gxms; + $verzoegerteAusfuehrungCond =~ s{$key}{$val}gxms + } + Log3( $hash, 4, "[$name] delayedExecutionCond:$verzoegerteAusfuehrungCond" ); # my $verzoegerteAusfuehrung = eval($verzoegerteAusfuehrungCond); @@ -1196,49 +1188,51 @@ sub checkDelayedExecution { $fensterKontakte = trim($fensterKontakte); Log3( $hash, 4, "[$name] list of window sensors found: '$fensterKontakte'" ); - if ($fensterKontakte ne "" ) { - my @kontakte = split m{\s+}xms, $fensterKontakte; - for my $fk (@kontakte) { + #if ($fensterKontakte ne "" ) { #rem.: Da muss eigentlich immer der Name drinstehen...? + #my @kontakte = split m{\s+}xms, $fensterKontakte; + #for my $fk (@kontakte) { + for my $fk (split m{\s+}xms, $fensterKontakte) { #hier flexible eigene Angaben ermöglichen?, Schreibweise: Device[:Reading[:ValueToCompare[:Comparator]]]; defaults: Reading=state, ValueToCompare=0/undef/false, all other true, Comparator=eq (options: eq, ne, lt, gt, ==, <,>,<>) my $fk_hash = $defs{$fk}; if (!$fk_hash) { Log3( $hash, 3, "[$name] sensor <$fk> not found - check name." ); - } else { - my $fk_typ = $fk_hash->{TYPE}; - if (!defined($contacts{$fk_typ})) { - Log3( $hash, 3, "[$name] TYPE '$fk_typ' of $fk not yet supported, $fk ignored - inform maintainer" ); - } else { + next; + } + my $fk_typ = $fk_hash->{TYPE}; + if (!defined($contacts{$fk_typ})) { + Log3( $hash, 3, "[$name] TYPE '$fk_typ' of $fk not yet supported, $fk ignored - inform maintainer" ); + next; + } - my $reading = $contacts{$fk_typ}{READING}; - my $statusReg = $contacts{$fk_typ}{STATUS}; - my $model = $contacts{$fk_typ}{MODEL}; + my $reading = $contacts{$fk_typ}{READING}; + my $statusReg = $contacts{$fk_typ}{STATUS}; + my $model = $contacts{$fk_typ}{MODEL}; - my $windowStatus = $model eq "r" ? ReadingsVal($fk,$reading,"nF") - : AttrVal ($fk,$reading,"nF"); + my $windowStatus = $model eq "r" ? ReadingsVal($fk,$reading,"nF") + : AttrVal ($fk,$reading,"nF"); - if ($windowStatus eq "nF") { - Log3( $hash, 3, "[$name] Reading/Attribute '$reading' of $fk not found, $fk ignored - inform maintainer" ) if ( $model eq "r" ); - } else { - Log3( $hash, 5, "[$name] sensor '$fk' Reading/Attribute '$reading' is '$windowStatus'" ); + if ($windowStatus eq "nF") { + Log3( $hash, 3, "[$name] Reading/Attribute '$reading' of $fk not found, $fk ignored - inform maintainer" ) if ( $model eq "r" ); + next; + } + + Log3( $hash, 5, "[$name] sensor '$fk' Reading/Attribute '$reading' is '$windowStatus'" ); - if ($windowStatus =~ m{\A$statusReg\z}xms) { - if (!defined($hash->{VERZOEGRUNG})) { - Log3( $hash, 3, "[$name] switch of $hash->{DEVICE} delayed - sensor '$fk' Reading/Attribute '$reading' is '$windowStatus'" ); - } - if (defined($hash->{VERZOEGRUNG_IDX}) && $hash->{VERZOEGRUNG_IDX}!=$time) { - Log3( $hash, 3, "[$name] timer at $hash->{profil}{$hash->{VERZOEGRUNG_IDX}}{TIME} skipped by new timer at $hash->{profil}{$time}{TIME} while window contact returned open state"); - deleteSingleRegisteredInternalTimer($hash->{VERZOEGRUNG_IDX},$hash); - } - $hash->{VERZOEGRUNG_IDX} = $time; - resetRegisteredInternalTimer("$time", $nextRetry, \&WeekdayTimer_Update, $hash, 0); - $hash->{VERZOEGRUNG} = 1; - return 1 - } - } + if ($windowStatus =~ m{\A$statusReg\z}xms) { + if (!defined($hash->{VERZOEGRUNG})) { + Log3( $hash, 3, "[$name] switch of $hash->{DEVICE} delayed - sensor '$fk' Reading/Attribute '$reading' is '$windowStatus'" ); } + if (defined($hash->{VERZOEGRUNG_IDX}) && $hash->{VERZOEGRUNG_IDX}!=$time) { + Log3( $hash, 3, "[$name] timer at $hash->{profil}{$hash->{VERZOEGRUNG_IDX}}{TIME} skipped by new timer at $hash->{profil}{$time}{TIME} while window contact returned open state"); + deleteSingleRegisteredInternalTimer($hash->{VERZOEGRUNG_IDX},$hash); + } + $hash->{VERZOEGRUNG_IDX} = $time; + resetRegisteredInternalTimer("$time", $nextRetry, \&WeekdayTimer_Update, $hash, 0); + $hash->{VERZOEGRUNG} = 1; + return 1 } } - } + #} if ($hash->{VERZOEGRUNG}) { Log3( $hash, 3, "[$name] delay of switching $hash->{DEVICE} stopped." ); } @@ -1354,7 +1348,7 @@ sub WeekdayTimer_Attr { my $hash = $defs{$name}; if ( $attrName eq "WDT_eventMap" ) { if($cmd eq "set") { - my @ret = split(/[: \r\n]/, $attrVal); + my @ret = split m{[: \r\n]}x, $attrVal; return "WDT_eventMap: Odd number of elements" if(int(@ret) % 2); my %ret = @ret; for (keys %ret) { @@ -1446,7 +1440,7 @@ sub updateWeekprofileReading { my @t = split m{\s+}xms, ReadingsVal( $name, 'weekprofiles', ''); my @newt = ( qq($wp_name:$wp_topic:$wp_profile) ); push @newt, grep { $_ !~ m{\A$wp_name\b}xms } @t; - readingsSingleUpdate( $hash, 'weekprofiles', join(' ', @newt), 1 ); + readingsSingleUpdate( $hash, 'weekprofiles', join (q{ }, @newt), 1 ); return 1; }