2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-07 19:04:20 +00:00

00_FBAHA: Compare the id with the stored FBNAME (Forum #40396)

git-svn-id: https://svn.fhem.de/fhem/trunk@9212 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2015-09-07 18:56:00 +00:00
parent 2095e72a20
commit 19567d6070
3 changed files with 91 additions and 12 deletions

View File

@ -28,6 +28,7 @@ FBAHA_Initialize($)
$hash->{UndefFn} = "FBAHA_Undef"; $hash->{UndefFn} = "FBAHA_Undef";
$hash->{ShutdownFn} = "FBAHA_Undef"; $hash->{ShutdownFn} = "FBAHA_Undef";
$hash->{ReadAnswerFn} = "FBAHA_ReadAnswer"; $hash->{ReadAnswerFn} = "FBAHA_ReadAnswer";
$hash->{NotifyFn} = "FBAHA_Notify";
# Normal devices # Normal devices
$hash->{DefFn} = "FBAHA_Define"; $hash->{DefFn} = "FBAHA_Define";
@ -62,6 +63,17 @@ FBAHA_Define($$)
return $ret; return $ret;
} }
#####################################
sub
FBAHA_Notify($$)
{
my ($ntfy, $dev) = @_;
return if($dev->{NAME} ne "global" ||
!grep(m/^INITIALIZED$/, @{$dev->{CHANGED}}));
delete $modules{telnet}{NotifyFn};
FBAHA_reassign($ntfy);
return;
}
##################################### #####################################
sub sub
@ -162,6 +174,7 @@ FBAHA_getDevList($$)
my $data = ""; my $data = "";
for(;;) { for(;;) {
my ($err, $buf) = FBAHA_ReadAnswer($hash, "CONFIG_RSP", "^06"); my ($err, $buf) = FBAHA_ReadAnswer($hash, "CONFIG_RSP", "^06");
last if($err && $err =~ m/Timeout/);
return ($err) if($err); return ($err) if($err);
$data .= substr($buf, 32); $data .= substr($buf, 32);
last if($buf =~ m/^060[23]/); last if($buf =~ m/^060[23]/);
@ -213,6 +226,45 @@ FBAHA_configInd($$)
return @answer; return @answer;
} }
#####################################
# Check all FBDECTs, reorg them if the id has changed and FBNAME is set.
sub
FBAHA_reassign($)
{
my ($me) = @_;
my $myname = $me->{NAME};
my $devList = FBAHA_Get($me, ($myname, "devList"));
my %fbdata;
foreach my $l (split("\n", $devList)) {
next if($l !~ m/NAME:(.*), ID:(.*), (.*), TYPE:(.*) PROP:(.*)/);
if($fbdata{$1}) {
Log 1, "FBAHA: multiple devices are using the same name, wont reorder";
return;
}
$fbdata{$1} = $2;
}
foreach my $sdev (devspec2array("TYPE=FBDECT")) {
my $hash = $defs{$sdev};
my $name = $hash->{NAME};
my $fbname = ReadingsVal($name, "FBNAME", "");
my $fbid = $fbdata{$fbname};
my $oldid = $hash->{id};
next if(!$fbid || $oldid eq $fbid || $hash->{IODev}{NAME} ne $myname);
Log 2, "FBAHA: changing the id of $name/$fbname from $oldid to $fbid";
delete $modules{FBDECT}{defptr}{"$myname:$oldid"};
$modules{FBDECT}{defptr}{"$myname:$fbid"} = $hash;
$hash->{DEF} =~ s/^$myname:$oldid /$myname:$fbid /; # New syntax
$hash->{DEF} =~ s/^$oldid /$myname:$fbid /; # Old Syntax
$hash->{id} = $fbid;
}
return;
}
##################################### #####################################
sub sub
FBAHA_DoInit($) FBAHA_DoInit($)
@ -221,6 +273,7 @@ FBAHA_DoInit($)
my $name = $hash->{NAME}; my $name = $hash->{NAME};
delete $hash->{HANDLE}; # else reregister fails / RELEASE is deadly delete $hash->{HANDLE}; # else reregister fails / RELEASE is deadly
my $ret = FBAHA_Set($hash, ($name, "reregister")); my $ret = FBAHA_Set($hash, ($name, "reregister"));
FBAHA_reassign($hash) if(!$ret && $init_done);
return $ret; return $ret;
} }
@ -314,8 +367,8 @@ FBAHA_ReadAnswer($$$)
if($nfound <= 0) { if($nfound <= 0) {
next if ($! == EAGAIN() || $! == EINTR()); next if ($! == EAGAIN() || $! == EINTR());
my $err = ($! ? $! : "Timeout"); my $err = ($! ? $! : "Timeout");
$hash->{TIMEOUT} = 1; #$hash->{TIMEOUT} = 1;
DevIo_Disconnected($hash); #DevIo_Disconnected($hash);
return("FBAHA_ReadAnswer $arg: $err", undef); return("FBAHA_ReadAnswer $arg: $err", undef);
} }
my $buf = DevIo_SimpleRead($hash); my $buf = DevIo_SimpleRead($hash);
@ -361,8 +414,8 @@ FBAHA_Ready($)
(fritz.box or localhost), and &lt;port&gt; 2002, or (fritz.box or localhost), and &lt;port&gt; 2002, or
UNIX:SEQPACKET:/var/tmp/me_avm_home_external.ctl, the latter only works on UNIX:SEQPACKET:/var/tmp/me_avm_home_external.ctl, the latter only works on
the fritz.box. With FRITZ!OS 5.50 the network port is available, on some the fritz.box. With FRITZ!OS 5.50 the network port is available, on some
Labor variants only the UNIX socket is available. Labor variants only the UNIX socket is available.<br>
<br>
Example: Example:
<ul> <ul>
<code>define fb1 FBAHA fritz.box:2002</code><br> <code>define fb1 FBAHA fritz.box:2002</code><br>
@ -410,6 +463,13 @@ FBAHA_Ready($)
</li> </li>
</ul> </ul>
<br>
As sometimes the FRITZ!Box reassigns the internal id's of the FBDECT devices,
the FBAHA module compares upon connect/reconnect the stored names (FBNAME)
with the current value. This feature will only work, if you assign each
FBDECT device a unique Name in the FRITZ!Box, and excecute the FHEM "get
FBDECTDEVICE devInfo" command, which saves the FBNAME reading.<br>
</ul> </ul>
@ -439,8 +499,9 @@ FBAHA_Ready($)
FRITZ.BOX) und &lt;port&gt; 2002 ist, oder FRITZ.BOX) und &lt;port&gt; 2002 ist, oder
UNIX:SEQPACKET:/var/tmp/me_avm_home_external.ctl, wobei das nur fuer UNIX:SEQPACKET:/var/tmp/me_avm_home_external.ctl, wobei das nur fuer
FHEM@FRITZ!BOX zur Verf&uuml;gung steht. Mit FRITZ!OS 5.50 steht auch der FHEM@FRITZ!BOX zur Verf&uuml;gung steht. Mit FRITZ!OS 5.50 steht auch der
Netzwerkport zur Verf&uuml;gung, auf manchen Laborvarianten nur das UNIX socket. Netzwerkport zur Verf&uuml;gung, auf manchen Laborvarianten nur das UNIX
<br> socket.<br>
Beispiel: Beispiel:
<ul> <ul>
<code>define fb1 FBAHA fritz.box:2002</code><br> <code>define fb1 FBAHA fritz.box:2002</code><br>
@ -491,6 +552,14 @@ FBAHA_Ready($)
</li> </li>
</ul> </ul>
<br>
Da manchmal die FRITZ!Box die interne Nummer der FBDECT Ger&auml;te
neu vergibt, werden beim Verbindungsaufbau zum AHA Server die gespeicherten
Namen (FBNAME) mit dem aktuellen Wert verglichen. Damit das funktioniert,
m&uuml;ssen alle FBDECT Ger&auml;te auf dem FRITZ!Box einen eindeutigen Namen
bekommen, und in FHEM muss f&uuml;r alle Ger&auml;te "get FBDECTDEVICE
devInfo" ausgef&uuml;hrt werden, um FBNAME als Reading zu speichern.<br>
</ul> </ul>
=end html_DE =end html_DE

