]> git.basschouten.com Git - openhab-addons.git/blob
d34828153e90262931f884cbbef84d31fd575050
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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;
14
15 import static org.openhab.binding.icloud.internal.ICloudBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Hashtable;
19 import java.util.Map;
20
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;
38
39 /**
40  * The {@link ICloudHandlerFactory} is responsible for creating things and thing
41  * handlers.
42  *
43  * @author Patrik Gfeller - Initial contribution
44  */
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;
50
51     @Override
52     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
53         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
54     }
55
56     @Override
57     protected @Nullable ThingHandler createHandler(Thing thing) {
58         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
59
60         if (thingTypeUID.equals(THING_TYPE_ICLOUD)) {
61             ICloudAccountBridgeHandler bridgeHandler = new ICloudAccountBridgeHandler((Bridge) thing);
62             registerDeviceDiscoveryService(bridgeHandler);
63             return bridgeHandler;
64         }
65
66         if (thingTypeUID.equals(THING_TYPE_ICLOUDDEVICE)) {
67             return new ICloudDeviceHandler(thing);
68         }
69         return null;
70     }
71
72     @Override
73     protected void removeHandler(ThingHandler thingHandler) {
74         if (thingHandler instanceof ICloudAccountBridgeHandler) {
75             unregisterDeviceDiscoveryService((ICloudAccountBridgeHandler) thingHandler);
76         }
77     }
78
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<>()));
85     }
86
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();
95             }
96             serviceRegistration.unregister();
97             discoveryServiceRegistrations.remove(bridgeHandler.getThing().getUID());
98         }
99     }
100
101     @Reference
102     protected void setLocaleProvider(LocaleProvider localeProvider) {
103         this.localeProvider = localeProvider;
104     }
105
106     protected void unsetLocaleProvider(LocaleProvider localeProvider) {
107         this.localeProvider = null;
108     }
109
110     @Reference
111     public void setTranslationProvider(TranslationProvider i18nProvider) {
112         this.i18nProvider = i18nProvider;
113     }
114
115     public void unsetTranslationProvider(TranslationProvider i18nProvider) {
116         this.i18nProvider = null;
117     }
118 }