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