Skip to content

Commit

Permalink
[pjlinkdevice] Remove org.apache.common (openhab#14430)
Browse files Browse the repository at this point in the history
* Remove org.apache.common

Signed-off-by: Leo Siepel <[email protected]>
  • Loading branch information
lsiepel authored and joni1993 committed Oct 15, 2024
1 parent 8d5c5d0 commit 658e226
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 56 deletions.
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();
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

0 comments on commit 658e226

Please sign in to comment.