ProtocolParam.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package com.jeesite.modules.utils;
  2. import com.jeesite.common.lang.StringUtils;
  3. // 固定格式报文各参数获取方法
  4. public class ProtocolParam {
  5. public static Integer fixedLength = 124; // 固定长度
  6. // 获取消息类型 所有报文都可使用
  7. public static String getMsgType(String msg){
  8. System.out.print(msg);
  9. if(msg.length() < 16){
  10. return "";
  11. }
  12. return msg.substring(12,16);
  13. }
  14. // 获取工位号
  15. public static String getOprno(String msg){
  16. if(msg.length() < 23){
  17. return "";
  18. }
  19. return msg.substring(18,23);
  20. }
  21. // 获取工艺号
  22. public static String getCraft(String msg){
  23. if(msg.length() < 31){
  24. return "";
  25. }
  26. return msg.substring(26,31);
  27. }
  28. // 获取类型
  29. public static String getLx(String msg){
  30. if(msg.length() < 73){
  31. return "";
  32. }
  33. return msg.substring(71,73);
  34. }
  35. // 获取返回类型
  36. public static String getReturnLx(String msg){
  37. if(msg.length() < 73){
  38. return "RS";
  39. }
  40. String lx = msg.substring(71,73);
  41. return StringUtils.isEmpty(lx.trim())?"RS":lx;
  42. }
  43. // 获取产线编号
  44. public static String getLineSn(String msg){
  45. if(msg.length() < 39){
  46. return "";
  47. }
  48. return msg.substring(34,39);
  49. }
  50. // 获取镭雕码或设备报警故障代码
  51. public static String getSn(String msg){
  52. if(msg.length() < 68){
  53. return "";
  54. }
  55. return msg.substring(39,68);
  56. }
  57. // 获取结果
  58. public static String getResult(String msg){
  59. if(msg.length() < 70){
  60. return "";
  61. }
  62. return msg.substring(68,70);
  63. }
  64. // 获取日期
  65. public static String getDay(String msg){
  66. if(msg.length() < 87){
  67. return "";
  68. }
  69. return msg.substring(77,87);
  70. }
  71. // 获取时间
  72. public static String getTime(String msg){
  73. if(msg.length() < 97){
  74. return "";
  75. }
  76. return msg.substring(89,97);
  77. }
  78. // 获取用户
  79. public static String getUserCode(String msg){
  80. if(msg.length() < 105){
  81. return "";
  82. }
  83. return msg.substring(98,105).trim();
  84. }
  85. // 获取参数个数
  86. public static Integer getParamNums(String msg){
  87. if(msg.length() < fixedLength){
  88. return 0;
  89. }
  90. String nn = msg.substring(122,fixedLength).trim();
  91. if(StringUtils.isEmpty(nn)){
  92. return 0;
  93. }
  94. return Integer.parseInt(nn);
  95. }
  96. // 获取参数的总长度
  97. public static Integer getParamLenth(String msg){
  98. if(msg.length() < fixedLength){
  99. return 0;
  100. }
  101. Integer length = getParamNums(msg);
  102. if(length == 0){
  103. return 0;
  104. }
  105. String msgType = getMsgType(msg);
  106. // String cate = getProductCate(msg);
  107. // String oprno = getOprno(msg);
  108. Integer res = 0;
  109. if(msgType.equals("MBDW") || msgType.equals("MJBW") || msgType.equals("AQDW") || msgType.equals("MCJW")||msgType.equals("MKSW")){ // 绑定报文,截取36个字符
  110. if(msg.length() >= fixedLength + length*36){
  111. res = length*36;
  112. }
  113. }else if(msgType.equals("MSBW")){ // 设备状态,9*26=234
  114. if(msg.length() >= fixedLength + length*4){
  115. res = length*4;
  116. }
  117. }else{
  118. if(msg.length() >= fixedLength + length*12){
  119. res = length*12;
  120. }
  121. }
  122. return res;
  123. }
  124. // 获取参数 num从1开始
  125. public static String getParam(String msg,Integer num){
  126. if(msg.length() < fixedLength){
  127. return "";
  128. }
  129. Integer length = getParamNums(msg);
  130. if(length == 0 || length < num){
  131. return "";
  132. }
  133. String msgType = getMsgType(msg);
  134. // String cate = getProductCate(msg);
  135. // String oprno = getOprno(msg);
  136. String res = "";
  137. if(msgType.equals("MBDW")||msgType.equals("MJBW")||msgType.equals("AQDW")||msgType.equals("MCJW")||msgType.equals("MKSW")){ // 绑定报文,截取36个字符
  138. if(msg.length() >= fixedLength + length*36){
  139. res = msg.substring(fixedLength+(num-1)*36,fixedLength + num*36);
  140. }
  141. }else if(msgType.equals("MSBW")){ // 设备状态,9*26=234
  142. if(msg.length() >= fixedLength + length*4){
  143. // res = msg.substring(fixedLength,fixedLength + 234);
  144. res = msg.substring(fixedLength+(num-1)*4,fixedLength + num*4);
  145. }
  146. }else{
  147. if(msg.length() >= fixedLength + length*12){
  148. res = msg.substring(fixedLength+(num-1)*12,fixedLength + num*12);
  149. }
  150. }
  151. return res.trim();
  152. }
  153. // 获取参数的结果
  154. public static String getParamResult(String param){
  155. if(param.length() >= 1){
  156. String ret = param.substring(0,1).trim();
  157. if(ret.equals("Y")){
  158. return "OK";
  159. }
  160. }
  161. return "NG";
  162. }
  163. // 获取参数结果值
  164. public static String getParamVal(String param){
  165. if(param.length() > 1){
  166. return param.substring(1).trim();
  167. }
  168. return "";
  169. }
  170. }