From b0b66fefa96c40546fef58427677024d0e9982a0 Mon Sep 17 00:00:00 2001 From: markusbloch <> Date: Sun, 3 Feb 2013 00:28:15 +0000 Subject: [PATCH] adding the presence check via fritzbox ctlmgr_ctl command git-svn-id: https://svn.fhem.de/fhem/trunk@2640 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/FHEM/73_PRESENCE.pm | 93 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 87 insertions(+), 6 deletions(-) diff --git a/fhem/FHEM/73_PRESENCE.pm b/fhem/FHEM/73_PRESENCE.pm index f58a27959..02996fc5b 100755 --- a/fhem/FHEM/73_PRESENCE.pm +++ b/fhem/FHEM/73_PRESENCE.pm @@ -62,7 +62,8 @@ PRESENCE_Define($$) my @a = split("[ \t]+", $def); my $dev; - if($a[2] ne "lan-bluetooth" and not (@a == 4 or @a == 5)) { + if($a[2] ne "lan-bluetooth" and not (@a == 4 or @a == 5)) + { my $msg = "wrong syntax: define PRESENCE [ ]"; Log 2, $msg; return $msg; @@ -106,10 +107,25 @@ PRESENCE_Define($$) { my $msg = "given address is not a bluetooth hardware address"; Log 2, "PRESENCE: ".$msg; - return $msg + return $msg; + } + + if($destination eq "fritzbox" and not -X "/usr/bin/ctlmgr_ctl") + { + my $msg = "this is not a fritzbox or you running FHEM with the AVM Beta Image. Please use the FHEM FritzBox Image from fhem.de"; + Log 2, "PRESENCE: ".$msg; + return $msg; + } + + if($destination eq "fritzbox" and not $< == 0) + { + + my $msg = "FHEM is not running under root (currently ".(getpwuid($<))[0].") This check can only performed with root access to the FritzBox"; + Log 2, "PRESENCE: ".$msg; + return $msg; } - if($destination eq "lan-ping" or $destination eq "local-bluetooth") + if($destination eq "lan-ping" or $destination eq "local-bluetooth" or $destination eq "fritzbox") { $hash->{MODE} = $destination; @@ -144,6 +160,11 @@ PRESENCE_Define($$) return $ret; } + else + { + + return "unknown mode: $destination Please use lan-ping, lan-bluetooth or local-bluetooth"; + } } @@ -321,6 +342,11 @@ my ($hash) = @_; BlockingCall("PRESENCE_DoLocalPingScan", $hash->{NAME}."|".$hash->{ADDRESS}, "PRESENCE_ProcessLocalScan", 20); } + elsif($hash->{MODE} eq "fritzbox") + { + BlockingCall("PRESENCE_DoLocalFritzBoxScan", $hash->{NAME}."|".$hash->{ADDRESS}, "PRESENCE_ProcessLocalScan", 20); + } + } sub @@ -351,9 +377,58 @@ return $return; } +sub +PRESENCE_DoLocalFritzBoxScan($) +{ + my ($string) = @_; + my ($name, $device) = split("\\|", $string); + + my $number=0; + + my $max = qx(/usr/bin/ctlmgr_ctl r landevice settings/landevice/count); + + chomp $max; + + if(not $max =~ /^\s*\d+\s*$/) + { + return "$name|error|could not execute ctlmgr_ctl"; + } + + + my $status=0; + my $net_device; + + while($number <= $max) + { + $net_device=qx(/usr/bin/ctlmgr_ctl r landevice settings/landevice$number/name); + + chomp $net_device; + + if($net_device eq $device) + { + $status=qx(/usr/bin/ctlmgr_ctl r landevice settings/landevice$number/active); + last; + } + + $number++; + } + + chomp $status; + + if($status == 1) + { + return "$name|present"; + } + else + { + return "$name|absent"; + } + + +} sub -PRESENCE_DoLocalBluetoothScan($$) +PRESENCE_DoLocalBluetoothScan($) { my ($string) = @_; my ($name, $device) = split("\\|", $string); @@ -450,6 +525,7 @@ PRESENCE_ProcessLocalScan($) This module provides several operational modes to serve your needs. These are:

  • lan-ping - A presence check of a device via network ping in your LAN/WLAN
  • +
  • fritzbox - A presence check by requesting the device state from the FritzBox internals (only available when running FHEM on a FritzBox!)
  • local-bluetooth - A presence check by searching directly for a given bluetooth device nearby
  • lan-bluetooth - A presence check of a bluetooth device via LAN network by connecting to a presenced or collectord instance
@@ -461,6 +537,11 @@ PRESENCE_ProcessLocalScan($)
Checks for a network device via PING requests and reports its presence state.

+ Mode: fritzbox

+ define <name> PRESENCE fritzbox <device-name> [ <timeout> ]
+
+ Checks for a network device by requesting the internal state on a FritzBox via ctlmgr_ctl. The device-name must be the same as shown in the network overview of the FritzBox
+
Mode: local-bluetooth

define <name> PRESENCE local-bluetooth <bluetooth-address> [ <timeout> ]

@@ -516,7 +597,7 @@ Options: It listens on TCP port 5222 for incoming connections from a FHEM presence instance.
 Usage:
-  collectord -c <configfile> [-d] [-p ] [-P <pidfile>]
+  collectord -c <configfile> [-d] [-p <port>] [-P <pidfile>]
   collectord [-h | --help]
 
 
@@ -616,4 +697,4 @@ Options:
 
 =end html
 
-=cut
+=cut
\ No newline at end of file