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