YgslUtil.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package com.mes.ui;
  2. import com.mes.ygsl.YgslClient;
  3. import io.netty.buffer.ByteBuf;
  4. import io.netty.buffer.Unpooled;
  5. public class YgslUtil {
  6. // 心跳
  7. public static Boolean comHeart(YgslClient ygslClient){
  8. try{
  9. String synr_str = "002099990010 ";
  10. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  11. return true;
  12. }catch (Exception e){
  13. e.printStackTrace();
  14. return false;
  15. }
  16. }
  17. // 通讯启动 对应回复 0002 通讯启动确认
  18. public static Boolean comStart(YgslClient ygslClient){
  19. try{
  20. String synr_str = "002000010050 ";
  21. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  22. return true;
  23. }catch (Exception e){
  24. e.printStackTrace();
  25. return false;
  26. }
  27. }
  28. // 通讯停止 对应回复 0005 命令被接受 0004 命令错误
  29. public static Boolean comStop(YgslClient ygslClient){
  30. try{
  31. String synr_str = "002000030050 ";
  32. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  33. return true;
  34. }catch (Exception e){
  35. e.printStackTrace();
  36. return false;
  37. }
  38. }
  39. // 使能工具 回复 0004/0005
  40. public static Boolean enableTool(YgslClient ygslClient){
  41. try{
  42. String synr_str = "00200043000 ";
  43. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  44. return true;
  45. }catch (Exception e){
  46. e.printStackTrace();
  47. return false;
  48. }
  49. }
  50. // 禁用工具 回复 0004/0005
  51. public static Boolean disableTool(YgslClient ygslClient){
  52. try{
  53. String synr_str = "00200042000 ";
  54. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  55. return true;
  56. }catch (Exception e){
  57. e.printStackTrace();
  58. return false;
  59. }
  60. }
  61. //上次拧紧结果数据订阅 0061 拧紧数据上报
  62. public static Boolean lastTighteningResultSubscribe(YgslClient ygslClient){
  63. try{
  64. String synr_str = "002000600010 ";
  65. System.out.println("lastTighteningResultSubscribe:"+synr_str);
  66. System.out.println(stringToHex(synr_str));
  67. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  68. return true;
  69. }catch (Exception e){
  70. e.printStackTrace();
  71. return false;
  72. }
  73. }
  74. // 接收到订阅拧紧数据后回复
  75. public static Boolean lastTighteningResultDataAcknowledge(YgslClient ygslClient){
  76. try{
  77. String synr_str = "0020006200010 ";
  78. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  79. return true;
  80. }catch (Exception e){
  81. e.printStackTrace();
  82. return false;
  83. }
  84. }
  85. // 设置setId 对应回复 0010 pset
  86. public static Boolean pSet(YgslClient ygslClient){
  87. try{
  88. String synr_str = "002000100010 ";
  89. System.out.println("pSet:"+synr_str);
  90. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  91. return true;
  92. }catch (Exception e){
  93. e.printStackTrace();
  94. return false;
  95. }
  96. }
  97. // 选择 setId 0018 pset
  98. public static Boolean selectSet(YgslClient ygslClient){
  99. try{
  100. String synr_str = "002300180010 001";
  101. System.out.println("selectSet:"+synr_str);
  102. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  103. return true;
  104. }catch (Exception e){
  105. e.printStackTrace();
  106. return false;
  107. }
  108. }
  109. public static Boolean jobIdUpload(YgslClient ygslClient){
  110. try{
  111. String synr_str = "002000300010 ";
  112. System.out.println("jobIdUpload:"+synr_str);
  113. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  114. return true;
  115. }catch (Exception e){
  116. e.printStackTrace();
  117. return false;
  118. }
  119. }
  120. public static Boolean selectJob(YgslClient ygslClient){
  121. try{
  122. String synr_str = "002000380010 01";
  123. System.out.println("selectJob:"+synr_str);
  124. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  125. return true;
  126. }catch (Exception e){
  127. e.printStackTrace();
  128. return false;
  129. }
  130. }
  131. public static Boolean restartJob(YgslClient ygslClient){
  132. try{
  133. String synr_str = "002000390010 01";
  134. System.out.println("restartJob:"+synr_str);
  135. ygslClient.future.channel().writeAndFlush(getSendByteBuf(stringToHex(synr_str)));
  136. return true;
  137. }catch (Exception e){
  138. e.printStackTrace();
  139. return false;
  140. }
  141. }
  142. public static String stringToHex(String input) {
  143. StringBuilder hexString = new StringBuilder();
  144. for (char ch : input.toCharArray()) {
  145. hexString.append(Integer.toHexString((int) ch));
  146. }
  147. return hexString.toString();
  148. }
  149. /**
  150. * 将字符串消息转为ByteBuf
  151. * 16进制字符串转二进制
  152. * @param hex 字符串消息
  153. */
  154. public static ByteBuf getSendByteBuf(String hex) {
  155. hex = hex + "00";
  156. byte[] bytes = new byte[hex.length() / 2];
  157. for (int i = 0; i < hex.length(); i += 2) {
  158. int value = Integer.parseInt(hex.substring(i, i + 2), 16);
  159. bytes[i/2] = (byte)(value & 0xFF);
  160. }
  161. return Unpooled.wrappedBuffer(bytes);
  162. }
  163. }