RtdeInDataPackage.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package at.acdp.urweb.rtde.packets;
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.util.HashMap;
  6. import static at.acdp.urweb.rtde.CommandType.RTDE_DATA_PACKAGE;
  7. import static at.acdp.urweb.rtde.CommandType.RTDE_TEXT_MESSAGE;
  8. public class RtdeInDataPackage implements IRtdeData {
  9. private final RtdeSetupInputs inputs;
  10. private HashMap<String, String> pdata=new HashMap<>();
  11. public RtdeInDataPackage(RtdeSetupInputs inputs) {
  12. this.inputs=inputs;
  13. }
  14. @Override
  15. public int getType() {
  16. return RTDE_DATA_PACKAGE;
  17. }
  18. public HashMap<String, String> getPdata() {
  19. return pdata;
  20. }
  21. @Override
  22. public RtdeInDataPackage read(DataInputStream di, int length) throws IOException {
  23. return this;
  24. }
  25. private int calcSize() {
  26. int size = 0;
  27. for (int i = 0; i < inputs.variables.size(); i++) {
  28. String output = inputs.variables.get(i);
  29. String type = inputs.types.get(i);
  30. switch (type) {
  31. case "BOOL":
  32. case "UINT8":
  33. size+=1;
  34. break;
  35. case "UINT32":
  36. case "INT32":
  37. size+=4;
  38. break;
  39. case "UINT64":
  40. case "DOUBLE":
  41. size+=8;
  42. break;
  43. case "VECTOR3D":
  44. case "VECTOR6INT32":
  45. case "VECTOR6UINT32":
  46. size+=24;
  47. break;
  48. case "VECTOR6D":
  49. size+=48;
  50. break;
  51. }
  52. }
  53. return size;
  54. }
  55. @Override
  56. public RtdeInDataPackage send(DataOutputStream dos) throws IOException {
  57. sendHeader(dos,calcSize()+1);
  58. dos.writeByte(inputs.recipe);
  59. for(int i=0; i<inputs.variables.size(); i++) {
  60. String output = inputs.variables.get(i);
  61. String type = inputs.types.get(i);
  62. switch (type) {
  63. case "BOOL":
  64. dos.writeBoolean(Boolean.parseBoolean(pdata.get(output)));
  65. break;
  66. case "UINT8":
  67. dos.writeByte(Byte.parseByte(pdata.get(output)));
  68. break;
  69. case "UINT32":
  70. case "INT32":
  71. dos.writeInt(Integer.parseInt(pdata.get(output)));
  72. break;
  73. case "UINT64":
  74. dos.writeLong(Long.parseLong(pdata.get(output)));
  75. break;
  76. case "DOUBLE":
  77. dos.writeDouble(Double.parseDouble(pdata.get(output)));
  78. break;
  79. case "VECTOR3D": {
  80. String s=pdata.get(output);
  81. var doubles=s.split(" ");
  82. dos.writeDouble(Double.parseDouble(doubles[0]));
  83. dos.writeDouble(Double.parseDouble(doubles[1]));
  84. dos.writeDouble(Double.parseDouble(doubles[2]));
  85. break;}
  86. case "VECTOR6D": {
  87. String s=pdata.get(output);
  88. var doubles=s.split(" ");
  89. dos.writeDouble(Double.parseDouble(doubles[0]));
  90. dos.writeDouble(Double.parseDouble(doubles[1]));
  91. dos.writeDouble(Double.parseDouble(doubles[2]));
  92. dos.writeDouble(Double.parseDouble(doubles[3]));
  93. dos.writeDouble(Double.parseDouble(doubles[4]));
  94. dos.writeDouble(Double.parseDouble(doubles[5]));
  95. break;}
  96. case "VECTOR6INT32":
  97. case "VECTOR6UINT32": {
  98. String s=pdata.get(output);
  99. var ints=s.split(" ");
  100. dos.writeInt(Integer.parseInt(ints[0]));
  101. dos.writeInt(Integer.parseInt(ints[1]));
  102. dos.writeInt(Integer.parseInt(ints[2]));
  103. dos.writeInt(Integer.parseInt(ints[3]));
  104. dos.writeInt(Integer.parseInt(ints[4]));
  105. dos.writeInt(Integer.parseInt(ints[5]));
  106. break;}
  107. default:
  108. throw new IllegalStateException("Unexpected value: " + type);
  109. }
  110. }
  111. return this;
  112. }
  113. public void put(String key, String value) {
  114. pdata.put(key, value);
  115. }
  116. @Override
  117. public boolean hasReply() {
  118. return false;
  119. }
  120. }