]> git.basschouten.com Git - openhab-addons.git/blob
3eee402a00b8cd2b0357961e1a897528839dc0fc
[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.sinope.internal.discovery;
14
15 import java.io.IOException;
16 import java.util.Set;
17
18 import org.openhab.binding.sinope.SinopeBindingConstants;
19 import org.openhab.binding.sinope.handler.SinopeGatewayHandler;
20 import org.openhab.binding.sinope.internal.util.ByteUtil;
21 import org.openhab.core.config.discovery.AbstractDiscoveryService;
22 import org.openhab.core.config.discovery.DiscoveryListener;
23 import org.openhab.core.config.discovery.DiscoveryResult;
24 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.ThingUID;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The {@link AbstractDiscoveryService} provides methods which handle the {@link DiscoveryListener}s.
32  *
33  * Subclasses do not have to care about adding and removing those listeners.
34  * They can use the protected methods
35  * {@link org.openhab.core.config.discovery.pAbstractDiscoveryService#thingDiscovered(DiscoveryResult)}
36  * and {@link org.openhab.core.config.discovery.AbstractDiscoveryService#thingRemoved(ThingUID)} in
37  * order to notify the registered {@link DiscoveryListener}s.
38  *
39  * @author Pascal Larin - Initial contribution
40  *
41  */
42 public class SinopeThingsDiscoveryService extends AbstractDiscoveryService {
43
44     private final Logger logger = LoggerFactory.getLogger(SinopeThingsDiscoveryService.class);
45
46     private static final int SEARCH_TIME = 120;
47
48     private SinopeGatewayHandler sinopeGatewayHandler;
49
50     public SinopeThingsDiscoveryService(SinopeGatewayHandler sinopeGatewayHandler) {
51         super(SinopeBindingConstants.SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME);
52         this.sinopeGatewayHandler = sinopeGatewayHandler;
53     }
54
55     @Override
56     public Set<ThingTypeUID> getSupportedThingTypes() {
57         return SinopeBindingConstants.SUPPORTED_THING_TYPES_UIDS;
58     }
59
60     @Override
61     public void startScan() {
62         logger.debug("Sinope Things starting scan");
63         try {
64             sinopeGatewayHandler.startSearch(this);
65         } catch (IOException e) {
66             logger.debug("Search failed with an exception", e);
67         }
68     }
69
70     @Override
71     protected synchronized void stopScan() {
72         super.stopScan();
73         removeOlderResults(getTimestampOfLastScan());
74         try {
75             sinopeGatewayHandler.stopSearch();
76         } catch (IOException e) {
77             logger.debug("Can't stop search with an exception", e);
78         } finally {
79             logger.debug("Sinope Things scan stopped");
80         }
81     }
82
83     public void newThermostat(byte[] deviceId) {
84         logger.debug("Sinope Things service discovered a new device with id: {}", ByteUtil.toString(deviceId));
85         ThingTypeUID thingTypeUID = SinopeBindingConstants.THING_TYPE_THERMO;
86         ThingUID bridgeUID = sinopeGatewayHandler.getThing().getUID();
87         ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, toUID(deviceId));
88
89         DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(thingTypeUID)
90                 .withBridge(bridgeUID).withLabel("Device-Sinope")
91                 .withProperty(SinopeBindingConstants.CONFIG_PROPERTY_DEVICE_ID, ByteUtil.toString(deviceId)).build();
92
93         thingDiscovered(discoveryResult);
94     }
95
96     private static String toUID(byte[] deviceId) {
97         StringBuilder sb = new StringBuilder();
98         for (byte b : deviceId) {
99             sb.append(String.format("%02X", b));
100         }
101         return sb.toString();
102     }
103 }