package com.jeesite.modules.utils; import com.jeesite.common.lang.StringUtils; // 固定格式报文各参数获取方法 public class ProtocolParam { public static Integer fixedLength = 124; // 固定长度 // 获取消息类型 所有报文都可使用 public static String getMsgType(String msg){ System.out.print(msg); if(msg.length() < 16){ return ""; } return msg.substring(12,16); } // 获取工位号 public static String getOprno(String msg){ if(msg.length() < 23){ return ""; } return msg.substring(18,23); } // 获取工艺号 public static String getCraft(String msg){ if(msg.length() < 31){ return ""; } return msg.substring(26,31); } // 获取类型 public static String getLx(String msg){ if(msg.length() < 73){ return ""; } return msg.substring(71,73); } // 获取返回类型 public static String getReturnLx(String msg){ if(msg.length() < 73){ return "RS"; } String lx = msg.substring(71,73); return StringUtils.isEmpty(lx.trim())?"RS":lx; } // 获取产线编号 public static String getLineSn(String msg){ if(msg.length() < 39){ return ""; } return msg.substring(34,39); } // 获取镭雕码或设备报警故障代码 public static String getSn(String msg){ if(msg.length() < 68){ return ""; } return msg.substring(39,68); } // 获取结果 public static String getResult(String msg){ if(msg.length() < 70){ return ""; } return msg.substring(68,70); } // 获取日期 public static String getDay(String msg){ if(msg.length() < 87){ return ""; } return msg.substring(77,87); } // 获取时间 public static String getTime(String msg){ if(msg.length() < 97){ return ""; } return msg.substring(89,97); } // 获取用户 public static String getUserCode(String msg){ if(msg.length() < 105){ return ""; } return msg.substring(98,105).trim(); } // 获取参数个数 public static Integer getParamNums(String msg){ if(msg.length() < fixedLength){ return 0; } String nn = msg.substring(122,fixedLength).trim(); if(StringUtils.isEmpty(nn)){ return 0; } return Integer.parseInt(nn); } // 获取参数的总长度 public static Integer getParamLenth(String msg){ if(msg.length() < fixedLength){ return 0; } Integer length = getParamNums(msg); if(length == 0){ return 0; } String msgType = getMsgType(msg); // String cate = getProductCate(msg); // String oprno = getOprno(msg); Integer res = 0; if(msgType.equals("MBDW") || msgType.equals("MJBW") || msgType.equals("AQDW") || msgType.equals("MCJW")||msgType.equals("MKSW")){ // 绑定报文,截取36个字符 if(msg.length() >= fixedLength + length*36){ res = length*36; } }else if(msgType.equals("MSBW")){ // 设备状态,9*26=234 if(msg.length() >= fixedLength + length*4){ res = length*4; } }else{ if(msg.length() >= fixedLength + length*12){ res = length*12; } } return res; } // 获取参数 num从1开始 public static String getParam(String msg,Integer num){ if(msg.length() < fixedLength){ return ""; } Integer length = getParamNums(msg); if(length == 0 || length < num){ return ""; } String msgType = getMsgType(msg); // String cate = getProductCate(msg); // String oprno = getOprno(msg); String res = ""; if(msgType.equals("MBDW")||msgType.equals("MJBW")||msgType.equals("AQDW")||msgType.equals("MCJW")||msgType.equals("MKSW")){ // 绑定报文,截取36个字符 if(msg.length() >= fixedLength + length*36){ res = msg.substring(fixedLength+(num-1)*36,fixedLength + num*36); } }else if(msgType.equals("MSBW")){ // 设备状态,9*26=234 if(msg.length() >= fixedLength + length*4){ // res = msg.substring(fixedLength,fixedLength + 234); res = msg.substring(fixedLength+(num-1)*4,fixedLength + num*4); } }else{ if(msg.length() >= fixedLength + length*12){ res = msg.substring(fixedLength+(num-1)*12,fixedLength + num*12); } } return res.trim(); } // 获取参数的结果 public static String getParamResult(String param){ if(param.length() >= 1){ String ret = param.substring(0,1).trim(); if(ret.equals("Y")){ return "OK"; } } return "NG"; } // 获取参数结果值 public static String getParamVal(String param){ if(param.length() > 1){ return param.substring(1).trim(); } return ""; } }