| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- package com.mes.ygsl;
- import com.mes.netty.MesMsgUtils;
- import com.mes.netty.ProtocolParam;
- import com.mes.ui.DataUtil;
- import com.mes.ui.MesClient;
- import com.mes.ui.MesRevice;
- import com.mes.util.JdbcUtils;
- import io.netty.buffer.ByteBuf;
- import io.netty.buffer.ByteBufUtil;
- import io.netty.channel.Channel;
- import io.netty.channel.ChannelHandlerContext;
- import io.netty.channel.ChannelInboundHandlerAdapter;
- import javax.swing.*;
- import java.awt.*;
- import java.nio.ByteBuffer;
- import java.nio.charset.StandardCharsets;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- public class YgslClientHandler extends ChannelInboundHandlerAdapter {
- private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- public static byte[] responseByte;
- @Override
- public void channelActive(ChannelHandlerContext ctx) throws Exception {
- System.out.println("mes connecting:" + sdf.format(new Date()));
- }
- @Override
- public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
- Channel channel = ctx.channel();
- System.err.println("close ygsl tcp, ip:" + channel.remoteAddress());
- // 关闭通道
- channel.close();
- //初始化TCP连接
- MesClient.ygslClient = null;
- //TCP连接状态
- MesClient.ygsl_tcp_connect_flag = false;
- //设置TCP请求状态
- MesClient.ygsl_connect_request_flag = true;
- MesClient.initYgslTcpConnection();
- }
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
- try{
- // System.out.println("msg1111111:"+msg);
- if (msg instanceof ByteBuf) {
- ByteBuf buf = (ByteBuf) msg;
- // 你的处理逻辑
- String content = buf.toString(StandardCharsets.UTF_8);
- // System.out.println("msg:"+content);
- // System.out.println("msg-size:"+content.length());
- String mid = YgslParam.getMid(content);
- // System.out.println("MID:"+mid);
- switch (mid){
- case "0002":
- YgslUtil.lastTighteningResultSubscribe(MesClient.ygslClient);
- // YgslUtil.pSet(MesClient.ygslClient);
- break;
- case "0011":
- YgslUtil.selectSet(MesClient.ygslClient);
- break;
- case "0031":
- YgslUtil.selectJob(MesClient.ygslClient);
- // YgslUtil.restartJob(MesClient.ygslClient);
- break;
- case "0004": // 错误信息
- System.out.println("MID:"+YgslParam.getReplyMid(content)+";ErrorCode:"+YgslParam.getErrCode(content));
- break;
- case "0005": // 命令被接受
- // System.out.println("命令被接受:"+YgslParam.getReplyMid(content));
- break;
- case "0061": // 拧紧数据
- // System.out.println("YgslA:");
- // System.out.println("getTorqueControllerName:"+YgslParam.getTorqueControllerName(content));
- // System.out.println("getTighteningStatus:"+YgslParam.getTighteningStatus(content));
- // System.out.println("getTorqueStatus:"+YgslParam.getTorqueStatus(content));
- // System.out.println("getAngleStatus:"+YgslParam.getAngleStatus(content));
- // System.out.println("getTighteningID:"+YgslParam.getTighteningID(content));
- // System.out.println("getJobID:"+YgslParam.getJobID(content));
- // System.out.println("getTorqueMinLimit:"+YgslParam.getTorqueMinLimit(content));
- // System.out.println("getTorqueMaxLimit:"+YgslParam.getTorqueMaxLimit(content));
- // System.out.println("getTorqueFinalTarget:"+YgslParam.getTorqueFinalTarget(content));
- // System.out.println("getTorque:"+YgslParam.getTorque(content));
- // System.out.println("getAngleMin:"+YgslParam.getAngleMin(content));
- // System.out.println("getAngleMax:"+YgslParam.getAngleMax(content));
- // System.out.println("getFinalAngleTarget:"+YgslParam.getFinalAngleTarget(content));
- // System.out.println("getAngle:"+YgslParam.getAngle(content));
- // System.out.println("getTimeStamp:"+YgslParam.getTimeStamp(content));
- // System.out.println("getBatchStatus:"+YgslParam.getBatchStatus(content));
- // System.out.println("getBatchSize:"+YgslParam.getBatchSize(content));
- // System.out.println("getBatchCounter:"+YgslParam.getBatchCounter(content));
- if(MesClient.work_status == 1){
- // if(MesClient.work_status == 1&&MesClient.lm_result){
- String tighteningStatus = YgslParam.getTighteningStatus(content);
- String torqueStatus = YgslParam.getTorqueStatus(content);
- String angleStatus = YgslParam.getAngleStatus(content);
- String torqueMin = YgslParam.getTorqueMinLimit(content);
- String torqueMax = YgslParam.getTorqueMaxLimit(content);
- String torqueFinal = YgslParam.getTorqueFinalTarget(content);
- String torque = YgslParam.getTorque(content);
- String angleMin = YgslParam.getAngleMin(content);
- String angleMax = YgslParam.getAngleMax(content);
- String angleFinal = YgslParam.getFinalAngleTarget(content);
- String angle = YgslParam.getAngle(content);
- String jobID = YgslParam.getJobID(content);
- String tighteningID = YgslParam.getTighteningID(content);
- if(!tighteningID.isEmpty() && !MesClient.atids.contains(tighteningID)){
- MesClient.atids.add(tighteningID);
- // if(Float.valueOf(torque) > Float.valueOf(torqueMin) && Float.valueOf(torque) < Float.valueOf(torqueMax)){
- // torqueStatus = "1";
- // }else if(Float.valueOf(torque) <= Float.valueOf(torqueMin)){
- // torqueStatus = "0";
- // }else{
- // torqueStatus = "2";
- // }
- // if(Float.valueOf(angle) > Float.valueOf(angleMin) && Float.valueOf(angle) < Float.valueOf(angleMax)){
- // angleStatus = "1";
- // }else if(Float.valueOf(angle) <= Float.valueOf(angleMin)){
- // angleStatus = "0";
- // }else{
- // angleStatus = "2";
- // }
- //
- // if(angleStatus.equals("1") && torqueStatus.equals("1")){
- // tighteningStatus = "1";
- // }else{
- // tighteningStatus = "0";
- // }
- String pos = "A";
- Boolean checkRet = JdbcUtils.checkTighteningById(tighteningID,pos,jobID,MesClient.product_sn.getText());
- if(!checkRet){
- if(tighteningStatus.equals("0")){ // NG
- if(torqueStatus.equals("1")){
- MesClient.torque1.setForeground(Color.BLACK);
- }else{
- MesClient.torque1.setForeground(Color.RED);
- }
- if(angleStatus.equals("1")){
- MesClient.angle1.setForeground(Color.BLACK);
- }else{
- MesClient.angle1.setForeground(Color.RED);
- }
- }else{
- MesClient.torque1.setForeground(Color.BLACK);
- MesClient.angle1.setForeground(Color.BLACK);
- if(pos.equals("A") && tighteningStatus.equals("1")){
- MesClient.ygsl_cura++;
- }
- if(pos.equals("B") && tighteningStatus.equals("1")){
- MesClient.ygsl_curb++;
- }
- }
- String serialNumber = String.valueOf(MesClient.ygsl_cura);
- if(!tighteningStatus.equals("1")){
- serialNumber = String.valueOf(MesClient.ygsl_cura + 1);
- }
- MesClient.torque1.setText(torque);
- MesClient.angle1.setText(angle);
- if(MesClient.ygsl_cura >= MesClient.ygsl_maxa){
- YgslUtil.disableTool(MesClient.ygslClient);
- }
- // MesClient.schedulea.setText(MesClient.ygsl_cura+"/"+MesClient.ygsl_maxa);
- MesClient.param3.setText(String.valueOf(MesClient.ygsl_maxa));
- // MesClient.param3.setText(String.valueOf(MesClient.ygsl_cura));
- MesClient.param4.setText(String.valueOf(MesClient.ygsl_cura));
- JdbcUtils.insertTighteningData(
- MesClient.mes_gw,MesClient.mes_line_sn,MesClient.product_sn.getText(),
- tighteningStatus,torqueStatus,angleStatus,torqueMin,torqueMax,torqueFinal,torque,
- angleMin,angleMax,angleFinal,angle,tighteningID,jobID,serialNumber,
- pos,MesClient.user_menu.getText()
- );
- }
- }
- YgslUtil.lastTighteningResultDataAcknowledge(MesClient.ygslClient);
- if(MesClient.ygslNum == 1){
- if(MesClient.ygsl_cura >= MesClient.ygsl_maxa && MesClient.lm_result){ // 提交质量
- MesClient.finish_ok_bt.setEnabled(true);
- MesClient.finish_ng_bt.setEnabled(true);
- if(MesClient.work_status == 1 && MesClient.check_quality_result){
- String sn = MesClient.product_sn.getText();
- MesClient.getUser();
- String qret = "OK";
- Boolean sendret = DataUtil.sendQuality(sn,qret,MesClient.user20);
- if(!sendret){
- MesClient.setMenuStatus("结果上传MES失败,请重试",-1);
- }
- }
- }
- }else{
- if(MesClient.ygsl_cura >= MesClient.ygsl_maxa && MesClient.ygsl_curb >= MesClient.ygsl_maxb){ // 提交质量
- MesClient.finish_ok_bt.setEnabled(true);
- MesClient.finish_ng_bt.setEnabled(true);
- if(MesClient.work_status == 1 && MesClient.check_quality_result){
- String sn = MesClient.product_sn.getText();
- MesClient.getUser();
- String qret = "OK";
- Boolean sendret = DataUtil.sendQuality(sn,qret,MesClient.user20);
- if(!sendret){
- MesClient.setMenuStatus("结果上传MES失败,请重试",-1);
- }
- }
- }
- }
- }else{
- YgslUtil.lastTighteningResultDataAcknowledge(MesClient.ygslClient);
- }
- break;
- default:
- break;
- }
- }else{
- }
- // System.out.println("msg:"+msg);
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
- System.out.println("mes exceptionCaught:" + sdf.format(new Date()));
- ctx.close();
- MesClient.ygslClient = null;
- MesClient.initYgslTcpConnection();
- }
- // 16字符串转Ascii
- private String hexStringToAscii(String hexString) {
- StringBuilder sbuilder = new StringBuilder();
- for (int i = 0; i < hexString.length(); i += 2) {
- String hexByte = hexString.substring(i, i + 2);
- int byteValue = Integer.parseInt(hexByte, 16);
- char c = (char) byteValue;
- sbuilder.append(c);
- }
- return sbuilder.toString();
- }
- }
|