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.websocket.client.WebSocketClient;
20 import org.openhab.binding.lgwebos.internal.handler.LGWebOSHandler;
21 import org.openhab.core.io.net.http.WebSocketFactory;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingTypeUID;
24 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
25 import org.openhab.core.thing.binding.ThingHandler;
26 import org.openhab.core.thing.binding.ThingHandlerFactory;
27 import org.osgi.service.component.ComponentContext;
28 import org.osgi.service.component.annotations.Activate;
29 import org.osgi.service.component.annotations.Component;
30 import org.osgi.service.component.annotations.Reference;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * The {@link LGWebOSHandlerFactory} is responsible for creating things and thing
38 * @author Sebastian Prehn - initial contribution
41 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.lgwebos")
42 public class LGWebOSHandlerFactory extends BaseThingHandlerFactory {
43 private final Logger logger = LoggerFactory.getLogger(LGWebOSHandlerFactory.class);
45 private final WebSocketClient webSocketClient;
47 private final LGWebOSStateDescriptionOptionProvider stateDescriptionProvider;
50 public LGWebOSHandlerFactory(final @Reference WebSocketFactory webSocketFactory,
51 final @Reference LGWebOSStateDescriptionOptionProvider stateDescriptionProvider) {
53 * Cannot use openHAB's shared web socket client (webSocketFactory.getCommonWebSocketClient()) as we have to
54 * change client settings.
56 this.webSocketClient = webSocketFactory.createWebSocketClient("lgwebos");
57 this.stateDescriptionProvider = stateDescriptionProvider;
61 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
62 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
66 protected @Nullable ThingHandler createHandler(Thing thing) {
67 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
68 if (thingTypeUID.equals(THING_TYPE_WEBOSTV)) {
69 return new LGWebOSHandler(thing, webSocketClient, stateDescriptionProvider);
75 protected void activate(ComponentContext componentContext) {
76 super.activate(componentContext);
77 // LGWebOS TVs only support WEAK cipher suites, thus not using SSL.
78 // SslContextFactory sslContextFactory = new SslContextFactory(true);
79 // sslContextFactory.addExcludeProtocols("tls/1.3");
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);