Ut5310TestResult.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package com.mes.util;
  2. import com.alibaba.fastjson2.JSONArray;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.List;
  7. public class Ut5310TestResult {
  8. public static class StepData {
  9. private final int step;
  10. private final String func;
  11. private final String stepJudge;
  12. private final String time;
  13. private final String scan;
  14. private final String volt;
  15. private final String limit;
  16. private final String dataValue;
  17. public StepData(int step, String func, String stepJudge,
  18. String time, String scan, String volt, String limit, String dataValue) {
  19. this.step = step;
  20. this.func = func;
  21. this.stepJudge = stepJudge;
  22. this.time = time;
  23. this.scan = scan;
  24. this.volt = volt;
  25. this.limit = limit;
  26. this.dataValue = dataValue;
  27. }
  28. public int getStep() {
  29. return step;
  30. }
  31. public String getFunc() {
  32. return func;
  33. }
  34. public String getStepJudge() {
  35. return stepJudge;
  36. }
  37. public String getTime() {
  38. return time;
  39. }
  40. public String getScan() {
  41. return scan;
  42. }
  43. public String getVolt() {
  44. return volt;
  45. }
  46. public String getLimit() {
  47. return limit;
  48. }
  49. public String getDataValue() {
  50. return dataValue;
  51. }
  52. public boolean isOk() {
  53. return "OK".equalsIgnoreCase(stepJudge) || "PASS".equalsIgnoreCase(stepJudge);
  54. }
  55. }
  56. private final boolean ok;
  57. private final String judge;
  58. private final String rawResponse;
  59. private final String totalJudge;
  60. private final List<StepData> steps;
  61. public Ut5310TestResult(boolean ok, String judge, String rawResponse) {
  62. this(ok, judge, rawResponse, null, Collections.emptyList());
  63. }
  64. public Ut5310TestResult(boolean ok, String judge, String rawResponse,
  65. String totalJudge, List<StepData> steps) {
  66. this.ok = ok;
  67. this.judge = judge;
  68. this.rawResponse = rawResponse;
  69. this.totalJudge = totalJudge;
  70. this.steps = steps == null ? Collections.emptyList() : new ArrayList<>(steps);
  71. }
  72. public boolean isOk() {
  73. return ok;
  74. }
  75. public String getJudge() {
  76. return judge;
  77. }
  78. public String getRawResponse() {
  79. return rawResponse;
  80. }
  81. public String getTotalJudge() {
  82. return totalJudge;
  83. }
  84. public List<StepData> getSteps() {
  85. return Collections.unmodifiableList(steps);
  86. }
  87. public Integer getStep() {
  88. if (steps.isEmpty()) {
  89. return null;
  90. }
  91. return steps.get(steps.size() - 1).getStep();
  92. }
  93. public String getFunc() {
  94. return lastStepField(StepData::getFunc);
  95. }
  96. public String getStepJudge() {
  97. return lastStepField(StepData::getStepJudge);
  98. }
  99. public String getTime() {
  100. return lastStepField(StepData::getTime);
  101. }
  102. public String getScan() {
  103. return lastStepField(StepData::getScan);
  104. }
  105. public String getVolt() {
  106. return lastStepField(StepData::getVolt);
  107. }
  108. public String getLimit() {
  109. return lastStepField(StepData::getLimit);
  110. }
  111. public String getDataValue() {
  112. return lastStepField(StepData::getDataValue);
  113. }
  114. private String lastStepField(java.util.function.Function<StepData, String> getter) {
  115. if (steps.isEmpty()) {
  116. return null;
  117. }
  118. return getter.apply(steps.get(steps.size() - 1));
  119. }
  120. public void printToConsole() {
  121. System.out.println("========== UT5310 原始报文(Modbus RTU) ==========");
  122. System.out.println(rawResponse == null ? "" : rawResponse);
  123. System.out.println("========== UT5310 解析结果 ==========");
  124. System.out.println("总判定(ok): " + ok);
  125. System.out.println("总判定(judge): " + safe(judge));
  126. if (totalJudge != null) {
  127. System.out.println("RU总判读: " + totalJudge);
  128. }
  129. if (steps.isEmpty()) {
  130. System.out.println("(无分步明细)");
  131. } else {
  132. for (StepData step : steps) {
  133. System.out.println("--- 步骤 " + step.getStep() + " (" + safe(step.getFunc()) + ") ---");
  134. System.out.println("测试类型: " + safe(step.getFunc()));
  135. System.out.println("当步判读: " + safe(step.getStepJudge()));
  136. System.out.println("测试电压(kV): " + safe(step.getVolt()));
  137. System.out.println("测试值(电阻GΩ或电流μA): " + safe(step.getDataValue()));
  138. }
  139. }
  140. System.out.println("====================================");
  141. }
  142. private static String safe(Object value) {
  143. return value == null ? "" : value.toString();
  144. }
  145. /** 转为 mesProductInsulation2/batchUpload 接口所需的 steps JSON */
  146. public JSONArray toUploadStepsArray() {
  147. JSONArray arr = new JSONArray();
  148. for (StepData step : steps) {
  149. JSONObject obj = new JSONObject();
  150. obj.put("step", String.valueOf(step.getStep()));
  151. obj.put("voltage", safe(step.getVolt()));
  152. obj.put("voltageUnit", "kV");
  153. obj.put("data", safe(step.getDataValue()));
  154. obj.put("dataUnit", dataUnitForStep(step.getStep()));
  155. obj.put("result", step.isOk() ? "OK" : "NG");
  156. arr.add(obj);
  157. }
  158. return arr;
  159. }
  160. private static String dataUnitForStep(int stepNo) {
  161. if (stepNo == 1) {
  162. return "GΩ";
  163. }
  164. if (stepNo == 2) {
  165. return "μA";
  166. }
  167. return "";
  168. }
  169. }