]> git.basschouten.com Git - openhab-addons.git/blob
5b811028b81352dc65c4693a9f6626b6ba7ffeab
[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.database.ShadeCapabilitiesDatabase;
30 import org.openhab.binding.hdpowerview.internal.database.ShadeCapabilitiesDatabase.Capabilities;
31 import org.openhab.binding.hdpowerview.internal.handler.HDPowerViewHubHandler;
32 import org.openhab.core.config.discovery.AbstractDiscoveryService;
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     private final ShadeCapabilitiesDatabase db = new ShadeCapabilitiesDatabase();
53
54     public HDPowerViewShadeDiscoveryService(HDPowerViewHubHandler hub) {
55         super(Collections.singleton(HDPowerViewBindingConstants.THING_TYPE_SHADE), 600, true);
56         this.hub = hub;
57         this.scanner = createScanner();
58     }
59
60     @Override
61     protected void startScan() {
62         scheduler.execute(scanner);
63     }
64
65     @Override
66     protected void startBackgroundDiscovery() {
67         ScheduledFuture<?> backgroundFuture = this.backgroundFuture;
68         if (backgroundFuture != null && !backgroundFuture.isDone()) {
69             backgroundFuture.cancel(true);
70         }
71         this.backgroundFuture = scheduler.scheduleWithFixedDelay(scanner, 0, 60, TimeUnit.SECONDS);
72     }
73
74     @Override
75     protected void stopBackgroundDiscovery() {
76         ScheduledFuture<?> backgroundFuture = this.backgroundFuture;
77         if (backgroundFuture != null && !backgroundFuture.isDone()) {
78             backgroundFuture.cancel(true);
79             this.backgroundFuture = null;
80         }
81         super.stopBackgroundDiscovery();
82     }
83
84     private Runnable createScanner() {
85         return () -> {
86             try {
87                 HDPowerViewWebTargets webTargets = hub.getWebTargets();
88                 if (webTargets == null) {
89                     throw new HubProcessingException("Web targets not initialized");
90                 }
91                 Shades shades = webTargets.getShades();
92                 if (shades != null && shades.shadeData != null) {
93                     ThingUID bridgeUID = hub.getThing().getUID();
94                     List<ShadeData> shadesData = shades.shadeData;
95                     if (shadesData != null) {
96                         for (ShadeData shadeData : shadesData) {
97                             if (shadeData.id != 0) {
98                                 String id = Integer.toString(shadeData.id);
99                                 ThingUID thingUID = new ThingUID(HDPowerViewBindingConstants.THING_TYPE_SHADE,
100                                         bridgeUID, id);
101                                 Integer caps = shadeData.capabilities;
102                                 Capabilities capabilities = db.getCapabilities((caps != null) ? caps.intValue() : -1);
103
104                                 DiscoveryResultBuilder builder = DiscoveryResultBuilder.create(thingUID)
105                                         .withLabel(shadeData.getName()).withBridge(bridgeUID)
106                                         .withProperty(HDPowerViewShadeConfiguration.ID, id)
107                                         .withProperty(HDPowerViewBindingConstants.PROPERTY_SHADE_TYPE,
108                                                 db.getType(shadeData.type).toString())
109                                         .withProperty(HDPowerViewBindingConstants.PROPERTY_SHADE_CAPABILITIES,
110                                                 capabilities.toString())
111                                         .withRepresentationProperty(HDPowerViewShadeConfiguration.ID);
112
113                                 logger.debug("Hub discovered shade '{}'", id);
114                                 thingDiscovered(builder.build());
115                             }
116                         }
117                     }
118                 }
119             } catch (HubProcessingException | JsonParseException e) {
120                 logger.warn("Unexpected error: {}", e.getMessage());
121             } catch (HubMaintenanceException e) {
122                 // exceptions are logged in HDPowerViewWebTargets
123             }
124             stopScan();
125         };
126     }
127 }