2222import java .util .Enumeration ;
2323import java .util .List ;
2424import java .util .Locale ;
25+ import java .util .Objects ;
2526import java .util .stream .Stream ;
2627
2728import org .eclipse .jdt .annotation .NonNullByDefault ;
3031import org .openhab .core .config .core .ParameterOption ;
3132import org .openhab .core .net .CidrAddress ;
3233import org .openhab .core .net .NetUtil ;
34+ import org .openhab .core .util .StringUtils ;
3335import org .osgi .service .component .annotations .Component ;
3436
3537/**
@@ -67,8 +69,10 @@ public class NetworkConfigOptionProvider implements ConfigOptionProvider {
6769 Enumeration <NetworkInterface > networkInterfaces = NetworkInterface .getNetworkInterfaces ();
6870 while (networkInterfaces .hasMoreElements ()) {
6971 NetworkInterface networkInterface = networkInterfaces .nextElement ();
70- options .add (new ParameterOption (networkInterface .getName (),
71- getNetworkInterfaceLabel (networkInterface )));
72+ if (networkInterface .isUp ()) {
73+ options .add (new ParameterOption (networkInterface .getName (),
74+ getNetworkInterfaceLabel (networkInterface )));
75+ }
7276 }
7377 return options ;
7478 } catch (SocketException e ) {
@@ -80,18 +84,19 @@ public class NetworkConfigOptionProvider implements ConfigOptionProvider {
8084 }
8185
8286 private String getNetworkInterfaceLabel (NetworkInterface networkInterface ) {
83- Enumeration <InetAddress > inetAddresses = networkInterface .getInetAddresses ();
84- String hostName = null ;
85- while (inetAddresses .hasMoreElements ()) {
86- InetAddress inetAddress = inetAddresses .nextElement ();
87- if (inetAddress instanceof Inet4Address ) {
88- hostName = inetAddress .getHostName ();
89- break ;
90- } else if (hostName == null ) {
91- hostName = inetAddress .getHostName ();
92- }
87+ StringBuilder result = new StringBuilder (Objects
88+ .requireNonNull (StringUtils .capitalizeByWhitespace (networkInterface .getName ().replace ('_' , ' ' ))));
89+
90+ // Sort IPv4 before IPv6
91+ List <InetAddress > addresses = networkInterface .inetAddresses ().sorted ((ia1 , ia2 ) -> {
92+ return (ia1 instanceof Inet4Address ) == (ia2 instanceof Inet4Address ) ? 0
93+ : ia1 instanceof Inet4Address ? -1 : 1 ;
94+ }).toList ();
95+
96+ if (!addresses .isEmpty ()) {
97+ result .append (" (" ).append (addresses .get (0 ).getHostAddress ()).append (')' );
9398 }
94- return hostName == null ? networkInterface . getName ()
95- : String . format ( "%s (%s)" , networkInterface . getName (), hostName );
99+
100+ return result . toString ( );
96101 }
97102}
0 commit comments