Skip to content

Commit 76eeed3

Browse files
author
Ravi Nadahar
committed
Fix network interface configuration option enumeration
Signed-off-by: Ravi Nadahar <nadahar@rediffmail.com>
1 parent b7c742f commit 76eeed3

1 file changed

Lines changed: 19 additions & 14 deletions

File tree

bundles/org.openhab.core.config.core/src/main/java/org/openhab/core/config/core/internal/net/NetworkConfigOptionProvider.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Enumeration;
2323
import java.util.List;
2424
import java.util.Locale;
25+
import java.util.Objects;
2526
import java.util.stream.Stream;
2627

2728
import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -30,6 +31,7 @@
3031
import org.openhab.core.config.core.ParameterOption;
3132
import org.openhab.core.net.CidrAddress;
3233
import org.openhab.core.net.NetUtil;
34+
import org.openhab.core.util.StringUtils;
3335
import 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

Comments
 (0)