]> git.basschouten.com Git - openhab-addons.git/blob
ad4ad4e390cf5e291162168862f09d0ab9ba0e30
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.List;
17 import java.util.concurrent.ScheduledFuture;
18 import java.util.concurrent.TimeUnit;
19
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;
36
37 import com.google.gson.JsonParseException;
38
39 /**
40  * Discovers an HD PowerView Shade from an existing hub
41  *
42  * @author Andy Lintner - Initial contribution
43  */
44 @NonNullByDefault
45 public class HDPowerViewShadeDiscoveryService extends AbstractDiscoveryService {
46
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
52     public HDPowerViewShadeDiscoveryService(HDPowerViewHubHandler hub) {
53         super(Collections.singleton(HDPowerViewBindingConstants.THING_TYPE_SHADE), 600, true);
54         this.hub = hub;
55         this.scanner = createScanner();
56     }
57
58     @Override
59     protected void startScan() {
60         scheduler.execute(scanner);
61     }
62
63     @Override
64     protected void startBackgroundDiscovery() {
65         ScheduledFuture<?> backgroundFuture = this.backgroundFuture;
66         if (backgroundFuture != null && !backgroundFuture.isDone()) {
67             backgroundFuture.cancel(true);
68         }
69         this.backgroundFuture = scheduler.scheduleWithFixedDelay(scanner, 0, 60, TimeUnit.SECONDS);
70     }
71
72     @Override
73     protected void stopBackgroundDiscovery() {
74         ScheduledFuture<?> backgroundFuture = this.backgroundFuture;
75         if (backgroundFuture != null && !backgroundFuture.isDone()) {
76             backgroundFuture.cancel(true);
77             this.backgroundFuture = null;
78         }
79         super.stopBackgroundDiscovery();
80     }
81
82     private Runnable createScanner() {
83         return () -> {
84             try {
85                 HDPowerViewWebTargets webTargets = hub.getWebTargets();
86                 if (webTargets == null) {
87                     throw new HubProcessingException("Web targets not initialized");
88                 }
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,
98                                         bridgeUID, id);
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);
105                             }
106                         }
107                     }
108                 }
109             } catch (HubProcessingException | JsonParseException e) {
110                 logger.warn("Unexpected error: {}", e.getMessage());
111             } catch (HubMaintenanceException e) {
112                 // exceptions are logged in HDPowerViewWebTargets
113             }
114             stopScan();
115         };
116     }
117 }