]> git.basschouten.com Git - openhab-addons.git/blob
41b8708c44fe9965f6a98d342f7b65654ae789a6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.velux.internal.factory;
14
15 import java.util.HashSet;
16 import java.util.Hashtable;
17 import java.util.Set;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.velux.internal.VeluxBindingConstants;
22 import org.openhab.binding.velux.internal.discovery.VeluxDiscoveryService;
23 import org.openhab.binding.velux.internal.handler.VeluxBindingHandler;
24 import org.openhab.binding.velux.internal.handler.VeluxBridgeHandler;
25 import org.openhab.binding.velux.internal.handler.VeluxHandler;
26 import org.openhab.binding.velux.internal.utils.Localization;
27 import org.openhab.core.config.discovery.DiscoveryService;
28 import org.openhab.core.i18n.LocaleProvider;
29 import org.openhab.core.i18n.TranslationProvider;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerFactory;
36 import org.osgi.framework.ServiceRegistration;
37 import org.osgi.service.component.annotations.Activate;
38 import org.osgi.service.component.annotations.Component;
39 import org.osgi.service.component.annotations.Reference;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * The {@link VeluxHandlerFactory} is responsible for creating things and thing
45  * handlers.
46  *
47  * @author Guenther Schreiner - Initial contribution
48  */
49 @NonNullByDefault
50 @Component(service = ThingHandlerFactory.class, name = "binding.velux")
51 public class VeluxHandlerFactory extends BaseThingHandlerFactory {
52     private final Logger logger = LoggerFactory.getLogger(VeluxHandlerFactory.class);
53
54     // Class internal
55
56     private @Nullable ServiceRegistration<?> discoveryServiceRegistration = null;
57     private @Nullable VeluxDiscoveryService discoveryService = null;
58
59     private Set<VeluxBindingHandler> veluxBindingHandlers = new HashSet<>();
60     private Set<VeluxBridgeHandler> veluxBridgeHandlers = new HashSet<>();
61     private Set<VeluxHandler> veluxHandlers = new HashSet<>();
62
63     private @NonNullByDefault({}) LocaleProvider localeProvider;
64     private @NonNullByDefault({}) TranslationProvider i18nProvider;
65     private Localization localization = Localization.UNKNOWN;
66
67     // Private
68
69     private void registerDeviceDiscoveryService(VeluxBridgeHandler bridgeHandler) {
70         logger.trace("registerDeviceDiscoveryService({}) called.", bridgeHandler);
71         boolean createNew = false;
72         VeluxDiscoveryService discoveryService = this.discoveryService;
73         if (discoveryService == null) {
74             discoveryService = new VeluxDiscoveryService(localization);
75             createNew = true;
76         }
77         discoveryService.addBridge(bridgeHandler);
78         if (createNew) {
79             discoveryServiceRegistration = bundleContext.registerService(DiscoveryService.class.getName(),
80                     discoveryService, new Hashtable<>());
81         }
82     }
83
84     private synchronized void unregisterDeviceDiscoveryService(VeluxBridgeHandler bridgeHandler) {
85         logger.trace("unregisterDeviceDiscoveryService({}) called.", bridgeHandler);
86         VeluxDiscoveryService discoveryService = this.discoveryService;
87         if (discoveryService != null) {
88             discoveryService.removeBridge(bridgeHandler);
89             if (discoveryService.isEmpty()) {
90                 ServiceRegistration<?> discoveryServiceRegistration = this.discoveryServiceRegistration;
91                 if (discoveryServiceRegistration != null) {
92                     discoveryServiceRegistration.unregister();
93                 }
94             }
95         }
96     }
97
98     private @Nullable ThingHandler createBindingHandler(Thing thing) {
99         logger.trace("createBindingHandler({}) called for thing named '{}'.", thing.getUID(), thing.getLabel());
100         VeluxBindingHandler veluxBindingHandler = new VeluxBindingHandler(thing, localization);
101         veluxBindingHandlers.add(veluxBindingHandler);
102         return veluxBindingHandler;
103     }
104
105     private @Nullable ThingHandler createBridgeHandler(Thing thing) {
106         logger.trace("createBridgeHandler({}) called for thing named '{}'.", thing.getUID(), thing.getLabel());
107         VeluxBridgeHandler veluxBridgeHandler = new VeluxBridgeHandler((Bridge) thing, localization);
108         veluxBridgeHandlers.add(veluxBridgeHandler);
109         registerDeviceDiscoveryService(veluxBridgeHandler);
110         return veluxBridgeHandler;
111     }
112
113     private @Nullable ThingHandler createThingHandler(Thing thing) {
114         logger.trace("createThingHandler({}) called for thing named '{}'.", thing.getUID(), thing.getLabel());
115         VeluxHandler veluxHandler = new VeluxHandler(thing, localization);
116         veluxHandlers.add(veluxHandler);
117         return veluxHandler;
118     }
119
120     private void updateBindingState() {
121         veluxBindingHandlers.forEach((VeluxBindingHandler veluxBindingHandler) -> {
122             veluxBindingHandler.updateBindingState(veluxBridgeHandlers.size(), veluxHandlers.size());
123         });
124     }
125
126     private void updateLocalization() {
127         if (localization == Localization.UNKNOWN && localeProvider != null && i18nProvider != null) {
128             logger.trace("updateLocalization(): creating Localization based on locale={},translation={}).",
129                     localeProvider, i18nProvider);
130             localization = new Localization(localeProvider, i18nProvider);
131         }
132     }
133
134     // Constructor
135
136     @Activate
137     public VeluxHandlerFactory(final @Reference LocaleProvider givenLocaleProvider,
138             final @Reference TranslationProvider givenI18nProvider) {
139         logger.trace("VeluxHandlerFactory(locale={},translation={}) called.", givenLocaleProvider, givenI18nProvider);
140         localeProvider = givenLocaleProvider;
141         i18nProvider = givenI18nProvider;
142     }
143
144     @Reference
145     protected void setLocaleProvider(final LocaleProvider givenLocaleProvider) {
146         logger.trace("setLocaleProvider(): provided locale={}.", givenLocaleProvider);
147         localeProvider = givenLocaleProvider;
148         updateLocalization();
149     }
150
151     @Reference
152     protected void setTranslationProvider(TranslationProvider givenI18nProvider) {
153         logger.trace("setTranslationProvider(): provided translation={}.", givenI18nProvider);
154         i18nProvider = givenI18nProvider;
155         updateLocalization();
156     }
157
158     // Utility methods
159
160     @Override
161     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
162         boolean result = VeluxBindingConstants.SUPPORTED_THINGS_BINDING.contains(thingTypeUID)
163                 || VeluxBindingConstants.SUPPORTED_THINGS_BRIDGE.contains(thingTypeUID)
164                 || VeluxBindingConstants.SUPPORTED_THINGS_ITEMS.contains(thingTypeUID);
165         logger.trace("supportsThingType({}) called and returns {}.", thingTypeUID, result);
166         return result;
167     }
168
169     @Override
170     protected @Nullable ThingHandler createHandler(Thing thing) {
171         ThingHandler resultHandler = null;
172         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
173
174         // Handle Binding creation
175         if (VeluxBindingConstants.SUPPORTED_THINGS_BINDING.contains(thingTypeUID)) {
176             resultHandler = createBindingHandler(thing);
177         } else
178         // Handle Bridge creation
179         if (VeluxBindingConstants.SUPPORTED_THINGS_BRIDGE.contains(thingTypeUID)) {
180             resultHandler = createBridgeHandler(thing);
181         } else
182         // Handle creation of Things behind the Bridge
183         if (VeluxBindingConstants.SUPPORTED_THINGS_ITEMS.contains(thingTypeUID)) {
184             resultHandler = createThingHandler(thing);
185         } else {
186             logger.warn("createHandler({}) failed: ThingHandler not found for {}.", thingTypeUID, thing.getLabel());
187         }
188         updateBindingState();
189         return resultHandler;
190     }
191
192     @Override
193     protected void removeHandler(ThingHandler thingHandler) {
194         // Handle Binding removal
195         if (thingHandler instanceof VeluxBindingHandler) {
196             logger.trace("removeHandler() removing information element '{}'.", thingHandler.toString());
197             veluxBindingHandlers.remove(thingHandler);
198         } else
199         // Handle Bridge removal
200         if (thingHandler instanceof VeluxBridgeHandler) {
201             logger.trace("removeHandler() removing bridge '{}'.", thingHandler.toString());
202             veluxBridgeHandlers.remove(thingHandler);
203             unregisterDeviceDiscoveryService((VeluxBridgeHandler) thingHandler);
204         } else
205         // Handle removal of Things behind the Bridge
206         if (thingHandler instanceof VeluxHandler) {
207             logger.trace("removeHandler() removing thing '{}'.", thingHandler.toString());
208             veluxHandlers.remove(thingHandler);
209         }
210         updateBindingState();
211         super.removeHandler(thingHandler);
212     }
213 }