]> git.basschouten.com Git - openhab-addons.git/blob
4c8e58436ca8b1813f02ffa3829f0cecc71c9c87
[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.km200.internal;
14
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.Hashtable;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
22
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;
45
46 /**
47  * The {@link KM200HandlerFactory} is responsible for creating things and thing
48  * handlers.
49  *
50  * @author Markus Eckhardt - Initial contribution
51  */
52 @NonNullByDefault
53 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.km200")
54 public class KM200HandlerFactory extends BaseThingHandlerFactory {
55
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()));
59
60     private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
61
62     private final Logger logger = LoggerFactory.getLogger(KM200HandlerFactory.class);
63
64     private final KM200ChannelTypeProvider channelTypeProvider;
65
66     /**
67      * shared instance of HTTP client for asynchronous calls
68      */
69     private final HttpClient httpClient;
70
71     @Activate
72     public KM200HandlerFactory(@Reference HttpClientFactory httpClientFactory,
73             @Reference KM200ChannelTypeProvider channelTypeProvider) {
74         this.httpClient = httpClientFactory.getCommonHttpClient();
75         this.channelTypeProvider = channelTypeProvider;
76     }
77
78     @Override
79     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
80         return SUPPORTED_ALL_THING_TYPES_UIDS.contains(thingTypeUID);
81     }
82
83     @Override
84     protected @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID) {
85         return createThing(thingTypeUID, configuration, thingUID);
86     }
87
88     @Override
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);
99         } else {
100             return null;
101         }
102     }
103
104     @Override
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();
110             }
111             discoveryServiceRegs.remove(thingHandler.getThing().getUID());
112         }
113     }
114
115     /**
116      * Adds KM200GatewayHandler to the discovery service to find the KMXXX device
117      *
118      * @param gatewayHandler
119      */
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<>()));
124     }
125 }