2 * Copyright (c) 2010-2022 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.Nullable;
22 import org.openhab.binding.icloud.internal.discovery.ICloudDeviceDiscovery;
23 import org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler;
24 import org.openhab.binding.icloud.internal.handler.ICloudDeviceHandler;
25 import org.openhab.core.config.discovery.DiscoveryService;
26 import org.openhab.core.i18n.LocaleProvider;
27 import org.openhab.core.i18n.TranslationProvider;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerFactory;
35 import org.osgi.framework.ServiceRegistration;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Reference;
40 * The {@link ICloudHandlerFactory} is responsible for creating things and thing
43 * @author Patrik Gfeller - Initial contribution
45 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.icloud")
46 public class ICloudHandlerFactory extends BaseThingHandlerFactory {
47 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegistrations = new HashMap<>();
48 private LocaleProvider localeProvider;
49 private TranslationProvider i18nProvider;
52 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
53 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
57 protected @Nullable ThingHandler createHandler(Thing thing) {
58 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
60 if (thingTypeUID.equals(THING_TYPE_ICLOUD)) {
61 ICloudAccountBridgeHandler bridgeHandler = new ICloudAccountBridgeHandler((Bridge) thing);
62 registerDeviceDiscoveryService(bridgeHandler);
66 if (thingTypeUID.equals(THING_TYPE_ICLOUDDEVICE)) {
67 return new ICloudDeviceHandler(thing);
73 protected void removeHandler(ThingHandler thingHandler) {
74 if (thingHandler instanceof ICloudAccountBridgeHandler) {
75 unregisterDeviceDiscoveryService((ICloudAccountBridgeHandler) thingHandler);
79 private synchronized void registerDeviceDiscoveryService(ICloudAccountBridgeHandler bridgeHandler) {
80 ICloudDeviceDiscovery discoveryService = new ICloudDeviceDiscovery(bridgeHandler, bundleContext.getBundle(),
81 i18nProvider, localeProvider);
82 discoveryService.activate();
83 this.discoveryServiceRegistrations.put(bridgeHandler.getThing().getUID(),
84 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
87 private synchronized void unregisterDeviceDiscoveryService(ICloudAccountBridgeHandler bridgeHandler) {
88 ServiceRegistration<?> serviceRegistration = this.discoveryServiceRegistrations
89 .get(bridgeHandler.getThing().getUID());
90 if (serviceRegistration != null) {
91 ICloudDeviceDiscovery discoveryService = (ICloudDeviceDiscovery) bundleContext
92 .getService(serviceRegistration.getReference());
93 if (discoveryService != null) {
94 discoveryService.deactivate();
96 serviceRegistration.unregister();
97 discoveryServiceRegistrations.remove(bridgeHandler.getThing().getUID());
102 protected void setLocaleProvider(LocaleProvider localeProvider) {
103 this.localeProvider = localeProvider;
106 protected void unsetLocaleProvider(LocaleProvider localeProvider) {
107 this.localeProvider = null;
111 public void setTranslationProvider(TranslationProvider i18nProvider) {
112 this.i18nProvider = i18nProvider;
115 public void unsetTranslationProvider(TranslationProvider i18nProvider) {
116 this.i18nProvider = null;