ProtocolParam.java 5.4 KB

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