2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.smgw.internal;
15 import static org.openhab.binding.smgw.internal.SmgwBindingConstants.*;
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;
38 * The {@link SmgwHandlerFactory} is responsible for creating things and thing
41 * @author Jan N. Klug - Initial contribution
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);
49 private final HttpClient httpClient;
50 private final CronScheduler cronScheduler;
53 public SmgwHandlerFactory(@Reference HttpClientFactory clientFactory, @Reference CronScheduler cronScheduler) {
54 this.cronScheduler = cronScheduler;
55 this.httpClient = clientFactory.createHttpClient("smgw", new SslContextFactory.Client(true));
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");
66 public void deactivate() {
69 } catch (Exception e) {
70 logger.warn("Failed to stop http client: {}", e.getMessage());
75 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
76 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
80 protected @Nullable ThingHandler createHandler(Thing thing) {
81 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
83 if (THING_TYPE_SMGW.equals(thingTypeUID)) {
84 return new SmgwHandler(thing, httpClient, cronScheduler);