]> git.basschouten.com Git - openhab-addons.git/blob
9e8eafd46c503f8df3b61731f847ca2269d55403
[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.orvibo.internal.discovery;
14
15 import java.net.SocketException;
16 import java.util.Collections;
17 import java.util.Map;
18 import java.util.Set;
19
20 import org.openhab.binding.orvibo.internal.OrviboBindingConstants;
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 import com.github.tavalin.s20.S20Client;
32 import com.github.tavalin.s20.S20Client.SocketDiscoveryListener;
33 import com.github.tavalin.s20.Socket;
34
35 /**
36  * The {@link SocketDiscoveryService} class defines a service used
37  * to discover S20 sockets on the local netowork.
38  *
39  * @author Daniel Walters - Initial contribution
40  */
41 @Component(service = DiscoveryService.class, configurationPid = "discovery.orvibo")
42 public class SocketDiscoveryService extends AbstractDiscoveryService implements SocketDiscoveryListener {
43
44     private final Logger logger = LoggerFactory.getLogger(SocketDiscoveryService.class);
45     private static final int SEARCH_TIME = 60;
46     private S20Client s20Client;
47
48     public SocketDiscoveryService() throws SocketException {
49         super(getSupportedThingTypeUIDs(), SEARCH_TIME);
50     }
51
52     private static Set<ThingTypeUID> getSupportedThingTypeUIDs() {
53         return Collections.singleton(OrviboBindingConstants.THING_TYPE_S20);
54     }
55
56     @Override
57     protected void activate(Map<String, Object> configProperties) {
58         try {
59             s20Client = S20Client.getInstance();
60             super.activate(configProperties);
61         } catch (SocketException ex) {
62             logger.error("Error occurred while activating S20 discovery service: {}", ex.getMessage(), ex);
63         }
64     }
65
66     @Override
67     protected void startScan() {
68         if (s20Client != null) {
69             logger.debug("starting manual scan");
70             s20Client.addSocketDiscoveryListener(this);
71             s20Client.globalDiscovery();
72             for (final Socket socket : s20Client.getAllSockets().values()) {
73                 doThingDiscovered(socket);
74             }
75         } else {
76             logger.debug("Client not initialised");
77         }
78     }
79
80     @Override
81     protected void startBackgroundDiscovery() {
82         if (s20Client != null) {
83             logger.debug("starting automatic background scan");
84             s20Client.addSocketDiscoveryListener(this);
85             s20Client.globalDiscovery();
86         } else {
87             logger.debug("Client not initialised");
88         }
89     }
90
91     @Override
92     protected void stopBackgroundDiscovery() {
93         s20Client.removeSocketDiscoveryListener(this);
94     }
95
96     @Override
97     public void socketDiscovered(Socket socket) {
98         doThingDiscovered(socket);
99     }
100
101     private DiscoveryResult createDiscoveryResult(Socket socket) {
102         ThingUID thingUID = getUID(socket);
103         String label = socket.getLabel();
104         if (label == null || label.isBlank()) {
105             label = "S20";
106         }
107         return DiscoveryResultBuilder.create(thingUID).withLabel(label)
108                 .withProperty(OrviboBindingConstants.CONFIG_PROPERTY_DEVICE_ID, socket.getDeviceId()).build();
109     }
110
111     private ThingUID getUID(Socket socket) {
112         ThingUID thingUID = new ThingUID(OrviboBindingConstants.THING_TYPE_S20, socket.getDeviceId());
113         return thingUID;
114     }
115
116     private void doThingDiscovered(Socket socket) {
117         DiscoveryResult discoveryResult = createDiscoveryResult(socket);
118         thingDiscovered(discoveryResult);
119     }
120 }