]> git.basschouten.com Git - openhab-addons.git/blob
1bd12323ba0f41218af3e807f1b46ea704bf8ca2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.etherrain.internal.discovery;
14
15 import java.util.Set;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.etherrain.internal.EtherRainBindingConstants;
19 import org.openhab.binding.etherrain.internal.api.EtherRainCommunication;
20 import org.openhab.binding.etherrain.internal.api.EtherRainUdpResponse;
21 import org.openhab.core.config.discovery.AbstractDiscoveryService;
22 import org.openhab.core.config.discovery.DiscoveryResult;
23 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
24 import org.openhab.core.config.discovery.DiscoveryService;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.ThingUID;
27 import org.osgi.service.component.annotations.Component;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * The {@link EtherrainDiscoveryService} class discovers Etherrain Device(s) and places them in the inbox.
33  *
34  * @author Joe Inkenbrandt - Initial contribution
35  */
36 @NonNullByDefault
37 @Component(service = DiscoveryService.class, configurationPid = "discovery.etherrain")
38 public class EtherrainDiscoveryService extends AbstractDiscoveryService {
39
40     private final Logger logger = LoggerFactory.getLogger(EtherrainDiscoveryService.class);
41
42     private static final int TIMEOUT = 15;
43
44     public EtherrainDiscoveryService() {
45         super(EtherRainBindingConstants.SUPPORTED_THING_TYPES_UIDS, TIMEOUT, true);
46     }
47
48     @Override
49     public Set<ThingTypeUID> getSupportedThingTypes() {
50         return EtherRainBindingConstants.SUPPORTED_THING_TYPES_UIDS;
51     }
52
53     @Override
54     protected void startScan() {
55         for (EtherRainUdpResponse rdp : EtherRainCommunication.autoDiscover()) {
56             if (rdp.isValid()) {
57                 ThingUID uid = new ThingUID(EtherRainBindingConstants.ETHERRAIN_THING,
58                         rdp.getAddress().replaceAll("[^A-Za-z0-9\\-_]", ""));
59                 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(uid)
60                         .withLabel("Etherrain " + rdp.getType() + " " + rdp.getUnqiueName())
61                         .withProperty("host", rdp.getAddress()).withProperty("port", rdp.getPort()).build();
62                 thingDiscovered(discoveryResult);
63             } else {
64                 logger.debug("Nothing responded to request");
65             }
66         }
67     }
68 }