[gelöst] FRITZBOX - Funktionserweiterung

Begonnen von pwlr, 23 Mai 2017, 14:46:36

Vorheriges Thema - Nächstes Thema

jackyno.71986

Hallo Bernd,

Danke für deine Hilfe, hab es eingepflegt bekomme aber leider 0 angezeigt im Log is auch ein fehler:

2019.10.09 20:18:20 1: PERL WARNING: Use of uninitialized value $1 in string eq at ./FHEM/99_myUtils.pm line 115.
2019.10.09 20:18:20 3: eval: {sip_connect_check($name)}
2019.10.09 20:18:20 1: stacktrace:
2019.10.09 20:18:20 1:     main::__ANON__                      called by ./FHEM/99_myUtils.pm (115)
2019.10.09 20:18:20 1:     main::sip_connect_check             called by (eval 239) (1)
2019.10.09 20:18:20 1:     (eval)                              called by fhem.pl (4678)
2019.10.09 20:18:20 1:     main::readingsEndUpdate             called by ./FHEM/72_FRITZBOX.pm (1920)
2019.10.09 20:18:20 1:     main::FRITZBOX_Readout_Process      called by ./FHEM/72_FRITZBOX.pm (1776)
2019.10.09 20:18:20 1:     main::FRITZBOX_Readout_Done         called by (eval 238) (1)
2019.10.09 20:18:20 1:     (eval)                              called by fhem.pl (1135)
2019.10.09 20:18:20 1:     main::AnalyzePerlCommand            called by fhem.pl (1160)
2019.10.09 20:18:20 1:     main::AnalyzeCommand                called by fhem.pl (1089)
2019.10.09 20:18:20 1:     main::AnalyzeCommandChain           called by ./FHEM/98_telnet.pm (255)
2019.10.09 20:18:20 1:     main::telnet_Read                   called by fhem.pl (3754)
2019.10.09 20:18:20 1:     main::CallFn                        called by fhem.pl (748)
2019.10.09 20:18:20 3: FritzBox sip_connect_check: 0 defined sips detected
2019.10.09 20:18:20 3: FritzBox sip_connect_check: 0 sips registered
2019.10.09 20:18:20 3: FritzBox sip_connect_check: 0 sips in error
2019.10.09 20:18:20 3: FritzBox sip_connect_check: end


Hier meine 99_myUtils.pm
##############################################
# $Id: myUtilsTemplate.pm 7570 2015-01-14 18:31:44Z rudolfkoenig $
#
# Save this file as 99_myUtils.pm, and create your own functions in the new
# file. They are then available in every Perl expression.

package main;

use strict;
use warnings;
use POSIX;

sub
myUtils_Initialize($$)
{
  my ($hash) = @_;
}

# Enter you functions below _this_ line.

