SecondaryClient.java 8.3 KB

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