2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.icloud.internal;
15 import static org.openhab.binding.icloud.internal.ICloudBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.Hashtable;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.icloud.internal.discovery.ICloudDeviceDiscovery;
24 import org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler;
25 import org.openhab.binding.icloud.internal.handler.ICloudDeviceHandler;
26 import org.openhab.core.config.discovery.DiscoveryService;
27 import org.openhab.core.i18n.LocaleProvider;
28 import org.openhab.core.i18n.TranslationProvider;
29 import org.openhab.core.storage.Storage;
30 import org.openhab.core.storage.StorageService;
31 import org.openhab.core.thing.Bridge;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.thing.ThingUID;
35 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
36 import org.openhab.core.thing.binding.ThingHandler;
37 import org.openhab.core.thing.binding.ThingHandlerFactory;
38 import org.osgi.framework.ServiceRegistration;
39 import org.osgi.service.component.annotations.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Reference;
44 * The {@link ICloudHandlerFactory} is responsible for creating things and thing handlers.
46 * @author Patrik Gfeller - Initial contribution
48 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.icloud")
50 public class ICloudHandlerFactory extends BaseThingHandlerFactory {
51 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegistrations = new HashMap<>();
53 private LocaleProvider localeProvider;
55 private TranslationProvider i18nProvider;
57 private final StorageService storageService;
60 public ICloudHandlerFactory(@Reference StorageService storageService, @Reference LocaleProvider localeProvider,
61 @Reference TranslationProvider i18nProvider) {
62 this.storageService = storageService;
63 this.localeProvider = localeProvider;
64 this.i18nProvider = i18nProvider;
68 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
69 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
73 protected @Nullable ThingHandler createHandler(Thing thing) {
74 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
76 if (thingTypeUID.equals(THING_TYPE_ICLOUD)) {
77 Storage<String> storage = this.storageService.getStorage(thing.getUID().toString(),
78 String.class.getClassLoader());
79 ICloudAccountBridgeHandler bridgeHandler = new ICloudAccountBridgeHandler((Bridge) thing, storage);
80 registerDeviceDiscoveryService(bridgeHandler);
84 if (thingTypeUID.equals(THING_TYPE_ICLOUDDEVICE)) {
85 return new ICloudDeviceHandler(thing);
91 protected void removeHandler(ThingHandler thingHandler) {
92 if (thingHandler instanceof ICloudAccountBridgeHandler) {
93 unregisterDeviceDiscoveryService((ICloudAccountBridgeHandler) thingHandler);
97 private synchronized void registerDeviceDiscoveryService(ICloudAccountBridgeHandler bridgeHandler) {
98 ICloudDeviceDiscovery discoveryService = new ICloudDeviceDiscovery(bridgeHandler,
99 this.bundleContext.getBundle(), this.i18nProvider, this.localeProvider);
100 discoveryService.activate();
101 this.discoveryServiceRegistrations.put(bridgeHandler.getThing().getUID(), this.bundleContext
102 .registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
105 private synchronized void unregisterDeviceDiscoveryService(ICloudAccountBridgeHandler bridgeHandler) {
106 ServiceRegistration<?> serviceRegistration = this.discoveryServiceRegistrations
107 .get(bridgeHandler.getThing().getUID());
108 if (serviceRegistration != null) {
109 ICloudDeviceDiscovery discoveryService = (ICloudDeviceDiscovery) this.bundleContext
110 .getService(serviceRegistration.getReference());
111 if (discoveryService != null) {
112 discoveryService.deactivate();
114 serviceRegistration.unregister();
115 this.discoveryServiceRegistrations.remove(bridgeHandler.getThing().getUID());