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.hdpowerview.internal.discovery;
15 import java.util.Collections;
16 import java.util.concurrent.ScheduledFuture;
17 import java.util.concurrent.TimeUnit;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.hdpowerview.internal.GatewayWebTargets;
22 import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants;
23 import org.openhab.binding.hdpowerview.internal.config.HDPowerViewShadeConfiguration;
24 import org.openhab.binding.hdpowerview.internal.dto.gen3.Shade;
25 import org.openhab.binding.hdpowerview.internal.exceptions.HubProcessingException;
26 import org.openhab.binding.hdpowerview.internal.handler.GatewayBridgeHandler;
27 import org.openhab.core.config.discovery.AbstractDiscoveryService;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.thing.ThingUID;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Discovers shades in an HD PowerView Generation 3 Gateway.
36 * @author Andrew Fiddian-Green - Initial contribution
39 public class ShadeDiscoveryService extends AbstractDiscoveryService {
41 private final Logger logger = LoggerFactory.getLogger(ShadeDiscoveryService.class);
42 private final GatewayBridgeHandler hub;
43 private final Runnable scanner;
44 private @Nullable ScheduledFuture<?> backgroundFuture;
46 public ShadeDiscoveryService(GatewayBridgeHandler hub) {
47 super(Collections.singleton(HDPowerViewBindingConstants.THING_TYPE_SHADE3), 60, true);
49 this.scanner = createScanner();
53 protected void startScan() {
54 scheduler.execute(scanner);
58 protected void startBackgroundDiscovery() {
59 ScheduledFuture<?> backgroundFuture = this.backgroundFuture;
60 if (backgroundFuture != null && !backgroundFuture.isDone()) {
61 backgroundFuture.cancel(true);
63 this.backgroundFuture = scheduler.scheduleWithFixedDelay(scanner, 0, 60, TimeUnit.SECONDS);
67 protected void stopBackgroundDiscovery() {
68 ScheduledFuture<?> backgroundFuture = this.backgroundFuture;
69 if (backgroundFuture != null && !backgroundFuture.isDone()) {
70 backgroundFuture.cancel(true);
71 this.backgroundFuture = null;
73 super.stopBackgroundDiscovery();
76 private Runnable createScanner() {
79 GatewayWebTargets webTargets = hub.getWebTargets();
80 discoverShades(webTargets);
81 } catch (HubProcessingException e) {
82 logger.warn("Unexpected exception:{}, message:{}", e.getClass().getSimpleName(), e.getMessage());
83 } catch (IllegalStateException e) {
90 private void discoverShades(GatewayWebTargets webTargets) throws HubProcessingException {
91 ThingUID bridgeUid = hub.getThing().getUID();
92 for (Shade shade : webTargets.getShades()) {
93 if (shade.getId() == 0) {
97 String id = Integer.toString(shade.getId());
98 ThingUID thingUID = new ThingUID(HDPowerViewBindingConstants.THING_TYPE_SHADE3, bridgeUid, id);
100 DiscoveryResultBuilder builder = DiscoveryResultBuilder.create(thingUID).withLabel(shade.getName())
101 .withBridge(bridgeUid).withProperty(HDPowerViewShadeConfiguration.ID, id)
102 .withRepresentationProperty(HDPowerViewShadeConfiguration.ID);
103 String type = shade.getTypeString();
105 builder.withProperty(HDPowerViewBindingConstants.PROPERTY_SHADE_TYPE, type);
107 logger.debug("Hub discovered shade '{}'", id);
108 thingDiscovered(builder.build());