implemented things to the point where simple set/get of color is possible

added complete add stripe mechanic
This commit is contained in:
Giovanni Harting
2015-09-13 17:00:00 +02:00
parent d700424610
commit 1fe91b728a
35 changed files with 2700 additions and 326 deletions

View File

@@ -23,49 +23,65 @@ import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.util.Log;
import com.idlegandalf.ledd.ColorApplication;
import com.idlegandalf.ledd.callbacks.AddControllerCallback;
import com.idlegandalf.ledd.callbacks.AddStripeCallback;
import com.idlegandalf.ledd.callbacks.DiscoverCallback;
import com.idlegandalf.ledd.callbacks.RecieveColorCallback;
import com.idlegandalf.ledd.callbacks.StripesCallback;
import com.idlegandalf.ledd.components.AnswerTask;
import com.idlegandalf.ledd.components.Controller;
import com.idlegandalf.ledd.components.HSV;
import com.idlegandalf.ledd.components.Host;
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 org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import hugo.weaving.DebugLog;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
public class LedDHelper {
final String ACTION_SETCOLOR = "set_color";
final String ACTION_GETCOLOR = "get_color";
final String ACTION_ADDCONTROLLER = "add_controller";
final String ACTION_GETCONTROLLER = "get_controller";
final String ACTION_ADDSTRIPES = "add_stripes";
private Host host;
final String ACTION_GETALLSTRIPES = "get_stripes";
final String ACTION_ADDSTRIPES = "add_stripe";
final String ACTION_TESTCHANNEL = "test_channel";
final String ACTION_DISCOVER = "discover";
private Context context;
private ColorService.ColorBinder binderService;
private boolean mBound = false;
private LedDDaemon ledDDaemon;
private Worker<LedDRequest> requestWorker;
private LinkedBlockingQueue<LedDRequest> dRequests;
ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
System.out.println("service bound!");
binderService = (ColorService.ColorBinder) service;
Log.d(ColorApplication.TAG, "ColorService bound!");
requestWorker = new Worker<>(dRequests, (ColorService.ColorBinder) service, ledDDaemon);
mBound = true;
new Thread(requestWorker).start();
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
requestWorker.stop();
mBound = false;
}
};
public LedDHelper(Host host, Context appl) throws IOException {
this.host = host;
public LedDHelper(LedDDaemon ledDDaemon, Context appl) throws IOException {
this.context = appl;
this.dRequests = new LinkedBlockingQueue<>();
this.ledDDaemon = ledDDaemon;
Intent intent = new Intent(appl, ColorService.class);
appl.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
@@ -74,25 +90,30 @@ public class LedDHelper {
/**
* Add controller to ledd daemon
*
* @param channels channel amount that this controller holds
* @param i2c_dev number of i2c device, e.g. /dev/i2c-1 would be 1
* @param c controller object
* @throws JSONException no valid json
* @throws IOException socket error
*/
public void addController(int channels, int i2c_dev, String address, final AddControllerCallback callback) throws JSONException, IOException {
public void addController(final Controller c, final AddControllerCallback callback) throws JSONException, IOException {
JSONObject jnson = new JSONObject();
jnson.put("action", ACTION_ADDCONTROLLER);
jnson.put("channels", channels);
jnson.put("i2c_dev", i2c_dev);
jnson.put("address", address);
jnson.put("channels", c.getChannels());
jnson.put("i2c_dev", c.getI2c_device());
jnson.put("address", c.getAddress());
sendJSON(jnson, new AnswerTask() {
addRequestToQueue(jnson, new AnswerTask() {
@Override
public void run() {
public void onConnectionFailed(String message) {
callback.onAddFailed(message, "");
}
@Override
public void onResponse(JSONObject response) {
try {
if (response.getBoolean("success")) {
callback.onControllerAdded(response.getInt("cid"));
c.setId(response.getInt("cid"));
callback.onControllerAdded(c);
} else {
callback.onAddFailed(response.getString("message"), response.getString("message_detail"));
}
@@ -104,51 +125,123 @@ public class LedDHelper {
}
/**
* Set color using the stripeid
* Get stripes known to daemon
*
* @param sid Stripeid
* @param color Color in HSV format
* @throws JSONException no valid json
* @throws IOException socket error
*/
public void setColor(int sid, HSV color) throws JSONException, IOException {
public void getStripes(final StripesCallback callback) throws JSONException, IOException {
JSONObject jnson = new JSONObject();
jnson.put("action", ACTION_GETALLSTRIPES);
addRequestToQueue(jnson, new AnswerTask() {
@Override
public void onConnectionFailed(String message) {
callback.onGetFailed(message);
}
@Override
public void onResponse(JSONObject response) {
try {
if (response.getBoolean("success")) {
ledDDaemon.getControllers().clear();
List<LedStripe> list = new ArrayList<>();
JSONArray jcontrollers = response.getJSONArray("controller");
for (int i = 0; i < jcontrollers.length(); i++) {
JSONObject row = jcontrollers.getJSONObject(i);
Controller nController = new Controller();
nController.setAddress(row.getString("address"));
nController.setChannels(row.getInt("channel"));
nController.setI2c_device(row.getInt("i2c_device"));
nController.setId(row.getInt("id"));
JSONArray jstripes = row.getJSONArray("stripes");
for (int o = 0; o < jstripes.length(); o++) {
JSONObject srow = jstripes.getJSONObject(i);
LedStripe nStripe = new LedStripe();
nStripe.setId(srow.getInt("id"));
nStripe.setRGB(srow.getBoolean("rgb"));
nStripe.setChannelRed(srow.getJSONArray("channel").getInt(0));
nStripe.setChannelGreen(srow.getJSONArray("channel").getInt(1));
nStripe.setChannelBlue(srow.getJSONArray("channel").getInt(2));
nStripe.setName(srow.getString("name"));
nStripe.setController(nController);
nStripe.setLedDDaemon(ledDDaemon);
list.add(nStripe);
}
ledDDaemon.getControllers().add(nController);
}
callback.onSuccess(list);
} else {
if (response.has("message")) callback.onGetFailed(response.getString("message"));
else callback.onGetFailed("unknown error");
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Set color using the stripeid
*
* @param ledStripe Stripe
*/
public void setColor(LedStripe ledStripe) {
JSONObject jnson = new JSONObject();
JSONObject hsv = new JSONObject();
hsv.put("h", color.getHue());
hsv.put("s", color.getSaturation());
hsv.put("v", color.getValue());
try {
hsv.put("h", ledStripe.getColor().getHue());
hsv.put("s", ledStripe.getColor().getSaturation());
jnson.put("action", ACTION_SETCOLOR);
jnson.put("sid", sid);
jnson.put("color", hsv);
hsv.put("v", ledStripe.getColor().getValue());
sendJSON(jnson, null);
jnson.put("action", ACTION_SETCOLOR);
jnson.put("sid", ledStripe.getId());
jnson.put("hsv", hsv);
} catch (JSONException e) {
e.printStackTrace();
}
addRequestToQueue(jnson, null);
}
/**
* Get color using the stripeid
*
* @param sid Stripeid
* @param ledStripe Stripe
* @throws JSONException no valid json
* @throws IOException socket error
*/
public void getColor(int sid, final RecieveColorCallback callback) throws JSONException, IOException {
public void getColor(final LedStripe ledStripe, final RecieveColorCallback callback) throws JSONException, IOException {
JSONObject jnson = new JSONObject();
jnson.put("action", ACTION_GETCOLOR);
jnson.put("sid", sid);
jnson.put("sid", ledStripe.getId());
sendJSON(jnson, new AnswerTask() {
addRequestToQueue(jnson, new AnswerTask() {
@Override
public void run() {
public void onConnectionFailed(String message) {
callback.onRecievFailed(message);
}
@Override
public void onResponse(JSONObject response) {
try {
if (response.getBoolean("success")) {
JSONObject hsv = null;
hsv = response.getJSONObject("hsv");
callback.onColorRecieved(new HSV(hsv.getDouble("h"), hsv.getDouble("s"), hsv.getDouble("v")));
JSONArray hsv = response.getJSONArray("color");
ledStripe.setColor(new HSV(hsv.getDouble(0), hsv.getDouble(1), hsv.getDouble(2)));
callback.onColorRecieved(ledStripe);
} else {
callback.onRecievFailed(response.getString("message"));
}
@@ -159,15 +252,151 @@ public class LedDHelper {
});
}
private void sendJSON(JSONObject json, AnswerTask task) throws IOException, JSONException {
if (mBound) binderService.queueSend(host, json, task);
/**
* Test a channel on the controller
*
* @param c controller
* @param channel channel number
* @param value value (1= on, 0 = off)
* @throws JSONException
*/
public void testChannel(Controller c, int channel, int value) throws JSONException {
JSONObject jnson = new JSONObject();
jnson.put("action", ACTION_TESTCHANNEL);
jnson.put("cid", c.getId());
jnson.put("channel", channel);
jnson.put("value", value);
addRequestToQueue(jnson, new AnswerTask() {
@Override
public void onConnectionFailed(String message) {
}
@Override
public void onResponse(JSONObject response) {
}
});
}
/**
* Get information about an ledd daemon
*
* @throws JSONException no valid json
* @throws IOException socket error
*/
public void discover(final DiscoverCallback callback) throws JSONException, IOException {
JSONObject jnson = new JSONObject();
jnson.put("action", ACTION_DISCOVER);
addRequestToQueue(jnson, new AnswerTask() {
@Override
public void onConnectionFailed(String message) {
callback.onConnectionFailed(message);
}
@Override
public void onResponse(JSONObject response) {
try {
if (response.getBoolean("success")) {
callback.onDiscoverSuccessfully(response.getString("version"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Get information about an ledd daemon
*
* @throws JSONException no valid json
* @throws IOException socket error
*/
public void addStripe(final LedStripe ledStripe, final AddStripeCallback callback) throws JSONException, IOException {
JSONObject jnson = new JSONObject();
jnson.put("action", ACTION_ADDSTRIPES);
JSONObject stripe = new JSONObject();
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);
addRequestToQueue(jnson, new AnswerTask() {
@Override
public void onConnectionFailed(String message) {
callback.onConnectionFailed(message);
}
@Override
public void onResponse(JSONObject response) {
try {
if (response.getBoolean("success")) {
ledStripe.setId(response.getInt("sid"));
callback.onAddSuccessfully(ledStripe);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private void addRequestToQueue(JSONObject json, AnswerTask task) {
dRequests.add(new LedDRequest(json, task));
}
@DebugLog
public void teardown() {
if (mBound) {
context.unbindService(mConnection);
mBound = false;
}
requestWorker.stop();
}
private static class Worker<T> implements Runnable {
private final LinkedBlockingQueue<T> workQueue;
private final ColorService.ColorBinder binder;
private final LedDDaemon ledDDaemon;
public Worker(LinkedBlockingQueue<T> workQueue, ColorService.ColorBinder colorBinder, LedDDaemon dDaemon) {
this.workQueue = workQueue;
this.binder = colorBinder;
this.ledDDaemon = dDaemon;
}
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
final T item = workQueue.take();
if (item instanceof LedDRequest) {
binder.queueSend(ledDDaemon, ((LedDRequest) item).getRequest(), ((LedDRequest) item).getTask());
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
break;
} catch (JSONException e) {
e.printStackTrace();
}
}
}
public void stop() {
Thread.currentThread().interrupt();
}
}
}