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