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.sinope.internal.discovery;
15 import java.io.IOException;
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;
31 * The {@link AbstractDiscoveryService} provides methods which handle the {@link DiscoveryListener}s.
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.
39 * @author Pascal Larin - Initial contribution
42 public class SinopeThingsDiscoveryService extends AbstractDiscoveryService {
44 private final Logger logger = LoggerFactory.getLogger(SinopeThingsDiscoveryService.class);
46 private static final int SEARCH_TIME = 120;
48 private SinopeGatewayHandler sinopeGatewayHandler;
50 public SinopeThingsDiscoveryService(SinopeGatewayHandler sinopeGatewayHandler) {
51 super(SinopeBindingConstants.SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME);
52 this.sinopeGatewayHandler = sinopeGatewayHandler;
56 public Set<ThingTypeUID> getSupportedThingTypes() {
57 return SinopeBindingConstants.SUPPORTED_THING_TYPES_UIDS;
61 public void startScan() {
62 logger.debug("Sinope Things starting scan");
64 sinopeGatewayHandler.startSearch(this);
65 } catch (IOException e) {
66 logger.debug("Search failed with an exception", e);
71 protected synchronized void stopScan() {
73 removeOlderResults(getTimestampOfLastScan());
75 sinopeGatewayHandler.stopSearch();
76 } catch (IOException e) {
77 logger.debug("Can't stop search with an exception", e);
79 logger.debug("Sinope Things scan stopped");
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));
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();
93 thingDiscovered(discoveryResult);
96 private static String toUID(byte[] deviceId) {
97 StringBuilder sb = new StringBuilder();
98 for (byte b : deviceId) {
99 sb.append(String.format("%02X", b));
101 return sb.toString();