]> git.basschouten.com Git - openhab-addons.git/blob
ddca4f06dc9b4e0a2c6e33438781b6cc721b12da
[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.unifi.internal;
14
15 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.ALL_THING_TYPE_SUPPORTED;
16 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.THING_TYPE_CONTROLLER;
17 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.THING_TYPE_POE_PORT;
18 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.THING_TYPE_SITE;
19 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.THING_TYPE_WIRED_CLIENT;
20 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.THING_TYPE_WIRELESS_CLIENT;
21 import static org.openhab.binding.unifi.internal.UniFiBindingConstants.THING_TYPE_WLAN;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.eclipse.jetty.client.HttpClient;
26 import org.eclipse.jetty.util.ssl.SslContextFactory;
27 import org.openhab.binding.unifi.internal.handler.UniFiClientThingHandler;
28 import org.openhab.binding.unifi.internal.handler.UniFiControllerThingHandler;
29 import org.openhab.binding.unifi.internal.handler.UniFiPoePortThingHandler;
30 import org.openhab.binding.unifi.internal.handler.UniFiSiteThingHandler;
31 import org.openhab.binding.unifi.internal.handler.UniFiWlanThingHandler;
32 import org.openhab.core.io.net.http.HttpClientFactory;
33 import org.openhab.core.io.net.http.HttpClientInitializationException;
34 import org.openhab.core.thing.Bridge;
35 import org.openhab.core.thing.Thing;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
38 import org.openhab.core.thing.binding.ThingHandler;
39 import org.openhab.core.thing.binding.ThingHandlerFactory;
40 import org.osgi.service.component.ComponentContext;
41 import org.osgi.service.component.annotations.Activate;
42 import org.osgi.service.component.annotations.Component;
43 import org.osgi.service.component.annotations.Reference;
44
45 /**
46  * The {@link UniFiThingHandlerFactory} is responsible for creating things and thing
47  * handlers.
48  *
49  * @author Matthew Bowman - Initial contribution
50  */
51 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.unifi")
52 @NonNullByDefault
53 public class UniFiThingHandlerFactory extends BaseThingHandlerFactory {
54
55     private final HttpClient httpClient;
56
57     @Activate
58     public UniFiThingHandlerFactory(@Reference final HttpClientFactory httpClientFactory) {
59         // [wip] mgb: disabled due to missing common name attributes with certs
60         // this.httpClient = httpClientFactory.getCommonHttpClient();
61         httpClient = new HttpClient(new SslContextFactory.Client(true));
62         try {
63             httpClient.start();
64         } catch (final Exception e) {
65             throw new HttpClientInitializationException("Could not start HttpClient", e);
66         }
67     }
68
69     @Override
70     protected void deactivate(final ComponentContext componentContext) {
71         try {
72             httpClient.stop();
73         } catch (final Exception e) {
74             // Eat http client stop exception.
75         } finally {
76             super.deactivate(componentContext);
77         }
78     }
79
80     @Override
81     public boolean supportsThingType(final ThingTypeUID thingTypeUID) {
82         return ALL_THING_TYPE_SUPPORTED.contains(thingTypeUID);
83     }
84
85     @Override
86     protected @Nullable ThingHandler createHandler(final Thing thing) {
87         final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
88         if (THING_TYPE_CONTROLLER.equals(thingTypeUID)) {
89             return new UniFiControllerThingHandler((Bridge) thing, httpClient);
90         } else if (THING_TYPE_SITE.equals(thingTypeUID)) {
91             return new UniFiSiteThingHandler(thing);
92         } else if (THING_TYPE_WLAN.equals(thingTypeUID)) {
93             return new UniFiWlanThingHandler(thing);
94         } else if (THING_TYPE_WIRELESS_CLIENT.equals(thingTypeUID) || THING_TYPE_WIRED_CLIENT.equals(thingTypeUID)) {
95             return new UniFiClientThingHandler(thing);
96         } else if (THING_TYPE_POE_PORT.equals(thingTypeUID)) {
97             return new UniFiPoePortThingHandler(thing);
98         }
99         return null;
100     }
101 }