]> git.basschouten.com Git - openhab-addons.git/blob
bf9d788088d881da3eb3ebe675c55f149fe54dac
[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.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;
35
36 /**
37  * The {@link LGWebOSHandlerFactory} is responsible for creating things and thing
38  * handlers.
39  *
40  * @author Sebastian Prehn - initial contribution
41  */
42 @NonNullByDefault
43 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.lgwebos")
44 public class LGWebOSHandlerFactory extends BaseThingHandlerFactory {
45     private final Logger logger = LoggerFactory.getLogger(LGWebOSHandlerFactory.class);
46
47     private final WebSocketClient webSocketClient;
48
49     private final LGWebOSStateDescriptionOptionProvider stateDescriptionProvider;
50
51     @Activate
52     public LGWebOSHandlerFactory(final @Reference WebSocketFactory webSocketFactory,
53             final @Reference LGWebOSStateDescriptionOptionProvider stateDescriptionProvider) {
54         /*
55          * Cannot use openHAB's shared web socket client (webSocketFactory.getCommonWebSocketClient()) as we have to
56          * change client settings.
57          */
58         var httpClient = new HttpClient(new SslContextFactory.Client(true));
59         this.webSocketClient = new WebSocketClient(httpClient);
60         this.stateDescriptionProvider = stateDescriptionProvider;
61     }
62
63     @Override
64     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
65         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
66     }
67
68     @Override
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);
73         }
74         return null;
75     }
76
77     @Override
78     protected void activate(ComponentContext componentContext) {
79         super.activate(componentContext);
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 }