2 * Copyright (c) 2010-2024 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.samsungtv.internal.protocol;
15 import java.io.IOException;
17 import java.util.concurrent.Future;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.websocket.api.Session;
22 import org.eclipse.jetty.websocket.api.WebSocketAdapter;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * Websocket base class
29 * @author Arjan Mels - Initial contribution
32 class WebSocketBase extends WebSocketAdapter {
33 private final Logger logger = LoggerFactory.getLogger(WebSocketBase.class);
37 final RemoteControllerWebSocket remoteControllerWebSocket;
39 private @Nullable Future<?> sessionFuture;
42 * @param remoteControllerWebSocket
44 WebSocketBase(RemoteControllerWebSocket remoteControllerWebSocket) {
45 this.remoteControllerWebSocket = remoteControllerWebSocket;
48 boolean isConnecting = false;
51 public void onWebSocketClose(int statusCode, @Nullable String reason) {
52 logger.debug("{} connection closed: {} - {}", this.getClass().getSimpleName(), statusCode, reason);
53 super.onWebSocketClose(statusCode, reason);
58 public void onWebSocketError(@Nullable Throwable error) {
59 if (logger.isTraceEnabled()) {
60 logger.trace("{} connection error", this.getClass().getSimpleName(), error);
62 logger.debug("{} connection error", this.getClass().getSimpleName());
64 super.onWebSocketError(error);
68 void connect(URI uri) throws RemoteControllerException {
69 if (isConnecting || isConnected()) {
70 logger.trace("{} already connecting or connected", this.getClass().getSimpleName());
74 logger.debug("{} connecting to: {}", this.getClass().getSimpleName(), uri);
78 sessionFuture = remoteControllerWebSocket.client.connect(this, uri);
79 logger.trace("Connecting session Future: {}", sessionFuture);
80 } catch (IOException | IllegalStateException e) {
81 throw new RemoteControllerException(e);
86 public void onWebSocketConnect(@Nullable Session session) {
87 logger.debug("{} connection established: {}", this.getClass().getSimpleName(),
88 session != null ? session.getRemoteAddress().getHostString() : "");
89 super.onWebSocketConnect(session);
95 logger.debug("{} connection close requested", this.getClass().getSimpleName());
97 Session session = getSession();
98 if (session != null) {
102 final Future<?> sessionFuture = this.sessionFuture;
103 logger.trace("Closing session Future: {}", sessionFuture);
104 if (sessionFuture != null && !sessionFuture.isDone()) {
105 sessionFuture.cancel(true);
109 void sendCommand(String cmd) {
112 getRemote().sendString(cmd);
113 logger.trace("{}: sendCommand: {}", this.getClass().getSimpleName(), cmd);
115 logger.warn("{} sending command while socket not connected: {}", this.getClass().getSimpleName(), cmd);
116 // retry opening connection just in case
117 remoteControllerWebSocket.openConnection();
119 getRemote().sendString(cmd);
120 logger.trace("{}: sendCommand: {}", this.getClass().getSimpleName(), cmd);
122 } catch (IOException | RemoteControllerException e) {
123 logger.warn("{}: cannot send command", this.getClass().getSimpleName(), e);
128 public void onWebSocketText(@Nullable String str) {
129 logger.trace("{}: onWebSocketText: {}", this.getClass().getSimpleName(), str);