From f589e2278ac13f8bc7f0b6e8a7fc639871e8c7db Mon Sep 17 00:00:00 2001 From: chendaofei <857448963@qq.com> Date: Mon, 30 Dec 2024 16:36:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/SocketDevResponseService.java | 149 +++++++++++++----- .../handler/SocketSourceResponseService.java | 27 ++-- .../service/impl/DetectionServiceImpl.java | 4 +- .../service/impl/PreDetectionServiceImpl.java | 7 +- .../detection/util/socket/SocketManager.java | 46 +++++- .../device/script/pojo/po/SourceIssue.java | 2 +- 6 files changed, 166 insertions(+), 69 deletions(-) diff --git a/detection/src/main/java/com/njcn/gather/detection/handler/SocketDevResponseService.java b/detection/src/main/java/com/njcn/gather/detection/handler/SocketDevResponseService.java index 1b5e818d..6647fc7d 100644 --- a/detection/src/main/java/com/njcn/gather/detection/handler/SocketDevResponseService.java +++ b/detection/src/main/java/com/njcn/gather/detection/handler/SocketDevResponseService.java @@ -23,12 +23,18 @@ import com.njcn.gather.device.device.service.IPqDevService; import com.njcn.gather.device.script.pojo.param.PqScriptIssueParam; import com.njcn.gather.device.script.pojo.po.SourceIssue; import com.njcn.gather.device.script.service.IPqScriptDtlsService; +import com.njcn.gather.storage.pojo.po.AdHarmonicResult; import com.njcn.gather.storage.pojo.po.AdNonHarmonicResult; import com.njcn.gather.system.dictionary.pojo.enums.DictDataEnum; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.math.BigDecimal; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; @@ -48,6 +54,9 @@ public class SocketDevResponseService { private final IPqScriptDtlsService pqScriptDtlsService; private final DetectionServiceImpl detectionServiceImpl; + + + /** * 存储的装置相序数据 */ @@ -240,7 +249,6 @@ public class SocketDevResponseService { System.out.println(successXieyi3.size() + "=====" + monitorIdListComm.size()); if (successXieyi3.size() == monitorIdListComm.size()) { - SocketDataMsg temMsg = new SocketDataMsg(); temMsg.setCode(SourceResponseCodeEnum.DEV_COMM_ALL_SUCCESS.getCode()); temMsg.setOperateCode(SourceOperateCodeEnum.DEV_INIT_GATHER_03.getValue()); @@ -248,34 +256,36 @@ public class SocketDevResponseService { webSocketHandler.sendMsgToUser(param.getUserPageId(), JSON.toJSONString(temMsg)); - System.out.println("开始相序校验++++++++++"); - System.out.println("开始相序校验,向源下参数++++++++++"); PqScriptIssueParam issueParam = new PqScriptIssueParam(); issueParam.setPlanId(param.getPlanId()); issueParam.setSourceId(param.getSourceId()); issueParam.setDevIds(param.getDevIds()); - //issueParam.setScriptId("2973cb938b591b93d0df2547592b8cd8"); + issueParam.setScriptId("a303b2224845fcc6f60198b8ca23dca9"); - SocketMsg xuMsg = new SocketMsg(); + SocketMsg xuMsg = new SocketMsg<>(); xuMsg.setOperateCode(SourceOperateCodeEnum.OPER_GATHER.getValue()); + List sourceIssues; if (SourceOperateCodeEnum.FORMAL_TEST.getValue().equals(param.getOperateType())) { //正式检测 - issueParam.setIsPhaseSequence(true); - xuMsg.setRequestId(SourceOperateCodeEnum.Test_VOL.getValue()); - } else if (SourceOperateCodeEnum.PRE_TEST.getValue().equals(param.getOperateType())) { - //预检测 issueParam.setIsPhaseSequence(false); + sourceIssues = pqScriptDtlsService.listSourceIssue(issueParam); + sourceIssues = sourceIssues.stream().sorted(Comparator.comparing(SourceIssue::getIndex)).collect(Collectors.toList()); + SocketManager.addSourceList(sourceIssues); + Map sourceIssueMap = sourceIssues.stream().collect(Collectors.groupingBy(SourceIssue::getType, Collectors.counting())); + SocketManager.initMap(sourceIssueMap); + xuMsg.setData(JSON.toJSONString(sourceIssues.get(0))); + xuMsg.setRequestId(SourceOperateCodeEnum.FORMAL_REAL.getValue() + "&&" + sourceIssues.get(0).getType()); + SocketManager.sendMsg(param.getUserPageId() + handlerSourceStr, MsgUtil.toJsonWithNewLinePlain(xuMsg)); + + } else if (SourceOperateCodeEnum.PRE_TEST.getValue().equals(param.getOperateType())) { + //预检测的相序检测 + issueParam.setIsPhaseSequence(true); xuMsg.setRequestId(SourceOperateCodeEnum.YJC_XUJY.getValue()); - } - List sourceIssues = pqScriptDtlsService.listSourceIssue(issueParam); - if (CollUtil.isNotEmpty(sourceIssues)) { - if(SourceOperateCodeEnum.FORMAL_TEST.getValue().equals(param.getOperateType())){ - SocketManager.addSourceTarget(SourceOperateCodeEnum.Test_VOL.getValue(), sourceIssues.get(0)); - } + sourceIssues = pqScriptDtlsService.listSourceIssue(issueParam); xuMsg.setData(JSON.toJSONString(sourceIssues.get(0))); SocketManager.sendMsg(param.getUserPageId() + handlerSourceStr, MsgUtil.toJsonWithNewLinePlain(xuMsg)); - successComm.clear(); } + successComm.clear(); } } @@ -418,45 +428,83 @@ public class SocketDevResponseService { String data = socketDataMsg.getData(); DevData devData = JSON.parseObject(data, DevData.class); SourceResponseCodeEnum dictDataEnumByCode = SourceResponseCodeEnum.getDictDataEnumByCode(socketDataMsg.getCode()); + + SourceIssue sourceIssue = SocketManager.getSourceList().get(0); + test(devData, dictDataEnumByCode, sourceIssue, socketDataMsg, param); + + + + } + + public void test(DevData devData, SourceResponseCodeEnum dictDataEnumByCode, SourceIssue sourceIssue, SocketDataMsg socketDataMsg, PreDetectionParam param) { if (ObjectUtil.isNotNull(dictDataEnumByCode)) { - SocketMsg socketMsg = new SocketMsg(); + SocketMsg socketMsg = new SocketMsg<>(); switch (dictDataEnumByCode) { case SUCCESS: //List sqlDataDTOList = devData.getSqlData(); devDataList.add(devData); successComm.add(socketDataMsg.getRequestId()); - System.out.println("devData............."+devDataList.size()); + System.out.println("获取devData数量............." + devDataList.size()); if (successComm.size() == monitorIdListComm.size()) { - SourceIssue sourceIssue = SocketManager.getSourceTarget(socketDataMsg.getRequestId()); - System.out.println("获取sourceIssue"+sourceIssue); - Map flag = detectionServiceImpl.text(devDataList,param.getErrorSysId(),devIdMapComm,sourceIssue, DictDataEnum.AT_WILL_VALUE); + System.out.println(sourceIssue.getType() +"_"+ sourceIssue.getIndex() + "当前测试小项读取数据已经全部结束。。。。。。。。。"); + System.out.println(JSON.toJSONString(sourceIssue)); + + //开启线程进行入库原始数据操作 + Map flag = detectionServiceImpl.text(devDataList, param.getErrorSysId(), devIdMapComm, sourceIssue, DictDataEnum.AT_WILL_VALUE); System.out.println(flag); - WebSocketVO> webSocketVO = new WebSocketVO<>(); - webSocketVO.setRequestId(socketDataMsg.getRequestId().split("&&")[1]+"_End"); + long tem = SocketManager.getSourceTarget(sourceIssue.getType()) - 1; + SocketManager.addTargetMap(sourceIssue.getSubType(), tem); - //组装实体推送给前台 - List devListRes = new ArrayList<>(); - devList.forEach(dev->{ - DevLineTestResult devLineTestResult = new DevLineTestResult(); - devLineTestResult.setDeviceId(dev.getDevId()); - devLineTestResult.setDeviceName(dev.getDevName()); + System.out.println("该大项还有"+tem+"个小项没有进行检测!!!!!!!!"); - List tt = new ArrayList<>(); - List monitorListDTOList = dev.getMonitorList(); - for(PreDetection.MonitorListDTO point : monitorListDTOList){ - Integer resultFlag = flag.get(dev.getDevIP()+"_"+point.getLine()); - tt.add(resultFlag); - } - devLineTestResult.setChnResult(tt.toArray(new Integer[monitorListDTOList.size()])); - devListRes.add(devLineTestResult); - }); - webSocketVO.setData(devListRes); - webSocketHandler.sendMsgToUser(param.getUserPageId(), JSON.toJSONString(webSocketVO)); + if (tem == 0) { + System.out.println(sourceIssue.getType() + sourceIssue.getIndex() + "当前测试大项已经全部结束》》》》》》》》"); + //当val为0则认为大项中的小项已经全部跑完,开始组装信息推送给前端 + WebSocketVO> webSocketVO = new WebSocketVO<>(); + webSocketVO.setRequestId(socketDataMsg.getRequestId().split("&&")[1] + "_End"); + //组装实体推送给前台 + List devListRes = new ArrayList<>(); + devList.forEach(dev -> { + DevLineTestResult devLineTestResult = new DevLineTestResult(); + devLineTestResult.setDeviceId(dev.getDevId()); + devLineTestResult.setDeviceName(dev.getDevName()); - quitSend(param); - quitSendSource(param); + List tt = new ArrayList<>(); + List monitorListDTOList = dev.getMonitorList(); + for (PreDetection.MonitorListDTO point : monitorListDTOList) { + Integer resultFlag = flag.get(dev.getDevIP() + "_" + point.getLine()); + tt.add(resultFlag); + } + devLineTestResult.setChnResult(tt.toArray(new Integer[monitorListDTOList.size()])); + devListRes.add(devLineTestResult); + }); + webSocketVO.setData(devListRes); + webSocketHandler.sendMsgToUser(param.getUserPageId(), JSON.toJSONString(webSocketVO)); + + } + + + //开始进行下一个大项检测 + List sourceIssueList = SocketManager.getSourceList(); + if (CollUtil.isNotEmpty(sourceIssueList)) { + SocketMsg xuMsg = new SocketMsg<>(); + xuMsg.setOperateCode(SourceOperateCodeEnum.OPER_GATHER.getValue()); + + + //正式检测 + SourceIssue sourceIssues = SocketManager.getSourceList().get(0); + xuMsg.setData(JSON.toJSONString(sourceIssues)); + xuMsg.setRequestId(SourceOperateCodeEnum.FORMAL_REAL.getValue() + "&&" + sourceIssues.getType()); + SocketManager.sendMsg(param.getUserPageId() + handlerSourceStr, MsgUtil.toJsonWithNewLinePlain(xuMsg)); + } + + //当小项结束后需要删除集合中的小项 + SocketManager.delSource(sourceIssue.getIndex()); + System.out.println("当前小项结束进行删除============"+sourceIssue.getType()+"_"+sourceIssue.getIndex()); + successComm.clear(); + devDataList.clear(); } break; @@ -484,7 +532,6 @@ public class SocketDevResponseService { break; } } - } @@ -685,4 +732,20 @@ public class SocketDevResponseService { } + private LocalDateTime timeFormat(String dateTimeStr, DateTimeFormatter formatter) { + //DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME; // ISO 8601格式 + try { + // 假设输入是UTC时间,将其解析为ZonedDateTime + ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTimeStr, formatter.withZone(ZoneId.of("UTC"))); + // 将ZonedDateTime转换为LocalDateTime(去除时区信息) + LocalDateTime localDateTime = zonedDateTime.toLocalDateTime(); + System.out.println("LocalDateTime: " + localDateTime); + + return localDateTime; + } catch (DateTimeParseException e) { + System.err.println("日期时间字符串格式错误: " + e.getMessage()); + return null; + } + } + } diff --git a/detection/src/main/java/com/njcn/gather/detection/handler/SocketSourceResponseService.java b/detection/src/main/java/com/njcn/gather/detection/handler/SocketSourceResponseService.java index 5a83e40a..4dce7e1d 100644 --- a/detection/src/main/java/com/njcn/gather/detection/handler/SocketSourceResponseService.java +++ b/detection/src/main/java/com/njcn/gather/detection/handler/SocketSourceResponseService.java @@ -17,6 +17,7 @@ import com.njcn.gather.detection.util.socket.cilent.NettyDevClientHandler; import com.njcn.gather.detection.util.socket.web.WebSocketHandler; import com.njcn.gather.device.device.pojo.vo.PreDetection; import com.njcn.gather.device.device.service.IPqDevService; +import com.njcn.gather.device.script.pojo.po.SourceIssue; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -65,7 +66,7 @@ public class SocketSourceResponseService { break; //正式检测 case FORMAL_REAL: - openSource(param, socketDataMsg); + senParamToDev(param, socketDataMsg); break; case QUITE_SOURCE: quitDeal(socketDataMsg,param); @@ -169,35 +170,39 @@ public class SocketSourceResponseService { /** - * 正式检测源输出 + * 组装和装置要数据 * * @param param * @param socketDataMsg */ - private void openSource(PreDetectionParam param, SocketDataMsg socketDataMsg) { + private void senParamToDev(PreDetectionParam param, SocketDataMsg socketDataMsg) { SourceResponseCodeEnum dictDataEnumByCode = SourceResponseCodeEnum.getDictDataEnumByCode(socketDataMsg.getCode()); if (ObjectUtil.isNotNull(dictDataEnumByCode)) { - SocketMsg socketMsg = new SocketMsg(); + SocketMsg socketMsg = new SocketMsg<>(); switch (dictDataEnumByCode) { case SUCCESS: //向前端推送信息 // webSocketHandler.sendMsgToUser(param.getUserPageId(), JSON.toJSONString(socketDataMsg)); String s = param.getUserPageId() + DEV; - socketMsg.setRequestId(SourceOperateCodeEnum.Test_VOL.getValue()); + SourceIssue sourceIssue = SocketManager.getSourceList().get(0); + String comm = sourceIssue.getDevValueTypeList().get(0); + + socketMsg.setRequestId(SourceOperateCodeEnum.FORMAL_REAL.getValue()+"&&"+sourceIssue.getType()); socketMsg.setOperateCode(SourceOperateCodeEnum.DEV_DATA_REQUEST_02.getValue()); - List moniterIdList = devList.stream().flatMap(x -> x.getMonitorList().stream()) - .map(PreDetection.MonitorListDTO::getLineId) - .collect(Collectors.toList()); + List moniterIdList = devList.stream().flatMap(x -> x.getMonitorList().stream()).map(PreDetection.MonitorListDTO::getLineId).collect(Collectors.toList()); DevPhaseSequenceParam phaseSequenceParam = new DevPhaseSequenceParam(); + + System.out.println("向装置下发的参数"+comm); phaseSequenceParam.setMoniterIdList(moniterIdList); - phaseSequenceParam.setDataType(Arrays.asList("real$VRMS")); - phaseSequenceParam.setReadCount(20); + phaseSequenceParam.setDataType(Arrays.asList(comm)); + phaseSequenceParam.setReadCount(5); phaseSequenceParam.setIgnoreCount(10); socketMsg.setData(JSON.toJSONString(phaseSequenceParam)); SocketManager.sendMsg(s, JSON.toJSONString(socketMsg)); + //告诉前端当前项开始了 WebSocketVO> webSocketVO = new WebSocketVO<>(); webSocketVO.setRequestId(socketDataMsg.getRequestId().split("&&")[1]+"_Start"); List devListRes = new ArrayList<>(); @@ -239,6 +244,8 @@ public class SocketSourceResponseService { break; case UNPROCESSED_BUSINESS: break; + case MESSAGE_PARSING_ERROR: + break; default: WebSocketVO webSocketVO = new WebSocketVO(); diff --git a/detection/src/main/java/com/njcn/gather/detection/service/impl/DetectionServiceImpl.java b/detection/src/main/java/com/njcn/gather/detection/service/impl/DetectionServiceImpl.java index a112dae4..2df37737 100644 --- a/detection/src/main/java/com/njcn/gather/detection/service/impl/DetectionServiceImpl.java +++ b/detection/src/main/java/com/njcn/gather/detection/service/impl/DetectionServiceImpl.java @@ -72,7 +72,7 @@ public class DetectionServiceImpl { param.setScriptId(sourceIssue.getScriptId()); param.setErrorSysId(errorSysId); List pqErrSysDtls = pqErrSysDtlsService.listPqErrSysDtlsByPqErrSysIdAndTypes(param); - switch ("V") { + switch (sourceIssue.getType()) { /** * 频率 */ @@ -82,7 +82,7 @@ public class DetectionServiceImpl { * 电压 */ case "V": - System.out.println(isQualified(dev, devIdMapComm, pqErrSysDtls, F, sourceIssue, dataRule, "FREQ")); + //System.out.println(isQualified(dev, devIdMapComm, pqErrSysDtls, F, sourceIssue, dataRule, "FREQ")); return isQualified(dev, devIdMapComm, pqErrSysDtls, U, sourceIssue, dataRule, "VRMS").getResultFlag(); /** * 电流 diff --git a/detection/src/main/java/com/njcn/gather/detection/service/impl/PreDetectionServiceImpl.java b/detection/src/main/java/com/njcn/gather/detection/service/impl/PreDetectionServiceImpl.java index 893e54d5..9de291f0 100644 --- a/detection/src/main/java/com/njcn/gather/detection/service/impl/PreDetectionServiceImpl.java +++ b/detection/src/main/java/com/njcn/gather/detection/service/impl/PreDetectionServiceImpl.java @@ -76,12 +76,7 @@ public class PreDetectionServiceImpl implements PreDetectionService { SocketDataMsg socketDataMsg = new SocketDataMsg(); socketDataMsg.setOperateCode(SourceOperateCodeEnum.CLOSE_GATHER.getValue()); socketDataMsg.setRequestId(SourceOperateCodeEnum.QUITE_SOURCE.getValue()); - - try { - channel.writeAndFlush(JSON.toJSONString(socketDataMsg)).sync(); - } catch (InterruptedException e) { - e.printStackTrace(); - } + SocketManager.sendMsg(param.getUserPageId()+source,JSON.toJSONString(socketDataMsg)); try { Thread.sleep(2000); diff --git a/detection/src/main/java/com/njcn/gather/detection/util/socket/SocketManager.java b/detection/src/main/java/com/njcn/gather/detection/util/socket/SocketManager.java index ee2d8d43..9bcca27b 100644 --- a/detection/src/main/java/com/njcn/gather/detection/util/socket/SocketManager.java +++ b/detection/src/main/java/com/njcn/gather/detection/util/socket/SocketManager.java @@ -5,8 +5,10 @@ import com.njcn.gather.device.script.pojo.po.SourceIssue; import io.netty.channel.Channel; import io.netty.channel.nio.NioEventLoopGroup; +import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArrayList; /** * @Description: webSocket存储的通道 @@ -64,23 +66,53 @@ public class SocketManager { /** - * 用于存储源操作的脚本信息 - * key: + * key:大类型code value:对应小项数量(成功一个后减一) */ - private static final Map targetMap = new ConcurrentHashMap<>(); + private static Map targetMap = new ConcurrentHashMap<>(); - public static void addSourceTarget(String sourceTag, SourceIssue sourceIssue) { - targetMap.put(sourceTag, sourceIssue); + /** + * 存储所有检测小项 + */ + private static List sourceIssueList = new CopyOnWriteArrayList<>(); + + + + public static void addSourceList(List sList) { + sourceIssueList = sList; + System.out.println(sList); + } + + public static List getSourceList() { + return sourceIssueList; + } + + + public static void delSource(Integer index) { + sourceIssueList.removeIf(s -> index.equals(s.getIndex())); } public static void delSourceTarget(String sourceTag) { targetMap.remove(sourceTag); } - public static SourceIssue getSourceTarget(String sourceTag) { - return targetMap.get(sourceTag); + + public static void initMap(Map map) { + targetMap = map; } + public static void addTargetMap(String scriptType,Long count) { + targetMap.put(scriptType,count); + } + + public static Long getSourceTarget(String scriptType) { + return targetMap.get(scriptType); + } + + + + + + } \ No newline at end of file diff --git a/device/src/main/java/com/njcn/gather/device/script/pojo/po/SourceIssue.java b/device/src/main/java/com/njcn/gather/device/script/pojo/po/SourceIssue.java index bb7105e8..e92a59a5 100644 --- a/device/src/main/java/com/njcn/gather/device/script/pojo/po/SourceIssue.java +++ b/device/src/main/java/com/njcn/gather/device/script/pojo/po/SourceIssue.java @@ -30,7 +30,7 @@ public class SourceIssue { /** * 总检测下的检测脚本序号 */ - @JSONField(serialize = false) + @JSONField(serialize = true) private Integer index; /**