From f45c7b0519a16f273e10bf8409796b3b92450298 Mon Sep 17 00:00:00 2001 From: Rik Veenboer Date: Sat, 21 Mar 2015 21:37:32 +0000 Subject: [PATCH] Migrate to protcol buffer communication (https://github.com/google/protobuf), use preliminary buffer format from https://github.com/Boukefalos/arduino-tm1638. --- build.gradle | 11 ++++ .../ibuddy/implementation/Local.java | 11 ++-- .../ibuddy/implementation/Remote.java | 33 +++++++++--- .../boukefalos/ibuddy/server/Server.java | 37 +++++++------ src/main/java/test/TestCommunication.java | 2 +- src/main/proto/ibuddy.proto | 52 +++++++++++++++++++ 6 files changed, 117 insertions(+), 29 deletions(-) create mode 100644 src/main/proto/ibuddy.proto diff --git a/build.gradle b/build.gradle index bff64f6..a7d800a 100644 --- a/build.gradle +++ b/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/' diff --git a/src/main/java/com/github/boukefalos/ibuddy/implementation/Local.java b/src/main/java/com/github/boukefalos/ibuddy/implementation/Local.java index b473f28..e7a7f5b 100644 --- a/src/main/java/com/github/boukefalos/ibuddy/implementation/Local.java +++ b/src/main/java/com/github/boukefalos/ibuddy/implementation/Local.java @@ -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(); } } diff --git a/src/main/java/com/github/boukefalos/ibuddy/implementation/Remote.java b/src/main/java/com/github/boukefalos/ibuddy/implementation/Remote.java index 36c2ad2..6e58bce 100644 --- a/src/main/java/com/github/boukefalos/ibuddy/implementation/Remote.java +++ b/src/main/java/com/github/boukefalos/ibuddy/implementation/Remote.java @@ -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 { diff --git a/src/main/java/com/github/boukefalos/ibuddy/server/Server.java b/src/main/java/com/github/boukefalos/ibuddy/server/Server.java index 01e5d50..f6fe8f9 100644 --- a/src/main/java/com/github/boukefalos/ibuddy/server/Server.java +++ b/src/main/java/com/github/boukefalos/ibuddy/server/Server.java @@ -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); } diff --git a/src/main/java/test/TestCommunication.java b/src/main/java/test/TestCommunication.java index d12c0a1..2c12304 100644 --- a/src/main/java/test/TestCommunication.java +++ b/src/main/java/test/TestCommunication.java @@ -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(); diff --git a/src/main/proto/ibuddy.proto b/src/main/proto/ibuddy.proto new file mode 100644 index 0000000..6c23aa2 --- /dev/null +++ b/src/main/proto/ibuddy.proto @@ -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]; +} \ No newline at end of file