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.util.ssl.SslContextFactory;
20 import org.eclipse.jetty.websocket.client.WebSocketClient;
21 import org.openhab.binding.lgwebos.internal.handler.LGWebOSHandler;
22 import org.openhab.core.io.net.http.WebSocketFactory;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingTypeUID;
25 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
26 import org.openhab.core.thing.binding.ThingHandler;
27 import org.openhab.core.thing.binding.ThingHandlerFactory;
28 import org.osgi.service.component.ComponentContext;
29 import org.osgi.service.component.annotations.Activate;
30 import org.osgi.service.component.annotations.Component;
31 import org.osgi.service.component.annotations.Reference;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The {@link LGWebOSHandlerFactory} is responsible for creating things and thing
39 * @author Sebastian Prehn - initial contribution
42 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.lgwebos")
43 public class LGWebOSHandlerFactory extends BaseThingHandlerFactory {
44 private final Logger logger = LoggerFactory.getLogger(LGWebOSHandlerFactory.class);
46 private final WebSocketClient webSocketClient;
48 private final LGWebOSStateDescriptionOptionProvider stateDescriptionProvider;
51 public LGWebOSHandlerFactory(final @Reference WebSocketFactory webSocketFactory,
52 final @Reference LGWebOSStateDescriptionOptionProvider stateDescriptionProvider) {
54 * Cannot use openHAB's shared web socket client (webSocketFactory.getCommonWebSocketClient()) as we have to
55 * change client settings.
57 this.webSocketClient = webSocketFactory.createWebSocketClient(BINDING_ID, new SslContextFactory.Client(true));
58 this.stateDescriptionProvider = stateDescriptionProvider;
62 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
63 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
67 protected @Nullable ThingHandler createHandler(Thing thing) {
68 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
69 if (thingTypeUID.equals(THING_TYPE_WEBOSTV)) {
70 return new LGWebOSHandler(thing, webSocketClient, stateDescriptionProvider);
76 protected void activate(ComponentContext componentContext) {
77 super.activate(componentContext);
79 // reduce timeout from default 15sec
80 this.webSocketClient.setConnectTimeout(1000);
82 // channel and app listing are json docs up to 4MB
83 this.webSocketClient.getPolicy().setMaxTextMessageSize(4 * 1024 * 1024);
85 // since this is not using openHAB's shared web socket client we need to start and stop
87 this.webSocketClient.start();
88 } catch (Exception e) {
89 logger.warn("Unable to to start websocket client.", e);
94 protected void deactivate(ComponentContext componentContext) {
95 super.deactivate(componentContext);
97 this.webSocketClient.stop();
98 } catch (Exception e) {
99 logger.warn("Unable to to stop websocket client.", e);