]> git.basschouten.com Git - openhab-addons.git/blob
36d7d395f3ffcdb48769c9c7e7cd20d104d9d2d5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.unifi.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.eclipse.jetty.util.ssl.SslContextFactory;
19 import org.openhab.binding.unifi.internal.handler.UniFiClientThingHandler;
20 import org.openhab.binding.unifi.internal.handler.UniFiControllerThingHandler;
21 import org.openhab.core.io.net.http.HttpClientFactory;
22 import org.openhab.core.io.net.http.HttpClientInitializationException;
23 import org.openhab.core.thing.Bridge;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.openhab.core.thing.binding.ThingHandlerFactory;
29 import org.osgi.service.component.annotations.Activate;
30 import org.osgi.service.component.annotations.Component;
31 import org.osgi.service.component.annotations.Reference;
32
33 /**
34  * The {@link UniFiThingHandlerFactory} is responsible for creating things and thing
35  * handlers.
36  *
37  * @author Matthew Bowman - Initial contribution
38  */
39 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.unifi")
40 @NonNullByDefault
41 public class UniFiThingHandlerFactory extends BaseThingHandlerFactory {
42
43     private final HttpClient httpClient;
44
45     @Activate
46     public UniFiThingHandlerFactory(@Reference final HttpClientFactory httpClientFactory) {
47         // [wip] mgb: disabled due to missing common name attributes with certs
48         // this.httpClient = httpClientFactory.getCommonHttpClient();
49         httpClient = new HttpClient(new SslContextFactory.Client(true));
50         try {
51             httpClient.start();
52         } catch (Exception e) {
53             throw new HttpClientInitializationException("Could not start HttpClient", e);
54         }
55     }
56
57     @Override
58     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
59         return UniFiControllerThingHandler.supportsThingType(thingTypeUID)
60                 || UniFiClientThingHandler.supportsThingType(thingTypeUID);
61     }
62
63     @Override
64     protected @Nullable ThingHandler createHandler(Thing thing) {
65         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
66         if (UniFiControllerThingHandler.supportsThingType(thingTypeUID)) {
67             return new UniFiControllerThingHandler((Bridge) thing, httpClient);
68         } else if (UniFiClientThingHandler.supportsThingType(thingTypeUID)) {
69             return new UniFiClientThingHandler(thing);
70         }
71         return null;
72     }
73 }