]> git.basschouten.com Git - openhab-addons.git/blob
8e92155b1618ba9aea54eb739f4bd70062e20b76
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.nuki.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.eclipse.jetty.client.HttpClient;
18 import org.openhab.binding.nuki.internal.constants.NukiBindingConstants;
19 import org.openhab.binding.nuki.internal.constants.NukiLinkBuilder;
20 import org.openhab.binding.nuki.internal.dataexchange.NukiApiServlet;
21 import org.openhab.binding.nuki.internal.handler.NukiBridgeHandler;
22 import org.openhab.binding.nuki.internal.handler.NukiOpenerHandler;
23 import org.openhab.binding.nuki.internal.handler.NukiSmartLockHandler;
24 import org.openhab.core.id.InstanceUUID;
25 import org.openhab.core.io.net.http.HttpClientFactory;
26 import org.openhab.core.net.HttpServiceUtil;
27 import org.openhab.core.net.NetworkAddressService;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerFactory;
35 import org.osgi.service.component.annotations.Activate;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Reference;
38 import org.osgi.service.http.HttpService;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * The {@link NukiHandlerFactory} is responsible for creating things and thing
44  * handlers.
45  *
46  * @author Markus Katter - Initial contribution
47  * @contributer Jan Vybíral - Improved thing id generation
48  */
49 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.nuki")
50 @NonNullByDefault
51 public class NukiHandlerFactory extends BaseThingHandlerFactory {
52
53     private final Logger logger = LoggerFactory.getLogger(NukiHandlerFactory.class);
54
55     private final HttpClient httpClient;
56     private final NetworkAddressService networkAddressService;
57     private NukiApiServlet nukiApiServlet;
58
59     @Activate
60     public NukiHandlerFactory(@Reference HttpService httpService, @Reference final HttpClientFactory httpClientFactory,
61             @Reference NetworkAddressService networkAddressService) {
62         this.httpClient = httpClientFactory.getCommonHttpClient();
63         this.networkAddressService = networkAddressService;
64         this.nukiApiServlet = new NukiApiServlet(httpService);
65     }
66
67     @Override
68     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
69         return NukiBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
70     }
71
72     @Override
73     protected @Nullable ThingHandler createHandler(Thing thing) {
74         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
75
76         if (NukiBindingConstants.THING_TYPE_BRIDGE_UIDS.contains(thingTypeUID)) {
77             String callbackUrl = createCallbackUrl(InstanceUUID.get());
78             NukiBridgeHandler nukiBridgeHandler = new NukiBridgeHandler((Bridge) thing, httpClient, callbackUrl);
79             nukiApiServlet.add(nukiBridgeHandler);
80             return nukiBridgeHandler;
81         } else if (NukiBindingConstants.THING_TYPE_SMARTLOCK_UIDS.contains(thingTypeUID)) {
82             return new NukiSmartLockHandler(thing);
83         } else if (NukiBindingConstants.THING_TYPE_OPENER_UIDS.contains(thingTypeUID)) {
84             return new NukiOpenerHandler(thing);
85         }
86         logger.warn("No valid Handler found for Thing[{}]!", thingTypeUID);
87         return null;
88     }
89
90     @Override
91     public void removeThing(ThingUID thingUID) {
92         super.removeThing(thingUID);
93     }
94
95     @Override
96     public void unregisterHandler(Thing thing) {
97         super.unregisterHandler(thing);
98         ThingHandler handler = thing.getHandler();
99         if (handler instanceof NukiBridgeHandler) {
100             nukiApiServlet.remove((NukiBridgeHandler) handler);
101         }
102     }
103
104     private @Nullable String createCallbackUrl(String id) {
105         final String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
106         if (ipAddress == null) {
107             logger.warn("No network interface could be found to get callback address");
108             return null;
109         }
110         // we do not use SSL as it can cause certificate validation issues.
111         final int port = HttpServiceUtil.getHttpServicePort(bundleContext);
112         if (port == -1) {
113             logger.warn("Cannot find port of the http service.");
114             return null;
115         }
116         String callbackUrl = NukiLinkBuilder.callbackUri(ipAddress, port, id).toString();
117         logger.trace("callbackUrl[{}]", callbackUrl);
118         return callbackUrl;
119     }
120 }