]> git.basschouten.com Git - openhab-addons.git/blob
20ccb0332cb7824b5d2c7efc396cd10d47dbf278
[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.harmonyhub.internal;
14
15 import java.util.HashMap;
16 import java.util.Hashtable;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.eclipse.jetty.client.HttpClient;
25 import org.openhab.binding.harmonyhub.internal.discovery.HarmonyDeviceDiscoveryService;
26 import org.openhab.binding.harmonyhub.internal.handler.HarmonyDeviceHandler;
27 import org.openhab.binding.harmonyhub.internal.handler.HarmonyHubHandler;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.io.net.http.HttpClientFactory;
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.ThingUID;
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
42 /**
43  * The {@link HarmonyHubHandlerFactory} is responsible for creating things and thing
44  * handlers.
45  *
46  * @author Dan Cunningham - Initial contribution
47  * @author Wouter Born - Add null annotations
48  */
49 @NonNullByDefault
50 @Component(service = { ThingHandlerFactory.class }, configurationPid = "binding.harmonyhub")
51 public class HarmonyHubHandlerFactory extends BaseThingHandlerFactory {
52
53     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
54             .concat(HarmonyHubHandler.SUPPORTED_THING_TYPES_UIDS.stream(),
55                     HarmonyDeviceHandler.SUPPORTED_THING_TYPES_UIDS.stream())
56             .collect(Collectors.toSet());
57
58     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
59     private final HttpClient httpClient;
60
61     private final HarmonyHubDynamicTypeProvider dynamicTypeProvider;
62
63     @Activate
64     public HarmonyHubHandlerFactory(@Reference final HttpClientFactory httpClientFactory,
65             @Reference HarmonyHubDynamicTypeProvider dynamicTypeProvider) {
66         this.httpClient = httpClientFactory.getCommonHttpClient();
67         this.dynamicTypeProvider = dynamicTypeProvider;
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 (thingTypeUID.equals(HarmonyHubBindingConstants.HARMONY_HUB_THING_TYPE)) {
80             HarmonyHubHandler harmonyHubHandler = new HarmonyHubHandler((Bridge) thing, dynamicTypeProvider,
81                     httpClient);
82             registerHarmonyDeviceDiscoveryService(harmonyHubHandler);
83             return harmonyHubHandler;
84         }
85
86         if (thingTypeUID.equals(HarmonyHubBindingConstants.HARMONY_DEVICE_THING_TYPE)) {
87             return new HarmonyDeviceHandler(thing, dynamicTypeProvider);
88         }
89
90         return null;
91     }
92
93     @Override
94     protected synchronized void removeHandler(ThingHandler thingHandler) {
95         if (thingHandler instanceof HarmonyHubHandler) {
96             ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
97             if (serviceReg != null) {
98                 serviceReg.unregister();
99             }
100         }
101     }
102
103     /**
104      * Adds HarmonyHubHandler to the discovery service to find Harmony Devices
105      *
106      * @param harmonyHubHandler
107      */
108     private synchronized void registerHarmonyDeviceDiscoveryService(HarmonyHubHandler harmonyHubHandler) {
109         HarmonyDeviceDiscoveryService discoveryService = new HarmonyDeviceDiscoveryService(harmonyHubHandler);
110         this.discoveryServiceRegs.put(harmonyHubHandler.getThing().getUID(),
111                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
112     }
113 }