]> git.basschouten.com Git - openhab-addons.git/blob
9398af4a157ceba173e991197acd76439185eef4
[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.nobohub.internal;
14
15 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.SUPPORTED_THING_TYPES_UIDS;
16 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.THING_TYPE_COMPONENT;
17 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.THING_TYPE_HUB;
18 import static org.openhab.binding.nobohub.internal.NoboHubBindingConstants.THING_TYPE_ZONE;
19
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.Hashtable;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.openhab.binding.nobohub.internal.discovery.NoboThingDiscoveryService;
30 import org.openhab.core.config.discovery.DiscoveryService;
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.binding.BaseThingHandlerFactory;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.ThingHandlerFactory;
37 import org.osgi.framework.ServiceRegistration;
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 NoboHubHandlerFactory} is responsible for creating things and thing
46  * handlers.
47  *
48  * @author Jørgen Austvik - Initial contribution
49  * @author Espen Fossen - Initial contribution
50  */
51 @NonNullByDefault
52 @Component(configurationPid = "binding.nobohub", service = ThingHandlerFactory.class)
53 public class NoboHubHandlerFactory extends BaseThingHandlerFactory {
54
55     private final Logger logger = LoggerFactory.getLogger(NoboHubHandlerFactory.class);
56     private final Map<ThingTypeUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
57     public static final Set<ThingTypeUID> DISCOVERABLE_DEVICE_TYPES_UIDS = new HashSet<>(List.of(THING_TYPE_HUB));
58     private @NonNullByDefault({}) WeekProfileStateDescriptionOptionsProvider stateDescriptionOptionsProvider;
59
60     private final NoboHubTranslationProvider i18nProvider;
61
62     @Activate
63     public NoboHubHandlerFactory(
64             final @Reference WeekProfileStateDescriptionOptionsProvider stateDescriptionOptionsProvider,
65             final @Reference NoboHubTranslationProvider i18nProvider) {
66         this.stateDescriptionOptionsProvider = stateDescriptionOptionsProvider;
67         this.i18nProvider = i18nProvider;
68     }
69
70     @Override
71     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
72         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
73     }
74
75     @Override
76     protected @Nullable ThingHandler createHandler(Thing thing) {
77         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
78
79         if (THING_TYPE_HUB.equals(thingTypeUID)) {
80             NoboHubBridgeHandler handler = new NoboHubBridgeHandler((Bridge) thing);
81             registerDiscoveryService(handler);
82             return handler;
83         } else if (THING_TYPE_ZONE.equals(thingTypeUID)) {
84             logger.debug("Setting WeekProfileStateDescriptionOptionsProvider for: {}", thing.getLabel());
85             return new ZoneHandler(thing, i18nProvider, stateDescriptionOptionsProvider);
86         } else if (THING_TYPE_COMPONENT.equals(thingTypeUID)) {
87             return new ComponentHandler(thing, i18nProvider);
88         }
89
90         return null;
91     }
92
93     @Override
94     protected void removeHandler(ThingHandler thingHandler) {
95         if (thingHandler instanceof NoboHubBridgeHandler bridgeHandler) {
96             unregisterDiscoveryService(bridgeHandler);
97         }
98     }
99
100     private synchronized void registerDiscoveryService(NoboHubBridgeHandler bridgeHandler) {
101         NoboThingDiscoveryService discoveryService = new NoboThingDiscoveryService(bridgeHandler);
102         bridgeHandler.setDicsoveryService(discoveryService);
103         this.discoveryServiceRegs.put(bridgeHandler.getThing().getThingTypeUID(), getBundleContext()
104                 .registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
105     }
106
107     private synchronized void unregisterDiscoveryService(NoboHubBridgeHandler bridgeHandler) {
108         try {
109             ServiceRegistration<?> serviceReg = this.discoveryServiceRegs
110                     .remove(bridgeHandler.getThing().getThingTypeUID());
111             if (null != serviceReg) {
112                 NoboThingDiscoveryService service = (NoboThingDiscoveryService) getBundleContext()
113                         .getService(serviceReg.getReference());
114                 serviceReg.unregister();
115                 if (null != service) {
116                     service.deactivate();
117                 }
118             }
119         } catch (IllegalArgumentException iae) {
120             logger.debug("Failed to unregister service", iae);
121         }
122     }
123
124     @Reference
125     protected void setDynamicStateDescriptionProvider(WeekProfileStateDescriptionOptionsProvider provider) {
126         this.stateDescriptionOptionsProvider = provider;
127     }
128
129     protected void unsetDynamicStateDescriptionProvider(WeekProfileStateDescriptionOptionsProvider provider) {
130         this.stateDescriptionOptionsProvider = null;
131     }
132 }