2 * Copyright (c) 2010-2020 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.nuki.internal;
15 import java.util.ArrayList;
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;
40 * The {@link NukiHandlerFactory} is responsible for creating things and thing
43 * @author Markus Katter - Initial contribution
45 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.nuki")
47 public class NukiHandlerFactory extends BaseThingHandlerFactory {
49 private final Logger logger = LoggerFactory.getLogger(NukiHandlerFactory.class);
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;
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;
66 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
67 return NukiBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
71 protected @Nullable ThingHandler createHandler(Thing thing) {
72 logger.debug("NukiHandlerFactory:createHandler({})", thing);
73 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
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()) {
81 if (nukiApiServlet == null) {
82 nukiApiServlet = new NukiApiServlet(httpService);
84 nukiApiServlet.add(nukiBridgeHandler);
85 return nukiBridgeHandler;
86 } else if (NukiBindingConstants.THING_TYPE_SMARTLOCK_UIDS.contains(thingTypeUID)) {
87 return new NukiSmartLockHandler(thing);
89 logger.trace("No valid Handler found for Thing[{}]!", thingTypeUID);
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;
105 private @Nullable String createCallbackUrl() {
106 logger.trace("createCallbackUrl()");
107 if (callbackUrl != null) {
110 final String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
111 if (ipAddress == null) {
112 logger.warn("No network interface could be found.");
115 // we do not use SSL as it can cause certificate validation issues.
116 final int port = HttpServiceUtil.getHttpServicePort(bundleContext);
118 logger.warn("Cannot find port of the http service.");
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);