SecondaryClient.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package at.acdp.urweb.sclient;
  2. import at.acdp.urweb.sclient.data.MessageType;
  3. import at.acdp.urweb.sclient.data.PackageType;
  4. import at.acdp.urweb.sclient.data.RobotMessageType;
  5. import at.acdp.urweb.sclient.data.VersionMessage;
  6. import java.io.*;
  7. import java.net.Socket;
  8. import java.nio.charset.StandardCharsets;
  9. public class SecondaryClient {
  10. private final String ip;
  11. private final int port;
  12. private volatile boolean _running = true;
  13. private Socket rt;
  14. private OutputStream os;
  15. private DataInputStream in;
  16. public SecondaryClient(String ip, int port) {
  17. this.ip = ip;
  18. this.port = port;
  19. }
  20. public void connect() throws IOException {
  21. this.rt = new Socket(ip, port);
  22. this.os = rt.getOutputStream();
  23. this.in = new DataInputStream(rt.getInputStream());
  24. VersionMessage vm = new VersionMessage();
  25. vm.readVersionMessage(in);
  26. System.out.println(vm);
  27. readReply(in);
  28. }
  29. private void readReply(DataInputStream di) throws IOException {
  30. int size=di.readInt(); //4
  31. System.out.println("size: " +size);
  32. int pType= di.readByte() &0xff; //+1=5
  33. switch (pType) {
  34. case MessageType.ROBOT_MESSAGE:
  35. readRobotMessage(di, size);
  36. break;
  37. case MessageType.ROBOT_STATE:
  38. readRobotState(di, size);
  39. break;
  40. case PackageType.ROBOT_MODE_DATA:
  41. readRobotModeData(di, size);
  42. break;
  43. case PackageType.JOINT_DATA:
  44. readJointData(di, size);
  45. break;
  46. }
  47. }
  48. void readRobotMessage(DataInputStream di, int size) throws IOException {
  49. long ts=di.readLong();
  50. char source= (char) (di.readByte() & 0xFF);
  51. char robotMessageType= (char) (di.readByte() & 0xFF);
  52. switch (robotMessageType) {
  53. default:
  54. System.out.println("unknown msg" + (int)robotMessageType);
  55. //byte[] buf=new byte[size-11];
  56. byte[] buf=new byte[size-10];
  57. di.readFully(buf);
  58. System.out.println(buf);
  59. }
  60. }
  61. void readRobotState(DataInputStream di, int size) throws IOException {
  62. long ts=di.readLong();
  63. char source= (char) (di.readByte() & 0xFF);
  64. char robotMessageType= (char) (di.readByte() & 0xFF);
  65. switch (robotMessageType) {
  66. default:
  67. System.out.println("unknown msg" + robotMessageType);
  68. byte[] buf=new byte[size-10];
  69. di.readFully(buf);
  70. }
  71. }
  72. ModeData readRobotModeData(DataInputStream di, int size) throws IOException {
  73. long ts=di.readLong();
  74. ModeData md=new ModeData();
  75. md.realRobotConnected=di.readBoolean();
  76. md.realRobotEnabled=di.readBoolean();
  77. md.robotPoweredOn=di.readBoolean();
  78. md.emergencyStopped=di.readBoolean();
  79. md.protectiveStopped=di.readBoolean();
  80. md.isProgramRunning=di.readBoolean();
  81. md.isProgramPaused=di.readBoolean();
  82. md.robotMode=di.readByte();
  83. md.controlMode=di.readByte();
  84. md.targetSpeedFraction=di.readDouble();
  85. md.speedScaling=di.readDouble();
  86. md.targetSpeedFractionLimit=di.readDouble();
  87. md.internal=di.readByte();
  88. //byte[] buf=new byte[50];
  89. //di.readFully(buf);
  90. return md;
  91. }
  92. JointData[] readJointData(DataInputStream di, int size) throws IOException {
  93. int joints=size/49;
  94. JointData[] jds=new JointData[5];
  95. for(int i=0;i<joints;i++) {
  96. JointData jd=new JointData();
  97. jd.read(di);
  98. jds[i]=jd;
  99. }
  100. return jds;
  101. }
  102. public void writeCmd(String cmd) {
  103. try {
  104. System.out.println("send cmd:" + cmd);
  105. os.write(cmd.getBytes(StandardCharsets.UTF_8));
  106. readReply(in);
  107. readReply(in);
  108. readReply(in);
  109. } catch (IOException e) {
  110. e.printStackTrace();
  111. }
  112. }
  113. }