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