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:
11
build.gradle
11
build.gradle
@@ -1,5 +1,6 @@
|
||||
apply plugin: 'java'
|
||||
apply plugin: 'eclipse'
|
||||
apply plugin: 'protobuf'
|
||||
apply plugin: 'maven'
|
||||
|
||||
group = 'com.github.boukefalos'
|
||||
@@ -10,6 +11,16 @@ task wrapper(type: Wrapper) {
|
||||
gradleVersion = '2.2'
|
||||
}
|
||||
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
classpath 'com.andrewkroh.gradle:gradle-protobuf-plugin:0.4.0'
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
maven {
|
||||
url 'https://github.com/Boukefalos/jlibloader/raw/mvn-repo/'
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ public class TestCommunication {
|
||||
Server server = localLoader.getServer();
|
||||
|
||||
server.start();
|
||||
remoteiBuddy.setHeadGreen(true);
|
||||
remoteiBuddy.setHeadRed(true);
|
||||
Thread.sleep(10000);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
52
src/main/proto/ibuddy.proto
Normal file
52
src/main/proto/ibuddy.proto
Normal file
@@ -0,0 +1,52 @@
|
||||
package ibuddy;
|
||||
|
||||
enum Color {
|
||||
GREEN = 1;
|
||||
RED = 2;
|
||||
BOTH = 3;
|
||||
NONE = 4;
|
||||
}
|
||||
|
||||
enum Module {
|
||||
TM1638 = 1;
|
||||
InvertedTM1638 = 2;
|
||||
TM1640 = 3;
|
||||
}
|
||||
|
||||
message Command {
|
||||
enum Type {
|
||||
PING = 1;
|
||||
CONSTRUCT = 2;
|
||||
SET_LED = 10;
|
||||
}
|
||||
|
||||
required Type type = 1;
|
||||
optional Ping ping = 2;
|
||||
optional Construct construct = 3;
|
||||
optional SetLed setLed = 10;
|
||||
}
|
||||
|
||||
message Ping {
|
||||
required int32 id = 1;
|
||||
}
|
||||
|
||||
message Echo {
|
||||
required int32 id = 1;
|
||||
optional string message = 2;
|
||||
}
|
||||
|
||||
message Construct {
|
||||
required int32 dataPin = 1;
|
||||
required int32 clockPin = 2;
|
||||
optional int32 strobePin = 3;
|
||||
optional bool activateDisplay = 4 [default = true];
|
||||
optional int32 intensity = 5 [default = 7];
|
||||
optional Module module = 6 [default = TM1638];
|
||||
optional int32 id = 7 [default = 0];
|
||||
}
|
||||
|
||||
message SetLed {
|
||||
required Color color = 1;
|
||||
required int32 pos = 2;
|
||||
optional int32 id = 3 [default = 1];
|
||||
}
|
||||
Reference in New Issue
Block a user