]> git.basschouten.com Git - openhab-addons.git/blob
0323ca6320cc6e9d431f0c2b29f109d5fddc4747
[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.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.JsonSyntaxException;
21
22 /**
23  * Websocket class to retrieve app status
24  *
25  * @author Arjan Mels - Initial contribution
26  */
27 @NonNullByDefault
28 class WebSocketV2 extends WebSocketBase {
29     private final Logger logger = LoggerFactory.getLogger(WebSocketV2.class);
30
31     WebSocketV2(RemoteControllerWebSocket remoteControllerWebSocket) {
32         super(remoteControllerWebSocket);
33     }
34
35     @SuppressWarnings("unused")
36     @NonNullByDefault({})
37     private static class JSONMessage {
38         String event;
39
40         @NonNullByDefault({})
41         static class Result {
42             String id;
43             String name;
44             String visible;
45         }
46
47         @NonNullByDefault({})
48         static class Data {
49             String id;
50             String token;
51         }
52
53         @NonNullByDefault({})
54         static class Error {
55             String code;
56             String details;
57             String message;
58             String status;
59         }
60
61         Result result;
62         Data data;
63         Error error;
64     }
65
66     @Override
67     public void onWebSocketText(@Nullable String msgarg) {
68         if (msgarg == null) {
69             return;
70         }
71         String msg = msgarg.replace('\n', ' ');
72         super.onWebSocketText(msg);
73         try {
74             JSONMessage jsonMsg = this.remoteControllerWebSocket.gson.fromJson(msg, JSONMessage.class);
75
76             if (jsonMsg.result != null) {
77                 handleResult(jsonMsg);
78                 return;
79             }
80             if (jsonMsg.error != null) {
81                 logger.debug("WebSocketV2 Error received: {}", msg);
82                 return;
83             }
84             if (jsonMsg.event == null) {
85                 logger.debug("WebSocketV2 Unknown response format: {}", msg);
86                 return;
87             }
88
89             switch (jsonMsg.event) {
90                 case "ms.channel.connect":
91                     logger.debug("V2 channel connected. Token = {}", jsonMsg.data.token);
92
93                     // update is requested from ed.installedApp.get event: small risk that this websocket is not
94                     // yet connected
95                     break;
96                 case "ms.channel.clientConnect":
97                     logger.debug("V2 client connected");
98                     break;
99                 case "ms.channel.clientDisconnect":
100                     logger.debug("V2 client disconnected");
101                     break;
102                 default:
103                     logger.debug("V2 Unknown event: {}", msg);
104             }
105         } catch (JsonSyntaxException e) {
106             logger.warn("{}: Error ({}) in message: {}", this.getClass().getSimpleName(), e.getMessage(), msg, e);
107         }
108     }
109
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);
117         }
118
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("");
123             }
124             remoteControllerWebSocket.lastApp = null;
125         }
126     }
127
128     @NonNullByDefault({})
129     static class JSONAppStatus {
130         public JSONAppStatus(String id) {
131             this.id = id;
132             params.id = id;
133         }
134
135         @NonNullByDefault({})
136         static class Params {
137             String id;
138         }
139
140         String method = "ms.application.get";
141         String id;
142         Params params = new Params();
143     }
144
145     void getAppStatus(String id) {
146         sendCommand(remoteControllerWebSocket.gson.toJson(new JSONAppStatus(id)));
147     }
148 }