2 * Copyright (c) 2010-2022 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.List;
17 import java.util.concurrent.ScheduledFuture;
18 import java.util.concurrent.TimeUnit;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants;
23 import org.openhab.binding.hdpowerview.internal.HDPowerViewWebTargets;
24 import org.openhab.binding.hdpowerview.internal.api.responses.Shades;
25 import org.openhab.binding.hdpowerview.internal.api.responses.Shades.ShadeData;
26 import org.openhab.binding.hdpowerview.internal.config.HDPowerViewShadeConfiguration;
27 import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase;
28 import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase.Capabilities;
29 import org.openhab.binding.hdpowerview.internal.exceptions.HubException;
30 import org.openhab.binding.hdpowerview.internal.exceptions.HubMaintenanceException;
31 import org.openhab.binding.hdpowerview.internal.exceptions.HubProcessingException;
32 import org.openhab.binding.hdpowerview.internal.handler.HDPowerViewHubHandler;
33 import org.openhab.core.config.discovery.AbstractDiscoveryService;
34 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
35 import org.openhab.core.thing.ThingUID;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Discovers an HD PowerView Shade from an existing hub
42 * @author Andy Lintner - Initial contribution
45 public class HDPowerViewShadeDiscoveryService extends AbstractDiscoveryService {
47 private final Logger logger = LoggerFactory.getLogger(HDPowerViewShadeDiscoveryService.class);
48 private final HDPowerViewHubHandler hub;
49 private final Runnable scanner;
50 private @Nullable ScheduledFuture<?> backgroundFuture;
51 private final ShadeCapabilitiesDatabase db = new ShadeCapabilitiesDatabase();
53 public HDPowerViewShadeDiscoveryService(HDPowerViewHubHandler hub) {
54 super(Collections.singleton(HDPowerViewBindingConstants.THING_TYPE_SHADE), 600, true);
56 this.scanner = createScanner();
60 protected void startScan() {
61 scheduler.execute(scanner);
65 protected void startBackgroundDiscovery() {
66 ScheduledFuture<?> backgroundFuture = this.backgroundFuture;
67 if (backgroundFuture != null && !backgroundFuture.isDone()) {
68 backgroundFuture.cancel(true);
70 this.backgroundFuture = scheduler.scheduleWithFixedDelay(scanner, 0, 60, TimeUnit.SECONDS);
74 protected void stopBackgroundDiscovery() {
75 ScheduledFuture<?> backgroundFuture = this.backgroundFuture;
76 if (backgroundFuture != null && !backgroundFuture.isDone()) {
77 backgroundFuture.cancel(true);
78 this.backgroundFuture = null;
80 super.stopBackgroundDiscovery();
83 private Runnable createScanner() {
86 HDPowerViewWebTargets webTargets = hub.getWebTargets();
87 if (webTargets == null) {
88 throw new HubProcessingException("Web targets not initialized");
90 Shades shades = webTargets.getShades();
91 if (shades.shadeData != null) {
92 ThingUID bridgeUID = hub.getThing().getUID();
93 List<ShadeData> shadesData = shades.shadeData;
94 if (shadesData != null) {
95 for (ShadeData shadeData : shadesData) {
96 if (shadeData.id != 0) {
97 String id = Integer.toString(shadeData.id);
98 ThingUID thingUID = new ThingUID(HDPowerViewBindingConstants.THING_TYPE_SHADE,
100 Integer caps = shadeData.capabilities;
101 Capabilities capabilities = db.getCapabilities((caps != null) ? caps.intValue() : -1);
103 DiscoveryResultBuilder builder = DiscoveryResultBuilder.create(thingUID)
104 .withLabel(shadeData.getName()).withBridge(bridgeUID)
105 .withProperty(HDPowerViewShadeConfiguration.ID, id)
106 .withProperty(HDPowerViewBindingConstants.PROPERTY_SHADE_TYPE,
107 db.getType(shadeData.type).toString())
108 .withProperty(HDPowerViewBindingConstants.PROPERTY_SHADE_CAPABILITIES,
109 capabilities.toString())
110 .withRepresentationProperty(HDPowerViewShadeConfiguration.ID);
112 logger.debug("Hub discovered shade '{}'", id);
113 thingDiscovered(builder.build());
118 } catch (HubMaintenanceException e) {
119 // exceptions are logged in HDPowerViewWebTargets
120 } catch (HubException e) {
121 logger.warn("Unexpected error: {}", e.getMessage());