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 {@link #thingDiscovered(DiscoveryResult)} and {@link #thingRemoved(String)} in
35 * order to notify the registered {@link DiscoveryListener}s.
37 * @author Pascal Larin - Initial contribution
40 public class SinopeThingsDiscoveryService extends AbstractDiscoveryService {
42 private final Logger logger = LoggerFactory.getLogger(SinopeThingsDiscoveryService.class);
44 private static final int SEARCH_TIME = 120;
46 private SinopeGatewayHandler sinopeGatewayHandler;
48 public SinopeThingsDiscoveryService(SinopeGatewayHandler sinopeGatewayHandler) {
49 super(SinopeBindingConstants.SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME);
50 this.sinopeGatewayHandler = sinopeGatewayHandler;
54 public Set<ThingTypeUID> getSupportedThingTypes() {
55 return SinopeBindingConstants.SUPPORTED_THING_TYPES_UIDS;
59 public void startScan() {
60 logger.debug("Sinope Things starting scan");
62 sinopeGatewayHandler.startSearch(this);
63 } catch (IOException e) {
64 logger.debug("Search failed with an exception", e);
69 protected synchronized void stopScan() {
71 removeOlderResults(getTimestampOfLastScan());
73 sinopeGatewayHandler.stopSearch();
74 } catch (IOException e) {
75 logger.debug("Can't stop search with an exception", e);
77 logger.debug("Sinope Things scan stopped");
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));
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();
91 thingDiscovered(discoveryResult);
94 private static String toUID(byte[] deviceId) {
95 StringBuilder sb = new StringBuilder();
96 for (byte b : deviceId) {
97 sb.append(String.format("%02X", b));