]> git.basschouten.com Git - openhab-addons.git/blob
85ee23609c8b540c2447024d6c2d9ab7df474618
[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.lgwebos.internal;
14
15 import static org.openhab.binding.lgwebos.internal.LGWebOSBindingConstants.*;
16
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;
33
34 /**
35  * The {@link LGWebOSHandlerFactory} is responsible for creating things and thing
36  * handlers.
37  *
38  * @author Sebastian Prehn - initial contribution
39  */
40 @NonNullByDefault
41 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.lgwebos")
42 public class LGWebOSHandlerFactory extends BaseThingHandlerFactory {
43     private final Logger logger = LoggerFactory.getLogger(LGWebOSHandlerFactory.class);
44
45     private final WebSocketClient webSocketClient;
46
47     private final LGWebOSStateDescriptionOptionProvider stateDescriptionProvider;
48
49     @Activate
50     public LGWebOSHandlerFactory(final @Reference WebSocketFactory webSocketFactory,
51             final @Reference LGWebOSStateDescriptionOptionProvider stateDescriptionProvider) {
52         /*
53          * Cannot use openHAB's shared web socket client (webSocketFactory.getCommonWebSocketClient()) as we have to
54          * change client settings.
55          */
56         this.webSocketClient = webSocketFactory.createWebSocketClient("lgwebos");
57         this.stateDescriptionProvider = stateDescriptionProvider;
58     }
59
60     @Override
61     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
62         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
63     }
64
65     @Override
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);
70         }
71         return null;
72     }
73
74     @Override
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");
80
81         // reduce timeout from default 15sec
82         this.webSocketClient.setConnectTimeout(1000);
83
84         // channel and app listing are json docs up to 4MB
85         this.webSocketClient.getPolicy().setMaxTextMessageSize(4 * 1024 * 1024);
86
87         // since this is not using openHAB's shared web socket client we need to start and stop
88         try {
89             this.webSocketClient.start();
90         } catch (Exception e) {
91             logger.warn("Unable to to start websocket client.", e);
92         }
93     }
94
95     @Override
96     protected void deactivate(ComponentContext componentContext) {
97         super.deactivate(componentContext);
98         try {
99             this.webSocketClient.stop();
100         } catch (Exception e) {
101             logger.warn("Unable to to stop websocket client.", e);
102         }
103     }
104 }