From 0558727c148df1128e7c6b052db1ae00b61683b1 Mon Sep 17 00:00:00 2001
From: rschoene <rene.schoene@tu-dresden.de>
Date: Thu, 4 Jun 2020 18:57:59 +0200
Subject: [PATCH] Use JSON config for setup (both starter and receiver).

- Starter: Send initial DataConfig
- Starter: Always send complete RobotConfig
---
 .../jastadd/ros2rag/compiler/SimpleMain.java  | 116 ++++++++++++++----
 1 file changed, 89 insertions(+), 27 deletions(-)

diff --git a/src/main/java/org/jastadd/ros2rag/compiler/SimpleMain.java b/src/main/java/org/jastadd/ros2rag/compiler/SimpleMain.java
index 7c54125..74406f8 100644
--- a/src/main/java/org/jastadd/ros2rag/compiler/SimpleMain.java
+++ b/src/main/java/org/jastadd/ros2rag/compiler/SimpleMain.java
@@ -7,6 +7,7 @@ import org.jastadd.ros2rag.scanner.Ros2RagScanner;
 
 import java.io.BufferedReader;
 import java.io.IOException;
+import java.nio.ByteBuffer;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -17,34 +18,95 @@ import java.nio.file.Paths;
  * @author rschoene - Initial contribution
  */
 public class SimpleMain {
+
+  // --- just testing byte[] conversion ---
+  public static void testing() {
+    byte[] bytes;
+    int i = 1;
+    double d = 2.3d;
+    float f = 4.2f;
+    short sh = 13;
+    long l = 7L;
+    String s = "Hello";
+    char c = 'a';
+
+    Integer ii = Integer.valueOf(1);
+    if (!ii.equals(i)) throw new AssertionError("Ints not equal");
+
+    // int to byte
+    ByteBuffer i2b = ByteBuffer.allocate(4);
+    i2b.putInt(i);
+    bytes = i2b.array();
+
+    // byte to int
+    ByteBuffer b2i = ByteBuffer.wrap(bytes);
+    int actual_i = b2i.getInt();
+    if (i != actual_i) throw new AssertionError("Ints not equal");
+
+    // double to byte
+    ByteBuffer d2b = ByteBuffer.allocate(8);
+    d2b.putDouble(d);
+    bytes = d2b.array();
+
+    // byte to double
+    ByteBuffer b2d = ByteBuffer.wrap(bytes);
+    double actual_d = b2d.getDouble();
+    if (d != actual_d) throw new AssertionError("Doubles not equal");
+
+    // float to byte
+    ByteBuffer f2b = ByteBuffer.allocate(4);
+    f2b.putFloat(f);
+    bytes = f2b.array();
+
+    // byte to float
+    ByteBuffer b2f = ByteBuffer.wrap(bytes);
+    float actual_f = b2f.getFloat();
+    if (f != actual_f) throw new AssertionError("Floats not equal");
+
+    // short to byte
+    ByteBuffer sh2b = ByteBuffer.allocate(2);
+    sh2b.putShort(sh);
+    bytes = sh2b.array();
+
+    // byte to short
+    ByteBuffer b2sh = ByteBuffer.wrap(bytes);
+    short actual_sh = b2sh.getShort();
+    if (sh != actual_sh) throw new AssertionError("Shorts not equal");
+
+    // long to byte
+    ByteBuffer l2b = ByteBuffer.allocate(8);
+    l2b.putLong(l);
+    bytes = l2b.array();
+
+    // byte to long
+    ByteBuffer b2l = ByteBuffer.wrap(bytes);
+    long actual_l = b2l.getLong();
+    if (l != actual_l) throw new AssertionError("Longs not equal");
+
+    // String to byte
+    bytes = s.getBytes();
+
+    // byte to String
+    String actual_s = new String(bytes);
+    if (!s.equals(actual_s)) throw new AssertionError("Strings not equal");
+
+    // char to byte
+    ByteBuffer c2b = ByteBuffer.allocate(2);
+    c2b.putChar(c);
+    bytes = c2b.array();
+
+    // byte to char
+    ByteBuffer b2c = ByteBuffer.wrap(bytes);
+    char actual_c = b2c.getChar();
+    if (c != actual_c) throw new AssertionError("Floats not equal");
+  }
+
   public static void main(String[] args) {
-    /*
-    // as soon as the cache of isInSafetyZone is invalidated, update the value of Robot.ShouldUseLowSpeed with its value
-    [always] update Robot.ShouldUseLowSpeed with isInSafetyZone() using transformation();
-
-    // when a (new?) value for ShouldUseLowSpeed is set, send it over via mqtt
-    [always] write Robot.ShouldUseLowSpeed;
-
-    // when an update of pose is read via mqtt, then update current position
-    [always] read Joint.CurrentPosition using PoseToPosition;
-
-    // PBPose is a datatype defined in protobuf
-    PoseToPosition: map PBPose to Position using
-      pose.position.x += sqrt(.5 * size.x)
-      MAP round(2)
-      x = x / 100
-      IGNORE_IF_SAME
-    ;
-
-    --- using generated methods ---
-    Joint j;
-    j.getCurrentPosition().connectTo("/robot/joint2/pos");
-
-    RobotArm r;
-    // this should not be required
-    r.getShouldUseLowSpeed().addObserver(j.getCurrentPosition());
-    r.getShouldUseLowSpeed().connectTo("/robot/config/speed");
-     */
+    testing();
+//    createManualAST();
+  }
+
+  private static void createManualAST() {
     Ros2Rag model = new Ros2Rag();
     Program program = parseProgram(Paths.get("src", "test", "resources", "Example.relast"));
     model.setProgram(program);
-- 
GitLab