YgslClientHandler.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package com.mes.ygsl;
  2. import com.mes.netty.MesMsgUtils;
  3. import com.mes.netty.ProtocolParam;
  4. import com.mes.ui.DataUtil;
  5. import com.mes.ui.MesClient;
  6. import com.mes.ui.MesRevice;
  7. import com.mes.util.JdbcUtils;
  8. import io.netty.buffer.ByteBuf;
  9. import io.netty.buffer.ByteBufUtil;
  10. import io.netty.channel.Channel;
  11. import io.netty.channel.ChannelHandlerContext;
  12. import io.netty.channel.ChannelInboundHandlerAdapter;
  13. import javax.swing.*;
  14. import java.awt.*;
  15. import java.nio.ByteBuffer;
  16. import java.nio.charset.StandardCharsets;
  17. import java.text.SimpleDateFormat;
  18. import java.util.Date;
  19. public class YgslClientHandler extends ChannelInboundHandlerAdapter {
  20. private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  21. public static byte[] responseByte;
  22. @Override
  23. public void channelActive(ChannelHandlerContext ctx) throws Exception {
  24. System.out.println("mes connecting:" + sdf.format(new Date()));
  25. }
  26. @Override
  27. public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
  28. Channel channel = ctx.channel();
  29. System.err.println("close ygsl tcp, ip:" + channel.remoteAddress());
  30. // 关闭通道
  31. channel.close();
  32. //初始化TCP连接
  33. MesClient.ygslClient = null;
  34. //TCP连接状态
  35. MesClient.ygsl_tcp_connect_flag = false;
  36. //设置TCP请求状态
  37. MesClient.ygsl_connect_request_flag = true;
  38. MesClient.initYgslTcpConnection();
  39. }
  40. @Override
  41. public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  42. try{
  43. // System.out.println("msg1111111:"+msg);
  44. if (msg instanceof ByteBuf) {
  45. ByteBuf buf = (ByteBuf) msg;
  46. // 你的处理逻辑
  47. String content = buf.toString(StandardCharsets.UTF_8);
  48. // System.out.println("msg:"+content);
  49. // System.out.println("msg-size:"+content.length());
  50. String mid = YgslParam.getMid(content);
  51. // System.out.println("MID:"+mid);
  52. switch (mid){
  53. case "0002":
  54. YgslUtil.lastTighteningResultSubscribe(MesClient.ygslClient);
  55. // YgslUtil.pSet(MesClient.ygslClient);
  56. break;
  57. case "0011":
  58. YgslUtil.selectSet(MesClient.ygslClient);
  59. break;
  60. case "0031":
  61. YgslUtil.selectJob(MesClient.ygslClient);
  62. // YgslUtil.restartJob(MesClient.ygslClient);
  63. break;
  64. case "0004": // 错误信息
  65. System.out.println("MID:"+YgslParam.getReplyMid(content)+";ErrorCode:"+YgslParam.getErrCode(content));
  66. break;
  67. case "0005": // 命令被接受
  68. // System.out.println("命令被接受:"+YgslParam.getReplyMid(content));
  69. break;
  70. case "0061": // 拧紧数据
  71. // System.out.println("YgslA:");
  72. // System.out.println("getTorqueControllerName:"+YgslParam.getTorqueControllerName(content));
  73. // System.out.println("getTighteningStatus:"+YgslParam.getTighteningStatus(content));
  74. // System.out.println("getTorqueStatus:"+YgslParam.getTorqueStatus(content));
  75. // System.out.println("getAngleStatus:"+YgslParam.getAngleStatus(content));
  76. // System.out.println("getTighteningID:"+YgslParam.getTighteningID(content));
  77. // System.out.println("getJobID:"+YgslParam.getJobID(content));
  78. // System.out.println("getTorqueMinLimit:"+YgslParam.getTorqueMinLimit(content));
  79. // System.out.println("getTorqueMaxLimit:"+YgslParam.getTorqueMaxLimit(content));
  80. // System.out.println("getTorqueFinalTarget:"+YgslParam.getTorqueFinalTarget(content));
  81. // System.out.println("getTorque:"+YgslParam.getTorque(content));
  82. // System.out.println("getAngleMin:"+YgslParam.getAngleMin(content));
  83. // System.out.println("getAngleMax:"+YgslParam.getAngleMax(content));
  84. // System.out.println("getFinalAngleTarget:"+YgslParam.getFinalAngleTarget(content));
  85. // System.out.println("getAngle:"+YgslParam.getAngle(content));
  86. // System.out.println("getTimeStamp:"+YgslParam.getTimeStamp(content));
  87. // System.out.println("getBatchStatus:"+YgslParam.getBatchStatus(content));
  88. // System.out.println("getBatchSize:"+YgslParam.getBatchSize(content));
  89. // System.out.println("getBatchCounter:"+YgslParam.getBatchCounter(content));
  90. if(MesClient.work_status == 1){
  91. // if(MesClient.work_status == 1&&MesClient.lm_result){
  92. String tighteningStatus = YgslParam.getTighteningStatus(content);
  93. String torqueStatus = YgslParam.getTorqueStatus(content);
  94. String angleStatus = YgslParam.getAngleStatus(content);
  95. String torqueMin = YgslParam.getTorqueMinLimit(content);
  96. String torqueMax = YgslParam.getTorqueMaxLimit(content);
  97. String torqueFinal = YgslParam.getTorqueFinalTarget(content);
  98. String torque = YgslParam.getTorque(content);
  99. String angleMin = YgslParam.getAngleMin(content);
  100. String angleMax = YgslParam.getAngleMax(content);
  101. String angleFinal = YgslParam.getFinalAngleTarget(content);
  102. String angle = YgslParam.getAngle(content);
  103. String jobID = YgslParam.getJobID(content);
  104. String tighteningID = YgslParam.getTighteningID(content);
  105. if(!tighteningID.isEmpty() && !MesClient.atids.contains(tighteningID)){
  106. MesClient.atids.add(tighteningID);
  107. // if(Float.valueOf(torque) > Float.valueOf(torqueMin) && Float.valueOf(torque) < Float.valueOf(torqueMax)){
  108. // torqueStatus = "1";
  109. // }else if(Float.valueOf(torque) <= Float.valueOf(torqueMin)){
  110. // torqueStatus = "0";
  111. // }else{
  112. // torqueStatus = "2";
  113. // }
  114. // if(Float.valueOf(angle) > Float.valueOf(angleMin) && Float.valueOf(angle) < Float.valueOf(angleMax)){
  115. // angleStatus = "1";
  116. // }else if(Float.valueOf(angle) <= Float.valueOf(angleMin)){
  117. // angleStatus = "0";
  118. // }else{
  119. // angleStatus = "2";
  120. // }
  121. //
  122. // if(angleStatus.equals("1") && torqueStatus.equals("1")){
  123. // tighteningStatus = "1";
  124. // }else{
  125. // tighteningStatus = "0";
  126. // }
  127. String pos = "A";
  128. Boolean checkRet = JdbcUtils.checkTighteningById(tighteningID,pos,jobID,MesClient.product_sn.getText());
  129. if(!checkRet){
  130. if(tighteningStatus.equals("0")){ // NG
  131. if(torqueStatus.equals("1")){
  132. MesClient.torque1.setForeground(Color.BLACK);
  133. }else{
  134. MesClient.torque1.setForeground(Color.RED);
  135. }
  136. if(angleStatus.equals("1")){
  137. MesClient.angle1.setForeground(Color.BLACK);
  138. }else{
  139. MesClient.angle1.setForeground(Color.RED);
  140. }
  141. }else{
  142. MesClient.torque1.setForeground(Color.BLACK);
  143. MesClient.angle1.setForeground(Color.BLACK);
  144. if(pos.equals("A") && tighteningStatus.equals("1")){
  145. MesClient.ygsl_cura++;
  146. }
  147. if(pos.equals("B") && tighteningStatus.equals("1")){
  148. MesClient.ygsl_curb++;
  149. }
  150. }
  151. String serialNumber = String.valueOf(MesClient.ygsl_cura);
  152. if(!tighteningStatus.equals("1")){
  153. serialNumber = String.valueOf(MesClient.ygsl_cura + 1);
  154. }
  155. MesClient.torque1.setText(torque);
  156. MesClient.angle1.setText(angle);
  157. if(MesClient.ygsl_cura >= MesClient.ygsl_maxa){
  158. YgslUtil.disableTool(MesClient.ygslClient);
  159. }
  160. // MesClient.schedulea.setText(MesClient.ygsl_cura+"/"+MesClient.ygsl_maxa);
  161. MesClient.param3.setText(String.valueOf(MesClient.ygsl_maxa));
  162. // MesClient.param3.setText(String.valueOf(MesClient.ygsl_cura));
  163. MesClient.param4.setText(String.valueOf(MesClient.ygsl_cura));
  164. JdbcUtils.insertTighteningData(
  165. MesClient.mes_gw,MesClient.mes_line_sn,MesClient.product_sn.getText(),
  166. tighteningStatus,torqueStatus,angleStatus,torqueMin,torqueMax,torqueFinal,torque,
  167. angleMin,angleMax,angleFinal,angle,tighteningID,jobID,serialNumber,
  168. pos,MesClient.user_menu.getText()
  169. );
  170. }
  171. }
  172. YgslUtil.lastTighteningResultDataAcknowledge(MesClient.ygslClient);
  173. if(MesClient.ygslNum == 1){
  174. if(MesClient.ygsl_cura >= MesClient.ygsl_maxa && MesClient.lm_result){ // 提交质量
  175. MesClient.finish_ok_bt.setEnabled(true);
  176. MesClient.finish_ng_bt.setEnabled(true);
  177. if(MesClient.work_status == 1 && MesClient.check_quality_result){
  178. String sn = MesClient.product_sn.getText();
  179. MesClient.getUser();
  180. String qret = "OK";
  181. Boolean sendret = DataUtil.sendQuality(sn,qret,MesClient.user20);
  182. if(!sendret){
  183. MesClient.setMenuStatus("结果上传MES失败,请重试",-1);
  184. }
  185. }
  186. }
  187. }else{
  188. if(MesClient.ygsl_cura >= MesClient.ygsl_maxa && MesClient.ygsl_curb >= MesClient.ygsl_maxb){ // 提交质量
  189. MesClient.finish_ok_bt.setEnabled(true);
  190. MesClient.finish_ng_bt.setEnabled(true);
  191. if(MesClient.work_status == 1 && MesClient.check_quality_result){
  192. String sn = MesClient.product_sn.getText();
  193. MesClient.getUser();
  194. String qret = "OK";
  195. Boolean sendret = DataUtil.sendQuality(sn,qret,MesClient.user20);
  196. if(!sendret){
  197. MesClient.setMenuStatus("结果上传MES失败,请重试",-1);
  198. }
  199. }
  200. }
  201. }
  202. }else{
  203. YgslUtil.lastTighteningResultDataAcknowledge(MesClient.ygslClient);
  204. }
  205. break;
  206. default:
  207. break;
  208. }
  209. }else{
  210. }
  211. // System.out.println("msg:"+msg);
  212. }catch (Exception e){
  213. e.printStackTrace();
  214. }
  215. }
  216. @Override
  217. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
  218. System.out.println("mes exceptionCaught:" + sdf.format(new Date()));
  219. ctx.close();
  220. MesClient.ygslClient = null;
  221. MesClient.initYgslTcpConnection();
  222. }
  223. // 16字符串转Ascii
  224. private String hexStringToAscii(String hexString) {
  225. StringBuilder sbuilder = new StringBuilder();
  226. for (int i = 0; i < hexString.length(); i += 2) {
  227. String hexByte = hexString.substring(i, i + 2);
  228. int byteValue = Integer.parseInt(hexByte, 16);
  229. char c = (char) byteValue;
  230. sbuilder.append(c);
  231. }
  232. return sbuilder.toString();
  233. }
  234. }