]> git.basschouten.com Git - openhab-addons.git/blob
52710a7912555885869c46bd99000b38938bb4ff
[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 = (discoveryService == null);
72         if (createNew) {
73             discoveryService = new VeluxDiscoveryService(localization);
74         }
75         discoveryService.addBridge(bridgeHandler);
76         if (createNew) {
77             discoveryServiceRegistration = bundleContext.registerService(DiscoveryService.class.getName(),
78                     discoveryService, new Hashtable<>());
79         }
80     }
81
82     private synchronized void unregisterDeviceDiscoveryService(VeluxBridgeHandler bridgeHandler) {
83         logger.trace("unregisterDeviceDiscoveryService({}) called.", bridgeHandler);
84         if (discoveryService != null) {
85             discoveryService.removeBridge(bridgeHandler);
86             if (discoveryService.isEmpty()) {
87                 discoveryServiceRegistration.unregister();
88             }
89         }
90     }
91
92     private @Nullable ThingHandler createBindingHandler(Thing thing) {
93         logger.trace("createBindingHandler({}) called for thing named '{}'.", thing.getUID(), thing.getLabel());
94         VeluxBindingHandler veluxBindingHandler = new VeluxBindingHandler(thing, localization);
95         veluxBindingHandlers.add(veluxBindingHandler);
96         return veluxBindingHandler;
97     }
98
99     private @Nullable ThingHandler createBridgeHandler(Thing thing) {
100         logger.trace("createBridgeHandler({}) called for thing named '{}'.", thing.getUID(), thing.getLabel());
101         VeluxBridgeHandler veluxBridgeHandler = new VeluxBridgeHandler((Bridge) thing, localization);
102         veluxBridgeHandlers.add(veluxBridgeHandler);
103         registerDeviceDiscoveryService(veluxBridgeHandler);
104         return veluxBridgeHandler;
105     }
106
107     private @Nullable ThingHandler createThingHandler(Thing thing) {
108         logger.trace("createThingHandler({}) called for thing named '{}'.", thing.getUID(), thing.getLabel());
109         VeluxHandler veluxHandler = new VeluxHandler(thing, localization);
110         veluxHandlers.add(veluxHandler);
111         return veluxHandler;
112     }
113
114     private void updateBindingState() {
115         veluxBindingHandlers.forEach((VeluxBindingHandler veluxBindingHandler) -> {
116             veluxBindingHandler.updateBindingState(veluxBridgeHandlers.size(), veluxHandlers.size());
117         });
118     }
119
120     private void updateLocalization() {
121         if (localization == Localization.UNKNOWN && localeProvider != null && i18nProvider != null) {
122             logger.trace("updateLocalization(): creating Localization based on locale={},translation={}).",
123                     localeProvider, i18nProvider);
124             localization = new Localization(localeProvider, i18nProvider);
125         }
126     }
127
128     // Constructor
129
130     @Activate
131     public VeluxHandlerFactory(final @Reference LocaleProvider givenLocaleProvider,
132             final @Reference TranslationProvider givenI18nProvider) {
133         logger.trace("VeluxHandlerFactory(locale={},translation={}) called.", givenLocaleProvider, givenI18nProvider);
134         localeProvider = givenLocaleProvider;
135         i18nProvider = givenI18nProvider;
136     }
137
138     @Reference
139     protected void setLocaleProvider(final LocaleProvider givenLocaleProvider) {
140         logger.trace("setLocaleProvider(): provided locale={}.", givenLocaleProvider);
141         localeProvider = givenLocaleProvider;
142         updateLocalization();
143     }
144
145     @Reference
146     protected void setTranslationProvider(TranslationProvider givenI18nProvider) {
147         logger.trace("setTranslationProvider(): provided translation={}.", givenI18nProvider);
148         i18nProvider = givenI18nProvider;
149         updateLocalization();
150     }
151
152     // Utility methods
153
154     @Override
155     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
156         boolean result = VeluxBindingConstants.SUPPORTED_THINGS_BINDING.contains(thingTypeUID)
157                 || VeluxBindingConstants.SUPPORTED_THINGS_BRIDGE.contains(thingTypeUID)
158                 || VeluxBindingConstants.SUPPORTED_THINGS_ITEMS.contains(thingTypeUID);
159         logger.trace("supportsThingType({}) called and returns {}.", thingTypeUID, result);
160         return result;
161     }
162
163     @Override
164     protected @Nullable ThingHandler createHandler(Thing thing) {
165         ThingHandler resultHandler = null;
166         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
167
168         // Handle Binding creation
169         if (VeluxBindingConstants.SUPPORTED_THINGS_BINDING.contains(thingTypeUID)) {
170             resultHandler = createBindingHandler(thing);
171         } else
172         // Handle Bridge creation
173         if (VeluxBindingConstants.SUPPORTED_THINGS_BRIDGE.contains(thingTypeUID)) {
174             resultHandler = createBridgeHandler(thing);
175         } else
176         // Handle creation of Things behind the Bridge
177         if (VeluxBindingConstants.SUPPORTED_THINGS_ITEMS.contains(thingTypeUID)) {
178             resultHandler = createThingHandler(thing);
179         } else {
180             logger.warn("createHandler({}) failed: ThingHandler not found for {}.", thingTypeUID, thing.getLabel());
181         }
182         updateBindingState();
183         return resultHandler;
184     }
185
186     @Override
187     protected void removeHandler(ThingHandler thingHandler) {
188         // Handle Binding removal
189         if (thingHandler instanceof VeluxBindingHandler) {
190             logger.trace("removeHandler() removing information element '{}'.", thingHandler.toString());
191             veluxBindingHandlers.remove(thingHandler);
192         } else
193         // Handle Bridge removal
194         if (thingHandler instanceof VeluxBridgeHandler) {
195             logger.trace("removeHandler() removing bridge '{}'.", thingHandler.toString());
196             veluxBridgeHandlers.remove(thingHandler);
197             unregisterDeviceDiscoveryService((VeluxBridgeHandler) thingHandler);
198         } else
199         // Handle removal of Things behind the Bridge
200         if (thingHandler instanceof VeluxHandler) {
201             logger.trace("removeHandler() removing thing '{}'.", thingHandler.toString());
202             veluxHandlers.remove(thingHandler);
203         }
204         updateBindingState();
205         super.removeHandler(thingHandler);
206     }
207 }