]> git.basschouten.com Git - openhab-addons.git/blob
5d89c4cb51ffacb65816fe6f7115a2e339f34972
[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;
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.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;
42
43 /**
44  * The {@link ICloudHandlerFactory} is responsible for creating things and thing handlers.
45  *
46  * @author Patrik Gfeller - Initial contribution
47  */
48 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.icloud")
49 @NonNullByDefault
50 public class ICloudHandlerFactory extends BaseThingHandlerFactory {
51     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegistrations = new HashMap<>();
52
53     private LocaleProvider localeProvider;
54
55     private TranslationProvider i18nProvider;
56
57     private final StorageService storageService;
58
59     @Activate
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;
65     }
66
67     @Override
68     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
69         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
70     }
71
72     @Override
73     protected @Nullable ThingHandler createHandler(Thing thing) {
74         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
75
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);
81             return bridgeHandler;
82         }
83
84         if (thingTypeUID.equals(THING_TYPE_ICLOUDDEVICE)) {
85             return new ICloudDeviceHandler(thing);
86         }
87         return null;
88     }
89
90     @Override
91     protected void removeHandler(ThingHandler thingHandler) {
92         if (thingHandler instanceof ICloudAccountBridgeHandler iCloudAccountBridgeHandler) {
93             unregisterDeviceDiscoveryService(iCloudAccountBridgeHandler);
94         }
95     }
96
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<>()));
103     }
104
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();
113             }
114             serviceRegistration.unregister();
115             this.discoveryServiceRegistrations.remove(bridgeHandler.getThing().getUID());
116         }
117     }
118 }