]> git.basschouten.com Git - openhab-addons.git/blob
bc5c26a3f32e1005f97ae9bc73e104481bcc6ddc
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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 java.util.ArrayList;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.eclipse.jetty.client.HttpClient;
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.NukiSmartLockHandler;
23 import org.openhab.core.io.net.http.HttpClientFactory;
24 import org.openhab.core.net.HttpServiceUtil;
25 import org.openhab.core.net.NetworkAddressService;
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.osgi.service.http.HttpService;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * The {@link NukiHandlerFactory} is responsible for creating things and thing
41  * handlers.
42  *
43  * @author Markus Katter - Initial contribution
44  */
45 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.nuki")
46 @NonNullByDefault
47 public class NukiHandlerFactory extends BaseThingHandlerFactory {
48
49     private final Logger logger = LoggerFactory.getLogger(NukiHandlerFactory.class);
50
51     private final HttpService httpService;
52     private final HttpClient httpClient;
53     private final NetworkAddressService networkAddressService;
54     private @Nullable String callbackUrl;
55     private @Nullable NukiApiServlet nukiApiServlet;
56
57     @Activate
58     public NukiHandlerFactory(@Reference HttpService httpService, @Reference final HttpClientFactory httpClientFactory,
59             @Reference NetworkAddressService networkAddressService) {
60         this.httpService = httpService;
61         this.httpClient = httpClientFactory.getCommonHttpClient();
62         this.networkAddressService = networkAddressService;
63     }
64
65     @Override
66     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
67         return NukiBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
68     }
69
70     @Override
71     protected @Nullable ThingHandler createHandler(Thing thing) {
72         logger.debug("NukiHandlerFactory:createHandler({})", thing);
73         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
74
75         if (NukiBindingConstants.THING_TYPE_BRIDGE_UIDS.contains(thingTypeUID)) {
76             callbackUrl = createCallbackUrl();
77             NukiBridgeHandler nukiBridgeHandler = new NukiBridgeHandler((Bridge) thing, httpClient, callbackUrl);
78             if (!nukiBridgeHandler.isInitializable()) {
79                 return null;
80             }
81             if (nukiApiServlet == null) {
82                 nukiApiServlet = new NukiApiServlet(httpService);
83             }
84             nukiApiServlet.add(nukiBridgeHandler);
85             return nukiBridgeHandler;
86         } else if (NukiBindingConstants.THING_TYPE_SMARTLOCK_UIDS.contains(thingTypeUID)) {
87             return new NukiSmartLockHandler(thing);
88         }
89         logger.trace("No valid Handler found for Thing[{}]!", thingTypeUID);
90         return null;
91     }
92
93     @Override
94     public void unregisterHandler(Thing thing) {
95         super.unregisterHandler(thing);
96         logger.trace("NukiHandlerFactory:unregisterHandler({})", thing);
97         if (thing.getHandler() instanceof NukiBridgeHandler && nukiApiServlet != null) {
98             nukiApiServlet.remove((NukiBridgeHandler) thing.getHandler());
99             if (nukiApiServlet.countNukiBridgeHandlers() == 0) {
100                 nukiApiServlet = null;
101             }
102         }
103     }
104
105     private @Nullable String createCallbackUrl() {
106         logger.trace("createCallbackUrl()");
107         if (callbackUrl != null) {
108             return callbackUrl;
109         }
110         final String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
111         if (ipAddress == null) {
112             logger.warn("No network interface could be found.");
113             return null;
114         }
115         // we do not use SSL as it can cause certificate validation issues.
116         final int port = HttpServiceUtil.getHttpServicePort(bundleContext);
117         if (port == -1) {
118             logger.warn("Cannot find port of the http service.");
119             return null;
120         }
121         ArrayList<String> parameters = new ArrayList<>();
122         parameters.add(ipAddress + ":" + port);
123         String callbackUrl = String.format(NukiBindingConstants.CALLBACK_URL, parameters.toArray());
124         logger.trace("callbackUrl[{}]", callbackUrl);
125         return callbackUrl;
126     }
127 }