]> git.basschouten.com Git - openhab-addons.git/blob
0eecec13794441f220980f016cefb374a6f5a854
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.api.responses.Shades;
25 import org.openhab.binding.hdpowerview.internal.api.responses.Shades.ShadeData;
26 import org.openhab.binding.hdpowerview.internal.config.HDPowerViewShadeConfiguration;
27 import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase;
28 import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase.Capabilities;
29 import org.openhab.binding.hdpowerview.internal.exceptions.HubException;
30 import org.openhab.binding.hdpowerview.internal.exceptions.HubMaintenanceException;
31 import org.openhab.binding.hdpowerview.internal.exceptions.HubProcessingException;
32 import org.openhab.binding.hdpowerview.internal.handler.HDPowerViewHubHandler;
33 import org.openhab.core.config.discovery.AbstractDiscoveryService;
34 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
35 import org.openhab.core.thing.ThingUID;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
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     private final ShadeCapabilitiesDatabase db = new ShadeCapabilitiesDatabase();
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 HubProcessingException("Web targets not initialized");
89                 }
90                 Shades shades = webTargets.getShades();
91                 if (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                                 Integer caps = shadeData.capabilities;
101                                 Capabilities capabilities = db.getCapabilities((caps != null) ? caps.intValue() : -1);
102
103                                 DiscoveryResultBuilder builder = DiscoveryResultBuilder.create(thingUID)
104                                         .withLabel(shadeData.getName()).withBridge(bridgeUID)
105                                         .withProperty(HDPowerViewShadeConfiguration.ID, id)
106                                         .withProperty(HDPowerViewBindingConstants.PROPERTY_SHADE_TYPE,
107                                                 db.getType(shadeData.type).toString())
108                                         .withProperty(HDPowerViewBindingConstants.PROPERTY_SHADE_CAPABILITIES,
109                                                 capabilities.toString())
110                                         .withRepresentationProperty(HDPowerViewShadeConfiguration.ID);
111
112                                 logger.debug("Hub discovered shade '{}'", id);
113                                 thingDiscovered(builder.build());
114                             }
115                         }
116                     }
117                 }
118             } catch (HubMaintenanceException e) {
119                 // exceptions are logged in HDPowerViewWebTargets
120             } catch (HubException e) {
121                 logger.warn("Unexpected error: {}", e.getMessage());
122             }
123             stopScan();
124         };
125     }
126 }