]> git.basschouten.com Git - openhab-addons.git/blob
82e6f12a7aeaf12933ab84aa4cd8fb6416b82739
[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 deviceDiscoveryId = deviceInformationRecord.getDeviceDiscoveryId();
67             if (deviceDiscoveryId == null || deviceDiscoveryId.isBlank()) {
68                 logger.debug("deviceDiscoveryId is empty, using device name for identification.");
69                 deviceDiscoveryId = deviceOwnerName;
70             }
71             String deviceIdHash = Integer.toHexString(deviceDiscoveryId.hashCode());
72
73             logger.debug("iCloud device discovery for [{}]", deviceInformationRecord.getDeviceDisplayName());
74
75             ThingUID uid = new ThingUID(THING_TYPE_ICLOUDDEVICE, bridgeUID, deviceIdHash);
76             DiscoveryResult result = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID)
77                     .withProperty(DEVICE_PROPERTY_ID, deviceDiscoveryId)
78                     .withProperty(translatorService.getText(DEVICE_PROPERTY_ID_LABEL), deviceDiscoveryId)
79                     .withProperty(translatorService.getText(DEVICE_PROPERTY_OWNER_LABEL), deviceOwnerName)
80                     .withRepresentationProperty(DEVICE_PROPERTY_ID).withLabel(thingLabel).build();
81
82             logger.debug("Device [{}, {}] found.", deviceIdHash, deviceDiscoveryId);
83
84             thingDiscovered(result);
85
86         }
87     }
88
89     @Override
90     protected void startScan() {
91     }
92
93     public void activate() {
94         handler.registerListener(this);
95     }
96
97     @Override
98     public void deactivate() {
99         super.deactivate();
100         handler.unregisterListener(this);
101     }
102 }