2 * Copyright (c) 2010-2023 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.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;
31 import com.github.tavalin.s20.S20Client;
32 import com.github.tavalin.s20.S20Client.SocketDiscoveryListener;
33 import com.github.tavalin.s20.Socket;
36 * The {@link SocketDiscoveryService} class defines a service used
37 * to discover S20 sockets on the local netowork.
39 * @author Daniel Walters - Initial contribution
41 @Component(service = DiscoveryService.class, configurationPid = "discovery.orvibo")
42 public class SocketDiscoveryService extends AbstractDiscoveryService implements SocketDiscoveryListener {
44 private final Logger logger = LoggerFactory.getLogger(SocketDiscoveryService.class);
45 private static final int SEARCH_TIME = 60;
46 private S20Client s20Client;
48 public SocketDiscoveryService() throws SocketException {
49 super(getSupportedThingTypeUIDs(), SEARCH_TIME);
52 private static Set<ThingTypeUID> getSupportedThingTypeUIDs() {
53 return Collections.singleton(OrviboBindingConstants.THING_TYPE_S20);
57 protected void activate(Map<String, Object> configProperties) {
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);
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);
76 logger.debug("Client not initialised");
81 protected void startBackgroundDiscovery() {
82 if (s20Client != null) {
83 logger.debug("starting automatic background scan");
84 s20Client.addSocketDiscoveryListener(this);
85 s20Client.globalDiscovery();
87 logger.debug("Client not initialised");
92 protected void stopBackgroundDiscovery() {
93 s20Client.removeSocketDiscoveryListener(this);
97 public void socketDiscovered(Socket socket) {
98 doThingDiscovered(socket);
101 private DiscoveryResult createDiscoveryResult(Socket socket) {
102 ThingUID thingUID = getUID(socket);
103 String label = socket.getLabel();
104 if (label == null || label.isBlank()) {
107 return DiscoveryResultBuilder.create(thingUID).withLabel(label)
108 .withProperty(OrviboBindingConstants.CONFIG_PROPERTY_DEVICE_ID, socket.getDeviceId()).build();
111 private ThingUID getUID(Socket socket) {
112 ThingUID thingUID = new ThingUID(OrviboBindingConstants.THING_TYPE_S20, socket.getDeviceId());
116 private void doThingDiscovered(Socket socket) {
117 DiscoveryResult discoveryResult = createDiscoveryResult(socket);
118 thingDiscovered(discoveryResult);