diff --git a/fhem/FHEM/10_OWServer.pm b/fhem/FHEM/10_OWServer.pm index 96ab10998..fb060081d 100644 --- a/fhem/FHEM/10_OWServer.pm +++ b/fhem/FHEM/10_OWServer.pm @@ -189,7 +189,7 @@ OWServer_DoInit($) my $name = $hash->{NAME}; $hash->{STATE} = "Initialized" if(!$hash->{STATE}); - OWServer_Autodiscovery($hash) if($init_done); + OWServer_Autocreate($hash) if($init_done); return undef; } @@ -224,47 +224,64 @@ OWServer_Dir($@) } ##################################### -# TODO: codereview - es war zu spät... sub -OWServer_Autodiscovery($) +OWServer_Autocreate($) { my ($hash)= @_; my $name = $hash->{NAME}; - - return undef; + foreach my $d (keys %defs) { + next if($defs{$d}{TYPE} ne "autocreate"); + return undef if(AttrVal($defs{$d}{NAME},"disable",undef)); + } + my $owserver= $hash->{fhem}{owserver}; my @dir= split(",", $owserver->dir()); my @devices= grep { m/^\/[0-9a-f]{2}.[0-9a-f]{12}$/i } @dir; + + my @defined = (); + foreach my $d (keys %defs) { + next if($defs{$d}{TYPE} ne "OWDevice"); + if(defined($defs{$d}{fhem}) && defined($defs{$d}{fhem}{address})) { + push(@defined,$defs{$d}{fhem}{address}); + } + } + for my $device (@devices) { my $address= substr($device,1); my $family= substr($address,0,2); if(!defined($owfamily{$family})) { + Log 2, "$name: Autocreate: unknown familycode '$family' found. Please report this!"; next; } else { my $type= $owserver->read($device . "/type"); if($type !~ m/$owfamily{$family}/) { - Log 2, "$name: Autodiscovery: type '$type' unknown"; + Log 2, "$name: Autocreate: type '$type' not defined in familycode '$family'. Please report this!"; next; } else { foreach my $d (keys %defs) { - next if($defs{$d}{TYPE} eq "OWDevice" && - defined($defs{$d}{fhem}) && - defined($defs{$d}{fhem}{address}) && $defs{$d}{fhem}{address} eq $address); - my $id= substr($address,3); - my $devname= $type . "_" . $id; - if(defined($defs{$devname})) { + next if($defs{$d}{TYPE} ne "OWDevice"); + if(defined($defs{$d}{fhem}) && + defined($defs{$d}{fhem}{address}) && $defs{$d}{fhem}{address} eq $address) { + Log 5, "$name address '$address' already defined as '$defs{$d}{NAME}'"; next; } else { - my $interval= ($family eq "81") ? "" : " 60"; - my $define= "$devname OWDevice $address" . $interval; - my $cmdret; - $cmdret= CommandDefine(undef,$define); - if($cmdret) { - Log 1, "$name: An error occurred while creating device for address '$address': $cmdret"; + my $id= substr($address,3); + my $devname= $type . "_" . $id; + if(defined($defs{$devname}) || grep {$_ eq $address} @defined) { + next; } else { - $cmdret= CommandAttr(undef,"$devname room OWDevice"); + Log 5, "$name create new device '$devname' for address '$address'"; + my $interval= ($family eq "81") ? "" : " 60"; + my $define= "$devname OWDevice $address" . $interval; + my $cmdret; + $cmdret= CommandDefine(undef,$define); + if($cmdret) { + Log 1, "$name: Autocreate: An error occurred while creating device for address '$address': $cmdret"; + } else { + $cmdret= CommandAttr(undef,"$devname room OWDevice"); + } } } } @@ -352,11 +369,12 @@ OWServer_Set($@) format <hostname>:<port>. For details see owserver documentation.

- You need OWNet.pm from owfs.org. Just drop it into your FHEM - folder alongside the 10_OWServer.pm module. As at 2012-12-23 the OWNet module + You need OWNet.pm from owfs.org, which is normally deployed with FHEM. As at 2012-12-23 the OWNet module on CPAN has an issue which renders it useless for remote connections.

The actual 1-wire devices are defined as OWDevice devices. + If autocreate is enabled, all the devices found are created at + start of FHEM automatically.

This module is completely unrelated to the 1-wire modules with names all in uppercase.