SecondaryClient.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. 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. var md=readRobotModeData(di, size);
  42. log.info(md.toString());
  43. break;
  44. case PackageType.JOINT_DATA:
  45. var jd=readJointData(di, size);
  46. log.info(jd.toString());
  47. break;
  48. case PackageType.TOOL_DATA:
  49. var td=readToolData(di, size);
  50. log.info(td.toString());
  51. break;
  52. case PackageType.MASTERBOARD_DATA:
  53. var mb=readMasterBoardData(di, size);
  54. log.info(mb.toString());
  55. break;
  56. case PackageType.CARTESIAN_INFO:
  57. var c=readCartesianInfo(di, size);
  58. log.info(c.toString());
  59. break;
  60. case PackageType.KINEMATICS_INFO:
  61. var ki=readKinemsticsInfo(di, size);
  62. log.info(ki.toString());
  63. break;
  64. case PackageType.CONFIGURATION_DATA:
  65. var cd=readConfigurationData(di, size);
  66. log.info(cd.toString());
  67. break;
  68. case PackageType.FORCE_MODE_DATA:
  69. var fmd=readForceModeData(di, size);
  70. log.info(fmd.toString());
  71. break;
  72. case PackageType.ADDITIONAL_INFO:
  73. var ai=readAdditionalInfo(di, size);
  74. log.info(ai.toString());
  75. break;
  76. case PackageType.NEEDED_FOR_CALIB_DATA:
  77. skip(PackageType.NEEDED_FOR_CALIB_DATA,di, size);
  78. break;
  79. case PackageType.SAFETY_DATA:
  80. skip(PackageType.SAFETY_DATA,di, size);
  81. break;
  82. case PackageType.TOOL_COMM_INFO:
  83. var tci=readToolCommInfo(di, size);
  84. log.info(tci.toString());
  85. break;
  86. default:
  87. log.warn("unknown ptype: "+pType+", size: "+size);
  88. byte[] pack=new byte[size-5];
  89. di.readFully(pack);
  90. }
  91. int afterCount=di.getCount();
  92. int diff=afterCount-beforeCount-size;
  93. if(diff!=0) {
  94. log.warn("size mismatch: " +diff + "package type: "+pType);
  95. }
  96. }
  97. private MasterBoardData readMasterBoardData(CountDataInputStream di, int size) throws IOException {
  98. MasterBoardData mb=new MasterBoardData();
  99. mb.read(di, size);
  100. return mb;
  101. }
  102. private ToolData readToolData(CountDataInputStream di, int size) throws IOException {
  103. ToolData td=new ToolData();
  104. td.read(di, size);
  105. return td;
  106. }
  107. private ToolCommInfo readToolCommInfo(CountDataInputStream di, int size) throws IOException {
  108. ToolCommInfo tci=new ToolCommInfo();
  109. tci.read(di, size);
  110. return tci;
  111. }
  112. private void skip(int pType, CountDataInputStream di, int size) throws IOException {
  113. log.trace("skip data for package type: "+pType);
  114. byte[] data=new byte[size-5];
  115. di.readFully(data);
  116. }
  117. private AdditionalInfo readAdditionalInfo(CountDataInputStream di, int size) throws IOException {
  118. AdditionalInfo ai=new AdditionalInfo();
  119. ai.read(di, size);
  120. return ai;
  121. }
  122. private ForceModeData readForceModeData(CountDataInputStream di, int size) throws IOException {
  123. ForceModeData fmd=new ForceModeData();
  124. fmd.read(di, size);
  125. return fmd;
  126. }
  127. private ConfigurationData readConfigurationData(CountDataInputStream di, int size) throws IOException {
  128. ConfigurationData cd=new ConfigurationData();
  129. cd.read(di, size);
  130. return cd;
  131. }
  132. void readRobotMessage(CountDataInputStream di, int size) throws IOException {
  133. long ts = di.readLong();
  134. char source = (char) (di.readByte() & 0xFF);
  135. char robotMessageType = (char) (di.readByte() & 0xFF);
  136. switch (robotMessageType) {
  137. default:
  138. log.warn("unknown msg" + (int) robotMessageType);
  139. //byte[] buf=new byte[size-11];
  140. byte[] buf = new byte[size - 10];
  141. di.readFully(buf);
  142. System.out.println(buf);
  143. }
  144. }
  145. void readRobotState(CountDataInputStream di, int size) throws IOException {
  146. long ts = di.readLong();
  147. char source = (char) (di.readByte() & 0xFF);
  148. char robotMessageType = (char) (di.readByte() & 0xFF);
  149. switch (robotMessageType) {
  150. default:
  151. log.info("unknown msg: " + (int)robotMessageType);
  152. byte[] buf = new byte[size - 10];
  153. di.readFully(buf);
  154. }
  155. }
  156. CartesianInfo readCartesianInfo(CountDataInputStream di, int size) throws IOException {
  157. CartesianInfo ci = new CartesianInfo();
  158. ci.read(di, size);
  159. return ci;
  160. }
  161. KinematicsInfo readKinemsticsInfo(CountDataInputStream di, int size) throws IOException {
  162. KinematicsInfo ki = new KinematicsInfo();
  163. ki.read(di, size);
  164. return ki;
  165. }
  166. ModeData readRobotModeData(CountDataInputStream di, int size) throws IOException {
  167. long ts = di.readLong();
  168. ModeData md = new ModeData();
  169. md.realRobotConnected = di.readBoolean();
  170. md.realRobotEnabled = di.readBoolean();
  171. md.robotPoweredOn = di.readBoolean();
  172. md.emergencyStopped = di.readBoolean();
  173. md.protectiveStopped = di.readBoolean();
  174. md.isProgramRunning = di.readBoolean();
  175. md.isProgramPaused = di.readBoolean();
  176. md.robotMode = di.readByte();
  177. md.controlMode = di.readByte();
  178. md.targetSpeedFraction = di.readDouble();
  179. md.speedScaling = di.readDouble();
  180. md.targetSpeedFractionLimit = di.readDouble();
  181. md.internal = di.readByte();
  182. return md;
  183. }
  184. JointData[] readJointData(CountDataInputStream di, int size) throws IOException {
  185. int joints = size / 41;
  186. JointData[] jds = new JointData[size];
  187. for (int i = 0; i < joints; i++) {
  188. JointData jd = new JointData();
  189. jd.read(di, size);
  190. jds[i] = jd;
  191. }
  192. return jds;
  193. }
  194. public void writeCmd(String cmd) {
  195. try {
  196. System.out.println("send cmd:" + cmd);
  197. os.write(cmd.getBytes(StandardCharsets.UTF_8));
  198. } catch (IOException e) {
  199. e.printStackTrace();
  200. }
  201. }
  202. public void readPackage() throws IOException {
  203. readReply(in);
  204. }
  205. }