2 * Copyright (c) 2010-2021 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.HubMaintenanceException;
25 import org.openhab.binding.hdpowerview.internal.HubProcessingException;
26 import org.openhab.binding.hdpowerview.internal.api.responses.Shades;
27 import org.openhab.binding.hdpowerview.internal.api.responses.Shades.ShadeData;
28 import org.openhab.binding.hdpowerview.internal.config.HDPowerViewShadeConfiguration;
29 import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase;
30 import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase.Capabilities;
31 import org.openhab.binding.hdpowerview.internal.handler.HDPowerViewHubHandler;
32 import org.openhab.core.config.discovery.AbstractDiscoveryService;
33 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
34 import org.openhab.core.thing.ThingUID;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.gson.JsonParseException;
41 * Discovers an HD PowerView Shade from an existing hub
43 * @author Andy Lintner - Initial contribution
46 public class HDPowerViewShadeDiscoveryService extends AbstractDiscoveryService {
48 private final Logger logger = LoggerFactory.getLogger(HDPowerViewShadeDiscoveryService.class);
49 private final HDPowerViewHubHandler hub;
50 private final Runnable scanner;
51 private @Nullable ScheduledFuture<?> backgroundFuture;
52 private final ShadeCapabilitiesDatabase db = new ShadeCapabilitiesDatabase();
54 public HDPowerViewShadeDiscoveryService(HDPowerViewHubHandler hub) {
55 super(Collections.singleton(HDPowerViewBindingConstants.THING_TYPE_SHADE), 600, true);
57 this.scanner = createScanner();
61 protected void startScan() {
62 scheduler.execute(scanner);
66 protected void startBackgroundDiscovery() {
67 ScheduledFuture<?> backgroundFuture = this.backgroundFuture;
68 if (backgroundFuture != null && !backgroundFuture.isDone()) {
69 backgroundFuture.cancel(true);
71 this.backgroundFuture = scheduler.scheduleWithFixedDelay(scanner, 0, 60, TimeUnit.SECONDS);
75 protected void stopBackgroundDiscovery() {
76 ScheduledFuture<?> backgroundFuture = this.backgroundFuture;
77 if (backgroundFuture != null && !backgroundFuture.isDone()) {
78 backgroundFuture.cancel(true);
79 this.backgroundFuture = null;
81 super.stopBackgroundDiscovery();
84 private Runnable createScanner() {
87 HDPowerViewWebTargets webTargets = hub.getWebTargets();
88 if (webTargets == null) {
89 throw new HubProcessingException("Web targets not initialized");
91 Shades shades = webTargets.getShades();
92 if (shades != null && shades.shadeData != null) {
93 ThingUID bridgeUID = hub.getThing().getUID();
94 List<ShadeData> shadesData = shades.shadeData;
95 if (shadesData != null) {
96 for (ShadeData shadeData : shadesData) {
97 if (shadeData.id != 0) {
98 String id = Integer.toString(shadeData.id);
99 ThingUID thingUID = new ThingUID(HDPowerViewBindingConstants.THING_TYPE_SHADE,
101 Integer caps = shadeData.capabilities;
102 Capabilities capabilities = db.getCapabilities((caps != null) ? caps.intValue() : -1);
104 DiscoveryResultBuilder builder = DiscoveryResultBuilder.create(thingUID)
105 .withLabel(shadeData.getName()).withBridge(bridgeUID)
106 .withProperty(HDPowerViewShadeConfiguration.ID, id)
107 .withProperty(HDPowerViewBindingConstants.PROPERTY_SHADE_TYPE,
108 db.getType(shadeData.type).toString())
109 .withProperty(HDPowerViewBindingConstants.PROPERTY_SHADE_CAPABILITIES,
110 capabilities.toString())
111 .withRepresentationProperty(HDPowerViewShadeConfiguration.ID);
113 logger.debug("Hub discovered shade '{}'", id);
114 thingDiscovered(builder.build());
119 } catch (HubProcessingException | JsonParseException e) {
120 logger.warn("Unexpected error: {}", e.getMessage());
121 } catch (HubMaintenanceException e) {
122 // exceptions are logged in HDPowerViewWebTargets