]> git.basschouten.com Git - openhab-addons.git/blob
80ae30968d61032f334ebee3cbe13e5a5d736f0f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.hdpowerview.internal.discovery;
14
15 import java.util.Collections;
16 import java.util.concurrent.ScheduledFuture;
17 import java.util.concurrent.TimeUnit;
18
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;
32
33 /**
34  * Discovers shades in an HD PowerView Generation 3 Gateway.
35  *
36  * @author Andrew Fiddian-Green - Initial contribution
37  */
38 @NonNullByDefault
39 public class ShadeDiscoveryService extends AbstractDiscoveryService {
40
41     private final Logger logger = LoggerFactory.getLogger(ShadeDiscoveryService.class);
42     private final GatewayBridgeHandler hub;
43     private final Runnable scanner;
44     private @Nullable ScheduledFuture<?> backgroundFuture;
45
46     public ShadeDiscoveryService(GatewayBridgeHandler hub) {
47         super(Collections.singleton(HDPowerViewBindingConstants.THING_TYPE_SHADE3), 60, true);
48         this.hub = hub;
49         this.scanner = createScanner();
50     }
51
52     @Override
53     protected void startScan() {
54         scheduler.execute(scanner);
55     }
56
57     @Override
58     protected void startBackgroundDiscovery() {
59         ScheduledFuture<?> backgroundFuture = this.backgroundFuture;
60         if (backgroundFuture != null && !backgroundFuture.isDone()) {
61             backgroundFuture.cancel(true);
62         }
63         this.backgroundFuture = scheduler.scheduleWithFixedDelay(scanner, 0, 60, TimeUnit.SECONDS);
64     }
65
66     @Override
67     protected void stopBackgroundDiscovery() {
68         ScheduledFuture<?> backgroundFuture = this.backgroundFuture;
69         if (backgroundFuture != null && !backgroundFuture.isDone()) {
70             backgroundFuture.cancel(true);
71             this.backgroundFuture = null;
72         }
73         super.stopBackgroundDiscovery();
74     }
75
76     private Runnable createScanner() {
77         return () -> {
78             try {
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) {
84                 // ignore
85             }
86             stopScan();
87         };
88     }
89
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) {
94                 continue;
95             }
96
97             String id = Integer.toString(shade.getId());
98             ThingUID thingUID = new ThingUID(HDPowerViewBindingConstants.THING_TYPE_SHADE3, bridgeUid, id);
99
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();
104             if (type != null) {
105                 builder.withProperty(HDPowerViewBindingConstants.PROPERTY_SHADE_TYPE, type);
106             }
107             logger.debug("Hub discovered shade '{}'", id);
108             thingDiscovered(builder.build());
109         }
110     }
111 }