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.km200.internal;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Hashtable;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.eclipse.jetty.client.HttpClient;
26 import org.openhab.binding.km200.internal.discovery.KM200GatewayDiscoveryService;
27 import org.openhab.binding.km200.internal.handler.KM200GatewayHandler;
28 import org.openhab.binding.km200.internal.handler.KM200ThingHandler;
29 import org.openhab.core.config.core.Configuration;
30 import org.openhab.core.config.discovery.DiscoveryService;
31 import org.openhab.core.io.net.http.HttpClientFactory;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.thing.ThingUID;
36 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
37 import org.openhab.core.thing.binding.ThingHandler;
38 import org.openhab.core.thing.binding.ThingHandlerFactory;
39 import org.osgi.framework.ServiceRegistration;
40 import org.osgi.service.component.annotations.Activate;
41 import org.osgi.service.component.annotations.Component;
42 import org.osgi.service.component.annotations.Reference;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * The {@link KM200HandlerFactory} is responsible for creating things and thing
50 * @author Markus Eckhardt - Initial contribution
53 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.km200")
54 public class KM200HandlerFactory extends BaseThingHandlerFactory {
56 public final Set<ThingTypeUID> SUPPORTED_ALL_THING_TYPES_UIDS = Collections
57 .unmodifiableSet(Stream.concat(KM200GatewayHandler.SUPPORTED_THING_TYPES_UIDS.stream(),
58 KM200ThingHandler.SUPPORTED_THING_TYPES_UIDS.stream()).collect(Collectors.toSet()));
60 private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
62 private final Logger logger = LoggerFactory.getLogger(KM200HandlerFactory.class);
64 private final KM200ChannelTypeProvider channelTypeProvider;
67 * shared instance of HTTP client for asynchronous calls
69 private final HttpClient httpClient;
72 public KM200HandlerFactory(@Reference HttpClientFactory httpClientFactory,
73 @Reference KM200ChannelTypeProvider channelTypeProvider) {
74 this.httpClient = httpClientFactory.getCommonHttpClient();
75 this.channelTypeProvider = channelTypeProvider;
79 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
80 return SUPPORTED_ALL_THING_TYPES_UIDS.contains(thingTypeUID);
84 protected @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID) {
85 return createThing(thingTypeUID, configuration, thingUID);
89 protected @Nullable ThingHandler createHandler(Thing thing) {
90 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
91 if (KM200GatewayHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
92 logger.debug("It's a gataway: {}", thingTypeUID.getAsString());
93 KM200GatewayHandler gatewayHandler = new KM200GatewayHandler((Bridge) thing, httpClient);
94 registerKM200GatewayDiscoveryService(gatewayHandler);
95 return gatewayHandler;
96 } else if (KM200ThingHandler.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
97 logger.debug("It's a thing: {}", thingTypeUID.getAsString());
98 return new KM200ThingHandler(thing, channelTypeProvider);
105 protected synchronized void removeHandler(ThingHandler thingHandler) {
106 if (thingHandler instanceof KM200GatewayHandler) {
107 ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.get(thingHandler.getThing().getUID());
108 if (serviceReg != null) {
109 serviceReg.unregister();
111 discoveryServiceRegs.remove(thingHandler.getThing().getUID());
116 * Adds KM200GatewayHandler to the discovery service to find the KMXXX device
118 * @param gatewayHandler
120 private synchronized void registerKM200GatewayDiscoveryService(KM200GatewayHandler gatewayHandler) {
121 KM200GatewayDiscoveryService discoveryService = new KM200GatewayDiscoveryService(gatewayHandler);
122 this.discoveryServiceRegs.put(gatewayHandler.getThing().getUID(),
123 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));