From 637969c835f902ad887a73dce81a95824f0c488c Mon Sep 17 00:00:00 2001 From: Beta-User Date: Mon, 24 May 2021 04:33:35 +0000 Subject: [PATCH] 59_Twilight: now internally uses Register.pm for timer admin git-svn-id: https://svn.fhem.de/fhem/trunk@24496 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/59_Twilight.pm | 96 +++++++++------------------------------- 1 file changed, 20 insertions(+), 76 deletions(-) diff --git a/fhem/FHEM/59_Twilight.pm b/fhem/FHEM/59_Twilight.pm index a90f93270..1aeba0624 100644 --- a/fhem/FHEM/59_Twilight.pm +++ b/fhem/FHEM/59_Twilight.pm @@ -44,6 +44,7 @@ use Scalar::Util qw( weaken ); use GPUtils qw(GP_Import GP_Export); #eval { use FHEM::Core::Timer::Helper qw( addTimer removeTimer optimizeLOT ); 1 }; use FHEM::Meta; +use FHEM::Core::Timer::Register qw(:ALL); #-- Run before package compilation BEGIN { @@ -77,7 +78,6 @@ BEGIN { IsDisabled Log3 InternalTimer - RemoveInternalTimer hms2h h2hms_fmt FmtTime @@ -177,7 +177,7 @@ sub Twilight_Undef { my $hash = shift; my $arg = shift // return; - deleteAllRegisteredInternalTimer($hash); + deleteAllRegIntTimer($hash); notifyRegexpChanged( $hash, "" ); for my $key ( keys %{ $hash->{helper}{extWeather} } ) { @@ -195,7 +195,7 @@ sub Twilight_Change_DEF { my $name = $hash->{NAME}; my $newdef = ""; my $weather = $hash->{DEFINE}; - $weather = "" if $weather eq "none"; + $weather = "" if $weather eq 'none'; if (looks_like_number($weather)) { my @wd = devspec2array("TYPE=Weather|PROPLANTA"); my ($err, $wreading); @@ -221,7 +221,7 @@ sub Twilight_Notify { my $wname = $whash->{NAME}; my $events = deviceEvents( $whash, 1 ); - my $re = $hash->{helper}{extWeather}{regexp} // "unknown"; + my $re = $hash->{helper}{extWeather}{regexp} // q{unknown}; return if(!$events); # Some previous notify deleted the array. my $max = int(@{$events}); @@ -318,21 +318,21 @@ sub Twilight_HandleWeatherData { } #renew dates and timers?, fire events? - my $nextevent = ReadingsVal($name,"nextEvent","none"); + my $nextevent = ReadingsVal($name,'nextEvent','none'); my $nextEventTime = FmtTime( $sr ); readingsBeginUpdate( $hash ); - readingsBulkUpdate( $hash, "cloudCover", $extWeather ); - readingsBulkUpdate( $hash, "cloudCover_sr", $sr_extWeather ) if $sr_wh && $now < $sr; - readingsBulkUpdate( $hash, "cloudCover_ss", $ss_extWeather ) if $ss_wh && $now < $ss; + readingsBulkUpdate( $hash, 'cloudCover', $extWeather ); + readingsBulkUpdate( $hash, 'cloudCover_sr', $sr_extWeather ) if $sr_wh && $now < $sr; + readingsBulkUpdate( $hash, 'cloudCover_ss', $ss_extWeather ) if $ss_wh && $now < $ss; if ( $now < $sr ) { $hash->{TW}{sr_weather}{TIME} = $sr; - resetRegisteredInternalTimer( 'sr_weather', $sr, \&Twilight_fireEvent, $hash, 0 ); + resetRegIntTimer( 'sr_weather', $sr, \&Twilight_fireEvent, $hash, 0 ); readingsBulkUpdate( $hash, 'sr_weather', $nextEventTime ) if $inNotify; readingsBulkUpdate( $hash, 'nextEventTime', $nextEventTime ) if $nextevent eq 'sr_weather'; } elsif ( $sr_passed ) { - deleteSingleRegisteredInternalTimer( 'sr_weather', $hash, \&Twilight_fireEvent); + deleteSingleRegIntTimer( 'sr_weather', $hash, \&Twilight_fireEvent); readingsBulkUpdate( $hash, 'sr_weather', $nextEventTime ); if ( $nextevent eq 'sr_weather' ) { readingsBulkUpdate( $hash, 'nextEvent', 'ss_weather' ) ; @@ -340,18 +340,18 @@ sub Twilight_HandleWeatherData { readingsBulkUpdate( $hash, 'state', '6' ); readingsBulkUpdate( $hash, 'light', '6' ); readingsBulkUpdate( $hash, 'aktEvent', 'sr_weather' ); - resetRegisteredInternalTimer( 'ss_weather', $ss, \&Twilight_fireEvent, $hash, 0 ); + resetRegIntTimer( 'ss_weather', $ss, \&Twilight_fireEvent, $hash, 0 ); } } if ( $now < $ss ) { $nextEventTime = FmtTime( $ss ); $hash->{TW}{ss_weather}{TIME} = $ss; - resetRegisteredInternalTimer( 'ss_weather', $ss, \&Twilight_fireEvent, $hash, 0 ); + resetRegIntTimer( 'ss_weather', $ss, \&Twilight_fireEvent, $hash, 0 ); readingsBulkUpdate( $hash, 'ss_weather', $nextEventTime ) if $inNotify; readingsBulkUpdate( $hash, 'nextEventTime', $nextEventTime ) if $nextevent eq 'ss_weather' && !$ss_passed; } elsif ( $ss_passed ) { - deleteSingleRegisteredInternalTimer( 'ss_weather', $hash, \&Twilight_fireEvent ); + deleteSingleRegIntTimer( 'ss_weather', $hash, \&Twilight_fireEvent ); readingsBulkUpdate( $hash, 'ss_weather', $nextEventTime ); if ( $nextevent eq 'ss_weather' ) { readingsBulkUpdate( $hash, 'nextEvent', 'ss_indoor' ) ; @@ -363,7 +363,7 @@ sub Twilight_HandleWeatherData { } readingsEndUpdate( $hash, defined( $hash->{LOCAL} ? 0 : 1 ) ); - resetRegisteredInternalTimer('sunpos', $now+1, \&Twilight_sunpos, $hash, 0); + resetRegIntTimer('sunpos', $now+1, \&Twilight_sunpos, $hash, 0); return; } @@ -506,63 +506,6 @@ sub Twilight_Get { return "@$aref[0] $reading => $value"; } -################################################################################ -sub resetRegisteredInternalTimer { - my ( $modifier, $tim, $callback, $hash, $waitIfInitNotDone ) = @_; - deleteSingleRegisteredInternalTimer( $modifier, $hash, $callback ); - return setRegisteredInternalTimer ( $modifier, $tim, $callback, $hash, $waitIfInitNotDone ); -} - -################################################################################ -sub setRegisteredInternalTimer { - my ( $modifier, $tim, $callback, $hash, $waitIfInitNotDone ) = @_; - - my $timerName = "$hash->{NAME}_$modifier"; - my $fnHash = { - HASH => $hash, - NAME => $timerName, - MODIFIER => $modifier - }; - weaken($fnHash->{HASH}); - - 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, $fnHash, $waitIfInitNotDone ); - return $fnHash; -} - -################################################################################ -sub deleteSingleRegisteredInternalTimer { - my $modifier = shift; - my $hash = shift // return; - - my $timerName = "$hash->{NAME}_$modifier"; - my $fnHash = $hash->{TIMER}{$timerName}; - if ( defined($fnHash) ) { - Log3( $hash, 5, "[$hash->{NAME}] removing Timer: $timerName" ); - RemoveInternalTimer($fnHash); - delete $hash->{TIMER}{$timerName}; - } - return; -} - -################################################################################ -sub deleteAllRegisteredInternalTimer { - my $hash = shift // return; - - for my $key ( keys %{ $hash->{TIMER} } ) { - deleteSingleRegisteredInternalTimer( $hash->{TIMER}{$key}{MODIFIER}, $hash ); - } - return; -} - ################################################################################ sub secondsSinceMidnight { @@ -679,10 +622,10 @@ sub Twilight_TwilightTimes { for my $ereignis ( sort keys %{ $hash->{TW} } ) { next if ( $whitchTimes eq 'weather' && $ereignis !~ m{weather}x ); - deleteSingleRegisteredInternalTimer( $ereignis, $hash, \&Twilight_fireEvent ); + deleteSingleRegIntTimer( $ereignis, $hash, \&Twilight_fireEvent ); if ( defined $hash->{TW}{$ereignis}{TIME} && ($hash->{TW}{$ereignis}{TIME} > $now || $firstrun) ) { # had been > 0 - setRegisteredInternalTimer( $ereignis, $hash->{TW}{$ereignis}{TIME}, + setRegIntTimer( $ereignis, $hash->{TW}{$ereignis}{TIME}, \&Twilight_fireEvent, $hash, 0 ); } } @@ -718,7 +661,7 @@ sub Twilight_fireEvent { my $nextEventTime = ( $hash->{TW}{$nextEvent}{TIME} > 0 ) ? FmtTime( $hash->{TW}{$nextEvent}{TIME} ) - : "undefined"; + : 'undefined'; my $doTrigger = !( defined( $hash->{LOCAL} ) ) && ( abs($delta) < 6 || $swip && $state gt $oldState ); @@ -730,6 +673,7 @@ sub Twilight_fireEvent { . sprintf( "(%2d/$light/%+5.1f°/$doTrigger) ", $state, $deg ) . sprintf( "===> %-10s %-19s ", $nextEvent, $nextEventTime ) ); + deleteSingleRegIntTimer($modifier, $hash, 1); readingsBeginUpdate($hash); readingsBulkUpdate( $hash, 'state', $state ); @@ -765,7 +709,7 @@ sub Twilight_Midnight { $midnight -= 19 * HOURSECONDS if $daylightsavingdelta == 1 && (localtime)[2] < 2; $midnight -= 20 * HOURSECONDS if $daylightsavingdelta == -1 && (localtime)[2] < 3; - return resetRegisteredInternalTimer( 'Midnight', $midnight, \&Twilight_Midnight, $hash, 0 ); + return resetRegIntTimer( 'Midnight', $midnight, \&Twilight_Midnight, $hash, 0 ); } @@ -775,7 +719,7 @@ sub Twilight_Midnight { sub Twilight_sunposTimerSet { my $hash = shift // return; - return resetRegisteredInternalTimer( 'sunpos', time + $hash->{SUNPOS_OFFSET}, \&Twilight_sunpos, $hash, 0 ); + return resetRegIntTimer( 'sunpos', time + $hash->{SUNPOS_OFFSET}, \&Twilight_sunpos, $hash, 0 ); } ################################################################################