SecondaryClient.java 8.2 KB

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