]> git.basschouten.com Git - openhab-addons.git/blob
9dc21f7c259bf1860dc4dd0068d0ace898a8092a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.samsungtv.internal.protocol;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 import com.google.gson.JsonElement;
21 import com.google.gson.JsonSyntaxException;
22
23 /**
24  * Websocket class to retrieve artmode status (on o.a. the Frame TV's)
25  *
26  * @author Arjan Mels - Initial contribution
27  */
28 @NonNullByDefault
29 class WebSocketArt extends WebSocketBase {
30     private final Logger logger = LoggerFactory.getLogger(WebSocketArt.class);
31
32     /**
33      * @param remoteControllerWebSocket
34      */
35     WebSocketArt(RemoteControllerWebSocket remoteControllerWebSocket) {
36         super(remoteControllerWebSocket);
37     }
38
39     @NonNullByDefault({})
40     private static class JSONMessage {
41         String event;
42
43         @NonNullByDefault({})
44         static class Data {
45             String event;
46             String status;
47             String value;
48         }
49
50         // data is sometimes a json object, sometimes a string representation of a json object for d2d_service_message
51         @Nullable
52         JsonElement data;
53     }
54
55     @Override
56     public void onWebSocketText(@Nullable String msgarg) {
57         if (msgarg == null) {
58             return;
59         }
60         String msg = msgarg.replace('\n', ' ');
61         super.onWebSocketText(msg);
62         try {
63             JSONMessage jsonMsg = remoteControllerWebSocket.gson.fromJson(msg, JSONMessage.class);
64
65             switch (jsonMsg.event) {
66                 case "ms.channel.connect":
67                     logger.debug("Art channel connected");
68                     break;
69                 case "ms.channel.ready":
70                     logger.debug("Art channel ready");
71                     getArtmodeStatus();
72                     break;
73                 case "ms.channel.clientConnect":
74                     logger.debug("Art client connected");
75                     break;
76                 case "ms.channel.clientDisconnect":
77                     logger.debug("Art client disconnected");
78                     break;
79
80                 case "d2d_service_message":
81                     if (jsonMsg.data != null) {
82                         handleD2DServiceMessage(jsonMsg.data.getAsString());
83                     } else {
84                         logger.debug("Empty d2d_service_message event: {}", msg);
85                     }
86                     break;
87                 default:
88                     logger.debug("WebSocketArt Unknown event: {}", msg);
89             }
90         } catch (JsonSyntaxException e) {
91             logger.warn("{}: Error ({}) in message: {}", this.getClass().getSimpleName(), e.getMessage(), msg, e);
92         }
93     }
94
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);
99             return;
100         } else {
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);
106                     } else {
107                         remoteControllerWebSocket.callback.powerUpdated(true, false);
108                     }
109                     remoteControllerWebSocket.updateCurrentApp();
110                     break;
111                 case "artmode_status":
112                     logger.debug("artmode_status: {}", data.value);
113                     if ("on".equals(data.value)) {
114                         remoteControllerWebSocket.callback.powerUpdated(false, true);
115                     } else {
116                         remoteControllerWebSocket.callback.powerUpdated(true, false);
117                     }
118                     remoteControllerWebSocket.updateCurrentApp();
119                     break;
120                 case "go_to_standby":
121                     logger.debug("go_to_standby");
122                     remoteControllerWebSocket.callback.powerUpdated(false, false);
123                     break;
124                 case "wakeup":
125                     logger.debug("wakeup");
126                     // check artmode status to know complete status before updating
127                     getArtmodeStatus();
128                     break;
129                 default:
130                     logger.debug("Unknown d2d_service_message event: {}", msg);
131             }
132         }
133     }
134
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);
141         }
142
143         @NonNullByDefault({})
144         class Params {
145             @NonNullByDefault({})
146             class Data {
147                 String request = "get_artmode_status";
148                 String id;
149             }
150
151             String event = "art_app_request";
152             String to = "host";
153             String data;
154         }
155
156         String method = "ms.channel.emit";
157         Params params = new Params();
158     }
159
160     void getArtmodeStatus() {
161         sendCommand(remoteControllerWebSocket.gson.toJson(new JSONArtModeStatus()));
162     }
163 }