Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pjlinkdevice] Remove org.apache.common #14430

Merged
merged 10 commits into from
Oct 13, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
package org.openhab.binding.pjlinkdevice.internal.discovery;

import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -27,6 +23,7 @@
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.pjlinkdevice.internal.PJLinkDeviceBindingConstants;
import org.openhab.core.config.discovery.AbstractDiscoveryService;
import org.openhab.core.net.NetUtil;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID;
import org.slf4j.Logger;
Expand Down Expand Up @@ -63,7 +60,7 @@ protected ExecutorService getExecutorService() {
@Override
protected void startScan() {
logger.trace("PJLinkProjectorDiscoveryParticipant startScan");
Set<InetAddress> addressesToScan = generateAddressesToScan();
List<InetAddress> addressesToScan = NetUtil.getFullRangeOfAddressesToScan();
lolodomo marked this conversation as resolved.
Show resolved Hide resolved
scannedIPcount = 0;
for (InetAddress ip : addressesToScan) {
getExecutorService().execute(() -> {
Expand Down Expand Up @@ -106,26 +103,4 @@ public static ThingUID createServiceUID(String ip, int tcpPort) {
}

protected abstract void checkAddress(InetAddress ip, int tcpPort, int timeout);

private Set<InetAddress> generateAddressesToScan() {
try {
Set<InetAddress> addressesToScan = new HashSet<>();
ArrayList<NetworkInterface> interfaces = java.util.Collections
.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface networkInterface : interfaces) {
if (networkInterface.isLoopback() || !networkInterface.isUp()) {
continue;
}
for (InterfaceAddress i : networkInterface.getInterfaceAddresses()) {
collectAddressesToScan(addressesToScan, i);
}
}
return addressesToScan;
} catch (SocketException e) {
logger.debug("Could not enumerate network interfaces", e);
}
return new HashSet<>();
}

protected abstract void collectAddressesToScan(Set<InetAddress> addressesToScan, InterfaceAddress i);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@
package org.openhab.binding.pjlinkdevice.internal.discovery;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.net.util.SubnetUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.pjlinkdevice.internal.PJLinkDeviceBindingConstants;
import org.openhab.binding.pjlinkdevice.internal.device.PJLinkDevice;
Expand All @@ -41,33 +37,11 @@
@NonNullByDefault
public class DiscoveryParticipantClass1 extends AbstractDiscoveryParticipant {
public DiscoveryParticipantClass1() throws IllegalArgumentException {
super(Set.of(PJLinkDeviceBindingConstants.THING_TYPE_PJLINK), 60, true);
super(Collections.singleton(PJLinkDeviceBindingConstants.THING_TYPE_PJLINK), 60, true);

logger.trace("PJLinkProjectorDiscoveryParticipant constructor");
}

@Override
protected void collectAddressesToScan(Set<InetAddress> addressesToScan, InterfaceAddress i) {
// only scan IPv4
if (!(i.getAddress() instanceof Inet4Address)) {
return;
}
// only scan Class C networks
if (i.getNetworkPrefixLength() < 24) {
return;
}

SubnetUtils utils = new SubnetUtils(i.getAddress().getHostAddress() + "/" + i.getNetworkPrefixLength());
for (String addressToScan : utils.getInfo().getAllAddresses()) {
try {
logger.debug("Add address to scan: {}", addressToScan);
addressesToScan.add(InetAddress.getByName(addressToScan));
} catch (UnknownHostException e) {
logger.debug("Unknown Host", e);
}
}
}

@Override
protected void checkAddress(InetAddress ip, int tcpPort, int timeout) {
PJLinkDevice device = new PJLinkDevice(tcpPort, ip, null, timeout);
Expand Down