123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- package com.ruoyi.sim.service.impl;
- import com.ruoyi.common.core.domain.AjaxResult;
- import com.ruoyi.sim.constant.CommConst;
- import com.ruoyi.sim.constant.FaultConst;
- import com.ruoyi.sim.domain.*;
- import com.ruoyi.sim.util.CRC16Modbus;
- import org.apache.commons.lang3.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.util.Arrays;
- import java.util.HashSet;
- import static com.ruoyi.sim.constant.CommConst.*;
- @Service
- // 多实例
- // 异步调用
- // @Scope("prototype")
- public class CommReceiveService {
- private static final Logger l = LoggerFactory.getLogger(CommReceiveService.class);
- @Autowired
- private RealExamFaultService realExamFaultService;
- @Autowired
- private SimService simService;
- @Autowired
- private DebugFaultService debugFaultService;
- @Autowired
- private FaultService faultService;
- public void clearOneFault(SimMsg sm, Sim s, RealExamFault reF, Fault f) {
- // check
- //
- //
- if (reF != null) {
- realExamFaultService.updateRefStateByRefId(reF.getRefId(), RealExamFault.State.CLEARED);
- }
- }
- /**
- * 设置出题值。
- *
- * @param sm
- * @param s
- * @param reF debug模式下为null。
- * @param f
- * @param faultIds debug模式必须有值
- */
- public void setFaultQuestionValue(SimMsg sm, Sim s, RealExamFault reF, Fault f, String[] faultIds) {
- // check
- //
- String faultQuestionValue = CommParseUtils.subContentData(sm);
- // todo:
- if (StringUtils.isBlank(faultQuestionValue)) {
- l.warn("faultQuestionValue is empty!");
- return;
- }
- l.info("faultQuestionValue = {}", faultQuestionValue);
- if (reF != null) {
- // 修改关联状态。
- reF.setSimFaultQuestionValue(faultQuestionValue);
- realExamFaultService.updateRealExamFault(reF);
- if (RealExamFault.Flag.YES.equals(reF.getFlag())) {
- realExamFaultService.updateRefStateByRefId(reF.getRefId(), RealExamFault.State.WRITTEN);
- } else if (RealExamFault.Flag.NO.equals(reF.getFlag())) {
- realExamFaultService.updateRefStateByRefId(reF.getRefId(), RealExamFault.State.LOOP_READ);
- }
- } else {
- //
- HashSet<String> fTempSet = new HashSet<>(Arrays.asList(faultIds));
- l.info("fTempSet.toArray = {}", fTempSet.toArray());
- String faultId = f.getFaultId();
- l.info("faultId = {}", faultId);
- // 不判断是否存在,因为之前已经删除所有表中数据,所以应该直接插入数据。
- DebugFault df = new DebugFault();
- df.setSimId(s.getSimId());
- df.setFaultId(f.getFaultId());
- if (fTempSet.contains(faultId)) {
- df.setFlag(DebugFault.Flag.YES);
- } else {
- df.setFlag(DebugFault.Flag.NO);
- }
- df.setSimFaultQuestionValue(faultQuestionValue);
- // 答题值为空。
- df.setSimFaultAnswerValue("");
- df.setAnswerRight(DebugFault.AnswerRight.UNKNOWN);
- debugFaultService.insertDebugFault(df);
- }
- }
- /**
- * 设置答题值。
- *
- * @param sm
- * @param s
- * @param reF debug模式为null
- * @param f
- * @param refState 轮询时候为null debug模式为null
- */
- public void setFaultAnswerValue(SimMsg sm, Sim s, RealExamFault reF, Fault f, String refState) {
- // check
- //
- String faultAnswerValue = CommParseUtils.subContentData(sm);
- // todo:
- if (StringUtils.isBlank(faultAnswerValue)) {
- l.warn("faultAnswerValue is empty!");
- return;
- }
- l.info("faultAnswerValue = {}", faultAnswerValue);
- if (reF != null && refState != null) {
- if (StringUtils.isNotBlank(refState)) {
- reF.setRefState(refState);
- }
- reF.setSimFaultAnswerValue(faultAnswerValue);
- realExamFaultService.updateRealExamFault(reF);
- } else {
- DebugFault df = debugFaultService.exist(s.getSimId(), f.getFaultId());
- if (df == null) {
- df = new DebugFault();
- df.setSimId(s.getSimId());
- df.setFaultId(f.getFaultId());
- df.setSimFaultAnswerValue(faultAnswerValue);
- debugFaultService.insertDebugFault(df);
- } else {
- df.setSimFaultAnswerValue(faultAnswerValue);
- debugFaultService.updateDebugFault(df);
- }
- }
- }
- /**
- * 开始考试 检查 故障部位 检查
- *
- * @param sm
- * @param s
- * @param f
- * @return
- */
- public AjaxResult getOneFaultCheck(SimMsg sm, Sim s, Fault f) {
- String checkValue = CommParseUtils.subContentData(sm);
- if (s == null) {
- return AjaxResult.error("没有对应模拟器!");
- }
- // 是否在 故障部位 跳过检查 白名单中。
- if (FaultConst.FAULT_SET_CHECK_PASS.contains(f.getFaultId())) {
- // 跳过检查,直接成功。
- return AjaxResult.success(f);
- }
- // 是否是 2型的维护管 或 3型的维护管
- if (FaultConst.FAULT_SET_WHG.contains(f.getFaultId())) {
- // 判断必须存在
- String WHG_EXIST_MSG = checkValue.substring(4, 6);
- if (WHG_MSG_EXIST_YES.equals(WHG_EXIST_MSG)) {
- return AjaxResult.success("", f);
- } else {
- return AjaxResult.success("故障部位[" + f.getBindHardwareMsg() + "][" + f.getReplaceName() + "]未正确安装;", f);
- }
- }
- if (BLANK_CONTENT.equals(checkValue)) {
- l.info("故障部位[" + f.getBindHardwareMsg() + "][" + f.getReplaceName() + "]未正确安装;");
- return AjaxResult.success("故障部位[" + f.getBindHardwareMsg() + "][" + f.getReplaceName() + "]未正确安装;", f);
- } else {
- return AjaxResult.success("", f);
- }
- }
- /**
- * 处理报文前端加00的情况,最多可能加5组00
- *
- * @param receiveMsg
- * @return
- */
- public String removeRrefix0(String receiveMsg) {
- int count = 0;
- while (StringUtils.startsWith(receiveMsg, CommConst.PREFIX_ERROR_0)) {
- receiveMsg = StringUtils.removeStartIgnoreCase(receiveMsg, CommConst.PREFIX_ERROR_0);
- count = count + 1;
- }
- l.info("####remove count#### = [{}]", count);
- return receiveMsg;
- }
- /**
- * 有返回报文的情况下,检查Receive的报文格式。
- *
- * @param receiveMsg
- * @return
- */
- public AjaxResult checkReceiveMsgFormat(final String receiveMsg) {
- l.info("####checkReceiveMsg#### = [{}]", receiveMsg);
- String msgErr = "ReceiveMsg ";
- // check:不能是empty
- if (StringUtils.isBlank(receiveMsg)) {
- return AjaxResult.error(msgErr + "isBlank");
- }
- // check:长度
- if (receiveMsg.length() != LENGTH_24) {
- return AjaxResult.error(msgErr + "length error.length not 24.");
- }
- // check:数据方向
- final String orn = StringUtils.substring(receiveMsg, 4, 6);
- if (!ORN_RECEIVE.equals(orn)) {
- return AjaxResult.error(msgErr + "orn error.");
- }
- // check:前缀
- if (!StringUtils.startsWith(receiveMsg, PREFIX)) {
- return AjaxResult.error(msgErr + "not start with AA.");
- }
- // check:后缀
- if (!StringUtils.endsWith(receiveMsg, SUFFIX)) {
- return AjaxResult.error(msgErr + "not end with 55.");
- }
- // 计算CRC16
- // todo:
- // todo: receive报文检验错误。
- if (false) {
- String crcContent = receiveMsg.substring(0, 18);
- l.debug("crcContent: {}", crcContent.toUpperCase());
- byte[] receiveByteContent = CommSendService.hexStrToByteArrs(crcContent);
- byte[] receiveByteCrc = CRC16Modbus.calculateCRC(receiveByteContent);
- String crc = CommSendService.bytesToHexV2(receiveByteCrc);
- l.debug("crc: {}", crc.toUpperCase());
- // if (!receiveMsg.substring(19, 22).equals(crc.toUpperCase())) {
- // throw new IllegalArgumentException("checkReceiveMsg length error");
- // }
- // todo: 比对校验值,不正确。
- }
- return AjaxResult.success("接收报文格式检查正确!");
- }
- public AjaxResult checkReceiveMsgMatch(final SimMsg sm) {
- if (sm == null) {
- return AjaxResult.error("空报文!");
- }
- final String s = sm.getSendMsg();
- final String r = sm.getReceiveMsg();
- if (StringUtils.isBlank(s) || StringUtils.isBlank(r)) {
- return AjaxResult.error("空报文!");
- }
- if (StringUtils.equals(CommParseUtils.subSimNum(s), "00")) {
- } else {
- if (!StringUtils.equals(CommParseUtils.subSimNum(s), CommParseUtils.subSimNum(r))) {
- return AjaxResult.error("subSimNum不对应!");
- }
- }
- if (!StringUtils.equals(CommParseUtils.subCmd(s), CommParseUtils.subCmd(r))) {
- return AjaxResult.error("subCmd不对应!");
- }
- if (!StringUtils.equals(CommParseUtils.subCmdId(s), CommParseUtils.subCmdId(r))) {
- return AjaxResult.error("subCmdId不对应!");
- }
- return AjaxResult.success("接收报文匹配正确!");
- }
- }
|