]> git.basschouten.com Git - openhab-addons.git/blob
27883b9a143b3d8ba5e75db3ce5477fe36b6c295
[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.konnected.internal;
14
15 import static org.openhab.binding.konnected.internal.KonnectedBindingConstants.*;
16
17 import java.util.Dictionary;
18 import java.util.Set;
19
20 import javax.servlet.ServletException;
21
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;
40
41 /**
42  * The {@link KonnectedHandlerFactory} is responsible for creating things and thing
43  * handlers.
44  *
45  * @author Zachary Christiansen - Initial contribution
46  */
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;
53
54     private HttpService httpService;
55     private String callbackUrl = null;
56     private NetworkAddressService networkAddressService;
57     private KonnectedHTTPServlet servlet;
58
59     @Override
60     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
61         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
62     }
63
64     @Override
65     protected void activate(ComponentContext componentContext) {
66         super.activate(componentContext);
67         Dictionary<String, Object> properties = componentContext.getProperties();
68         callbackUrl = (String) properties.get("callbackUrl");
69         try {
70             this.servlet = registerWebHookServlet();
71         } catch (KonnectedWebHookFail e) {
72             logger.error("Failed registering Konnected servlet - binding is not functional!", e);
73         }
74     }
75
76     @Override
77     protected void deactivate(ComponentContext componentContext) {
78         super.deactivate(componentContext);
79         httpService.unregister(alias);
80     }
81
82     @Override
83     protected @Nullable ThingHandler createHandler(Thing thing) {
84         KonnectedHandler thingHandler = new KonnectedHandler(thing, '/' + BINDING_ID, createCallbackUrl(),
85                 createCallbackPort());
86         if (servlet != null) {
87             logger.debug("Adding thinghandler for thing {} to webhook.", thing.getUID().getId());
88             servlet.add(thingHandler);
89         }
90         return thingHandler;
91     }
92
93     /**
94      * @param thingHandler thing handler to be removed
95      */
96     @Override
97     protected void removeHandler(ThingHandler thingHandler) {
98         servlet.remove((KonnectedHandler) thingHandler);
99         thingHandler.dispose();
100         super.removeHandler(thingHandler);
101     }
102
103     private KonnectedHTTPServlet registerWebHookServlet() throws KonnectedWebHookFail {
104         KonnectedHTTPServlet servlet = new KonnectedHTTPServlet();
105         try {
106             httpService.registerServlet(alias, servlet, null, httpService.createDefaultHttpContext());
107             return servlet;
108         } catch (ServletException | NamespaceException e) {
109             throw new KonnectedWebHookFail("Could not start Konnected Webhook servlet: " + e.getMessage(), e);
110         }
111     }
112
113     @Reference
114     public void setHttpService(HttpService httpService) {
115         this.httpService = httpService;
116     }
117
118     public void unsetHttpService(HttpService httpService) {
119         this.httpService = null;
120     }
121
122     private String createCallbackUrl() {
123         if (callbackUrl != null) {
124             logger.debug("The callback ip address from the OSGI is:{}", callbackUrl);
125             return callbackUrl;
126         } else {
127             final String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
128             if (ipAddress == null) {
129                 logger.warn("No network interface could be found.");
130                 return null;
131             }
132             logger.debug("The callback ip address obtained from the Network Address Service was:{}", ipAddress);
133             return ipAddress;
134         }
135     }
136
137     private String createCallbackPort() {
138         // we do not use SSL as it can cause certificate validation issues.
139         final int port = HttpServiceUtil.getHttpServicePort(bundleContext);
140         if (port == -1) {
141             logger.warn("Cannot find port of the http service.");
142             return null;
143         }
144         logger.debug("the port for the callback is: {}", port);
145         return Integer.toString(port);
146     }
147
148     @Reference
149     protected void setNetworkAddressService(NetworkAddressService networkAddressService) {
150         this.networkAddressService = networkAddressService;
151     }
152
153     protected void unsetNetworkAddressService(NetworkAddressService networkAddressService) {
154         this.networkAddressService = null;
155     }
156 }