]> git.basschouten.com Git - openhab-addons.git/blob
ddd6d0128e2ea8a983d30cc9ae946205716676bc
[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 {@link #thingDiscovered(DiscoveryResult)} and {@link #thingRemoved(String)} in
35  * order to notify the registered {@link DiscoveryListener}s.
36  *
37  * @author Pascal Larin - Initial contribution
38  *
39  */
40 public class SinopeThingsDiscoveryService extends AbstractDiscoveryService {
41
42     private final Logger logger = LoggerFactory.getLogger(SinopeThingsDiscoveryService.class);
43
44     private static final int SEARCH_TIME = 120;
45
46     private SinopeGatewayHandler sinopeGatewayHandler;
47
48     public SinopeThingsDiscoveryService(SinopeGatewayHandler sinopeGatewayHandler) {
49         super(SinopeBindingConstants.SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME);
50         this.sinopeGatewayHandler = sinopeGatewayHandler;
51     }
52
53     @Override
54     public Set<ThingTypeUID> getSupportedThingTypes() {
55         return SinopeBindingConstants.SUPPORTED_THING_TYPES_UIDS;
56     }
57
58     @Override
59     public void startScan() {
60         logger.debug("Sinope Things starting scan");
61         try {
62             sinopeGatewayHandler.startSearch(this);
63         } catch (IOException e) {
64             logger.debug("Search failed with an exception", e);
65         }
66     }
67
68     @Override
69     protected synchronized void stopScan() {
70         super.stopScan();
71         removeOlderResults(getTimestampOfLastScan());
72         try {
73             sinopeGatewayHandler.stopSearch();
74         } catch (IOException e) {
75             logger.debug("Can't stop search with an exception", e);
76         } finally {
77             logger.debug("Sinope Things scan stopped");
78         }
79     }
80
81     public void newThermostat(byte[] deviceId) {
82         logger.debug("Sinope Things service discovered a new device with id: {}", ByteUtil.toString(deviceId));
83         ThingTypeUID thingTypeUID = SinopeBindingConstants.THING_TYPE_THERMO;
84         ThingUID bridgeUID = sinopeGatewayHandler.getThing().getUID();
85         ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, toUID(deviceId));
86
87         DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(thingTypeUID)
88                 .withBridge(bridgeUID).withLabel("Device-Sinope")
89                 .withProperty(SinopeBindingConstants.CONFIG_PROPERTY_DEVICE_ID, ByteUtil.toString(deviceId)).build();
90
91         thingDiscovered(discoveryResult);
92     }
93
94     private static String toUID(byte[] deviceId) {
95         StringBuilder sb = new StringBuilder();
96         for (byte b : deviceId) {
97             sb.append(String.format("%02X", b));
98         }
99         return sb.toString();
100     }
101 }