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.lgwebos.internal;
15 import static org.openhab.binding.lgwebos.internal.LGWebOSBindingConstants.*;
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.eclipse.jetty.websocket.client.WebSocketClient;
22 import org.openhab.binding.lgwebos.internal.handler.LGWebOSHandler;
23 import org.openhab.core.io.net.http.WebSocketFactory;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.openhab.core.thing.binding.ThingHandlerFactory;
29 import org.osgi.service.component.ComponentContext;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Reference;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * The {@link LGWebOSHandlerFactory} is responsible for creating things and thing
40 * @author Sebastian Prehn - initial contribution
43 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.lgwebos")
44 public class LGWebOSHandlerFactory extends BaseThingHandlerFactory {
45 private final Logger logger = LoggerFactory.getLogger(LGWebOSHandlerFactory.class);
47 private final WebSocketClient webSocketClient;
49 private final LGWebOSStateDescriptionOptionProvider stateDescriptionProvider;
52 public LGWebOSHandlerFactory(final @Reference WebSocketFactory webSocketFactory,
53 final @Reference LGWebOSStateDescriptionOptionProvider stateDescriptionProvider) {
55 * Cannot use openHAB's shared web socket client (webSocketFactory.getCommonWebSocketClient()) as we have to
56 * change client settings.
58 var httpClient = new HttpClient(new SslContextFactory.Client(true));
59 this.webSocketClient = new WebSocketClient(httpClient);
60 this.stateDescriptionProvider = stateDescriptionProvider;
64 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
65 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
69 protected @Nullable ThingHandler createHandler(Thing thing) {
70 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
71 if (thingTypeUID.equals(THING_TYPE_WEBOSTV)) {
72 return new LGWebOSHandler(thing, webSocketClient, stateDescriptionProvider);
78 protected void activate(ComponentContext componentContext) {
79 super.activate(componentContext);
81 // reduce timeout from default 15sec
82 this.webSocketClient.setConnectTimeout(1000);
84 // channel and app listing are json docs up to 4MB
85 this.webSocketClient.getPolicy().setMaxTextMessageSize(4 * 1024 * 1024);
87 // since this is not using openHAB's shared web socket client we need to start and stop
89 this.webSocketClient.start();
90 } catch (Exception e) {
91 logger.warn("Unable to to start websocket client.", e);
96 protected void deactivate(ComponentContext componentContext) {
97 super.deactivate(componentContext);
99 this.webSocketClient.stop();
100 } catch (Exception e) {
101 logger.warn("Unable to to stop websocket client.", e);