2 * Copyright (c) 2010-2022 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.harmonyhub.internal;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.Hashtable;
19 import java.util.List;
20 import java.util.Locale;
23 import java.util.concurrent.CopyOnWriteArrayList;
24 import java.util.stream.Collectors;
25 import java.util.stream.Stream;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.eclipse.jetty.client.HttpClient;
30 import org.openhab.binding.harmonyhub.internal.discovery.HarmonyDeviceDiscoveryService;
31 import org.openhab.binding.harmonyhub.internal.handler.HarmonyDeviceHandler;
32 import org.openhab.binding.harmonyhub.internal.handler.HarmonyHubHandler;
33 import org.openhab.core.config.discovery.DiscoveryService;
34 import org.openhab.core.io.net.http.HttpClientFactory;
35 import org.openhab.core.thing.Bridge;
36 import org.openhab.core.thing.Thing;
37 import org.openhab.core.thing.ThingTypeUID;
38 import org.openhab.core.thing.ThingUID;
39 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
40 import org.openhab.core.thing.binding.ThingHandler;
41 import org.openhab.core.thing.binding.ThingHandlerFactory;
42 import org.openhab.core.thing.type.ChannelGroupType;
43 import org.openhab.core.thing.type.ChannelGroupTypeProvider;
44 import org.openhab.core.thing.type.ChannelGroupTypeUID;
45 import org.openhab.core.thing.type.ChannelType;
46 import org.openhab.core.thing.type.ChannelTypeProvider;
47 import org.openhab.core.thing.type.ChannelTypeUID;
48 import org.osgi.framework.ServiceRegistration;
49 import org.osgi.service.component.annotations.Activate;
50 import org.osgi.service.component.annotations.Component;
51 import org.osgi.service.component.annotations.Reference;
54 * The {@link HarmonyHubHandlerFactory} is responsible for creating things and thing
57 * @author Dan Cunningham - Initial contribution
58 * @author Wouter Born - Add null annotations
61 @Component(service = { ThingHandlerFactory.class, ChannelTypeProvider.class,
62 ChannelGroupTypeProvider.class }, configurationPid = "binding.harmonyhub")
63 public class HarmonyHubHandlerFactory extends BaseThingHandlerFactory
64 implements ChannelTypeProvider, ChannelGroupTypeProvider {
66 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
67 .concat(HarmonyHubHandler.SUPPORTED_THING_TYPES_UIDS.stream(),
68 HarmonyDeviceHandler.SUPPORTED_THING_TYPES_UIDS.stream())
69 .collect(Collectors.toSet());
71 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
72 private final HttpClient httpClient;
74 private final List<ChannelType> channelTypes = new CopyOnWriteArrayList<>();
75 private final List<ChannelGroupType> channelGroupTypes = new CopyOnWriteArrayList<>();
78 public HarmonyHubHandlerFactory(@Reference final HttpClientFactory httpClientFactory) {
79 this.httpClient = httpClientFactory.getCommonHttpClient();
83 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
84 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
88 protected @Nullable ThingHandler createHandler(Thing thing) {
89 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
91 if (thingTypeUID.equals(HarmonyHubBindingConstants.HARMONY_HUB_THING_TYPE)) {
92 HarmonyHubHandler harmonyHubHandler = new HarmonyHubHandler((Bridge) thing, this);
93 registerHarmonyDeviceDiscoveryService(harmonyHubHandler);
94 return harmonyHubHandler;
97 if (thingTypeUID.equals(HarmonyHubBindingConstants.HARMONY_DEVICE_THING_TYPE)) {
98 return new HarmonyDeviceHandler(thing, this);
105 protected synchronized void removeHandler(ThingHandler thingHandler) {
106 if (thingHandler instanceof HarmonyHubHandler) {
107 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
108 if (serviceReg != null) {
109 serviceReg.unregister();
115 * Adds HarmonyHubHandler to the discovery service to find Harmony Devices
117 * @param harmonyHubHandler
119 private synchronized void registerHarmonyDeviceDiscoveryService(HarmonyHubHandler harmonyHubHandler) {
120 HarmonyDeviceDiscoveryService discoveryService = new HarmonyDeviceDiscoveryService(harmonyHubHandler);
121 this.discoveryServiceRegs.put(harmonyHubHandler.getThing().getUID(),
122 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
126 public Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
131 public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
132 for (ChannelType channelType : channelTypes) {
133 if (channelType.getUID().equals(channelTypeUID)) {
141 public @Nullable ChannelGroupType getChannelGroupType(ChannelGroupTypeUID channelGroupTypeUID,
142 @Nullable Locale locale) {
143 for (ChannelGroupType channelGroupType : channelGroupTypes) {
144 if (channelGroupType.getUID().equals(channelGroupTypeUID)) {
145 return channelGroupType;
152 public Collection<ChannelGroupType> getChannelGroupTypes(@Nullable Locale locale) {
153 return channelGroupTypes;
156 public HttpClient getHttpClient() {
157 return this.httpClient;
160 public void addChannelType(ChannelType type) {
161 channelTypes.add(type);
164 public void removeChannelType(ChannelType type) {
165 channelTypes.remove(type);
168 public void removeChannelTypesForThing(ThingUID uid) {
169 List<ChannelType> removes = new ArrayList<>();
170 for (ChannelType c : channelTypes) {
171 if (c.getUID().getAsString().startsWith(uid.getAsString())) {
175 channelTypes.removeAll(removes);