####### Netzwerk Gerät über Fritzbox abfragen ############
sub
NetDevDa($)
{
    my $n = 0;
    my ($Reading) = @_;
    $Reading =~ tr/:/_/;
    my @fbs = devspec2array("TYPE=FRITZBOX");
       foreach( @fbs ) {
          my $Name = ReadingsVal($_,"mac_" . $Reading,"");
          if( ($Name ne "") && ($Name ne "inactive") ) {
              $n++;
          }
       }
    if( $n == 0) {
        return 0;
    } else {
        return 1;
    }
}
1;
# [http://www.somacon.com/p114.php http://www.somacon.com/p114.php]
# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
  my $string = shift;
  $string =~ s/^\s+//;
  $string =~ s/\s+$//;
  return $string;
}
# Left trim function to remove leading whitespace
sub ltrim($)
{
  my $string = shift;
  $string =~ s/^\s+//;
  return $string;
}
# Right trim function to remove trailing whitespace
sub rtrim($)
{
  my $string = shift;
  $string =~ s/\s+$//;
  return $string;
}
sub sip_connect_check($) {

# Doc
# target : Check max 20 sip accounts for sip in error ( no registration )
# returns the amount of unregistrated sips
#
# displayname from sips in error will be shown in log (loglevel 2)
# start and stop information will be shown in log with loglevel 3
# debugging information will be shown in log with loglevel 5
#
# used modul : FRITZBOX - see reference in https://wiki.fhem.de/wiki/FRITZBOX
# used hardware : Fritz!Box 7490
#
# activated = 0 => sip ist definiert aber Haken "Internetrufnummer verwenden" ist nicht gesetzt
# activated = 1 => sip ist definiert und Haken "Internetrufnummer verwenden" ist gesetzt
#
# connect = 0 => sip nicht activated und (somit) nicht registriert
# connect = 1 => sip activated aber nicht registriert
# connect = 2 => sip activated und registriert
#
# some samples for luaQuery commands :
#  <device>  must be set to devicename of the FritzBox
#  <sip>     must be set to the sip-number as defined within the FritzBox  e.g. sip0
# get <device> luaQuery sip:settings/<sip>/connect
# get <device> luaQuery sip:settings/<sip>/displayname
# get <device> luaQuery sip:settings/<sip>/activated
# get <device> luaQuery sip:settings/<sip>/registrar
# get <device> luaQuery sip:status/sip/list(activated,connect,displayname,registrar)

my($name) = @_;
my$sub_name = "sip_connect_check";
my$sip_count = 0;
my$sip_register = 0;
my$sip_in_error = 0;
my$sip_raw = "nix";
my$laenge = 0;
my$connect = 99;
my$display_name = "nix";

my$index = 0;
my$sip_max = 20;

Log3 $name, 3, "$name $sub_name: start";

for($index = 0;$index <= $sip_max;$index++) {
$sip_raw=fhem("get $name luaQuery sip:settings/sip$index/connect",1);
$laenge = length($sip_raw);
$connect=substr($sip_raw,$laenge-1,1);
$connect =~/([0-9])$/;

if ($1 eq "") {
$index=$sip_max;
next;
} else {
$sip_count++;
};

# debugging
Log3 $name, 5, "$name $sub_name: sip_count = $sip_count connect = $connect";
Log3 $name, 5, "$name $sub_name: sip_count = $sip_count laenge = $laenge";
Log3 $name, 5, "$name $sub_name: sip_count = $sip_count Dollar1 = $1";

$sip_raw=fhem("get $name luaQuery sip:settings/sip$index/displayname",1);
$laenge = length($sip_raw);
$display_name=substr($sip_raw,$laenge-20,20);
$display_name =~/([0-9]{0,20})$/;

if ($connect == 2) { # sip activated und registriert
Log3 $name, 4, "$name $sub_name: sip $1 registration ok";
$sip_register++;
};
if ($connect == 0) { # sip not activated
Log3 $name, 4, "$name $sub_name: sip $1 not aktivated";
};
if ($connect == 1) { # error condition for aktivated and unregistrated sips
$sip_in_error++;
Log3 $name, 2, "$name $sub_name: sip $1 connect error $connect - no registration";

# debugging
Log3 $name, 5, "$name $sub_name: sip_count = $sip_count sip_in_error = $sip_in_error";
Log3 $name, 5, "$name $sub_name: sip_count = $sip_count displayname = $display_name";
Log3 $name, 5, "$name $sub_name: sip_count = $sip_count Dollar1 = $1";
};

};
# debugging
Log3 $name, 3, "$name $sub_name: $sip_count defined sips detected";
Log3 $name, 3, "$name $sub_name: $sip_register sips registered";
Log3 $name, 3, "$name $sub_name: $sip_in_error sips in error";

Log3 $name, 3, "$name $sub_name: end";
return $sip_count;
};


Wenn ich die Fehlermeldung richtig teut, vermute ich dass $1 nicht definiert ist

Schönen Gruß

Matthias

JoWiemann

Hast Du das Attribut allowTR064Command gesetzt?

Grüße Jörg


Gesendet von iPad mit Tapatalk
Jörg Wiemann

Slave: RPi B+ mit 512 MB, COC (868 MHz), CUL V3 (433.92MHz SlowRF); FHEMduino, Aktuelles FHEM

Master: CubieTruck; Debian; Aktuelles FHEM

pwlr

moin,

allowTR064Command wird wirklich benötigt !

meine Attr:
Attributes:
   DbLogInclude box_connect
   INTERVAL   120
   allowShellCommand 1
   allowTR064Command 1


Diese $1 Variable ist in der Tat nicht explizit in der sub definiert. Ich glaube, es ist eine temporäre Variable.... Ich nix Ahnung, wo die herkommt. Vieleicht kann uns ein Perl-Spezi mal aufklären - würde mich freuen.

