mirror of
https://github.com/badaix/snapcast.git
synced 2025-08-06 10:09:33 +02:00
check json request ID
This commit is contained in:
parent
030fe1024f
commit
6b67d80bc7
1 changed files with 19 additions and 17 deletions
|
@ -1,11 +1,13 @@
|
||||||
package de.badaix.snapcast.control;
|
package de.badaix.snapcast.control;
|
||||||
|
|
||||||
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by johannes on 13.01.16.
|
* Created by johannes on 13.01.16.
|
||||||
*/
|
*/
|
||||||
|
@ -19,11 +21,13 @@ public class RemoteControl implements TcpClient.TcpClientListener {
|
||||||
private ServerInfo serverInfo;
|
private ServerInfo serverInfo;
|
||||||
private String host;
|
private String host;
|
||||||
private int port;
|
private int port;
|
||||||
|
private HashMap<Long, String> pendingRequests;
|
||||||
|
|
||||||
public RemoteControl(RemoteControlListener listener) {
|
public RemoteControl(RemoteControlListener listener) {
|
||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
serverInfo = new ServerInfo();
|
serverInfo = new ServerInfo();
|
||||||
msgId = 0;
|
msgId = 0;
|
||||||
|
pendingRequests = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getHost() {
|
public String getHost() {
|
||||||
|
@ -54,6 +58,7 @@ public class RemoteControl implements TcpClient.TcpClientListener {
|
||||||
if ((tcpClient != null) && (tcpClient.isConnected()))
|
if ((tcpClient != null) && (tcpClient.isConnected()))
|
||||||
tcpClient.stop();
|
tcpClient.stop();
|
||||||
tcpClient = null;
|
tcpClient = null;
|
||||||
|
pendingRequests.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isConnected() {
|
public boolean isConnected() {
|
||||||
|
@ -67,26 +72,22 @@ public class RemoteControl implements TcpClient.TcpClientListener {
|
||||||
JSONObject json = new JSONObject(message);
|
JSONObject json = new JSONObject(message);
|
||||||
if (json.has("id")) {
|
if (json.has("id")) {
|
||||||
Log.d(TAG, "ID: " + json.getString("id"));
|
Log.d(TAG, "ID: " + json.getString("id"));
|
||||||
|
long id = json.getLong("id");
|
||||||
|
String request = "";
|
||||||
|
if (pendingRequests.containsKey(id)) {
|
||||||
|
request = pendingRequests.get(id);
|
||||||
|
Log.d(TAG, "Response to: " + request);
|
||||||
|
pendingRequests.remove(id);
|
||||||
|
}
|
||||||
if (json.has("error")) {
|
if (json.has("error")) {
|
||||||
JSONObject error = json.getJSONObject("error");
|
JSONObject error = json.getJSONObject("error");
|
||||||
Log.e(TAG, "error " + error.getInt("code") + ": " + error.getString("message"));
|
Log.e(TAG, "error " + error.getInt("code") + ": " + error.getString("message"));
|
||||||
} else if (json.has("result") && (json.get("result") instanceof JSONObject) &&
|
} else if (!TextUtils.isEmpty(request)) {
|
||||||
json.getJSONObject("result").has("clients")) {
|
if (request.equals("Server.GetStatus")) {
|
||||||
serverInfo.clear();
|
serverInfo.fromJson(json.getJSONObject("result"));
|
||||||
JSONArray clients = json.getJSONObject("result").getJSONArray("clients");
|
if (listener != null)
|
||||||
for (int i = 0; i < clients.length(); i++) {
|
listener.onServerInfo(this, serverInfo);
|
||||||
final ClientInfo clientInfo = new ClientInfo(clients.getJSONObject(i));
|
|
||||||
serverInfo.updateClient(clientInfo);
|
|
||||||
}
|
}
|
||||||
if (json.getJSONObject("result").has("streams")) {
|
|
||||||
JSONArray streams = json.getJSONObject("result").getJSONArray("streams");
|
|
||||||
for (int i = 0; i < streams.length(); i++) {
|
|
||||||
final Stream stream = new Stream(streams.getJSONObject(i));
|
|
||||||
serverInfo.updateStream(stream);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (listener != null)
|
|
||||||
listener.onServerInfo(this, serverInfo);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
String method = json.getString("method");
|
String method = json.getString("method");
|
||||||
|
@ -145,6 +146,7 @@ public class RemoteControl implements TcpClient.TcpClientListener {
|
||||||
request.put("id", msgId);
|
request.put("id", msgId);
|
||||||
if (params != null)
|
if (params != null)
|
||||||
request.put("params", params);
|
request.put("params", params);
|
||||||
|
pendingRequests.put(msgId, method);
|
||||||
msgId++;
|
msgId++;
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue