RtdeInDataPackage.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. int recipeID=di.readByte();
  24. return this;
  25. }
  26. @Override
  27. public RtdeInDataPackage send(DataOutputStream dos) throws IOException {
  28. sendHeader(dos,0);
  29. dos.writeByte(inputs.recipe);
  30. for(int i=0; i<inputs.variables.size(); i++) {
  31. String output = inputs.variables.get(i);
  32. String type = inputs.types.get(i);
  33. switch (type) {
  34. case "BOOL":
  35. dos.writeBoolean(Boolean.parseBoolean(pdata.get(output)));
  36. break;
  37. case "UINT8":
  38. dos.writeByte(Byte.parseByte(pdata.get(output)));
  39. break;
  40. case "UINT32":
  41. case "INT32":
  42. dos.writeInt(Integer.parseInt(pdata.get(output)));
  43. break;
  44. case "UINT64":
  45. dos.writeLong(Long.parseLong(pdata.get(output)));
  46. break;
  47. case "DOUBLE":
  48. dos.writeDouble(Double.parseDouble(pdata.get(output)));
  49. break;
  50. case "VECTOR3D": {
  51. String s=pdata.get(output);
  52. var doubles=s.split(" ");
  53. dos.writeDouble(Double.parseDouble(doubles[0]));
  54. dos.writeDouble(Double.parseDouble(doubles[1]));
  55. dos.writeDouble(Double.parseDouble(doubles[2]));
  56. break;}
  57. case "VECTOR6D": {
  58. String s=pdata.get(output);
  59. var doubles=s.split(" ");
  60. dos.writeDouble(Double.parseDouble(doubles[0]));
  61. dos.writeDouble(Double.parseDouble(doubles[1]));
  62. dos.writeDouble(Double.parseDouble(doubles[2]));
  63. dos.writeDouble(Double.parseDouble(doubles[3]));
  64. dos.writeDouble(Double.parseDouble(doubles[4]));
  65. dos.writeDouble(Double.parseDouble(doubles[5]));
  66. break;}
  67. case "VECTOR6INT32":
  68. case "VECTOR6UINT32": {
  69. String s=pdata.get(output);
  70. var ints=s.split(" ");
  71. dos.writeInt(Integer.parseInt(ints[0]));
  72. dos.writeInt(Integer.parseInt(ints[1]));
  73. dos.writeInt(Integer.parseInt(ints[2]));
  74. dos.writeInt(Integer.parseInt(ints[3]));
  75. dos.writeInt(Integer.parseInt(ints[4]));
  76. dos.writeInt(Integer.parseInt(ints[5]));
  77. break;}
  78. default:
  79. throw new IllegalStateException("Unexpected value: " + type);
  80. }
  81. }
  82. return this;
  83. }
  84. }