SecondaryClient.java 6.9 KB

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