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 javax.ws.rs.ProcessingException;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants;
25 import org.openhab.binding.hdpowerview.internal.HDPowerViewWebTargets;
26 import org.openhab.binding.hdpowerview.internal.HubMaintenanceException;
27 import org.openhab.binding.hdpowerview.internal.api.responses.Shades;
28 import org.openhab.binding.hdpowerview.internal.api.responses.Shades.ShadeData;
29 import org.openhab.binding.hdpowerview.internal.config.HDPowerViewShadeConfiguration;
30 import org.openhab.binding.hdpowerview.internal.handler.HDPowerViewHubHandler;
31 import org.openhab.core.config.discovery.AbstractDiscoveryService;
32 import org.openhab.core.config.discovery.DiscoveryResult;
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;
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 ProcessingException("Web targets not initialized");
90 Shades shades = webTargets.getShades();
91 if (shades != null && 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 DiscoveryResult result = DiscoveryResultBuilder.create(thingUID)
101 .withProperty(HDPowerViewShadeConfiguration.ID, id)
102 .withRepresentationProperty(HDPowerViewShadeConfiguration.ID)
103 .withLabel(shadeData.getName()).withBridge(bridgeUID).build();
104 logger.debug("Hub discovered shade '{}'", id);
105 thingDiscovered(result);
110 } catch (ProcessingException | JsonParseException e) {
111 logger.warn("Unexpected error: {}", e.getMessage());
112 } catch (HubMaintenanceException e) {
113 // exceptions are logged in HDPowerViewWebTargets