switched to json-rpc (ref LedD d5f403d5573d13e6b8f3113a1c62a096ea721f19)

fixed duplicate connections were made when adding a daemon
This commit is contained in:
Giovanni Harting
2015-10-11 21:52:39 +02:00
parent 6c321aaa90
commit 60f805f15c
26 changed files with 165 additions and 175 deletions

View File

@@ -38,13 +38,19 @@ import com.idlegandalf.ledd.components.LedDDaemon;
import com.idlegandalf.ledd.components.LedDRequest;
import com.idlegandalf.ledd.components.LedStripe;
import com.idlegandalf.ledd.services.ColorService;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Error;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Request;
import com.thetransactioncompany.jsonrpc2.JSONRPC2Response;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.LinkedBlockingQueue;
public class LedDHelper {
@@ -52,7 +58,7 @@ public class LedDHelper {
final String ACTION_GETCOLOR = "get_color";
final String ACTION_ADDCONTROLLER = "add_controller";
final String ACTION_GETALLSTRIPES = "get_stripes";
final String ACTION_ADDSTRIPES = "add_stripe";
final String ACTION_ADDSTRIPE = "add_stripe";
final String ACTION_TESTCHANNEL = "test_channel";
final String ACTION_DISCOVER = "discover";
private Context context;
@@ -92,34 +98,33 @@ public class LedDHelper {
* @param c controller object
*/
public void addController(final Controller c, final AddControllerCallback callback) {
JSONObject jnson = new JSONObject();
Map<String, Object> params = new HashMap<>();
try {
jnson.put("action", ACTION_ADDCONTROLLER);
jnson.put("channels", c.getChannels());
jnson.put("i2c_dev", c.getI2c_device());
jnson.put("address", c.getAddress());
} catch (JSONException e) {
e.printStackTrace();
}
params.put("channels", c.getChannels());
params.put("i2c_dev", c.getI2c_device());
params.put("address", c.getAddress());
addRequestToQueue(jnson, new AnswerTask() {
JSONRPC2Request request = new JSONRPC2Request(ACTION_ADDCONTROLLER, params, UUID.randomUUID().toString());
addRequestToQueue(request, new AnswerTask() {
@Override
public void onConnectionFailed(String message) {
callback.onAddFailed(message, "");
callback.onConnectionFailed(message);
}
@Override
public void onResponse(JSONObject response) {
try {
if (response.getBoolean("success")) {
c.setId(response.getInt("cid"));
public void onResponse(JSONRPC2Response response) {
if (response.indicatesSuccess()) {
try {
JSONObject json = new JSONObject(response.getResult().toString());
c.setId(json.getInt("cid"));
callback.onControllerAdded(c);
} else {
callback.onAddFailed(response.getString("message"), response.getString("message_detail"));
} catch (JSONException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
} else {
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") JSONRPC2Error error = response.getError();
callback.onAddFailed(error.getCode(), error.getMessage());
}
}
});
@@ -129,27 +134,20 @@ public class LedDHelper {
* Get stripes known to daemon
*/
public void getStripes(final StripesCallback callback) {
JSONObject jnson = new JSONObject();
try {
jnson.put("action", ACTION_GETALLSTRIPES);
} catch (JSONException e) {
e.printStackTrace();
}
addRequestToQueue(jnson, new AnswerTask() {
addRequestToQueue(new JSONRPC2Request(ACTION_GETALLSTRIPES, UUID.randomUUID().toString()), new AnswerTask() {
@Override
public void onConnectionFailed(String message) {
callback.onGetFailed(message);
callback.onConnectionFailed(message);
}
@Override
public void onResponse(JSONObject response) {
public void onResponse(JSONRPC2Response response) {
try {
if (response.getBoolean("success")) {
if (response.indicatesSuccess()) {
ledDDaemon.getControllers().clear();
List<LedStripe> list = new ArrayList<>();
JSONArray jcontrollers = response.getJSONArray("controller");
JSONObject json = new JSONObject(response.getResult().toString());
JSONArray jcontrollers = json.getJSONArray("controller");
for (int i = 0; i < jcontrollers.length(); i++) {
JSONObject row = jcontrollers.getJSONObject(i);
@@ -180,8 +178,8 @@ public class LedDHelper {
callback.onSuccess(list);
} else {
if (response.has("message")) callback.onGetFailed(response.getString("message"));
else callback.onGetFailed("unknown error");
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") JSONRPC2Error error = response.getError();
callback.onGetFailed(error.getCode(), error.getMessage());
}
} catch (Exception e) {
e.printStackTrace();
@@ -196,26 +194,20 @@ public class LedDHelper {
* @param ledStripe Stripe
*/
public void setColor(LedStripe ledStripe) {
Map<String, Object> hsv = new HashMap<>();
JSONObject jnson = new JSONObject();
JSONObject hsv = new JSONObject();
hsv.put("h", ledStripe.getColor().getHue());
hsv.put("s", ledStripe.getColor().getSaturation());
hsv.put("v", ledStripe.getColor().getValue());
try {
hsv.put("h", ledStripe.getColor().getHue());
hsv.put("s", ledStripe.getColor().getSaturation());
hsv.put("v", ledStripe.getColor().getValue());
Map<String, Object> params = new HashMap<>();
params.put("sid", ledStripe.getId());
params.put("hsv", hsv);
jnson.put("action", ACTION_SETCOLOR);
jnson.put("sid", ledStripe.getId());
jnson.put("hsv", hsv);
JSONRPC2Request request = new JSONRPC2Request(ACTION_SETCOLOR, params, UUID.randomUUID().toString());
} catch (JSONException e) {
e.printStackTrace();
}
addRequestToQueue(jnson, null);
addRequestToQueue(request, null);
}
/**
@@ -224,33 +216,29 @@ public class LedDHelper {
* @param ledStripe Stripe
*/
public void getColor(final LedStripe ledStripe, final RecieveColorCallback callback) {
JSONObject jnson = new JSONObject();
HashMap<String, Object> params = new HashMap<>();
params.put("sid", ledStripe.getId());
try {
jnson.put("action", ACTION_GETCOLOR);
jnson.put("sid", ledStripe.getId());
} catch (JSONException e) {
e.printStackTrace();
}
addRequestToQueue(jnson, new AnswerTask() {
addRequestToQueue(new JSONRPC2Request(ACTION_GETCOLOR, params, UUID.randomUUID().toString()), new AnswerTask() {
@Override
public void onConnectionFailed(String message) {
callback.onRecievFailed(message);
callback.onConnectionFailed(message);
}
@Override
public void onResponse(JSONObject response) {
try {
if (response.getBoolean("success")) {
JSONArray hsv = response.getJSONArray("color");
public void onResponse(JSONRPC2Response response) {
if (response.indicatesSuccess()) {
try {
JSONObject json = new JSONObject(response.getResult().toString());
JSONArray hsv = json.getJSONArray("color");
ledStripe.setColor(new HSV(hsv.getDouble(0), hsv.getDouble(1), hsv.getDouble(2)));
callback.onColorRecieved(ledStripe);
} else {
callback.onRecievFailed(response.getString("message"));
} catch (JSONException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
} else {
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") JSONRPC2Error error = response.getError();
callback.onRecievFailed(error.getCode(), error.getMessage());
}
}
});
@@ -264,26 +252,20 @@ public class LedDHelper {
* @param value value (1= on, 0 = off)
*/
public void testChannel(Controller c, int channel, int value) {
JSONObject jnson = new JSONObject();
HashMap<String, Object> params = new HashMap<>();
try {
jnson.put("action", ACTION_TESTCHANNEL);
params.put("cid", c.getId());
params.put("channel", channel);
params.put("value", value);
jnson.put("cid", c.getId());
jnson.put("channel", channel);
jnson.put("value", value);
} catch (JSONException e) {
e.printStackTrace();
}
addRequestToQueue(jnson, new AnswerTask() {
addRequestToQueue(new JSONRPC2Request(ACTION_TESTCHANNEL, params, UUID.randomUUID().toString()), new AnswerTask() {
@Override
public void onConnectionFailed(String message) {
}
@Override
public void onResponse(JSONObject response) {
public void onResponse(JSONRPC2Response response) {
}
});
@@ -293,25 +275,18 @@ public class LedDHelper {
* Get information about an ledd daemon
*/
public void discover(final DiscoverCallback callback) {
JSONObject jnson = new JSONObject();
try {
jnson.put("action", ACTION_DISCOVER);
} catch (JSONException e) {
e.printStackTrace();
}
addRequestToQueue(jnson, new AnswerTask() {
addRequestToQueue(new JSONRPC2Request(ACTION_DISCOVER, UUID.randomUUID().toString()), new AnswerTask() {
@Override
public void onConnectionFailed(String message) {
callback.onConnectionFailed(message);
}
@Override
public void onResponse(JSONObject response) {
public void onResponse(JSONRPC2Response response) {
try {
if (response.getBoolean("success")) {
callback.onDiscoverSuccessfully(response.getString("version"));
if (response.indicatesSuccess()) {
JSONObject json = new JSONObject(response.getResult().toString());
callback.onDiscoverSuccessfully(json.getString("version"));
}
} catch (Exception e) {
e.printStackTrace();
@@ -324,40 +299,34 @@ public class LedDHelper {
* Get information about an ledd daemon
*/
public void addStripe(final LedStripe ledStripe, final AddStripeCallback callback) {
JSONObject jnson = new JSONObject();
HashMap<String, Object> params = new HashMap<>();
HashMap<String, Object> mapping = new HashMap<>();
try {
jnson.put("action", ACTION_ADDSTRIPES);
params.put("name", ledStripe.getName());
params.put("rgb", ledStripe.isRGB());
JSONObject stripe = new JSONObject();
mapping.put("r", ledStripe.getChannelRed());
mapping.put("g", ledStripe.getChannelGreen());
mapping.put("b", ledStripe.getChannelBlue());
params.put("map", mapping);
params.put("cid", ledStripe.getController().getId());
stripe.put("name", ledStripe.getName());
stripe.put("rgb", ledStripe.isRGB());
JSONObject mapping = new JSONObject();
mapping.put("r", ledStripe.getChannelRed());
mapping.put("g", ledStripe.getChannelGreen());
mapping.put("b", ledStripe.getChannelBlue());
stripe.put("map", mapping);
stripe.put("cid", ledStripe.getController().getId());
jnson.put("stripe", stripe);
} catch (JSONException e) {
e.printStackTrace();
}
addRequestToQueue(jnson, new AnswerTask() {
addRequestToQueue(new JSONRPC2Request(ACTION_ADDSTRIPE, params, UUID.randomUUID().toString()), new AnswerTask() {
@Override
public void onConnectionFailed(String message) {
callback.onConnectionFailed(message);
}
@Override
public void onResponse(JSONObject response) {
public void onResponse(JSONRPC2Response response) {
try {
if (response.getBoolean("success")) {
ledStripe.setId(response.getInt("sid"));
if (response.indicatesSuccess()) {
JSONObject json = new JSONObject(response.getResult().toString());
ledStripe.setId(json.getInt("sid"));
callback.onAddSuccessfully(ledStripe);
} else {
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") JSONRPC2Error error = response.getError();
callback.onAddFailed(error.getCode(), error.getMessage());
}
} catch (Exception e) {
e.printStackTrace();
@@ -366,8 +335,8 @@ public class LedDHelper {
});
}
private void addRequestToQueue(JSONObject json, AnswerTask task) {
if (json != null && json.length() > 0) dRequests.add(new LedDRequest(json, task));
private void addRequestToQueue(JSONRPC2Request request, AnswerTask task) {
if (request != null) dRequests.add(new LedDRequest(request, task));
}
public void teardown() {