package com.mes.util; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Ut5310TestResult { public static class StepData { private final int step; private final String func; private final String stepJudge; private final String time; private final String scan; private final String volt; private final String limit; private final String dataValue; public StepData(int step, String func, String stepJudge, String time, String scan, String volt, String limit, String dataValue) { this.step = step; this.func = func; this.stepJudge = stepJudge; this.time = time; this.scan = scan; this.volt = volt; this.limit = limit; this.dataValue = dataValue; } public int getStep() { return step; } public String getFunc() { return func; } public String getStepJudge() { return stepJudge; } public String getTime() { return time; } public String getScan() { return scan; } public String getVolt() { return volt; } public String getLimit() { return limit; } public String getDataValue() { return dataValue; } public boolean isOk() { return "OK".equalsIgnoreCase(stepJudge) || "PASS".equalsIgnoreCase(stepJudge); } } private final boolean ok; private final String judge; private final String rawResponse; private final String totalJudge; private final List steps; public Ut5310TestResult(boolean ok, String judge, String rawResponse) { this(ok, judge, rawResponse, null, Collections.emptyList()); } public Ut5310TestResult(boolean ok, String judge, String rawResponse, String totalJudge, List steps) { this.ok = ok; this.judge = judge; this.rawResponse = rawResponse; this.totalJudge = totalJudge; this.steps = steps == null ? Collections.emptyList() : new ArrayList<>(steps); } public boolean isOk() { return ok; } public String getJudge() { return judge; } public String getRawResponse() { return rawResponse; } public String getTotalJudge() { return totalJudge; } public List getSteps() { return Collections.unmodifiableList(steps); } public Integer getStep() { if (steps.isEmpty()) { return null; } return steps.get(steps.size() - 1).getStep(); } public String getFunc() { return lastStepField(StepData::getFunc); } public String getStepJudge() { return lastStepField(StepData::getStepJudge); } public String getTime() { return lastStepField(StepData::getTime); } public String getScan() { return lastStepField(StepData::getScan); } public String getVolt() { return lastStepField(StepData::getVolt); } public String getLimit() { return lastStepField(StepData::getLimit); } public String getDataValue() { return lastStepField(StepData::getDataValue); } private String lastStepField(java.util.function.Function getter) { if (steps.isEmpty()) { return null; } return getter.apply(steps.get(steps.size() - 1)); } public void printToConsole() { System.out.println("========== UT5310 原始报文(Modbus RTU) =========="); System.out.println(rawResponse == null ? "" : rawResponse); System.out.println("========== UT5310 解析结果 =========="); System.out.println("总判定(ok): " + ok); System.out.println("总判定(judge): " + safe(judge)); if (totalJudge != null) { System.out.println("RU总判读: " + totalJudge); } if (steps.isEmpty()) { System.out.println("(无分步明细)"); } else { for (StepData step : steps) { System.out.println("--- 步骤 " + step.getStep() + " (" + safe(step.getFunc()) + ") ---"); System.out.println("测试类型: " + safe(step.getFunc())); System.out.println("当步判读: " + safe(step.getStepJudge())); System.out.println("测试电压(kV): " + safe(step.getVolt())); System.out.println("测试值(电阻GΩ或电流μA): " + safe(step.getDataValue())); } } System.out.println("===================================="); } private static String safe(Object value) { return value == null ? "" : value.toString(); } /** 转为 mesProductInsulation2/batchUpload 接口所需的 steps JSON */ public JSONArray toUploadStepsArray() { JSONArray arr = new JSONArray(); for (StepData step : steps) { JSONObject obj = new JSONObject(); obj.put("step", String.valueOf(step.getStep())); obj.put("voltage", safe(step.getVolt())); obj.put("voltageUnit", "kV"); obj.put("data", safe(step.getDataValue())); obj.put("dataUnit", dataUnitForStep(step.getStep())); obj.put("result", step.isOk() ? "OK" : "NG"); arr.add(obj); } return arr; } private static String dataUnitForStep(int stepNo) { if (stepNo == 1) { return "GΩ"; } if (stepNo == 2) { return "μA"; } return ""; } }