]> git.basschouten.com Git - openhab-addons.git/blob
f8bc1cfdc890c279d7fe9a0116056e6f095c0653
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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         httpClient = httpClientFactory.createHttpClient(BINDING_ID, new SslContextFactory.Client(true));
54         try {
55             httpClient.start();
56         } catch (final Exception e) {
57             throw new HttpClientInitializationException("Could not start HttpClient", e);
58         }
59     }
60
61     @Override
62     protected void deactivate(final ComponentContext componentContext) {
63         try {
64             httpClient.stop();
65         } catch (final Exception e) {
66             // Eat http client stop exception.
67         } finally {
68             super.deactivate(componentContext);
69         }
70     }
71
72     @Override
73     public boolean supportsThingType(final ThingTypeUID thingTypeUID) {
74         return ALL_THING_TYPE_SUPPORTED.contains(thingTypeUID);
75     }
76
77     @Override
78     protected @Nullable ThingHandler createHandler(final Thing thing) {
79         final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
80         if (THING_TYPE_CONTROLLER.equals(thingTypeUID)) {
81             return new UniFiControllerThingHandler((Bridge) thing, httpClient);
82         } else if (THING_TYPE_SITE.equals(thingTypeUID)) {
83             return new UniFiSiteThingHandler(thing);
84         } else if (THING_TYPE_WLAN.equals(thingTypeUID)) {
85             return new UniFiWlanThingHandler(thing);
86         } else if (THING_TYPE_WIRELESS_CLIENT.equals(thingTypeUID) || THING_TYPE_WIRED_CLIENT.equals(thingTypeUID)) {
87             return new UniFiClientThingHandler(thing);
88         } else if (THING_TYPE_POE_PORT.equals(thingTypeUID)) {
89             return new UniFiPoePortThingHandler(thing);
90         }
91         return null;
92     }
93 }