2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.velux.internal.factory;
15 import java.util.HashSet;
16 import java.util.Hashtable;
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;
45 * The {@link VeluxHandlerFactory} is responsible for creating things and thing
48 * @author Guenther Schreiner - Initial contribution
51 @Component(service = ThingHandlerFactory.class, name = "binding.velux")
52 public class VeluxHandlerFactory extends BaseThingHandlerFactory {
53 private final Logger logger = LoggerFactory.getLogger(VeluxHandlerFactory.class);
57 private @Nullable ServiceRegistration<?> discoveryServiceRegistration = null;
58 private @Nullable VeluxDiscoveryService discoveryService = null;
60 private Set<VeluxBindingHandler> veluxBindingHandlers = new HashSet<>();
61 private Set<VeluxBridgeHandler> veluxBridgeHandlers = new HashSet<>();
62 private Set<VeluxHandler> veluxHandlers = new HashSet<>();
64 private @NonNullByDefault({}) LocaleProvider localeProvider;
65 private @NonNullByDefault({}) TranslationProvider i18nProvider;
66 private Localization localization = Localization.UNKNOWN;
68 private @Nullable static VeluxHandlerFactory activeInstance = null;
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);
78 discoveryService.addBridge(bridgeHandler);
79 if (discoveryServiceRegistration == null) {
80 discoveryServiceRegistration = bundleContext.registerService(DiscoveryService.class.getName(),
81 discoveryService, new Hashtable<>());
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;
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;
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;
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);
122 private void updateBindingState() {
123 veluxBindingHandlers.forEach((VeluxBindingHandler veluxBindingHandler) -> {
124 veluxBindingHandler.updateBindingState(veluxBridgeHandlers.size(), veluxHandlers.size());
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);
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;
147 protected void setLocaleProvider(final LocaleProvider givenLocaleProvider) {
148 logger.trace("setLocaleProvider(): provided locale={}.", givenLocaleProvider);
149 localeProvider = givenLocaleProvider;
150 updateLocalization();
154 protected void setTranslationProvider(TranslationProvider givenI18nProvider) {
155 logger.trace("setTranslationProvider(): provided translation={}.", givenI18nProvider);
156 i18nProvider = givenI18nProvider;
157 updateLocalization();
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);
172 protected @Nullable ThingHandler createHandler(Thing thing) {
173 ThingHandler resultHandler = null;
174 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
176 // Handle Binding creation
177 if (VeluxBindingConstants.SUPPORTED_THINGS_BINDING.contains(thingTypeUID)) {
178 resultHandler = createBindingHandler(thing);
180 // Handle Bridge creation
181 if (VeluxBindingConstants.SUPPORTED_THINGS_BRIDGE.contains(thingTypeUID)) {
182 resultHandler = createBridgeHandler(thing);
184 // Handle creation of Things behind the Bridge
185 if (VeluxBindingConstants.SUPPORTED_THINGS_ITEMS.contains(thingTypeUID)) {
186 resultHandler = createThingHandler(thing);
188 logger.warn("createHandler({}) failed: ThingHandler not found for {}.", thingTypeUID, thing.getLabel());
190 updateBindingState();
191 return resultHandler;
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);
201 // Handle Bridge removal
202 if (thingHandler instanceof VeluxBridgeHandler) {
203 logger.trace("removeHandler() removing bridge '{}'.", thingHandler.toString());
204 veluxBridgeHandlers.remove(thingHandler);
205 unregisterDeviceDiscoveryService((VeluxBridgeHandler) thingHandler);
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);
212 updateBindingState();
213 super.removeHandler(thingHandler);
217 protected void activate(ComponentContext componentContext) {
218 activeInstance = this;
219 super.activate(componentContext);
223 protected void deactivate(ComponentContext componentContext) {
224 activeInstance = null;
225 super.deactivate(componentContext);
228 public static void refreshBindingInfo() {
229 VeluxHandlerFactory instance = VeluxHandlerFactory.activeInstance;
230 if (instance != null) {
231 instance.updateBindingState();