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.samsungtv.internal.protocol;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 import com.google.gson.JsonSyntaxException;
23 * Websocket class to retrieve app status
25 * @author Arjan Mels - Initial contribution
28 class WebSocketV2 extends WebSocketBase {
29 private final Logger logger = LoggerFactory.getLogger(WebSocketV2.class);
31 WebSocketV2(RemoteControllerWebSocket remoteControllerWebSocket) {
32 super(remoteControllerWebSocket);
35 @SuppressWarnings("unused")
37 private static class JSONMessage {
67 public void onWebSocketText(@Nullable String msgarg) {
71 String msg = msgarg.replace('\n', ' ');
72 super.onWebSocketText(msg);
74 JSONMessage jsonMsg = this.remoteControllerWebSocket.gson.fromJson(msg, JSONMessage.class);
76 if (jsonMsg.result != null) {
77 handleResult(jsonMsg);
80 if (jsonMsg.error != null) {
81 logger.debug("WebSocketV2 Error received: {}", msg);
84 if (jsonMsg.event == null) {
85 logger.debug("WebSocketV2 Unknown response format: {}", msg);
89 switch (jsonMsg.event) {
90 case "ms.channel.connect":
91 logger.debug("V2 channel connected. Token = {}", jsonMsg.data.token);
93 // update is requested from ed.installedApp.get event: small risk that this websocket is not
96 case "ms.channel.clientConnect":
97 logger.debug("V2 client connected");
99 case "ms.channel.clientDisconnect":
100 logger.debug("V2 client disconnected");
103 logger.debug("V2 Unknown event: {}", msg);
105 } catch (JsonSyntaxException e) {
106 logger.warn("{}: Error ({}) in message: {}", this.getClass().getSimpleName(), e.getMessage(), msg, e);
110 private void handleResult(JSONMessage jsonMsg) {
111 if ((remoteControllerWebSocket.currentSourceApp == null
112 || remoteControllerWebSocket.currentSourceApp.trim().isEmpty())
113 && "true".equals(jsonMsg.result.visible)) {
114 logger.debug("Running app: {} = {}", jsonMsg.result.id, jsonMsg.result.name);
115 remoteControllerWebSocket.currentSourceApp = jsonMsg.result.name;
116 remoteControllerWebSocket.callback.currentAppUpdated(remoteControllerWebSocket.currentSourceApp);
119 if (remoteControllerWebSocket.lastApp != null && remoteControllerWebSocket.lastApp.equals(jsonMsg.result.id)) {
120 if (remoteControllerWebSocket.currentSourceApp == null
121 || remoteControllerWebSocket.currentSourceApp.trim().isEmpty()) {
122 remoteControllerWebSocket.callback.currentAppUpdated("");
124 remoteControllerWebSocket.lastApp = null;
128 @NonNullByDefault({})
129 static class JSONAppStatus {
130 public JSONAppStatus(String id) {
135 @NonNullByDefault({})
136 static class Params {
140 String method = "ms.application.get";
142 Params params = new Params();
145 void getAppStatus(String id) {
146 sendCommand(remoteControllerWebSocket.gson.toJson(new JSONAppStatus(id)));