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;
19 import org.openhab.binding.orvibo.internal.OrviboBindingConstants;
20 import org.openhab.core.config.discovery.AbstractDiscoveryService;
21 import org.openhab.core.config.discovery.DiscoveryResult;
22 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
23 import org.openhab.core.config.discovery.DiscoveryService;
24 import org.openhab.core.thing.ThingTypeUID;
25 import org.openhab.core.thing.ThingUID;
26 import org.osgi.service.component.annotations.Component;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.github.tavalin.s20.S20Client;
31 import com.github.tavalin.s20.S20Client.SocketDiscoveryListener;
32 import com.github.tavalin.s20.Socket;
35 * The {@link SocketDiscoveryService} class defines a service used
36 * to discover S20 sockets on the local netowork.
38 * @author Daniel Walters - Initial contribution
40 @Component(service = DiscoveryService.class, configurationPid = "discovery.orvibo")
41 public class SocketDiscoveryService extends AbstractDiscoveryService implements SocketDiscoveryListener {
43 private final Logger logger = LoggerFactory.getLogger(SocketDiscoveryService.class);
44 private static final int SEARCH_TIME = 60;
45 private S20Client s20Client;
47 public SocketDiscoveryService() throws SocketException {
48 super(getSupportedThingTypeUIDs(), SEARCH_TIME);
51 private static Set<ThingTypeUID> getSupportedThingTypeUIDs() {
52 return Set.of(OrviboBindingConstants.THING_TYPE_S20);
56 protected void activate(Map<String, Object> configProperties) {
58 s20Client = S20Client.getInstance();
59 super.activate(configProperties);
60 } catch (SocketException ex) {
61 logger.error("Error occurred while activating S20 discovery service: {}", ex.getMessage(), ex);
66 protected void startScan() {
67 if (s20Client != null) {
68 logger.debug("starting manual scan");
69 s20Client.addSocketDiscoveryListener(this);
70 s20Client.globalDiscovery();
71 for (final Socket socket : s20Client.getAllSockets().values()) {
72 doThingDiscovered(socket);
75 logger.debug("Client not initialised");
80 protected void startBackgroundDiscovery() {
81 if (s20Client != null) {
82 logger.debug("starting automatic background scan");
83 s20Client.addSocketDiscoveryListener(this);
84 s20Client.globalDiscovery();
86 logger.debug("Client not initialised");
91 protected void stopBackgroundDiscovery() {
92 s20Client.removeSocketDiscoveryListener(this);
96 public void socketDiscovered(Socket socket) {
97 doThingDiscovered(socket);
100 private DiscoveryResult createDiscoveryResult(Socket socket) {
101 ThingUID thingUID = getUID(socket);
102 String label = socket.getLabel();
103 if (label == null || label.isBlank()) {
106 return DiscoveryResultBuilder.create(thingUID).withLabel(label)
107 .withProperty(OrviboBindingConstants.CONFIG_PROPERTY_DEVICE_ID, socket.getDeviceId()).build();
110 private ThingUID getUID(Socket socket) {
111 return new ThingUID(OrviboBindingConstants.THING_TYPE_S20, socket.getDeviceId());
114 private void doThingDiscovered(Socket socket) {
115 DiscoveryResult discoveryResult = createDiscoveryResult(socket);
116 thingDiscovered(discoveryResult);