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.JsonElement;
21 import com.google.gson.JsonSyntaxException;
24 * Websocket class to retrieve artmode status (on o.a. the Frame TV's)
26 * @author Arjan Mels - Initial contribution
29 class WebSocketArt extends WebSocketBase {
30 private final Logger logger = LoggerFactory.getLogger(WebSocketArt.class);
33 * @param remoteControllerWebSocket
35 WebSocketArt(RemoteControllerWebSocket remoteControllerWebSocket) {
36 super(remoteControllerWebSocket);
40 private static class JSONMessage {
50 // data is sometimes a json object, sometimes a string representation of a json object for d2d_service_message
56 public void onWebSocketText(@Nullable String msgarg) {
60 String msg = msgarg.replace('\n', ' ');
61 super.onWebSocketText(msg);
63 JSONMessage jsonMsg = remoteControllerWebSocket.gson.fromJson(msg, JSONMessage.class);
65 switch (jsonMsg.event) {
66 case "ms.channel.connect":
67 logger.debug("Art channel connected");
69 case "ms.channel.ready":
70 logger.debug("Art channel ready");
73 case "ms.channel.clientConnect":
74 logger.debug("Art client connected");
76 case "ms.channel.clientDisconnect":
77 logger.debug("Art client disconnected");
80 case "d2d_service_message":
81 if (jsonMsg.data != null) {
82 handleD2DServiceMessage(jsonMsg.data.getAsString());
84 logger.debug("Empty d2d_service_message event: {}", msg);
88 logger.debug("WebSocketArt Unknown event: {}", msg);
90 } catch (JsonSyntaxException e) {
91 logger.warn("{}: Error ({}) in message: {}", this.getClass().getSimpleName(), e.getMessage(), msg, e);
95 private void handleD2DServiceMessage(String msg) {
96 JSONMessage.Data data = remoteControllerWebSocket.gson.fromJson(msg, JSONMessage.Data.class);
97 if (data.event == null) {
98 logger.debug("Unknown d2d_service_message event: {}", msg);
101 switch (data.event) {
102 case "art_mode_changed":
103 logger.debug("art_mode_changed: {}", data.status);
104 if ("on".equals(data.status)) {
105 remoteControllerWebSocket.callback.powerUpdated(false, true);
107 remoteControllerWebSocket.callback.powerUpdated(true, false);
109 remoteControllerWebSocket.updateCurrentApp();
111 case "artmode_status":
112 logger.debug("artmode_status: {}", data.value);
113 if ("on".equals(data.value)) {
114 remoteControllerWebSocket.callback.powerUpdated(false, true);
116 remoteControllerWebSocket.callback.powerUpdated(true, false);
118 remoteControllerWebSocket.updateCurrentApp();
120 case "go_to_standby":
121 logger.debug("go_to_standby");
122 remoteControllerWebSocket.callback.powerUpdated(false, false);
125 logger.debug("wakeup");
126 // check artmode status to know complete status before updating
130 logger.debug("Unknown d2d_service_message event: {}", msg);
135 @NonNullByDefault({})
136 class JSONArtModeStatus {
137 public JSONArtModeStatus() {
138 Params.Data data = params.new Data();
139 data.id = remoteControllerWebSocket.uuid.toString();
140 params.data = remoteControllerWebSocket.gson.toJson(data);
143 @NonNullByDefault({})
145 @NonNullByDefault({})
147 String request = "get_artmode_status";
151 String event = "art_app_request";
156 String method = "ms.channel.emit";
157 Params params = new Params();
160 void getArtmodeStatus() {
161 sendCommand(remoteControllerWebSocket.gson.toJson(new JSONArtModeStatus()));