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