Mein Log:
2019.10.10 00:48:33 3: FRITZBOX: set my_FB update
2019.10.10 00:48:35 3: my_FB sip_connect_check: start
2019.10.10 00:48:35 3: FRITZBOX: get my_FB luaQuery sip:settings/sip0/connect
2019.10.10 00:48:35 3: FRITZBOX: get my_FB luaQuery sip:settings/sip0/displayname
2019.10.10 00:48:35 3: FRITZBOX: get my_FB luaQuery sip:settings/sip1/connect
2019.10.10 00:48:36 3: FRITZBOX: get my_FB luaQuery sip:settings/sip1/displayname
2019.10.10 00:48:36 3: FRITZBOX: get my_FB luaQuery sip:settings/sip2/connect
2019.10.10 00:48:36 3: FRITZBOX: get my_FB luaQuery sip:settings/sip2/displayname
2019.10.10 00:48:37 3: FRITZBOX: get my_FB luaQuery sip:settings/sip3/connect
2019.10.10 00:48:37 3: FRITZBOX: get my_FB luaQuery sip:settings/sip3/displayname
2019.10.10 00:48:37 3: FRITZBOX: get my_FB luaQuery sip:settings/sip4/connect
2019.10.10 00:48:38 3: FRITZBOX: get my_FB luaQuery sip:settings/sip4/displayname
2019.10.10 00:48:38 3: FRITZBOX: get my_FB luaQuery sip:settings/sip5/connect
2019.10.10 00:48:38 3: FRITZBOX: get my_FB luaQuery sip:settings/sip5/displayname
2019.10.10 00:48:39 3: FRITZBOX: get my_FB luaQuery sip:settings/sip6/connect
2019.10.10 00:48:39 3: my_FB sip_connect_check: 6 defined sips detected
2019.10.10 00:48:39 3: my_FB sip_connect_check: 5 sips registered
2019.10.10 00:48:39 3: my_FB sip_connect_check: 0 sips in error
2019.10.10 00:48:39 3: my_FB sip_connect_check: end


wenn Du my_FB in Deinen FB-Namen änderst, kannst Du die get-Befehle auch in der Commandzeile eingeben.

Moin und viel Erfolg
Bernd




JoWiemann

Die Perl Variablen $1, $2, ... werden z.B. durch RegEx Funktionen mit Inhalt gefüllt, wenn diese etwas finden. Hier ist die RegEx nicht erfolgreich, weil ohne das Attribut keine Rückgabe, die durchsucht werden kann, erfolgt. Das kann man Programmtechnisch abfangen.

Grüße Jörg


Gesendet von iPhone mit Tapatalk
Jörg Wiemann

Slave: RPi B+ mit 512 MB, COC (868 MHz), CUL V3 (433.92MHz SlowRF); FHEMduino, Aktuelles FHEM

Master: CubieTruck; Debian; Aktuelles FHEM

JoWiemann

#19
Hallo,

ich habe den Code um eine Fehlerbehandlung für fehlendes allowTR064Command erweitert.


sub sip_connect_check($) {

# Doc
# target : Check max 20 sip accounts for sip in error ( no registration )
# returns the amount of unregistrated sips or (-1) if allowTR064Command not defined or (-2) if error on calling TR064
#
# displayname from sips in error will be shown in log (loglevel 2)
# start and stop information will be shown in log with loglevel 3
# debugging information will be shown in log with loglevel 5
#
# used modul : FRITZBOX - see reference in https://wiki.fhem.de/wiki/FRITZBOX
# used hardware : Fritz!Box 7490
#
# activated = 0 => sip ist definiert aber Haken "Internetrufnummer verwenden" ist nicht gesetzt
# activated = 1 => sip ist definiert und Haken "Internetrufnummer verwenden" ist gesetzt
#
# connect = 0 => sip nicht activated und (somit) nicht registriert
# connect = 1 => sip activated aber nicht registriert
# connect = 2 => sip activated und registriert
#
# some samples for luaQuery commands :
#  <device>  must be set to devicename of the FritzBox
#  <sip>     must be set to the sip-number as defined within the FritzBox  e.g. sip0
# get <device> luaQuery sip:settings/<sip>/connect
# get <device> luaQuery sip:settings/<sip>/displayname
# get <device> luaQuery sip:settings/<sip>/activated
# get <device> luaQuery sip:settings/<sip>/registrar
# get <device> luaQuery sip:status/sip/list(activated,connect,displayname,registrar)

my($name) = @_;
my $sub_name = "sip_connect_check";
my $sip_count = 0;
my $sip_in_error = 0;
my $sip_raw = "nix";
my $laenge = 0;
my $connect = 99;
my $display_name = "nix";

my $index = 0;
my $sip_max = 20;

Log3 $name, 3, "$name $sub_name: start";
my $TestAttr = AttrVal($name, "allowTR064Command", -1);

if($TestAttr <= 0) {
Log3 $name, 2, "$name $sub_name: attr allowTR064Command not defined or define with 0";
return (-1);
}


for($index = 0; $index <= $sip_max; $index++) {
$sip_raw =  fhem("get $name luaQuery sip:settings/sip$index/connect", 1);

        # Log3 $name, 3, "$name $sub_name: $sip_raw";

$laenge  =  length($sip_raw);
$connect =  substr($sip_raw,$laenge-1,1);
if($connect =~ /([0-9])$/) {
if ($1 eq "") {
$index=$sip_max;
next;
} else {
$sip_count++;
};
} else {
Log3 $name, 2, "$name $sub_name: error while calling TR064";
return (-2);
};


# debugging
Log3 $name, 5, "$name $sub_name: sip_count = $sip_count connect = $connect";
Log3 $name, 5, "$name $sub_name: sip_count = $sip_count laenge = $laenge";
Log3 $name, 5, "$name $sub_name: sip_count = $sip_count Dollar1 = $1";

if ($connect == 1) { # error condition for unregistrated sips
$sip_in_error++;
$sip_raw=fhem("get $name luaQuery sip:settings/sip$index/displayname",1);
$laenge = length($sip_raw);
$display_name=substr($sip_raw,$laenge-20,20);
$display_name =~/([0-9]{0,20})$/;
Log3 $name, 2, "$name $sub_name: sip $1 connect error $connect - no registration";

# debugging
Log3 $name, 5, "$name $sub_name: sip_count = $sip_count sip_in_error = $sip_in_error";
Log3 $name, 5, "$name $sub_name: sip_count = $sip_count displayname = $display_name";
Log3 $name, 5, "$name $sub_name: sip_count = $sip_count Dollar1 = $1";
};

};
Log3 $name, 3, "$name $sub_name: end";
return $sip_in_error;
};


