]> git.basschouten.com Git - openhab-addons.git/blob
cbc57d6b962c22378c4a19ca70e84217970a157d
[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.innogysmarthome.internal;
14
15 import java.util.Set;
16 import java.util.stream.Collectors;
17 import java.util.stream.Stream;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.openhab.binding.innogysmarthome.internal.handler.InnogyBridgeHandler;
23 import org.openhab.binding.innogysmarthome.internal.handler.InnogyDeviceHandler;
24 import org.openhab.core.auth.client.oauth2.OAuthFactory;
25 import org.openhab.core.io.net.http.HttpClientFactory;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
30 import org.openhab.core.thing.binding.ThingHandler;
31 import org.openhab.core.thing.binding.ThingHandlerFactory;
32 import org.osgi.service.component.annotations.Activate;
33 import org.osgi.service.component.annotations.Component;
34 import org.osgi.service.component.annotations.Reference;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * The {@link InnogyHandlerFactory} is responsible for creating things and thing
40  * handlers.
41  *
42  * @author Oliver Kuhl - Initial contribution
43  * @author Hilbrand Bouwkamp - Refactored to use openHAB http and oauth2 libraries
44  */
45 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.innogysmarthome")
46 @NonNullByDefault
47 public class InnogyHandlerFactory extends BaseThingHandlerFactory implements ThingHandlerFactory {
48
49     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Stream
50             .concat(InnogyBridgeHandler.SUPPORTED_THING_TYPES.stream(),
51                     InnogyDeviceHandler.SUPPORTED_THING_TYPES.stream())
52             .collect(Collectors.toSet());
53
54     private final Logger logger = LoggerFactory.getLogger(InnogyHandlerFactory.class);
55
56     private final OAuthFactory oAuthFactory;
57     private final HttpClient httpClient;
58
59     @Activate
60     public InnogyHandlerFactory(@Reference OAuthFactory oAuthFactory, @Reference HttpClientFactory httpClientFactory) {
61         this.oAuthFactory = oAuthFactory;
62         httpClient = httpClientFactory.getCommonHttpClient();
63     }
64
65     @Override
66     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
67         return SUPPORTED_THING_TYPES.contains(thingTypeUID);
68     }
69
70     @Override
71     protected @Nullable ThingHandler createHandler(Thing thing) {
72         if (InnogyBridgeHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
73             return new InnogyBridgeHandler((Bridge) thing, oAuthFactory, httpClient);
74         } else if (InnogyDeviceHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
75             return new InnogyDeviceHandler(thing);
76         } else {
77             logger.debug("Unsupported thing {}.", thing.getThingTypeUID());
78             return null;
79         }
80     }
81 }