View File

@ -133,21 +133,29 @@ FBDECT_Get($@)
if($cmd eq "devInfo") { if($cmd eq "devInfo") {
my @answ = FBAHA_getDevList($hash->{IODev}, $hash->{id}); my @answ = FBAHA_getDevList($hash->{IODev}, $hash->{id});
return $answ[0] if(@answ == 1); return $answ[0] if(@answ == 1);
readingsBeginUpdate($hash);
if($answ[0] &&
$answ[0] =~ m/NAME:(.*), ID:(.*), (.*), TYPE:(.*) PROP:(.*)/) {
readingsBulkUpdate($hash, "FBNAME", $1, 1);
readingsBulkUpdate($hash, "FBTYPE", $4, 1);
readingsBulkUpdate($hash, "FBPROP", $5, 1);
}
my $d = pop @answ; my $d = pop @answ;
my $state = "inactive" if($answ[0] =~ m/ inactive,/);
while($d) { while($d) {
my ($ptyp, $plen, $pyld) = FBDECT_decodePayload($d, $hash, 0); my ($ptyp, $plen, $pyld) = FBDECT_decodePayload($d, $hash, 0);
Log3 $hash, 4, "Payload: $d -> $ptyp: $pyld"; Log3 $hash, 4, "Payload: $d -> $ptyp: $pyld";
last if($ptyp eq ""); last if($ptyp eq "");
if($ptyp eq "state" && readingsBulkUpdate($hash, $ptyp, $pyld, 1);
ReadingsVal($hash->{NAME}, $ptyp, "") ne $pyld) {
readingsSingleUpdate($hash, $ptyp, ($state ? $state : $pyld), 1);
}
push @answ, " $ptyp: $pyld"; push @answ, " $ptyp: $pyld";
$d = substr($d, 16+$plen*2); $d = substr($d, 16+$plen*2);
} }
readingsEndUpdate($hash, 1);
return join("\n", @answ); return join("\n", @answ);
} }
return undef; return undef;
} }

View File

@ -353,7 +353,9 @@ HttpUtils_ParseAnswer($$)
my $code= $header0[1]; my $code= $header0[1];
# Close if server doesn't support keepalive # Close if server doesn't support keepalive
HttpUtils_Close($hash) if($hash->{keepalive} and $hash->{httpheader} =~ m/^Connection:\s*close\s*$/mi); HttpUtils_Close($hash)
if($hash->{keepalive} &&
$hash->{httpheader} =~ m/^Connection:\s*close\s*$/mi);
if(!defined($code) || $code eq "") { if(!defined($code) || $code eq "") {
return ("$hash->{displayurl}: empty answer received", ""); return ("$hash->{displayurl}: empty answer received", "");