2 * Copyright (c) 2010-2023 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.konnected.internal;
15 import static org.openhab.binding.konnected.internal.KonnectedBindingConstants.*;
17 import java.util.Dictionary;
20 import javax.servlet.ServletException;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.konnected.internal.handler.KonnectedHandler;
24 import org.openhab.binding.konnected.internal.servlet.KonnectedHTTPServlet;
25 import org.openhab.binding.konnected.internal.servlet.KonnectedWebHookFail;
26 import org.openhab.core.net.HttpServiceUtil;
27 import org.openhab.core.net.NetworkAddressService;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerFactory;
33 import org.osgi.service.component.ComponentContext;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Reference;
36 import org.osgi.service.http.HttpService;
37 import org.osgi.service.http.NamespaceException;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * The {@link KonnectedHandlerFactory} is responsible for creating things and thing
45 * @author Zachary Christiansen - Initial contribution
47 @Component(configurationPid = "binding.konnected", service = ThingHandlerFactory.class)
48 public class KonnectedHandlerFactory extends BaseThingHandlerFactory {
49 private final Logger logger = LoggerFactory.getLogger(KonnectedHandlerFactory.class);
50 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_PROMODULE,
51 THING_TYPE_WIFIMODULE);
52 private static final String alias = "/" + BINDING_ID;
54 private HttpService httpService;
55 private String callbackUrl = null;
56 private NetworkAddressService networkAddressService;
57 private KonnectedHTTPServlet servlet;
60 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
61 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
65 protected void activate(ComponentContext componentContext) {
66 super.activate(componentContext);
67 Dictionary<String, Object> properties = componentContext.getProperties();
68 callbackUrl = (String) properties.get(CALLBACK_URL);
69 logger.debug("Callback URL from OSGI service: {}", callbackUrl);
71 this.servlet = registerWebHookServlet();
72 } catch (KonnectedWebHookFail e) {
73 logger.error("Failed registering Konnected servlet - binding is not functional!", e);
78 protected void deactivate(ComponentContext componentContext) {
79 super.deactivate(componentContext);
80 httpService.unregister(alias);
84 protected @Nullable ThingHandler createHandler(Thing thing) {
86 KonnectedHandler thingHandler = new KonnectedHandler(thing, getCallbackUrl());
87 if (servlet != null) {
88 logger.debug("Adding thinghandler for thing {} to webhook.", thing.getUID().getId());
89 servlet.add(thingHandler);
95 * @param thingHandler thing handler to be removed
98 protected void removeHandler(ThingHandler thingHandler) {
99 servlet.remove((KonnectedHandler) thingHandler);
100 thingHandler.dispose();
101 super.removeHandler(thingHandler);
104 private KonnectedHTTPServlet registerWebHookServlet() throws KonnectedWebHookFail {
105 KonnectedHTTPServlet servlet = new KonnectedHTTPServlet();
107 httpService.registerServlet(alias, servlet, null, httpService.createDefaultHttpContext());
109 } catch (ServletException | NamespaceException e) {
110 throw new KonnectedWebHookFail("Could not start Konnected Webhook servlet: " + e.getMessage(), e);
115 public void setHttpService(HttpService httpService) {
116 this.httpService = httpService;
119 public void unsetHttpService(HttpService httpService) {
120 this.httpService = null;
123 private String getCallbackUrl() {
124 if (callbackUrl == null) {
125 String callbackIP = discoverCallbackIP();
126 String callbackPort = discoverCallbackPort();
127 if (callbackPort != null && callbackIP != null) {
128 callbackUrl = "http://" + discoverCallbackIP() + ":" + discoverCallbackPort() + '/' + BINDING_ID;
134 private String discoverCallbackIP() {
135 final String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
136 if (ipAddress == null) {
137 logger.warn("No network interface could be found.");
140 logger.debug("The callback ip address obtained from the Network Address Service was:{}", ipAddress);
144 private String discoverCallbackPort() {
145 // we do not use SSL as it can cause certificate validation issues.
146 final int port = HttpServiceUtil.getHttpServicePort(bundleContext);
148 logger.warn("Cannot find port of the http service.");
151 logger.debug("the port for the callback is: {}", port);
152 return Integer.toString(port);
156 protected void setNetworkAddressService(NetworkAddressService networkAddressService) {
157 this.networkAddressService = networkAddressService;
160 protected void unsetNetworkAddressService(NetworkAddressService networkAddressService) {
161 this.networkAddressService = null;