Skip to content

Commit

Permalink
[zway] Remove org.apache.commons.net.util.SubnetUtils (openhab#17410)
Browse files Browse the repository at this point in the history
* Remove apache

Signed-off-by: Leo Siepel <[email protected]>
  • Loading branch information
lsiepel authored Nov 17, 2024
1 parent 4a5aa04 commit fe91fc7
Showing 1 changed file with 7 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.net.URL;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.net.util.SubnetUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.zway.internal.ZWayBindingConstants;
import org.openhab.core.config.discovery.AbstractDiscoveryService;
import org.openhab.core.config.discovery.DiscoveryResult;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.config.discovery.DiscoveryService;
import org.openhab.core.net.NetUtil;
import org.openhab.core.thing.ThingUID;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
Expand All @@ -56,36 +54,11 @@ public ZWayBridgeDiscoveryService() {
private void scan() {
logger.debug("Starting scan for Z-Way Server");

ValidateIPV4 validator = new ValidateIPV4();
List<InetAddress> addressesToScan = NetUtil.getFullRangeOfAddressesToScan();
logger.debug("Performing discovery on {} ip addresses", addressesToScan.size());

try {
Enumeration<NetworkInterface> enumNetworkInterface = NetworkInterface.getNetworkInterfaces();
while (enumNetworkInterface.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterface.nextElement();
if (networkInterface.isUp() && !networkInterface.isVirtual() && !networkInterface.isLoopback()) {
for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
if (validator.isValidIPV4(address.getAddress().getHostAddress())) {
String ipAddress = address.getAddress().getHostAddress();
Short prefix = address.getNetworkPrefixLength();

logger.debug("Scan IP address for Z-Way Server: {}", ipAddress);

// Search on localhost first
scheduler.execute(new ZWayServerScan(ipAddress));

String subnet = ipAddress + "/" + prefix;
SubnetUtils utils = new SubnetUtils(subnet);
String[] addresses = utils.getInfo().getAllAddresses();

for (String addressInSubnet : addresses) {
scheduler.execute(new ZWayServerScan(addressInSubnet));
}
}
}
}
}
} catch (SocketException e) {
logger.warn("Error occurred while searching Z-Way servers ({})", e.getMessage());
for (final InetAddress address : addressesToScan) {
scheduler.execute(new ZWayServerScan(address.getHostAddress()));
}
}

Expand Down

0 comments on commit fe91fc7

Please sign in to comment.