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