]> git.basschouten.com Git - openhab-addons.git/blob
78971286f4daed78e1416a42697f77b76c7ecb71
[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.mynice.internal;
14
15 import static org.openhab.binding.mynice.internal.MyNiceBindingConstants.*;
16
17 import java.security.KeyManagementException;
18 import java.security.NoSuchAlgorithmException;
19 import java.util.Set;
20
21 import javax.net.ssl.SSLContext;
22 import javax.net.ssl.SSLSocketFactory;
23 import javax.net.ssl.TrustManager;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.mynice.internal.handler.GateHandler;
28 import org.openhab.binding.mynice.internal.handler.It4WifiHandler;
29 import org.openhab.core.io.net.http.TrustAllTrustManager;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingTypeUID;
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
39 /**
40  * The {@link MyNiceHandlerFactory} is responsible for creating thing handlers.
41  *
42  * @author GaĆ«l L'hopital - Initial contribution
43  */
44 @NonNullByDefault
45 @Component(configurationPid = "binding.mynice", service = ThingHandlerFactory.class)
46 public class MyNiceHandlerFactory extends BaseThingHandlerFactory {
47     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(BRIDGE_TYPE_IT4WIFI, THING_TYPE_SWING,
48             THING_TYPE_SLIDING);
49     private final SSLSocketFactory socketFactory;
50
51     @Activate
52     public MyNiceHandlerFactory() {
53         try {
54             SSLContext sslContext = SSLContext.getInstance("SSL");
55             sslContext.init(null, new TrustManager[] { TrustAllTrustManager.getInstance() }, null);
56             socketFactory = sslContext.getSocketFactory();
57         } catch (NoSuchAlgorithmException | KeyManagementException e) {
58             throw new IllegalArgumentException(e);
59         }
60     }
61
62     @Override
63     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
64         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
65     }
66
67     @Override
68     protected @Nullable ThingHandler createHandler(Thing thing) {
69         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
70
71         if (BRIDGE_TYPE_IT4WIFI.equals(thingTypeUID)) {
72             return new It4WifiHandler((Bridge) thing, socketFactory);
73         } else if (THING_TYPE_SWING.equals(thingTypeUID)) {
74             return new GateHandler(thing);
75         } else if (THING_TYPE_SLIDING.equals(thingTypeUID)) {
76             return new GateHandler(thing);
77         }
78
79         return null;
80     }
81 }