Migrate to protcol buffer communication (https://github.com/google/protobuf), use preliminary buffer format from https://github.com/Boukefalos/arduino-tm1638.

This commit is contained in:
2015-03-21 21:37:32 +00:00
parent 5dc25b8858
commit f45c7b0519
6 changed files with 117 additions and 29 deletions

View File

@@ -40,15 +40,16 @@ public class Local implements iBuddy {
}
@Override
public void setHeadRed(boolean headRed) throws iBuddyException {
// TODO Auto-generated method stub
try {
IBuddy.sendHeadRed(headRed);
} catch (IBuddyException e) {
throw new iBuddyException();
}
}
@Override
public void test() throws IBuddyException {
IBuddy.sendAllOff();
IBuddy.sendAllOff();
}
}

View File

@@ -1,5 +1,6 @@
package com.github.boukefalos.ibuddy.implementation;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
@@ -11,9 +12,19 @@ import org.jraf.jlibibuddy.IBuddyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.boukefalos.ibuddy.iBuddy;
import com.github.boukefalos.ibuddy.exception.iBuddyException;
import ibuddy.Ibuddy.Color;
import ibuddy.Ibuddy.Command;
import ibuddy.Ibuddy.Command.Type;
import ibuddy.Ibuddy.SetLed;
public class Remote implements iBuddy {
protected Logger logger = LoggerFactory.getLogger(getClass());
@@ -28,8 +39,21 @@ public class Remote implements iBuddy {
this.port = port;
}
public void sendHeadRed(boolean headRed) {
send("RED");
public void setHeadRed(boolean headRed) {
Command command = Command.newBuilder()
.setType(Type.SET_LED)
.setSetLed(
SetLed.newBuilder()
.setColor(Color.RED)
.setPos(0).build()).build();
ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
try {
command.writeDelimitedTo(output);
send(output.toByteArray());
} catch (IOException e) {
logger.error("Failed to send command");
}
}
public void setHeadGreen(boolean headGreen) {
@@ -72,11 +96,6 @@ public class Remote implements iBuddy {
return true;
}
@Override
public void setHeadRed(boolean headRed) throws iBuddyException {
// TODO Auto-generated method stub
}
@Override
public void test() throws IBuddyException {

View File

@@ -1,5 +1,10 @@
package com.github.boukefalos.ibuddy.server;
import ibuddy.Ibuddy.Command;
import ibuddy.Ibuddy.Command.Type;
import ibuddy.Ibuddy.SetLed;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
@@ -42,24 +47,24 @@ public class Server extends Thread {
} catch (IOException e) {
logger.error("Failed to receive packet", e);
}
// Rewrite with protocol buffers!
String received = new String(datagramPacket.getData(), datagramPacket.getOffset(), datagramPacket.getLength());
ByteArrayInputStream input = new ByteArrayInputStream(buffer);
logger.debug("Received input");
try {
Color color = IBuddy.Color.valueOf(received);
switch (color) {
case RED:
iBuddy.setHeadRed(true);
break;
case GREEN:
iBuddy.setHeadGreen(true);
break;
case BLUE:
iBuddy.setHeadBlue(true);
break;
Command command = Command.parseDelimitedFrom(input);
logger.debug("Command type = " + command.getType().name());
switch (command.getType()) {
case SET_LED:
SetLed setLed = command.getSetLed();
logger.debug("Color = " + setLed.getColor().name());
switch (setLed.getColor()) {
case RED:
iBuddy.setHeadRed(true);
}
break;
}
} catch (IllegalArgumentException e) {
logger.error("No such command", e);
} catch (IOException e) {
logger.error("Failed to parse input");
return;
} catch (iBuddyException e) {
logger.error("Failed to send command to iBuddy", e);
}