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.handler.HDPowerViewHubHandler;
30 import org.openhab.core.config.discovery.AbstractDiscoveryService;
31 import org.openhab.core.config.discovery.DiscoveryResult;
32 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
33 import org.openhab.core.thing.ThingUID;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import com.google.gson.JsonParseException;
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;
52 public HDPowerViewShadeDiscoveryService(HDPowerViewHubHandler hub) {
53 super(Collections.singleton(HDPowerViewBindingConstants.THING_TYPE_SHADE), 600, true);
55 this.scanner = createScanner();
59 protected void startScan() {
60 scheduler.execute(scanner);
64 protected void startBackgroundDiscovery() {
65 ScheduledFuture<?> backgroundFuture = this.backgroundFuture;
66 if (backgroundFuture != null && !backgroundFuture.isDone()) {
67 backgroundFuture.cancel(true);
69 this.backgroundFuture = scheduler.scheduleWithFixedDelay(scanner, 0, 60, TimeUnit.SECONDS);
73 protected void stopBackgroundDiscovery() {
74 ScheduledFuture<?> backgroundFuture = this.backgroundFuture;
75 if (backgroundFuture != null && !backgroundFuture.isDone()) {
76 backgroundFuture.cancel(true);
77 this.backgroundFuture = null;
79 super.stopBackgroundDiscovery();
82 private Runnable createScanner() {
85 HDPowerViewWebTargets webTargets = hub.getWebTargets();
86 if (webTargets == null) {
87 throw new HubProcessingException("Web targets not initialized");
89 Shades shades = webTargets.getShades();
90 if (shades != null && shades.shadeData != null) {
91 ThingUID bridgeUID = hub.getThing().getUID();
92 List<ShadeData> shadesData = shades.shadeData;
93 if (shadesData != null) {
94 for (ShadeData shadeData : shadesData) {
95 if (shadeData.id != 0) {
96 String id = Integer.toString(shadeData.id);
97 ThingUID thingUID = new ThingUID(HDPowerViewBindingConstants.THING_TYPE_SHADE,
99 DiscoveryResult result = DiscoveryResultBuilder.create(thingUID)
100 .withProperty(HDPowerViewShadeConfiguration.ID, id)
101 .withRepresentationProperty(HDPowerViewShadeConfiguration.ID)
102 .withLabel(shadeData.getName()).withBridge(bridgeUID).build();
103 logger.debug("Hub discovered shade '{}'", id);
104 thingDiscovered(result);
109 } catch (HubProcessingException | JsonParseException e) {
110 logger.warn("Unexpected error: {}", e.getMessage());
111 } catch (HubMaintenanceException e) {
112 // exceptions are logged in HDPowerViewWebTargets