Grüße Jörg
Jörg Wiemann

Slave: RPi B+ mit 512 MB, COC (868 MHz), CUL V3 (433.92MHz SlowRF); FHEMduino, Aktuelles FHEM

Master: CubieTruck; Debian; Aktuelles FHEM

pwlr

Moin Jörg,

erstmal vielen Dank für Deine Erklärung zum $1 ! Ist also das Ergebnis aus
$connect =~/([0-9])$/; Jetzt versteh ich auch meine eigene sub wieder  :)

Danke auch für Deine Ergänzung, das ist sicherlich sehr hilfreich.
Allerdings habe ich Probleme mit dem 2. Check, weil immer die Fehlermeldung "$name $sub_name: error while calling TR064" am Ende der Liste kommt und die sub mit -2 ended.
Grund ist m.E. die Endebedingung der Liste mit if ($1 eq ""), diese Bedingung kann aber niemals durch die regEx
if($connect =~ /([0-9])$/) { usw...
erfüllt werden.

Zum Erkennen eines Laufzeitfehlers müsste man den tatsächlichen Fehlercode beim Aufruf kennen, der dann wohl in $sip_raw stehen müsste. Hast Du da Infos oder nen Link auf eine Doku ? Ich würde Deine Idee gern weiter verfolgen.

Hier der geänderte Code Deiner Erweiterung. Den Loglevel der letzten Infos habe ich wieder auf 5 gesetzt.

sub sip_connect_check($) {

# Doc
# Version 02 with good extensions by JoWiemann - thx
# target : Check max 20 sip accounts for sip in error ( no registration )
# returns :
# -> the amount of unregistrated sips
# -> (-1) if attr allowTR064Command not defined
# -> (-2) if error on calling TR064 occurs
#
# displayname from sips in error will be shown in log with verbose >= 2
# error conditions while calling TR64Command will be shown in log with verbose >= 2
# start and stop information will be shown in log with verbose >= 3
# debugging information will be shown in log with verbose >= 5
#
# used modul : FRITZBOX - see reference in https://wiki.fhem.de/wiki/FRITZBOX
# used hardware : Fritz!Box 7490
# used attr : allowTR064Command 1
#
# activated = 0 => sip ist definiert aber Haken "Internetrufnummer verwenden" ist nicht gesetzt
# activated = 1 => sip ist definiert und Haken "Internetrufnummer verwenden" ist gesetzt
#
# connect = 0 => sip nicht activated und (somit) nicht registriert
# connect = 1 => sip activated aber nicht registriert
# connect = 2 => sip activated und registriert
#
# some samples for luaQuery commands :
#  <device>  must be set to devicename of the FritzBox
#  <sip>     must be set to the sip-number as defined within the FritzBox  e.g. sip0
# get <device> luaQuery sip:settings/<sip>/connect
# get <device> luaQuery sip:settings/<sip>/displayname
# get <device> luaQuery sip:settings/<sip>/activated
# get <device> luaQuery sip:settings/<sip>/registrar
# get <device> luaQuery sip:status/sip/list(activated,connect,displayname,registrar)

my($name) = @_;
my$sub_name = "sip_connect_check";
my$sip_count = 0;
my$sip_register = 0;
my$sip_in_error = 0;
my$sip_raw = "nix";
my$laenge = 0;
my$connect = 99;
my$display_name = "nix";

my$index = 0;
my$sip_max = 20;
my$TestAttr = AttrVal($name, "allowTR064Command", -1);

Log3 $name, 3, "$name $sub_name: start";

if($TestAttr <= 0) {
Log3 $name, 2, "$name $sub_name: attr allowTR064Command not defined or defined with 0";
Log3 $name, 3, "$name $sub_name: abend";
return (-1); #error attr allowTR064Command not defined or set to 0
};

for($index = 0;$index < $sip_max;$index++) {
$sip_raw=fhem("get $name luaQuery sip:settings/sip$index/connect",1);

# hier returncode für Laufzeitfehler abfragen if($sip_raw == ?? oder so etwas in der Art... {
#Log3 $name, 2, "$name $sub_name: error while calling TR064";
#Log3 $name, 2, "$name $sub_name: abend";
#return (-2);
#};

$laenge = length($sip_raw);
$connect=substr($sip_raw,$laenge-1,1);

if($connect =~ /([0-9])$/) {
Log3 $name, 5, "$name $sub_name: sip$index found";
$sip_count++;
} else {
Log3 $name,5, "$name $sub_name: last entry = sip$index not found";
last;
};

# debugging
Log3 $name, 5, "$name $sub_name: sip_count = $sip_count connect = $connect";
Log3 $name, 5, "$name $sub_name: sip_count = $sip_count laenge = $laenge";
Log3 $name, 5, "$name $sub_name: sip_count = $sip_count Dollar1 = $1";

$sip_raw=fhem("get $name luaQuery sip:settings/sip$index/displayname",1);
$laenge = length($sip_raw);
$display_name=substr($sip_raw,$laenge-20,20);
$display_name =~/([0-9]{0,20})$/;

if ($connect == 2) { # sip activated und registriert
Log3 $name, 4, "$name $sub_name: sip $1 registration ok";
$sip_register++;
};
if ($connect == 0) { # sip not activated
Log3 $name, 4, "$name $sub_name: sip $1 not aktivated";
};
if ($connect == 1) { # error condition for aktivated and unregistrated sips
$sip_in_error++;
Log3 $name, 2, "$name $sub_name: sip $1 connect error $connect - no registration";

# debugging
Log3 $name, 5, "$name $sub_name: sip_count = $sip_count sip_in_error = $sip_in_error";
Log3 $name, 5, "$name $sub_name: sip_count = $sip_count displayname = $display_name";
Log3 $name, 5, "$name $sub_name: sip_count = $sip_count Dollar1 = $1";
};

};

# debugging
Log3 $name, 5, "$name $sub_name: $sip_count defined sips detected";
Log3 $name, 5, "$name $sub_name: $sip_register sips registered";
Log3 $name, 5, "$name $sub_name: $sip_in_error sips in error";

Log3 $name, 3, "$name $sub_name: end";
return $sip_in_error;
};


Moin
Bernd


JoWiemann

Hallo Bernd,

das

if ($1 eq "") {
$index=$sip_max;
next;
} else {
$sip_count++;
};


ist nicht von mir. Stand so schon im ersten Post der Sub.

Ich habe zur Absicherung das

if($connect =~ /([0-9])$/) {

dann darüber gelegt.

Grüße Jörg
Jörg Wiemann

Slave: RPi B+ mit 512 MB, COC (868 MHz), CUL V3 (433.92MHz SlowRF); FHEMduino, Aktuelles FHEM

Master: CubieTruck; Debian; Aktuelles FHEM

jackyno.71986

#22
So, heute hatte ich nun endlich mal wieder zeit zum testen,
lag tasächlich am fehlenden allowTR64Command.

Jetzt funktioniert es wie gewünscht.

Allerdings schaffe ich es nicht die Fehlermeldung bezüglich des $1 zu unterbinden. Habe die zwei letzten Codes versucht einmal bekomme ich dann -2 und das andere mal 21 angezeigt antstatt 9

Danke für eure Hilfe.