From a54c7c70dc36f9042b0aaba0eedee93e916c8e2b Mon Sep 17 00:00:00 2001 From: hexenmeister Date: Sat, 9 Jan 2021 22:38:50 +0000 Subject: [PATCH] change: non blocking by MadMax-FHEM git-svn-id: https://svn.fhem.de/fhem/trunk@23503 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/42_SMARTMON.pm | 107 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 106 insertions(+), 1 deletion(-) diff --git a/fhem/FHEM/42_SMARTMON.pm b/fhem/FHEM/42_SMARTMON.pm index 0aa76ff9d..952b0279d 100644 --- a/fhem/FHEM/42_SMARTMON.pm +++ b/fhem/FHEM/42_SMARTMON.pm @@ -269,7 +269,7 @@ sub SMARTMON_Update($) } # Alle Readings neuerstellen -sub SMARTMON_refreshReadings($) { +sub SMARTMON_refreshReadings_($) { my ($hash) = @_; SMARTMON_Log($hash, 5, "Refresh readings"); @@ -309,6 +309,111 @@ sub SMARTMON_refreshReadings($) { readingsEndUpdate($hash,1); } +# MadMax-Fhem: start non blocking +# MadMax-Fhem: start non blocking +# MadMax-Fhem: start non blocking +sub SMARTMON_refreshReadings($) { + my ($hash) = @_; + + my $name = $hash->{NAME}; + + if( AttrVal($name, "disable", "") eq "1" ) + { + SMARTMON_Log($hash, 5, "Update disabled"); + $hash->{STATE} = "Inactive"; + } + else + { + if(exists($hash->{helper}{RUNNING_PID})) + { + # should there be additionally a reading indication the error? + SMARTMON_Log($hash, 3, "Blocing call already running!"); + } + else + { + # timeout 30s maybe too long? maybe attribute? / parameter could also be Name instead of hash!? + $hash->{helper}{RUNNING_PID} = BlockingCall("SMARTMON_refreshReadingsNonBlocking", $hash, "SMARTMON_refreshReadingsDone", 30, "SMARTMON_refreshReadingsAbort", $hash); + SMARTMON_Log($hash, 5, "Blocing call started."); + } + } +} + +sub SMARTMON_refreshReadingsNonBlocking($) +{ + my ($hash) = @_; + + SMARTMON_Log($hash, 5, "Blocking-fn entered."); + + my $map = SMARTMON_obtainParameters($hash); + + # puting the return value together (must be one string when using blocking call) + my $ret = $hash->{NAME} . "|" . encode_json($map); + + SMARTMON_Log($hash, 5, "Blocking-fn return value $ret."); + + return $ret; +} + +sub SMARTMON_refreshReadingsDone($) +{ + my ($Data) = @_; + +# but who is deleting RUNNING_PID then!? + return unless(defined($Data)); + + # get the parts from given parameter (must be one string when using blocking call) + my @DataParts = split("\\|", $Data); + + my $hash = $defs{$DataParts[0]}; + # and "rebuild" the map + my $map = decode_json($DataParts[1]); + + SMARTMON_Log($hash, 5, "Blocing call finished. Updating Readings."); + + readingsBeginUpdate($hash); + + $hash->{STATE} = "Active"; + + foreach my $aName (keys %{$map}) + { + my $value = $map->{$aName}; + #SMARTMON_Log($hash, 5, "Update: ".$value); + # Nur aktualisieren, wenn ein gueltiges Value vorliegt + if(defined $value) + { + readingsBulkUpdate($hash,$aName,$value); + } + } + + # Alle anderen Readings entfernen + foreach my $rName (sort keys %{$hash->{READINGS}}) + { + if(!defined($map->{$rName})) + { + delete $hash->{READINGS}->{$rName}; + } + } + + readingsEndUpdate($hash,1); + + delete($hash->{helper}{RUNNING_PID}); +} + +sub SMARTMON_refreshReadingsAbort($) +{ + my ($hash) = @_; + + # should there be additionally a reading indication the error? + SMARTMON_Log($hash, 1, "Blocing call aborted!"); + + # is there additional things to do? But what? + + delete($hash->{helper}{RUNNING_PID}); +} +# MadMax-Fhem: end non blocking +# MadMax-Fhem: end non blocking +# MadMax-Fhem: end non blocking + # Alle Readings erstellen sub SMARTMON_obtainParameters($) { my ($hash) = @_;