]> git.basschouten.com Git - openhab-addons.git/blob
7a5193f75b8366c1ca394837100c2af304bc91e8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 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.constants.NukiBindingConstants;
19 import org.openhab.binding.nuki.internal.constants.NukiLinkBuilder;
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.NukiOpenerHandler;
23 import org.openhab.binding.nuki.internal.handler.NukiSmartLockHandler;
24 import org.openhab.core.id.InstanceUUID;
25 import org.openhab.core.io.net.http.HttpClientFactory;
26 import org.openhab.core.net.HttpServiceUtil;
27 import org.openhab.core.net.NetworkAddressService;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.ManagedThingProvider;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerFactory;
36 import org.osgi.service.component.annotations.Activate;
37 import org.osgi.service.component.annotations.Component;
38 import org.osgi.service.component.annotations.Reference;
39 import org.osgi.service.http.HttpService;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * The {@link NukiHandlerFactory} is responsible for creating things and thing
45  * handlers.
46  *
47  * @author Markus Katter - Initial contribution
48  * @author Jan Vybíral - Improved thing id generation
49  */
50 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.nuki")
51 @NonNullByDefault
52 public class NukiHandlerFactory extends BaseThingHandlerFactory {
53
54     private final Logger logger = LoggerFactory.getLogger(NukiHandlerFactory.class);
55
56     private final HttpClient httpClient;
57     private final NetworkAddressService networkAddressService;
58     private final ManagedThingProvider managedThingProvider;
59     private NukiApiServlet nukiApiServlet;
60
61     @Activate
62     public NukiHandlerFactory(@Reference HttpService httpService, @Reference final HttpClientFactory httpClientFactory,
63             @Reference NetworkAddressService networkAddressService,
64             @Reference ManagedThingProvider managedThingProvider) {
65         this.httpClient = httpClientFactory.getCommonHttpClient();
66         this.networkAddressService = networkAddressService;
67         this.nukiApiServlet = new NukiApiServlet(httpService);
68         this.managedThingProvider = managedThingProvider;
69     }
70
71     @Override
72     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
73         return NukiBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
74     }
75
76     @Override
77     protected @Nullable ThingHandler createHandler(Thing thing) {
78         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
79         boolean readOnly = managedThingProvider.get(thing.getUID()) == null;
80
81         if (NukiBindingConstants.THING_TYPE_BRIDGE_UIDS.contains(thingTypeUID)) {
82             String callbackUrl = createCallbackUrl(InstanceUUID.get());
83             NukiBridgeHandler nukiBridgeHandler = new NukiBridgeHandler((Bridge) thing, httpClient, callbackUrl);
84             nukiApiServlet.add(nukiBridgeHandler);
85             return nukiBridgeHandler;
86         } else if (NukiBindingConstants.THING_TYPE_SMARTLOCK_UIDS.contains(thingTypeUID)) {
87             return new NukiSmartLockHandler(thing, readOnly);
88         } else if (NukiBindingConstants.THING_TYPE_OPENER_UIDS.contains(thingTypeUID)) {
89             return new NukiOpenerHandler(thing, readOnly);
90         }
91         logger.warn("No valid Handler found for Thing[{}]!", thingTypeUID);
92         return null;
93     }
94
95     @Override
96     public void removeThing(ThingUID thingUID) {
97         super.removeThing(thingUID);
98     }
99
100     @Override
101     public void unregisterHandler(Thing thing) {
102         super.unregisterHandler(thing);
103         ThingHandler handler = thing.getHandler();
104         if (handler instanceof NukiBridgeHandler bridgeHandler) {
105             nukiApiServlet.remove(bridgeHandler);
106         }
107     }
108
109     private @Nullable String createCallbackUrl(String id) {
110         final String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
111         if (ipAddress == null) {
112             logger.warn("No network interface could be found to get callback address");
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         String callbackUrl = NukiLinkBuilder.callbackUri(ipAddress, port, id).toString();
122         logger.trace("callbackUrl[{}]", callbackUrl);
123         return callbackUrl;
124     }
125 }