1
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-05-07 22:29:19 +00:00

74_UnififSwitch: support for disabling ports

git-svn-id: https://svn.fhem.de/fhem/trunk@19925 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
wuehler 2019-07-31 05:48:19 +00:00
parent cac67bd0f1
commit fa29b390bd

View File

@ -34,6 +34,7 @@ use strict;
use warnings; use warnings;
use POSIX; use POSIX;
use JSON qw(decode_json); use JSON qw(decode_json);
use JSON qw(encode_json);
### Forward declarations ####################################################{ ### Forward declarations ####################################################{
sub UnifiSwitch_Initialize($$); sub UnifiSwitch_Initialize($$);
@ -55,7 +56,8 @@ sub UnifiSwitch_Initialize($$) {
$hash->{AttrFn} = "UnifiSwitch_Attr"; $hash->{AttrFn} = "UnifiSwitch_Attr";
$hash->{SetFn} = "UnifiSwitch_Set"; $hash->{SetFn} = "UnifiSwitch_Set";
$hash->{GetFn} = "UnifiSwitch_Get"; $hash->{GetFn} = "UnifiSwitch_Get";
$hash->{AttrList} = $readingFnAttributes; $hash->{AttrList} = "portProfileDisableID ".
$readingFnAttributes;
# TODO: notwendig? # TODO: notwendig?
$hash->{Match} = "^UnifiSwitch"; $hash->{Match} = "^UnifiSwitch";
# TODO ATTR wird nicht übernommen # TODO ATTR wird nicht übernommen
@ -94,8 +96,18 @@ sub UnifiSwitch_Undef($$){
} }
sub UnifiSwitch_Attr(@){ sub UnifiSwitch_Attr(@){
my @a = @_;
my ($cmd,$name,$attr_name,$attr_value) = @_;
my $hash = $defs{$name};
#if($cmd eq "set") {
# if($attr_name eq "portProfileDisableID") {
# }
#}
#elsif($cmd eq "del") {
# if($attr_name eq "portProfileDisableID") {
# }
#}
return undef; return undef;
} }
@ -111,36 +123,34 @@ sub UnifiSwitch_Set($@){
# Log3 $name, 5, "$name: set called with $setName but device is disabled!" if($setName ne "?"); # Log3 $name, 5, "$name: set called with $setName but device is disabled!" if($setName ne "?");
# return undef; # return undef;
#} #}
if($setName !~ /clear|poeMode/) { my $portProfileDisableID=AttrVal($name,"portProfileDisableID",undef);
my $isPortprofileID = undef;
$isPortprofileID ="disablePort" if (defined $portProfileDisableID);
if($setName !~ /clear|poeMode|disablePort/) {
return "Unknown argument $setName, choose one of " return "Unknown argument $setName, choose one of "
."clear:all,readings poeMode "; #TODO: PortNamen sowie die Modes als Auswahl anhängen ."clear:all,readings poeMode ".$isPortprofileID; #TODO: PortNamen sowie die Modes als Auswahl anhängen
} else { } else {
Log3 $name, 4, "$name: set $setName"; Log3 $name, 4, "$name: set $setName";
if ($setName eq 'poeMode') { my $apRef = $hash->{usw};
return "usage: $setName <port> <off|auto|passive|passthrough|restart>" if( !$setVal ); if( $setVal !~ m/\d+/ ) { #falls der Portname angegeben wurde, diesen in eine ID umwandeln.
my $apRef;
my $ap = $hash->{usw};
return "switch has no poe-ports!" if( !$ap->{port_table} );
$apRef = $ap;
if( $setVal !~ m/\d+/ ) {
for my $port (@{$apRef->{port_table}}) { for my $port (@{$apRef->{port_table}}) {
next if( $port->{name} !~ $setVal ); next if( $port->{name} !~ $setVal );
$setVal = $port->{port_idx}; $setVal = $port->{port_idx};
last; last;
} }
} }
return "port-ID musst be numeric" if( $setVal !~ m/\d+/ );
return "port musst be in [1..". scalar @{$apRef->{port_table}} ."] " if( $setVal < 1 || $setVal > scalar @{$apRef->{port_table}} );
return "switch '$apRef->{name}' has no port $setVal" if( !defined(@{$apRef->{port_table}}[$setVal-1] ) );
return "port $setVal of switch '$apRef->{name}' is not poe capable" if( !@{$apRef->{port_table}}[$setVal-1]->{port_poe} );
my $port_overrides = $apRef->{port_overrides}; my $port_overrides = $apRef->{port_overrides};
my $idx; my $idx;
my $i = 0; my $i = 0;
for my $entry (@{$port_overrides}) { for my $entry (@{$port_overrides}) {
if( defined $entry->{port_idx} && ($entry->{port_idx} eq $setVal) ) { if( defined $entry->{port_idx}){
if($entry->{port_idx} eq $setVal) {
$idx = $i; $idx = $i;
last; #last;
}
}
else{
splice(@{$port_overrides},$i,1); #manchmal kommen Einträge ohne ID hinzu. Dies führt bei vielen Overrides zu einem Fehler, da mehr Overrides als Ports vorhanden sein können. Hier wird aufgeräumt.
} }
++$i; ++$i;
} }
@ -148,6 +158,18 @@ sub UnifiSwitch_Set($@){
push @{$port_overrides}, {port_idx => $setVal+0}; push @{$port_overrides}, {port_idx => $setVal+0};
$idx = scalar @{$port_overrides}; $idx = scalar @{$port_overrides};
} }
return "port-ID musst be numeric" if( $setVal !~ m/\d+/ );
return "switch has no ports!" if( !$apRef->{port_table} );
return "port musst be in [1..". scalar @{$apRef->{port_table}} ."] " if( $setVal < 1 || $setVal > scalar @{$apRef->{port_table}} );
return "switch '$apRef->{name}' has no port $setVal" if( !defined(@{$apRef->{port_table}}[$setVal-1] ) );
if( $setName eq 'disablePort' ) {
$port_overrides->[$idx]{portconf_id} = $portProfileDisableID;
IOWrite($hash, "Unifi_DeviceRestJson_Send", $apRef->{device_id}, $port_overrides);
}elsif ($setName eq 'poeMode') {
return "usage: $setName <port> <off|auto|passive|passthrough|restart>" if( !$setVal );
return "port $setVal of switch '$apRef->{name}' is not poe capable" if( !@{$apRef->{port_table}}[$setVal-1]->{port_poe} );
if( $setVal2 eq 'off' ) { if( $setVal2 eq 'off' ) {
$port_overrides->[$idx]{poe_mode} = "off"; $port_overrides->[$idx]{poe_mode} = "off";
@ -171,7 +193,7 @@ sub UnifiSwitch_Set($@){
} elsif( $setVal2 eq 'restart' ) {#TODO: Was wir hier gemacht? Funktioniert das noch? } elsif( $setVal2 eq 'restart' ) {#TODO: Was wir hier gemacht? Funktioniert das noch?
IOWrite($hash, "Unifi_ApJson_Send", $apRef->{device_id}, {cmd => 'power-cycle', mac => $apRef->{mac}, port_idx => $setVal}); IOWrite($hash, "Unifi_ApJson_Send", $apRef->{device_id}, {cmd => 'power-cycle', mac => $apRef->{mac}, port_idx => $setVal});
} else { }else {
return "unknwon poe mode $setVal2"; return "unknwon poe mode $setVal2";
} }
}elsif ($setName eq 'clear') { }elsif ($setName eq 'clear') {
@ -197,8 +219,8 @@ sub UnifiSwitch_Get($@){
Log3 $name, 5, "$name: get called with $getName."; Log3 $name, 5, "$name: get called with $getName.";
} }
if($getName !~ /poeState/) { if($getName !~ /poeState|portOverrides/) {
return "Unknown argument $getName, choose one of poeState"; return "Unknown argument $getName, choose one of poeState:noArg portOverrides:noArg";
} }
elsif ($getName eq 'poeState') { elsif ($getName eq 'poeState') {
my $poeState; my $poeState;
@ -209,12 +231,12 @@ sub UnifiSwitch_Get($@){
$poeState .= "\n" if( $poeState ); $poeState .= "\n" if( $poeState );
$poeState .= sprintf( "%-20s (mac:%-17s, id:%s)\n", $apRef->{name}, $apRef->{mac}, $apRef->{device_id} ); $poeState .= sprintf( "%-20s (mac:%-17s, id:%s)\n", $apRef->{name}, $apRef->{mac}, $apRef->{device_id} );
$poeState .= sprintf( " %2s %-15s", "id", "name" ); $poeState .= sprintf( " %2s %-15s", "id", "name" );
$poeState .= sprintf( " %s %s %-6s %-4s %-10s", "", "on", "mode", "", "class" ); $poeState .= sprintf( " %s %-6s %-4s %s %-10s", " ", "on", "mode", " ", "class" );
$poeState .= "\n"; $poeState .= "\n";
for my $port (@{$apRef->{port_table}}) { for my $port (@{$apRef->{port_table}}) {
#next if( !$port->{port_poe} ); #next if( !$port->{port_poe} );
$poeState .= sprintf( " %2i %-15s", $port->{port_idx}, $port->{name} ); $poeState .= sprintf( " %2i %-15s", $port->{port_idx}, $port->{name} );
$poeState .= sprintf( " %s %s %-6s %-4s %-10s", $port->{poe_caps}, $port->{poe_enable}, $port->{poe_mode}, defined($port->{poe_good})?($port->{poe_good}?"good":""):"", defined($port->{poe_class})?$port->{poe_class}:"" ) if( $port->{port_poe} ); $poeState .= sprintf( " %s %-6s %-4s %s %-10s", $port->{poe_caps}, $port->{poe_enable}, $port->{poe_mode}, defined($port->{poe_good})?($port->{poe_good}?"good":""):"", defined($port->{poe_class})?$port->{poe_class}:"" ) if( $port->{port_poe} );
$poeState .= sprintf( " %5.2fW %5.2fV %5.2fmA", $port->{poe_power}?$port->{poe_power}:0, $port->{poe_voltage}, $port->{poe_current}?$port->{poe_current}:0 ) if( $port->{port_poe} ); $poeState .= sprintf( " %5.2fW %5.2fV %5.2fmA", $port->{poe_power}?$port->{poe_power}:0, $port->{poe_voltage}, $port->{poe_current}?$port->{poe_current}:0 ) if( $port->{port_poe} );
$poeState .= "\n"; $poeState .= "\n";
} }
@ -223,6 +245,29 @@ sub UnifiSwitch_Get($@){
$poeState .= "====================================================\n"; $poeState .= "====================================================\n";
return $poeState; return $poeState;
} }
elsif ($getName eq 'portOverrides') {
my $portOverrides;
my $apRef = $hash->{usw};
next if( $apRef->{type} ne 'usw' );
next if( !$apRef->{port_table} );
next if( $getVal && $getVal ne $apRef->{mac} && $getVal ne $apRef->{device_id} && $apRef->{name} !~ $getVal );
$portOverrides .= "\n" if( $portOverrides );
$portOverrides .= sprintf( "%-20s (mac:%-17s, id:%s)\n", $apRef->{name}, $apRef->{mac}, $apRef->{device_id} );
$portOverrides .= sprintf( " %2s %-15s", "id", "name" );
$portOverrides .= sprintf( " %-10s %-25s", "poe_mode", "PortConfigID" );
$portOverrides .= "\n";
for my $port (@{$apRef->{port_overrides}}) {
if(defined $port->{port_idx}){
$portOverrides .= sprintf( " %2i %-15s", $port->{port_idx}, $port->{name}?$port->{name}:"" );
$portOverrides .= sprintf( " %-10s %-25s", $port->{poe_mode}?$port->{poe_mode}:"", $port->{portconf_id}?$port->{portconf_id}:"");
$portOverrides .= "\n";
}
}
$portOverrides = "====================================================\n". $portOverrides;
$portOverrides .= "====================================================\n";
return $portOverrides;
}
return undef; return undef;
} }
@ -272,6 +317,9 @@ sub UnifiSwitch_Parse($$) {
} }
} }
} }
readingsEndUpdate($hash,1); readingsEndUpdate($hash,1);
} }
Log3 $name, 5, "$name ($self) - return: ".$hash->{NAME}; Log3 $name, 5, "$name ($self) - return: ".$hash->{NAME};
@ -346,6 +394,10 @@ You can use the readings or set features to control your unifi-switch.
<br> <br>
<li><code>set &lt;name&gt; poeMode &lt;port&gt; &lt;off|auto|passive|passthrough|restart&gt;</code><br> <li><code>set &lt;name&gt; poeMode &lt;port&gt; &lt;off|auto|passive|passthrough|restart&gt;</code><br>
Set PoE mode for &lt;port&gt;. </li> Set PoE mode for &lt;port&gt;. </li>
<br>
<li><code>set &lt;name&gt; disablePort &lt;port&gt;</code><br>
Only visible when Attr portProfileDisableID is set.<br>
Set the PortProfile from Attr portProfileDisableID for &lt;port&gt;. </li>
</ul> </ul>
<h4>Get</h4> <h4>Get</h4>
@ -353,9 +405,24 @@ You can use the readings or set features to control your unifi-switch.
<li><code>get &lt;name&gt; poeState</code><br> <li><code>get &lt;name&gt; poeState</code><br>
Show more details about the ports of the switch.</li> Show more details about the ports of the switch.</li>
<br> <br>
<li><code>get &lt;name&gt; portOverrides</code><br>
Show more details about the portOverrides of the switch.</li>
<br>
</ul> </ul>
<h4>Attributes</h4>
<ul>
<li>attr portProfileDisableID<br>
Quick Workaround for special purposes: Must contain the ID off the disable-PortConfig. Then set disablePort can be used.<br>
The ID can be found with get portOverrides.<br>
To enable the port the UnifiController-Software must be used.</li>
<br>
<li><a href="#readingFnAttributes">readingFnAttributes</a></li>
</ul>
<h4>Readings</h4> <h4>Readings</h4>
<ul> <ul>
Note: All readings generate events. You can control this with <a href="#readingFnAttributes">these global attributes</a>. Note: All readings generate events. You can control this with <a href="#readingFnAttributes">these global attributes</a>.