From 47a172967da3bfda9be4af695abad17b2a02dda7 Mon Sep 17 00:00:00 2001 From: markusbloch <> Date: Mon, 18 Feb 2013 17:49:27 +0000 Subject: [PATCH] caching the device number at fritzbox checks for better performance (by erwin with minor modifications) git-svn-id: https://svn.fhem.de/fhem/trunk@2757 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/73_PRESENCE.pm | 53 ++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/fhem/FHEM/73_PRESENCE.pm b/fhem/FHEM/73_PRESENCE.pm index acb9d1046..15c3c1e6c 100755 --- a/fhem/FHEM/73_PRESENCE.pm +++ b/fhem/FHEM/73_PRESENCE.pm @@ -1,4 +1,4 @@ -# $Id$ +# $Id$ ############################################################################## # # 73_PRESENCE.pm @@ -362,6 +362,7 @@ PRESENCE_Ready($) { my ($hash) = @_; + delete $hash->{helper}{cachednr}; ##MH clear cache on start return DevIo_OpenDev($hash, 1, "PRESENCE_DoInit"); } @@ -437,6 +438,30 @@ PRESENCE_DoLocalFritzBoxScan($) my $number=0; + + my $status=0; + + if (defined($defs{$name}{helper}{cachednr})) + { + $number = $defs{$name}{helper}{cachednr}; + + Log GetLogLevel($name, 5), "PRESENCE_DoLocalFritzBoxScan: name=$name device=$device cachednr=$number"; + + my $cached_name = qx(/usr/bin/ctlmgr_ctl r landevice settings/landevice$number/name); + chomp $cached_name; + + # only use the cached $number if it has still the correct device name + if($cached_name eq $device) + { + $status = qx(/usr/bin/ctlmgr_ctl r landevice settings/landevice$number/speed); + if(not $status =~ /^\s*\d+\s*$/) + { + return "$name|$local|error|could not execute ctlmgr_ctl (cached)"; + } + return ($status == 0)? "$name|$local|absent|$number" : "$name|$local|present|$number"; ###MH + } + } + my $max = qx(/usr/bin/ctlmgr_ctl r landevice settings/landevice/count); chomp $max; @@ -447,7 +472,7 @@ PRESENCE_DoLocalFritzBoxScan($) } - my $status=0; + my $net_device; while($number <= $max) @@ -458,7 +483,7 @@ PRESENCE_DoLocalFritzBoxScan($) if($net_device eq $device) { - $status=qx(/usr/bin/ctlmgr_ctl r landevice settings/landevice$number/active); + $status=qx(/usr/bin/ctlmgr_ctl r landevice settings/landevice$number/speed); last; } @@ -467,17 +492,8 @@ PRESENCE_DoLocalFritzBoxScan($) } chomp $status; - - if($status == 1) - { - return "$name|$local|present"; - } - else - { - return "$name|$local|absent"; - } - + return ($status == 0 ? "$name|$local|absent|$number" : "$name|$local|present|$number"); } sub @@ -542,12 +558,18 @@ PRESENCE_ProcessLocalScan($) return if($hash->{helper}{DISABLED}); + Log GetLogLevel($hash->{NAME}, 5), "PRESENCE_ProcessLocalScan: $string"; + + if($hash->{MODE} eq "fritzbox" and defined($a[3])) + { + $hash->{helper}{cachednr} = $a[3] if(($a[2] eq "present") || ($a[2] eq "absent")); + } readingsBeginUpdate($hash); if($a[2] eq "present") { readingsBulkUpdate($hash, "state", "present"); - readingsBulkUpdate($hash, "device_name", $a[3]) if(defined($a[3])); + readingsBulkUpdate($hash, "device_name", $a[3]) if(defined($a[3]) and $hash->{MODE} =~ /^(lan-bluetooth|local-bluetooth)$/ ); } elsif($a[2] eq "absent") { @@ -582,7 +604,7 @@ PRESENCE_ProcessLocalScan($) This module provides several operational modes to serve your needs. These are:

@@ -894,6 +916,7 @@ Options: Set