]> git.basschouten.com Git - openhab-addons.git/blob
af54aeca4c51224e1b3a531e19b380038723e649
[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.smgw.internal;
14
15 import static org.openhab.binding.smgw.internal.SmgwBindingConstants.*;
16
17 import java.util.Set;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.eclipse.jetty.util.ssl.SslContextFactory;
23 import org.openhab.core.io.net.http.HttpClientFactory;
24 import org.openhab.core.scheduler.CronScheduler;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
28 import org.openhab.core.thing.binding.ThingHandler;
29 import org.openhab.core.thing.binding.ThingHandlerFactory;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Deactivate;
33 import org.osgi.service.component.annotations.Reference;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * The {@link SmgwHandlerFactory} is responsible for creating things and thing
39  * handlers.
40  *
41  * @author Jan N. Klug - Initial contribution
42  */
43 @NonNullByDefault
44 @Component(service = ThingHandlerFactory.class)
45 public class SmgwHandlerFactory extends BaseThingHandlerFactory {
46     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_SMGW);
47     private final Logger logger = LoggerFactory.getLogger(SmgwHandlerFactory.class);
48
49     private final HttpClient httpClient;
50     private final CronScheduler cronScheduler;
51
52     @Activate
53     public SmgwHandlerFactory(@Reference HttpClientFactory clientFactory, @Reference CronScheduler cronScheduler) {
54         this.cronScheduler = cronScheduler;
55         this.httpClient = clientFactory.createHttpClient("smgw", new SslContextFactory.Client(true));
56         try {
57             this.httpClient.start();
58         } catch (Exception e) {
59             // catching exception is necessary due to the signature of HttpClient.start()
60             logger.warn("Failed to start http client: {}", e.getMessage());
61             throw new IllegalStateException("Could not create HttpClient");
62         }
63     }
64
65     @Deactivate
66     public void deactivate() {
67         try {
68             httpClient.stop();
69         } catch (Exception e) {
70             logger.warn("Failed to stop http client: {}", e.getMessage());
71         }
72     }
73
74     @Override
75     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
76         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
77     }
78
79     @Override
80     protected @Nullable ThingHandler createHandler(Thing thing) {
81         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
82
83         if (THING_TYPE_SMGW.equals(thingTypeUID)) {
84             return new SmgwHandler(thing, httpClient, cronScheduler);
85         }
86
87         return null;
88     }
89 }