2 * Copyright (c) 2010-2021 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.orvibo.internal.discovery;
15 import java.net.SocketException;
16 import java.util.Collections;
20 import org.apache.commons.lang.StringUtils;
21 import org.openhab.binding.orvibo.internal.OrviboBindingConstants;
22 import org.openhab.core.config.discovery.AbstractDiscoveryService;
23 import org.openhab.core.config.discovery.DiscoveryResult;
24 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
25 import org.openhab.core.config.discovery.DiscoveryService;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.osgi.service.component.annotations.Component;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import com.github.tavalin.s20.S20Client;
33 import com.github.tavalin.s20.S20Client.SocketDiscoveryListener;
34 import com.github.tavalin.s20.Socket;
37 * The {@link SocketDiscoveryService} class defines a service used
38 * to discover S20 sockets on the local netowork.
40 * @author Daniel Walters - Initial contribution
42 @Component(service = DiscoveryService.class, configurationPid = "discovery.orvibo")
43 public class SocketDiscoveryService extends AbstractDiscoveryService implements SocketDiscoveryListener {
45 private final Logger logger = LoggerFactory.getLogger(SocketDiscoveryService.class);
46 private static final int SEARCH_TIME = 60;
47 private S20Client s20Client;
49 public SocketDiscoveryService() throws SocketException {
50 super(getSupportedThingTypeUIDs(), SEARCH_TIME);
53 private static Set<ThingTypeUID> getSupportedThingTypeUIDs() {
54 return Collections.singleton(OrviboBindingConstants.THING_TYPE_S20);
58 protected void activate(Map<String, Object> configProperties) {
60 s20Client = S20Client.getInstance();
61 super.activate(configProperties);
62 } catch (SocketException ex) {
63 logger.error("Error occurred while activating S20 discovery service: {}", ex.getMessage(), ex);
68 protected void startScan() {
69 if (s20Client != null) {
70 logger.debug("starting manual scan");
71 s20Client.addSocketDiscoveryListener(this);
72 s20Client.globalDiscovery();
73 for (final Socket socket : s20Client.getAllSockets().values()) {
74 doThingDiscovered(socket);
77 logger.debug("Client not initialised");
82 protected void startBackgroundDiscovery() {
83 if (s20Client != null) {
84 logger.debug("starting automatic background scan");
85 s20Client.addSocketDiscoveryListener(this);
86 s20Client.globalDiscovery();
88 logger.debug("Client not initialised");
93 protected void stopBackgroundDiscovery() {
94 s20Client.removeSocketDiscoveryListener(this);
98 public void socketDiscovered(Socket socket) {
99 doThingDiscovered(socket);
102 private DiscoveryResult createDiscoveryResult(Socket socket) {
103 ThingUID thingUID = getUID(socket);
104 String label = socket.getLabel();
105 if (StringUtils.isBlank(label)) {
108 return DiscoveryResultBuilder.create(thingUID).withLabel(label)
109 .withProperty(OrviboBindingConstants.CONFIG_PROPERTY_DEVICE_ID, socket.getDeviceId()).build();
112 private ThingUID getUID(Socket socket) {
113 ThingUID thingUID = new ThingUID(OrviboBindingConstants.THING_TYPE_S20, socket.getDeviceId());
117 private void doThingDiscovered(Socket socket) {
118 DiscoveryResult discoveryResult = createDiscoveryResult(socket);
119 thingDiscovered(discoveryResult);