SecondaryClient.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package at.acdp.urweb.sclient;
  2. import at.acdp.urweb.sclient.data.*;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import java.io.*;
  6. import java.net.Socket;
  7. import java.nio.charset.StandardCharsets;
  8. public class SecondaryClient {
  9. private static final Logger log = LoggerFactory.getLogger(SecondaryClient.class);
  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 VersionMessage 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. return vm;
  27. }
  28. public void readReply(DataInputStream di) throws IOException {
  29. int size = di.readInt(); //4
  30. int pType = di.readByte() & 0xff; //+1=5
  31. log.info("ptype: "+pType);
  32. switch (pType) {
  33. case MessageType.ROBOT_MESSAGE:
  34. readRobotMessage(di, size);
  35. break;
  36. case MessageType.ROBOT_STATE:
  37. readRobotState(di, size);
  38. break;
  39. case PackageType.ROBOT_MODE_DATA:
  40. readRobotModeData(di, size);
  41. break;
  42. case PackageType.JOINT_DATA:
  43. readJointData(di, size);
  44. break;
  45. case PackageType.CARTESIAN_INFO:
  46. readCartesianInfo(di, size);
  47. break;
  48. case PackageType.KINEMATICS_INFO:
  49. readKinemsticsInfo(di, size);
  50. break;
  51. case PackageType.CONFIGURATION_DATA:
  52. readConfigurationData(di, size);
  53. break;
  54. case PackageType.FORCE_MODE_DATA:
  55. readForceModeData(di, size);
  56. break;
  57. case PackageType.ADDITIONAL_INFO:
  58. readAdditionalInfo(di, size);
  59. break;
  60. case PackageType.NEEDED_FOR_CALIB_DATA:
  61. skip(PackageType.NEEDED_FOR_CALIB_DATA,di, size);
  62. break;
  63. case PackageType.SAFETY_DATA:
  64. skip(PackageType.SAFETY_DATA,di, size);
  65. break;
  66. case PackageType.TOOL_COMM_INFO:
  67. readToolCommInfo(di, size);
  68. break;
  69. default:
  70. log.warn("unknown ptype: "+pType+", size: "+size);
  71. byte[] pack=new byte[size-5];
  72. di.readFully(pack);
  73. }
  74. }
  75. private ToolCommInfo readToolCommInfo(DataInputStream di, int size) throws IOException {
  76. ToolCommInfo tci=new ToolCommInfo();
  77. tci.read(di);
  78. return tci;
  79. }
  80. private void skip(int pType, DataInputStream di, int size) throws IOException {
  81. log.trace("skip data for package type: "+pType);
  82. byte[] data=new byte[size-5];
  83. di.readFully(data);
  84. }
  85. private AdditionalInfo readAdditionalInfo(DataInputStream di, int size) throws IOException {
  86. AdditionalInfo ai=new AdditionalInfo();
  87. ai.read(di, size);
  88. return ai;
  89. }
  90. private ForceModeData readForceModeData(DataInputStream di, int size) throws IOException {
  91. ForceModeData fmd=new ForceModeData();
  92. fmd.read(di, size);
  93. return fmd;
  94. }
  95. private ConfigurationData readConfigurationData(DataInputStream di, int size) throws IOException {
  96. ConfigurationData cd=new ConfigurationData();
  97. cd.read(di, size);
  98. return cd;
  99. }
  100. void readRobotMessage(DataInputStream di, int size) throws IOException {
  101. long ts = di.readLong();
  102. char source = (char) (di.readByte() & 0xFF);
  103. char robotMessageType = (char) (di.readByte() & 0xFF);
  104. switch (robotMessageType) {
  105. default:
  106. System.out.println("unknown msg" + (int) robotMessageType);
  107. //byte[] buf=new byte[size-11];
  108. byte[] buf = new byte[size - 10];
  109. di.readFully(buf);
  110. System.out.println(buf);
  111. }
  112. }
  113. void readRobotState(DataInputStream di, int size) throws IOException {
  114. long ts = di.readLong();
  115. char source = (char) (di.readByte() & 0xFF);
  116. char robotMessageType = (char) (di.readByte() & 0xFF);
  117. switch (robotMessageType) {
  118. default:
  119. System.out.println("unknown msg" + robotMessageType);
  120. byte[] buf = new byte[size - 10];
  121. di.readFully(buf);
  122. }
  123. }
  124. CartesianInfo readCartesianInfo(DataInputStream di, int size) throws IOException {
  125. CartesianInfo ci = new CartesianInfo();
  126. ci.read(di);
  127. return ci;
  128. }
  129. KinematicsInfo readKinemsticsInfo(DataInputStream di, int size) throws IOException {
  130. KinematicsInfo ki = new KinematicsInfo();
  131. ki.read(di, size);
  132. return ki;
  133. }
  134. ModeData readRobotModeData(DataInputStream di, int size) throws IOException {
  135. long ts = di.readLong();
  136. ModeData md = new ModeData();
  137. md.realRobotConnected = di.readBoolean();
  138. md.realRobotEnabled = di.readBoolean();
  139. md.robotPoweredOn = di.readBoolean();
  140. md.emergencyStopped = di.readBoolean();
  141. md.protectiveStopped = di.readBoolean();
  142. md.isProgramRunning = di.readBoolean();
  143. md.isProgramPaused = di.readBoolean();
  144. md.robotMode = di.readByte();
  145. md.controlMode = di.readByte();
  146. md.targetSpeedFraction = di.readDouble();
  147. md.speedScaling = di.readDouble();
  148. md.targetSpeedFractionLimit = di.readDouble();
  149. md.internal = di.readByte();
  150. //byte[] buf=new byte[50];
  151. //di.readFully(buf);
  152. return md;
  153. }
  154. JointData[] readJointData(DataInputStream di, int size) throws IOException {
  155. int joints = size / 41;
  156. // byte[] buf=new byte[300];
  157. // di.readFully(buf);
  158. JointData[] jds = new JointData[size];
  159. for (int i = 0; i < joints; i++) {
  160. JointData jd = new JointData();
  161. jd.read(di);
  162. jds[i] = jd;
  163. }
  164. return jds;
  165. }
  166. public void writeCmd(String cmd) {
  167. try {
  168. System.out.println("send cmd:" + cmd);
  169. os.write(cmd.getBytes(StandardCharsets.UTF_8));
  170. } catch (IOException e) {
  171. e.printStackTrace();
  172. }
  173. }
  174. public void readPackage() throws IOException {
  175. readReply(in);
  176. }
  177. }