2 * Copyright (c) 2010-2021 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 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.dataexchange.NukiApiServlet;
19 import org.openhab.binding.nuki.internal.handler.NukiBridgeHandler;
20 import org.openhab.binding.nuki.internal.handler.NukiSmartLockHandler;
21 import org.openhab.core.io.net.http.HttpClientFactory;
22 import org.openhab.core.net.HttpServiceUtil;
23 import org.openhab.core.net.NetworkAddressService;
24 import org.openhab.core.thing.Bridge;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
28 import org.openhab.core.thing.binding.ThingHandler;
29 import org.openhab.core.thing.binding.ThingHandlerFactory;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Reference;
33 import org.osgi.service.http.HttpService;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link NukiHandlerFactory} is responsible for creating things and thing
41 * @author Markus Katter - Initial contribution
43 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.nuki")
45 public class NukiHandlerFactory extends BaseThingHandlerFactory {
47 private final Logger logger = LoggerFactory.getLogger(NukiHandlerFactory.class);
49 private final HttpService httpService;
50 private final HttpClient httpClient;
51 private final NetworkAddressService networkAddressService;
52 private @Nullable String callbackUrl;
53 private @Nullable NukiApiServlet nukiApiServlet;
56 public NukiHandlerFactory(@Reference HttpService httpService, @Reference final HttpClientFactory httpClientFactory,
57 @Reference NetworkAddressService networkAddressService) {
58 this.httpService = httpService;
59 this.httpClient = httpClientFactory.getCommonHttpClient();
60 this.networkAddressService = networkAddressService;
64 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
65 return NukiBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
69 protected @Nullable ThingHandler createHandler(Thing thing) {
70 logger.debug("NukiHandlerFactory:createHandler({})", thing);
71 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
73 if (NukiBindingConstants.THING_TYPE_BRIDGE_UIDS.contains(thingTypeUID)) {
74 callbackUrl = createCallbackUrl();
75 NukiBridgeHandler nukiBridgeHandler = new NukiBridgeHandler((Bridge) thing, httpClient, callbackUrl);
76 if (!nukiBridgeHandler.isInitializable()) {
79 if (nukiApiServlet == null) {
80 nukiApiServlet = new NukiApiServlet(httpService);
82 nukiApiServlet.add(nukiBridgeHandler);
83 return nukiBridgeHandler;
84 } else if (NukiBindingConstants.THING_TYPE_SMARTLOCK_UIDS.contains(thingTypeUID)) {
85 return new NukiSmartLockHandler(thing);
87 logger.trace("No valid Handler found for Thing[{}]!", thingTypeUID);
92 public void unregisterHandler(Thing thing) {
93 super.unregisterHandler(thing);
94 logger.trace("NukiHandlerFactory:unregisterHandler({})", thing);
95 if (thing.getHandler() instanceof NukiBridgeHandler && nukiApiServlet != null) {
96 nukiApiServlet.remove((NukiBridgeHandler) thing.getHandler());
97 if (nukiApiServlet.countNukiBridgeHandlers() == 0) {
98 nukiApiServlet = null;
103 private @Nullable String createCallbackUrl() {
104 logger.trace("createCallbackUrl()");
105 if (callbackUrl != null) {
108 final String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
109 if (ipAddress == null) {
110 logger.warn("No network interface could be found.");
113 // we do not use SSL as it can cause certificate validation issues.
114 final int port = HttpServiceUtil.getHttpServicePort(bundleContext);
116 logger.warn("Cannot find port of the http service.");
119 String callbackUrl = String.format(NukiBindingConstants.CALLBACK_URL, ipAddress + ":" + port);
120 logger.trace("callbackUrl[{}]", callbackUrl);