]> git.basschouten.com Git - openhab-addons.git/blob
429795bf584d92f980d955650be78551b739a579
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.icloud.internal.discovery;
14
15 import static org.openhab.binding.icloud.internal.ICloudBindingConstants.*;
16
17 import java.util.List;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.icloud.internal.ICloudDeviceInformationListener;
21 import org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler;
22 import org.openhab.binding.icloud.internal.handler.dto.json.response.ICloudDeviceInformation;
23 import org.openhab.binding.icloud.internal.utilities.ICloudTextTranslator;
24 import org.openhab.core.config.discovery.AbstractDiscoveryService;
25 import org.openhab.core.config.discovery.DiscoveryResult;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.i18n.LocaleProvider;
28 import org.openhab.core.i18n.TranslationProvider;
29 import org.openhab.core.thing.ThingUID;
30 import org.osgi.framework.Bundle;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Device discovery creates a thing in the inbox for each icloud device
36  * found in the data received from {@link ICloudAccountBridgeHandler}.
37  *
38  * @author Patrik Gfeller - Initial Contribution
39  *
40  */
41 @NonNullByDefault
42 public class ICloudDeviceDiscovery extends AbstractDiscoveryService implements ICloudDeviceInformationListener {
43     private final Logger logger = LoggerFactory.getLogger(ICloudDeviceDiscovery.class);
44     private static final int TIMEOUT = 10;
45     private ThingUID bridgeUID;
46     private ICloudAccountBridgeHandler handler;
47     private ICloudTextTranslator translatorService;
48
49     public ICloudDeviceDiscovery(ICloudAccountBridgeHandler bridgeHandler, Bundle bundle,
50             TranslationProvider i18nProvider, LocaleProvider localeProvider) {
51         super(SUPPORTED_THING_TYPES_UIDS, TIMEOUT);
52
53         this.handler = bridgeHandler;
54         this.bridgeUID = bridgeHandler.getThing().getUID();
55         this.translatorService = new ICloudTextTranslator(bundle, i18nProvider, localeProvider);
56     }
57
58     @Override
59     public void deviceInformationUpdate(List<ICloudDeviceInformation> deviceInformationList) {
60         for (ICloudDeviceInformation deviceInformationRecord : deviceInformationList) {
61
62             String deviceTypeName = deviceInformationRecord.getDeviceDisplayName();
63             String deviceOwnerName = deviceInformationRecord.getName();
64
65             String thingLabel = deviceOwnerName + " (" + deviceTypeName + ")";
66             String deviceId = deviceInformationRecord.getId();
67             String deviceIdHash = Integer.toHexString(deviceId.hashCode());
68
69             logger.debug("iCloud device discovery for [{}]", deviceInformationRecord.getDeviceDisplayName());
70
71             ThingUID uid = new ThingUID(THING_TYPE_ICLOUDDEVICE, bridgeUID, deviceIdHash);
72             DiscoveryResult result = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID)
73                     .withProperty(DEVICE_PROPERTY_ID, deviceId)
74                     .withProperty(translatorService.getText(DEVICE_PROPERTY_ID_LABEL), deviceId)
75                     .withProperty(translatorService.getText(DEVICE_PROPERTY_OWNER_LABEL), deviceOwnerName)
76                     .withRepresentationProperty(DEVICE_PROPERTY_ID).withLabel(thingLabel).build();
77
78             logger.debug("Device [{}, {}] found.", deviceIdHash, deviceId);
79
80             thingDiscovered(result);
81
82         }
83     }
84
85     @Override
86     protected void startScan() {
87     }
88
89     public void activate() {
90         handler.registerListener(this);
91     }
92
93     @Override
94     public void deactivate() {
95         super.deactivate();
96         handler.unregisterListener(this);
97     }
98 }