|
|
@@ -1,85 +1,217 @@
|
|
|
package com.mes.util.ytnetty;
|
|
|
|
|
|
-import com.mes.util.DataUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
-import java.util.Arrays;
|
|
|
-
|
|
|
+/**
|
|
|
+ * 最终结果 4353 / CS。
|
|
|
+ * 旧实现按 hex 下标截取,字段一错位 OK/NG、压强、泄漏全歪;
|
|
|
+ * 现先 hex→ASCII,再按定长或 CSV 解析。
|
|
|
+ */
|
|
|
public class QmResultUtil {
|
|
|
- public static final Logger log = LoggerFactory.getLogger(QmResultUtil.class);
|
|
|
-
|
|
|
- public final String num; // 记录编号
|
|
|
- public final String testDate; // 记录编号
|
|
|
- public final String sn; // 工件号
|
|
|
- public final String resultType; // 结果符号
|
|
|
- public final String result; // 结果
|
|
|
- public final String programNumber; // 程序号
|
|
|
- public final String pressureValue; // 压力值
|
|
|
- public final String leakValue; // 泄漏值
|
|
|
- public final String pressureUnit; // 压力单位
|
|
|
- public final String leakUnit; // 泄漏单位
|
|
|
+ public static final Logger log = LoggerFactory.getLogger(QmResultUtil.class);
|
|
|
+
|
|
|
+ public final String num;
|
|
|
+ public final String testDate;
|
|
|
+ public final String sn;
|
|
|
+ public final String resultType;
|
|
|
+ public final String result;
|
|
|
+ public final String programNumber;
|
|
|
+ public final String pressureValue;
|
|
|
+ public final String leakValue;
|
|
|
+ public final String pressureUnit;
|
|
|
+ public final String leakUnit;
|
|
|
|
|
|
public QmResultUtil(String msg) {
|
|
|
- this.num = new String(YtUtil.hexStringToByteArray(msg.substring(4,14)));
|
|
|
- this.testDate = new String(YtUtil.hexStringToByteArray(msg.substring(14,42)));
|
|
|
- this.sn = new String(YtUtil.hexStringToByteArray(msg.substring(48,108)));
|
|
|
- this.resultType = new String(YtUtil.hexStringToByteArray(msg.substring(108,110)));
|
|
|
- String leak = Double.valueOf(new String(YtUtil.hexStringToByteArray(msg.substring(110,122))))+"";
|
|
|
- this.leakUnit = formatLeakUnit(new String(YtUtil.hexStringToByteArray(msg.substring(122,124))));
|
|
|
- this.result = formatResult(new String(YtUtil.hexStringToByteArray(msg.substring(124,126))));
|
|
|
- this.programNumber = new String(YtUtil.hexStringToByteArray(msg.substring(126,130)));
|
|
|
- this.pressureValue = Double.valueOf(new String(YtUtil.hexStringToByteArray(msg.substring(130,146))))+"";
|
|
|
- this.pressureUnit = "kpa";
|
|
|
- if(this.resultType.equals("1")){
|
|
|
- leak = "-"+leak;
|
|
|
- }
|
|
|
- this.leakValue = leak;
|
|
|
+ String ascii = new String(YtUtil.hexStringToByteArray(msg));
|
|
|
+ log.info("QmResultUtil ascii={}", ascii);
|
|
|
+
|
|
|
+ if (ascii.indexOf(',') >= 0) {
|
|
|
+ Parsed p = parseCsv(ascii);
|
|
|
+ this.num = p.num;
|
|
|
+ this.testDate = p.testDate;
|
|
|
+ this.sn = p.sn;
|
|
|
+ this.resultType = p.resultType;
|
|
|
+ this.result = p.result;
|
|
|
+ this.programNumber = p.programNumber;
|
|
|
+ this.pressureValue = p.pressureValue;
|
|
|
+ this.leakValue = p.leakValue;
|
|
|
+ this.pressureUnit = p.pressureUnit;
|
|
|
+ this.leakUnit = p.leakUnit;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Parsed p = parseFixed(ascii);
|
|
|
+ this.num = p.num;
|
|
|
+ this.testDate = p.testDate;
|
|
|
+ this.sn = p.sn;
|
|
|
+ this.resultType = p.resultType;
|
|
|
+ this.result = p.result;
|
|
|
+ this.programNumber = p.programNumber;
|
|
|
+ this.pressureValue = p.pressureValue;
|
|
|
+ this.leakValue = p.leakValue;
|
|
|
+ this.pressureUnit = p.pressureUnit;
|
|
|
+ this.leakUnit = p.leakUnit;
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public String toString() {
|
|
|
- return "QmResultUtil{" +
|
|
|
- "num='" + num + '\'' +
|
|
|
- ", testDate='" + testDate + '\'' +
|
|
|
- ", sn='" + sn + '\'' +
|
|
|
- ", resultType='" + resultType + '\'' +
|
|
|
- ", result='" + result + '\'' +
|
|
|
- ", programNumber='" + programNumber + '\'' +
|
|
|
- ", pressureValue='" + pressureValue + '\'' +
|
|
|
- ", leakValue='" + leakValue + '\'' +
|
|
|
- ", pressureUnit='" + pressureUnit + '\'' +
|
|
|
- ", leakUnit='" + leakUnit + '\'' +
|
|
|
- '}';
|
|
|
+ /**
|
|
|
+ * 定长:CS + num(5) + date(14) + reserved(3) + sn(30)
|
|
|
+ * + resultType(1) + leak(6) + leakUnit(1) + result(1) + program(2) + pressure(8)
|
|
|
+ * 例:CS0035620260706160102000...(sn)...0001.55310002013.2800 → result='1'=OK
|
|
|
+ */
|
|
|
+ private Parsed parseFixed(String ascii) {
|
|
|
+ Parsed p = new Parsed();
|
|
|
+ if (ascii == null || ascii.length() < 73) {
|
|
|
+ log.warn("CS定长包过短 len={}", ascii == null ? 0 : ascii.length());
|
|
|
+ p.result = "NG";
|
|
|
+ p.pressureUnit = "kPa";
|
|
|
+ return p;
|
|
|
+ }
|
|
|
+
|
|
|
+ p.num = safeSub(ascii, 2, 7).trim();
|
|
|
+ p.testDate = safeSub(ascii, 7, 21).trim();
|
|
|
+ p.sn = safeSub(ascii, 24, 54).trim();
|
|
|
+ p.resultType = safeSub(ascii, 54, 55);
|
|
|
+ String leakRaw = safeSub(ascii, 55, 61).trim();
|
|
|
+ p.leakUnit = formatLeakUnit(safeSub(ascii, 61, 62));
|
|
|
+ p.result = formatResult(safeSub(ascii, 62, 63));
|
|
|
+ p.programNumber = safeSub(ascii, 63, 65).trim();
|
|
|
+ p.pressureValue = toNumberString(safeSub(ascii, 65, 73));
|
|
|
+ p.pressureUnit = "kPa";
|
|
|
+
|
|
|
+ String leak = toNumberString(leakRaw);
|
|
|
+ if ("1".equals(p.resultType) && leak.length() > 0 && !leak.startsWith("-")) {
|
|
|
+ leak = "-" + leak;
|
|
|
+ }
|
|
|
+ p.leakValue = leak;
|
|
|
+ return p;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * CSV:与实时 RS 一样整包为逗号分隔。
|
|
|
+ * 优先识别字面 OK/NG;否则按 CS,num,date,sn,resultType,leak,unit,result,program,pressure 取值。
|
|
|
+ */
|
|
|
+ private Parsed parseCsv(String ascii) {
|
|
|
+ Parsed p = new Parsed();
|
|
|
+ String[] parts = ascii.split(",", -1);
|
|
|
+ p.pressureUnit = "kPa";
|
|
|
+
|
|
|
+ for (String part : parts) {
|
|
|
+ if (part == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String t = part.trim();
|
|
|
+ if ("OK".equalsIgnoreCase(t) || "NG".equalsIgnoreCase(t)) {
|
|
|
+ p.result = t.toUpperCase();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ p.num = part(parts, 1);
|
|
|
+ p.testDate = part(parts, 2);
|
|
|
+ p.sn = part(parts, 3);
|
|
|
+ p.resultType = part(parts, 4);
|
|
|
+ if (p.leakValue == null || p.leakValue.isEmpty()) {
|
|
|
+ p.leakValue = toNumberString(part(parts, 5));
|
|
|
+ }
|
|
|
+ String unitCode = part(parts, 6);
|
|
|
+ p.leakUnit = formatLeakUnit(unitCode);
|
|
|
+ if (p.result == null || p.result.isEmpty()) {
|
|
|
+ p.result = formatResult(part(parts, 7));
|
|
|
+ }
|
|
|
+ p.programNumber = part(parts, 8);
|
|
|
+ p.pressureValue = toNumberString(part(parts, 9));
|
|
|
+ // 若压力在更前(类似 RS: 第5/6 段是压强/泄漏)
|
|
|
+ if (p.pressureValue.isEmpty() && parts.length >= 6) {
|
|
|
+ String maybePressure = toNumberString(part(parts, 4));
|
|
|
+ String maybeLeak = toNumberString(part(parts, 5));
|
|
|
+ if (!maybePressure.isEmpty()) {
|
|
|
+ p.pressureValue = maybePressure;
|
|
|
+ }
|
|
|
+ if (!maybeLeak.isEmpty()) {
|
|
|
+ p.leakValue = maybeLeak;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ("1".equals(p.resultType) && p.leakValue.length() > 0 && !p.leakValue.startsWith("-")) {
|
|
|
+ p.leakValue = "-" + p.leakValue;
|
|
|
+ }
|
|
|
+ if (p.result == null || p.result.isEmpty()) {
|
|
|
+ p.result = "NG";
|
|
|
+ }
|
|
|
+ return p;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String part(String[] parts, int index) {
|
|
|
+ if (parts == null || index < 0 || index >= parts.length || parts[index] == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return parts[index].trim();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String safeSub(String s, int start, int end) {
|
|
|
+ if (s == null || start >= s.length()) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ if (end > s.length()) {
|
|
|
+ end = s.length();
|
|
|
+ }
|
|
|
+ if (start < 0) {
|
|
|
+ start = 0;
|
|
|
+ }
|
|
|
+ if (start >= end) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return s.substring(start, end);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String toNumberString(String raw) {
|
|
|
+ if (raw == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ String t = raw.trim();
|
|
|
+ if (t.isEmpty()) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ return Double.valueOf(t) + "";
|
|
|
+ } catch (Exception e) {
|
|
|
+ return t;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public String formatLeakUnit(String leakUnit) {
|
|
|
- String unit = "";
|
|
|
- switch (leakUnit) {
|
|
|
+ if (leakUnit == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ switch (leakUnit.trim()) {
|
|
|
case "0":
|
|
|
- unit = "P";
|
|
|
- break;
|
|
|
+ return "P";
|
|
|
case "1":
|
|
|
- unit = "Pa/s";
|
|
|
- break;
|
|
|
+ return "Pa/s";
|
|
|
case "2":
|
|
|
- unit = "mL/s";
|
|
|
- break;
|
|
|
+ return "mL/s";
|
|
|
case "3":
|
|
|
- unit = "mL/min";
|
|
|
- break;
|
|
|
+ return "mL/min";
|
|
|
case "4":
|
|
|
- unit = "cc/min";
|
|
|
- break;
|
|
|
+ return "cc/min";
|
|
|
+ default:
|
|
|
+ // 已是单位文本则原样返回
|
|
|
+ if (leakUnit.contains("Pa") || leakUnit.contains("mL") || leakUnit.contains("cc") || "P".equals(leakUnit.trim())) {
|
|
|
+ return leakUnit.trim();
|
|
|
+ }
|
|
|
+ return "";
|
|
|
}
|
|
|
- return unit;
|
|
|
}
|
|
|
|
|
|
public String formatResult(String result) {
|
|
|
if (result == null || result.trim().isEmpty()) {
|
|
|
return "NG";
|
|
|
}
|
|
|
- switch (result.trim()) {
|
|
|
+ String r = result.trim();
|
|
|
+ if ("OK".equalsIgnoreCase(r) || "NG".equalsIgnoreCase(r)) {
|
|
|
+ return r.toUpperCase();
|
|
|
+ }
|
|
|
+ switch (r) {
|
|
|
case "1":
|
|
|
return "OK";
|
|
|
case "2": // +NG
|
|
|
@@ -94,4 +226,33 @@ public class QmResultUtil {
|
|
|
return "NG";
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String toString() {
|
|
|
+ return "QmResultUtil{" +
|
|
|
+ "num='" + num + '\'' +
|
|
|
+ ", testDate='" + testDate + '\'' +
|
|
|
+ ", sn='" + sn + '\'' +
|
|
|
+ ", resultType='" + resultType + '\'' +
|
|
|
+ ", result='" + result + '\'' +
|
|
|
+ ", programNumber='" + programNumber + '\'' +
|
|
|
+ ", pressureValue='" + pressureValue + '\'' +
|
|
|
+ ", leakValue='" + leakValue + '\'' +
|
|
|
+ ", pressureUnit='" + pressureUnit + '\'' +
|
|
|
+ ", leakUnit='" + leakUnit + '\'' +
|
|
|
+ '}';
|
|
|
+ }
|
|
|
+
|
|
|
+ private static class Parsed {
|
|
|
+ String num = "";
|
|
|
+ String testDate = "";
|
|
|
+ String sn = "";
|
|
|
+ String resultType = "";
|
|
|
+ String result = "";
|
|
|
+ String programNumber = "";
|
|
|
+ String pressureValue = "";
|
|
|
+ String leakValue = "";
|
|
|
+ String pressureUnit = "";
|
|
|
+ String leakUnit = "";
|
|
|
+ }
|
|
|
}
|