]> git.basschouten.com Git - openhab-addons.git/blob
9316a46c53d5e7338ac630aab877175ebb7150ea
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.amazonechocontrol.internal.discovery;
14
15 import static org.openhab.binding.amazonechocontrol.internal.AmazonEchoControlBindingConstants.DEVICE_PROPERTY_ID;
16 import static org.openhab.binding.amazonechocontrol.internal.AmazonEchoControlBindingConstants.SUPPORTED_SMART_HOME_THING_TYPES_UIDS;
17 import static org.openhab.binding.amazonechocontrol.internal.AmazonEchoControlBindingConstants.THING_TYPE_SMART_HOME_DEVICE;
18 import static org.openhab.binding.amazonechocontrol.internal.AmazonEchoControlBindingConstants.THING_TYPE_SMART_HOME_DEVICE_GROUP;
19
20 import java.util.Date;
21 import java.util.HashMap;
22 import java.util.Hashtable;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.concurrent.ScheduledFuture;
27 import java.util.concurrent.TimeUnit;
28 import java.util.stream.Stream;
29
30 import org.eclipse.jdt.annotation.NonNullByDefault;
31 import org.eclipse.jdt.annotation.Nullable;
32 import org.openhab.binding.amazonechocontrol.internal.Connection;
33 import org.openhab.binding.amazonechocontrol.internal.handler.AccountHandler;
34 import org.openhab.binding.amazonechocontrol.internal.handler.SmartHomeDeviceHandler;
35 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.DriverIdentity;
36 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.SmartHomeDevice;
37 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeGroups.SmartHomeGroup;
38 import org.openhab.binding.amazonechocontrol.internal.jsons.SmartHomeBaseDevice;
39 import org.openhab.binding.amazonechocontrol.internal.smarthome.Constants;
40 import org.openhab.core.config.discovery.AbstractDiscoveryService;
41 import org.openhab.core.config.discovery.DiscoveryResult;
42 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
43 import org.openhab.core.thing.ThingUID;
44 import org.osgi.service.component.annotations.Activate;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * @author Lukas Knoeller - Initial contribution
50  */
51 @NonNullByDefault
52 public class SmartHomeDevicesDiscovery extends AbstractDiscoveryService {
53     private AccountHandler accountHandler;
54     private final Logger logger = LoggerFactory.getLogger(SmartHomeDevicesDiscovery.class);
55
56     private @Nullable ScheduledFuture<?> startScanStateJob;
57     private @Nullable Long activateTimeStamp;
58
59     public SmartHomeDevicesDiscovery(AccountHandler accountHandler) {
60         super(SUPPORTED_SMART_HOME_THING_TYPES_UIDS, 10);
61         this.accountHandler = accountHandler;
62     }
63
64     public void activate() {
65         activate(new Hashtable<String, @Nullable Object>());
66     }
67
68     @Override
69     public void deactivate() {
70         super.deactivate();
71     }
72
73     @Override
74     protected void startScan() {
75         stopScanJob();
76         Long activateTimeStamp = this.activateTimeStamp;
77         if (activateTimeStamp != null) {
78             removeOlderResults(activateTimeStamp);
79         }
80         setSmartHomeDevices(accountHandler.updateSmartHomeDeviceList(false));
81     }
82
83     protected void startAutomaticScan() {
84         if (!this.accountHandler.getThing().getThings().isEmpty()) {
85             stopScanJob();
86             return;
87         }
88         Connection connection = this.accountHandler.findConnection();
89         if (connection == null) {
90             return;
91         }
92         Date verifyTime = connection.tryGetVerifyTime();
93         if (verifyTime == null) {
94             return;
95         }
96         if (new Date().getTime() - verifyTime.getTime() < 10000) {
97             return;
98         }
99         startScan();
100     }
101
102     @Override
103     protected void startBackgroundDiscovery() {
104         stopScanJob();
105         startScanStateJob = scheduler.scheduleWithFixedDelay(this::startAutomaticScan, 3000, 1000,
106                 TimeUnit.MILLISECONDS);
107     }
108
109     @Override
110     protected void stopBackgroundDiscovery() {
111         stopScanJob();
112     }
113
114     void stopScanJob() {
115         ScheduledFuture<?> currentStartScanStateJob = startScanStateJob;
116         if (currentStartScanStateJob != null) {
117             currentStartScanStateJob.cancel(false);
118             startScanStateJob = null;
119         }
120         super.stopScan();
121     }
122
123     @Override
124     @Activate
125     public void activate(@Nullable Map<String, @Nullable Object> config) {
126         super.activate(config);
127         if (config != null) {
128             modified(config);
129         }
130         Long activateTimeStamp = this.activateTimeStamp;
131         if (activateTimeStamp == null) {
132             this.activateTimeStamp = new Date().getTime();
133         }
134     };
135
136     synchronized void setSmartHomeDevices(List<SmartHomeBaseDevice> deviceList) {
137         int smartHomeDeviceDiscoveryMode = accountHandler.getSmartHomeDevicesDiscoveryMode();
138         if (smartHomeDeviceDiscoveryMode == 0) {
139             return;
140         }
141
142         for (Object smartHomeDevice : deviceList) {
143             ThingUID bridgeThingUID = this.accountHandler.getThing().getUID();
144             ThingUID thingUID = null;
145             String deviceName = null;
146             Map<String, Object> props = new HashMap<>();
147
148             if (smartHomeDevice instanceof SmartHomeDevice) {
149                 SmartHomeDevice shd = (SmartHomeDevice) smartHomeDevice;
150
151                 String entityId = shd.entityId;
152                 if (entityId == null) {
153                     // No entity id
154                     continue;
155                 }
156                 String id = shd.findId();
157                 if (id == null) {
158                     // No id
159                     continue;
160                 }
161                 boolean isSkillDevice = false;
162                 DriverIdentity driverIdentity = shd.driverIdentity;
163                 isSkillDevice = driverIdentity != null && "SKILL".equals(driverIdentity.namespace);
164
165                 if (smartHomeDeviceDiscoveryMode == 1 && isSkillDevice) {
166                     // Connected through skill
167                     continue;
168                 }
169                 if (!(smartHomeDeviceDiscoveryMode == 2) && "openHAB".equalsIgnoreCase(shd.manufacturerName)) {
170                     // OpenHAB device
171                     continue;
172                 }
173
174                 if (Stream.of(shd.capabilities).noneMatch(capability -> capability != null
175                         && Constants.SUPPORTED_INTERFACES.contains(capability.interfaceName))) {
176                     // No supported interface found
177                     continue;
178                 }
179
180                 thingUID = new ThingUID(THING_TYPE_SMART_HOME_DEVICE, bridgeThingUID, entityId.replace(".", "-"));
181
182                 if ("Amazon".equals(shd.manufacturerName) && driverIdentity != null
183                         && "SonarCloudService".equals(driverIdentity.identifier)) {
184                     deviceName = "Alexa Guard on " + shd.friendlyName;
185                 } else if ("Amazon".equals(shd.manufacturerName) && driverIdentity != null
186                         && "OnGuardSmartHomeBridgeService".equals(driverIdentity.identifier)) {
187                     deviceName = "Alexa Guard";
188                 } else if (shd.aliases != null && shd.aliases.length > 0 && shd.aliases[0] != null
189                         && shd.aliases[0].friendlyName != null) {
190                     deviceName = shd.aliases[0].friendlyName;
191                 } else {
192                     deviceName = shd.friendlyName;
193                 }
194                 props.put(DEVICE_PROPERTY_ID, id);
195             }
196
197             if (smartHomeDevice instanceof SmartHomeGroup) {
198                 SmartHomeGroup shg = (SmartHomeGroup) smartHomeDevice;
199                 String id = shg.findId();
200                 if (id == null) {
201                     // No id
202                     continue;
203                 }
204                 Set<SmartHomeDevice> supportedChildren = SmartHomeDeviceHandler.getSupportedSmartHomeDevices(shg,
205                         deviceList);
206                 if (supportedChildren.size() == 0) {
207                     // No children with an supported interface
208                     continue;
209                 }
210                 thingUID = new ThingUID(THING_TYPE_SMART_HOME_DEVICE_GROUP, bridgeThingUID, id.replace(".", "-"));
211                 deviceName = shg.applianceGroupName;
212                 props.put(DEVICE_PROPERTY_ID, id);
213             }
214
215             if (thingUID != null) {
216                 DiscoveryResult result = DiscoveryResultBuilder.create(thingUID).withLabel(deviceName)
217                         .withProperties(props).withBridge(bridgeThingUID).build();
218
219                 logger.debug("Device [{}] found.", deviceName);
220
221                 thingDiscovered(result);
222             }
223         }
224     }
225 }