]> git.basschouten.com Git - openhab-addons.git/blob
642fd5153751c191b7d796c4446aa4084d1b0c0c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.pjlinkdevice.internal.discovery;
14
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;
21 import java.util.Map;
22 import java.util.Set;
23
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;
33
34 /**
35  * Implementation of {@link AbstractDiscoveryParticipant} for IPv4 address ranges and PJLink Class 1 devices.
36  * 
37  * @author Nils Schnabel - Initial contribution
38  */
39
40 @Component(service = DiscoveryService.class, configurationPid = "org.openhab.binding.pjlinkdevice.internal.discovery.DiscoveryParticipantClass1")
41 @NonNullByDefault
42 public class DiscoveryParticipantClass1 extends AbstractDiscoveryParticipant {
43     public DiscoveryParticipantClass1() throws IllegalArgumentException {
44         super(Set.of(PJLinkDeviceBindingConstants.THING_TYPE_PJLINK), 60, true);
45
46         logger.trace("PJLinkProjectorDiscoveryParticipant constructor");
47     }
48
49     @Override
50     protected void collectAddressesToScan(Set<InetAddress> addressesToScan, InterfaceAddress i) {
51         // only scan IPv4
52         if (!(i.getAddress() instanceof Inet4Address)) {
53             return;
54         }
55         // only scan Class C networks
56         if (i.getNetworkPrefixLength() < 24) {
57             return;
58         }
59
60         SubnetUtils utils = new SubnetUtils(i.getAddress().getHostAddress() + "/" + i.getNetworkPrefixLength());
61         for (String addressToScan : utils.getInfo().getAllAddresses()) {
62             try {
63                 logger.debug("Add address to scan: {}", addressToScan);
64                 addressesToScan.add(InetAddress.getByName(addressToScan));
65             } catch (UnknownHostException e) {
66                 logger.debug("Unknown Host", e);
67             }
68         }
69     }
70
71     @Override
72     protected void checkAddress(InetAddress ip, int tcpPort, int timeout) {
73         PJLinkDevice device = new PJLinkDevice(tcpPort, ip, null, timeout);
74         try {
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";
79             try {
80                 device.checkAvailability();
81
82                 try {
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);
87                     // okay, no name
88                 }
89             } catch (AuthenticationException e) {
90                 properties.put(PJLinkDeviceBindingConstants.PROPERTY_AUTHENTICATION_REQUIRED, true);
91             }
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());
99             // no device here
100         }
101     }
102 }