-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
UnifiedRemoteConnection.java
266 lines (248 loc) · 10.1 KB
/
UnifiedRemoteConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.unifiedremote.internal;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.client.util.StringContentProvider;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
* The {@link UnifiedRemoteConnection} Handles Remote Server Communications
*
* @author Miguel Alvarez - Initial contribution
*/
@NonNullByDefault
public class UnifiedRemoteConnection {
private static final int WEB_CLIENT_PORT = 9510;
private static final int TIMEOUT_SEC = 10;
private static final String CONNECTION_ID_HEADER = "UR-Connection-ID";
private static final String MOUSE_REMOTE = "Relmtech.Basic Input";
private static final String NAVIGATION_REMOTE = "Unified.Navigation";
private static final String POWER_REMOTE = "Unified.Power";
private static final String MEDIA_REMOTE = "Unified.Media";
private static final String MONITOR_REMOTE = "Unified.Monitor";
private Logger logger = LoggerFactory.getLogger(UnifiedRemoteConnection.class);
private final String url;
private HttpClient httpClient;
private @Nullable String connectionID;
private @Nullable String connectionGUID;
public UnifiedRemoteConnection(HttpClient httpClient, String host) {
this.httpClient = httpClient;
url = "http://" + host + ":" + WEB_CLIENT_PORT + "/client/";
}
public void authenticate() throws InterruptedException, ExecutionException, TimeoutException {
ContentResponse response = null;
connectionGUID = "web-" + UUID.randomUUID().toString();
response = httpClient.newRequest(getPath("connect")).method(HttpMethod.GET)
.timeout(TIMEOUT_SEC, TimeUnit.SECONDS).send();
JsonObject responseBody = JsonParser.parseString(response.getContentAsString()).getAsJsonObject();
connectionID = responseBody.get("id").getAsString();
String password = UUID.randomUUID().toString();
JsonObject authPayload = new JsonObject();
authPayload.addProperty("Action", 0);
authPayload.addProperty("Request", 0);
authPayload.addProperty("Version", 10);
authPayload.addProperty("Password", password);
authPayload.addProperty("Platform", "web");
authPayload.addProperty("Source", connectionGUID);
request(authPayload);
JsonObject capabilitiesPayload = new JsonObject();
JsonObject capabilitiesInnerPayload = new JsonObject();
capabilitiesInnerPayload.addProperty("Actions", true);
capabilitiesInnerPayload.addProperty("Sync", true);
capabilitiesInnerPayload.addProperty("Grid", true);
capabilitiesInnerPayload.addProperty("Fast", false);
capabilitiesInnerPayload.addProperty("Loading", true);
capabilitiesInnerPayload.addProperty("Encryption2", true);
capabilitiesPayload.add("Capabilities", capabilitiesInnerPayload);
capabilitiesPayload.addProperty("Action", 1);
capabilitiesPayload.addProperty("Request", 1);
capabilitiesPayload.addProperty("Source", connectionGUID);
request(capabilitiesPayload);
}
public ContentResponse mouseMove(String jsonIntArray)
throws InterruptedException, ExecutionException, TimeoutException {
JsonArray cordinates = JsonParser.parseString(jsonIntArray).getAsJsonArray();
int x = cordinates.get(0).getAsInt();
int y = cordinates.get(1).getAsInt();
return this.execRemoteAction("Relmtech.Basic Input", "delta",
wrapValues(new String[] { "0", Integer.toString(x), Integer.toString(y) }));
}
public ContentResponse sendKey(String key) throws InterruptedException, ExecutionException, TimeoutException {
String remoteID = "";
String actionName = "";
String value = null;
switch (key) {
case "LEFT_CLICK":
remoteID = MOUSE_REMOTE;
actionName = "left";
break;
case "RIGHT_CLICK":
remoteID = MOUSE_REMOTE;
actionName = "right";
break;
case "LOCK":
remoteID = POWER_REMOTE;
actionName = "lock";
break;
case "UNLOCK":
remoteID = POWER_REMOTE;
actionName = "unlock";
break;
case "SLEEP":
remoteID = POWER_REMOTE;
actionName = "sleep";
break;
case "SHUTDOWN":
remoteID = POWER_REMOTE;
actionName = "shutdown";
break;
case "RESTART":
remoteID = POWER_REMOTE;
actionName = "restart";
break;
case "LOGOFF":
remoteID = POWER_REMOTE;
actionName = "logoff";
break;
case "PLAY/PAUSE":
case "PLAY":
case "PAUSE":
remoteID = MEDIA_REMOTE;
actionName = "play_pause";
break;
case "NEXT":
remoteID = MEDIA_REMOTE;
actionName = "next";
break;
case "PREVIOUS":
remoteID = MEDIA_REMOTE;
actionName = "previous";
break;
case "STOP":
remoteID = MEDIA_REMOTE;
actionName = "stop";
break;
case "VOLUME_MUTE":
remoteID = MEDIA_REMOTE;
actionName = "volume_mute";
break;
case "VOLUME_UP":
remoteID = MEDIA_REMOTE;
actionName = "volume_up";
break;
case "VOLUME_DOWN":
remoteID = MEDIA_REMOTE;
actionName = "volume_down";
break;
case "BRIGHTNESS_UP":
remoteID = MONITOR_REMOTE;
actionName = "brightness_up";
break;
case "BRIGHTNESS_DOWN":
remoteID = MONITOR_REMOTE;
actionName = "brightness_down";
break;
case "MONITOR_OFF":
remoteID = MONITOR_REMOTE;
actionName = "turn_off";
break;
case "MONITOR_ON":
remoteID = MONITOR_REMOTE;
actionName = "turn_on";
break;
case "ESCAPE":
case "SPACE":
case "BACK":
case "LWIN":
case "CONTROL":
case "TAB":
case "MENU":
case "RETURN":
case "UP":
case "DOWN":
case "LEFT":
case "RIGHT":
remoteID = NAVIGATION_REMOTE;
actionName = "toggle";
value = key;
break;
}
JsonArray wrappedValues = null;
if (value != null) {
wrappedValues = wrapValues(new String[] { value });
}
return this.execRemoteAction(remoteID, actionName, wrappedValues);
}
public ContentResponse keepAlive() throws InterruptedException, ExecutionException, TimeoutException {
JsonObject payload = new JsonObject();
payload.addProperty("KeepAlive", true);
payload.addProperty("Source", connectionGUID);
return request(payload);
}
private ContentResponse execRemoteAction(String remoteID, String name, @Nullable JsonElement values)
throws InterruptedException, ExecutionException, TimeoutException {
JsonObject payload = new JsonObject();
JsonObject runInnerPayload = new JsonObject();
JsonObject extrasInnerPayload = new JsonObject();
if (values != null) {
extrasInnerPayload.add("Values", values);
runInnerPayload.add("Extras", extrasInnerPayload);
}
runInnerPayload.addProperty("Name", name);
payload.addProperty("ID", remoteID);
payload.addProperty("Action", 7);
payload.addProperty("Request", 7);
payload.add("Run", runInnerPayload);
payload.addProperty("Source", connectionGUID);
return request(payload);
}
private ContentResponse request(JsonObject content)
throws InterruptedException, ExecutionException, TimeoutException {
Request request = httpClient.newRequest(getPath("request")).method(HttpMethod.POST).timeout(TIMEOUT_SEC,
TimeUnit.SECONDS);
request.header(HttpHeader.CONTENT_TYPE, "application/json");
if (connectionID != null) {
request.header(CONNECTION_ID_HEADER, connectionID);
}
String stringContent = content.toString();
logger.debug("[Request Payload {} ]", stringContent);
request.content(new StringContentProvider(stringContent, "utf-8"));
return request.send();
}
private JsonArray wrapValues(String[] commandValues) {
JsonArray values = new JsonArray();
for (String value : commandValues) {
JsonObject valueWrapper = new JsonObject();
valueWrapper.addProperty("Value", value);
values.add(valueWrapper);
}
return values;
}
private String getPath(String path) {
return url + path;
}
}