2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.pjlinkdevice.internal.discovery;
15 import java.io.IOException;
16 import java.net.Inet4Address;
17 import java.net.InetAddress;
18 import java.net.InterfaceAddress;
19 import java.net.UnknownHostException;
20 import java.util.HashMap;
24 import org.apache.commons.net.util.SubnetUtils;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.openhab.binding.pjlinkdevice.internal.PJLinkDeviceBindingConstants;
27 import org.openhab.binding.pjlinkdevice.internal.device.PJLinkDevice;
28 import org.openhab.binding.pjlinkdevice.internal.device.command.AuthenticationException;
29 import org.openhab.binding.pjlinkdevice.internal.device.command.ResponseException;
30 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
31 import org.openhab.core.config.discovery.DiscoveryService;
32 import org.osgi.service.component.annotations.Component;
35 * Implementation of {@link AbstractDiscoveryParticipant} for IPv4 address ranges and PJLink Class 1 devices.
37 * @author Nils Schnabel - Initial contribution
40 @Component(service = DiscoveryService.class, configurationPid = "org.openhab.binding.pjlinkdevice.internal.discovery.DiscoveryParticipantClass1")
42 public class DiscoveryParticipantClass1 extends AbstractDiscoveryParticipant {
43 public DiscoveryParticipantClass1() throws IllegalArgumentException {
44 super(Set.of(PJLinkDeviceBindingConstants.THING_TYPE_PJLINK), 60, true);
46 logger.trace("PJLinkProjectorDiscoveryParticipant constructor");
50 protected void collectAddressesToScan(Set<InetAddress> addressesToScan, InterfaceAddress i) {
52 if (!(i.getAddress() instanceof Inet4Address)) {
55 // only scan Class C networks
56 if (i.getNetworkPrefixLength() < 24) {
60 SubnetUtils utils = new SubnetUtils(i.getAddress().getHostAddress() + "/" + i.getNetworkPrefixLength());
61 for (String addressToScan : utils.getInfo().getAllAddresses()) {
63 logger.debug("Add address to scan: {}", addressToScan);
64 addressesToScan.add(InetAddress.getByName(addressToScan));
65 } catch (UnknownHostException e) {
66 logger.debug("Unknown Host", e);
72 protected void checkAddress(InetAddress ip, int tcpPort, int timeout) {
73 PJLinkDevice device = new PJLinkDevice(tcpPort, ip, null, timeout);
75 Map<String, Object> properties = new HashMap<>();
76 properties.put(PJLinkDeviceBindingConstants.PARAMETER_HOSTNAME, ip.getHostAddress());
77 properties.put(PJLinkDeviceBindingConstants.PARAMETER_PORT, tcpPort);
78 String description = "Unknown PJLink Device";
80 device.checkAvailability();
83 description = device.getFullDescription();
84 logger.debug("got name {}", description);
85 } catch (ResponseException e) {
86 logger.debug("Could not find a name for PJLink device", e);
89 } catch (AuthenticationException e) {
90 properties.put(PJLinkDeviceBindingConstants.PROPERTY_AUTHENTICATION_REQUIRED, true);
92 logger.debug("Adding thing");
93 thingDiscovered(DiscoveryResultBuilder.create(createServiceUID(ip.getHostAddress(), tcpPort))
94 .withTTL(PJLinkDeviceBindingConstants.DISCOVERY_RESULT_TTL_SECONDS).withProperties(properties)
95 .withLabel(description).build());
96 logger.debug("Added thing");
97 } catch (ResponseException | IOException e) {
98 logger.debug("No PJLinkDevice here {} {}", ip, e.getStackTrace());