CommReceiveService.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package com.ruoyi.sim.service.impl;
  2. import com.ruoyi.common.core.domain.AjaxResult;
  3. import com.ruoyi.sim.constant.CommConst;
  4. import com.ruoyi.sim.constant.FaultConst;
  5. import com.ruoyi.sim.domain.*;
  6. import com.ruoyi.sim.util.CRC16Modbus;
  7. import org.apache.commons.lang3.StringUtils;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Service;
  12. import java.util.Arrays;
  13. import java.util.HashSet;
  14. import static com.ruoyi.sim.constant.CommConst.*;
  15. @Service
  16. // 多实例
  17. // 异步调用
  18. // @Scope("prototype")
  19. public class CommReceiveService {
  20. private static final Logger l = LoggerFactory.getLogger(CommReceiveService.class);
  21. @Autowired
  22. private RealExamFaultService realExamFaultService;
  23. @Autowired
  24. private SimService simService;
  25. @Autowired
  26. private DebugFaultService debugFaultService;
  27. @Autowired
  28. private FaultService faultService;
  29. public void clearOneFault(SimMsg sm, Sim s, RealExamFault reF, Fault f) {
  30. // check
  31. //
  32. //
  33. if (reF != null) {
  34. realExamFaultService.updateRefStateByRefId(reF.getRefId(), RealExamFault.State.CLEARED);
  35. }
  36. }
  37. /**
  38. * 设置出题值。
  39. *
  40. * @param sm
  41. * @param s
  42. * @param reF debug模式下为null。
  43. * @param f
  44. * @param faultIds debug模式必须有值
  45. */
  46. public void setFaultQuestionValue(SimMsg sm, Sim s, RealExamFault reF, Fault f, String[] faultIds) {
  47. // check
  48. //
  49. String faultQuestionValue = CommParseUtils.subContentData(sm);
  50. // todo:
  51. if (StringUtils.isBlank(faultQuestionValue)) {
  52. l.warn("faultQuestionValue is empty!");
  53. return;
  54. }
  55. l.info("faultQuestionValue = {}", faultQuestionValue);
  56. if (reF != null) {
  57. // 修改关联状态。
  58. reF.setSimFaultQuestionValue(faultQuestionValue);
  59. realExamFaultService.updateRealExamFault(reF);
  60. if (RealExamFault.Flag.YES.equals(reF.getFlag())) {
  61. realExamFaultService.updateRefStateByRefId(reF.getRefId(), RealExamFault.State.WRITTEN);
  62. } else if (RealExamFault.Flag.NO.equals(reF.getFlag())) {
  63. realExamFaultService.updateRefStateByRefId(reF.getRefId(), RealExamFault.State.LOOP_READ);
  64. }
  65. } else {
  66. //
  67. HashSet<String> fTempSet = new HashSet<>(Arrays.asList(faultIds));
  68. l.info("fTempSet.toArray = {}", fTempSet.toArray());
  69. String faultId = f.getFaultId();
  70. l.info("faultId = {}", faultId);
  71. // 不判断是否存在,因为之前已经删除所有表中数据,所以应该直接插入数据。
  72. DebugFault df = new DebugFault();
  73. df.setSimId(s.getSimId());
  74. df.setFaultId(f.getFaultId());
  75. if (fTempSet.contains(faultId)) {
  76. df.setFlag(DebugFault.Flag.YES);
  77. } else {
  78. df.setFlag(DebugFault.Flag.NO);
  79. }
  80. df.setSimFaultQuestionValue(faultQuestionValue);
  81. // 答题值为空。
  82. df.setSimFaultAnswerValue("");
  83. df.setAnswerRight(DebugFault.AnswerRight.UNKNOWN);
  84. debugFaultService.insertDebugFault(df);
  85. }
  86. }
  87. /**
  88. * 设置答题值。
  89. *
  90. * @param sm
  91. * @param s
  92. * @param reF debug模式为null
  93. * @param f
  94. * @param refState 轮询时候为null debug模式为null
  95. */
  96. public void setFaultAnswerValue(SimMsg sm, Sim s, RealExamFault reF, Fault f, String refState) {
  97. // check
  98. //
  99. String faultAnswerValue = CommParseUtils.subContentData(sm);
  100. // todo:
  101. if (StringUtils.isBlank(faultAnswerValue)) {
  102. l.warn("faultAnswerValue is empty!");
  103. return;
  104. }
  105. l.info("faultAnswerValue = {}", faultAnswerValue);
  106. if (reF != null && refState != null) {
  107. if (StringUtils.isNotBlank(refState)) {
  108. reF.setRefState(refState);
  109. }
  110. reF.setSimFaultAnswerValue(faultAnswerValue);
  111. realExamFaultService.updateRealExamFault(reF);
  112. } else {
  113. DebugFault df = debugFaultService.exist(s.getSimId(), f.getFaultId());
  114. if (df == null) {
  115. df = new DebugFault();
  116. df.setSimId(s.getSimId());
  117. df.setFaultId(f.getFaultId());
  118. df.setSimFaultAnswerValue(faultAnswerValue);
  119. debugFaultService.insertDebugFault(df);
  120. } else {
  121. df.setSimFaultAnswerValue(faultAnswerValue);
  122. debugFaultService.updateDebugFault(df);
  123. }
  124. }
  125. }
  126. /**
  127. * 开始考试 检查 故障部位 检查
  128. *
  129. * @param sm
  130. * @param s
  131. * @param f
  132. * @return
  133. */
  134. public AjaxResult getOneFaultCheck(SimMsg sm, Sim s, Fault f) {
  135. String checkValue = CommParseUtils.subContentData(sm);
  136. if (s == null) {
  137. return AjaxResult.error("没有对应模拟器!");
  138. }
  139. // 是否在 故障部位 跳过检查 白名单中。
  140. if (FaultConst.FAULT_SET_CHECK_PASS.contains(f.getFaultId())) {
  141. // 跳过检查,直接成功。
  142. return AjaxResult.success(f);
  143. }
  144. // 是否是 2型的维护管 或 3型的维护管
  145. if (FaultConst.FAULT_SET_WHG.contains(f.getFaultId())) {
  146. // 判断必须存在
  147. String WHG_EXIST_MSG = checkValue.substring(4, 6);
  148. if (WHG_MSG_EXIST_YES.equals(WHG_EXIST_MSG)) {
  149. return AjaxResult.success("", f);
  150. } else {
  151. return AjaxResult.success("故障部位[" + f.getBindHardwareMsg() + "][" + f.getReplaceName() + "]未正确安装;", f);
  152. }
  153. }
  154. if (BLANK_CONTENT.equals(checkValue)) {
  155. l.info("故障部位[" + f.getBindHardwareMsg() + "][" + f.getReplaceName() + "]未正确安装;");
  156. return AjaxResult.success("故障部位[" + f.getBindHardwareMsg() + "][" + f.getReplaceName() + "]未正确安装;", f);
  157. } else {
  158. return AjaxResult.success("", f);
  159. }
  160. }
  161. /**
  162. * 处理报文前端加00的情况,最多可能加5组00
  163. *
  164. * @param receiveMsg
  165. * @return
  166. */
  167. public String removeRrefix0(String receiveMsg) {
  168. int count = 0;
  169. while (StringUtils.startsWith(receiveMsg, CommConst.PREFIX_ERROR_0)) {
  170. receiveMsg = StringUtils.removeStartIgnoreCase(receiveMsg, CommConst.PREFIX_ERROR_0);
  171. count = count + 1;
  172. }
  173. l.info("####remove count#### = [{}]", count);
  174. return receiveMsg;
  175. }
  176. /**
  177. * 有返回报文的情况下,检查Receive的报文格式。
  178. *
  179. * @param receiveMsg
  180. * @return
  181. */
  182. public AjaxResult checkReceiveMsgFormat(final String receiveMsg) {
  183. l.info("####checkReceiveMsg#### = [{}]", receiveMsg);
  184. String msgErr = "ReceiveMsg ";
  185. // check:不能是empty
  186. if (StringUtils.isBlank(receiveMsg)) {
  187. return AjaxResult.error(msgErr + "isBlank");
  188. }
  189. // check:长度
  190. if (receiveMsg.length() != LENGTH_24) {
  191. return AjaxResult.error(msgErr + "length error.length not 24.");
  192. }
  193. // check:数据方向
  194. final String orn = StringUtils.substring(receiveMsg, 4, 6);
  195. if (!ORN_RECEIVE.equals(orn)) {
  196. return AjaxResult.error(msgErr + "orn error.");
  197. }
  198. // check:前缀
  199. if (!StringUtils.startsWith(receiveMsg, PREFIX)) {
  200. return AjaxResult.error(msgErr + "not start with AA.");
  201. }
  202. // check:后缀
  203. if (!StringUtils.endsWith(receiveMsg, SUFFIX)) {
  204. return AjaxResult.error(msgErr + "not end with 55.");
  205. }
  206. // 计算CRC16
  207. // todo:
  208. // todo: receive报文检验错误。
  209. if (false) {
  210. String crcContent = receiveMsg.substring(0, 18);
  211. l.debug("crcContent: {}", crcContent.toUpperCase());
  212. byte[] receiveByteContent = CommSendService.hexStrToByteArrs(crcContent);
  213. byte[] receiveByteCrc = CRC16Modbus.calculateCRC(receiveByteContent);
  214. String crc = CommSendService.bytesToHexV2(receiveByteCrc);
  215. l.debug("crc: {}", crc.toUpperCase());
  216. // if (!receiveMsg.substring(19, 22).equals(crc.toUpperCase())) {
  217. // throw new IllegalArgumentException("checkReceiveMsg length error");
  218. // }
  219. // todo: 比对校验值,不正确。
  220. }
  221. return AjaxResult.success("接收报文格式检查正确!");
  222. }
  223. public AjaxResult checkReceiveMsgMatch(final SimMsg sm) {
  224. if (sm == null) {
  225. return AjaxResult.error("空报文!");
  226. }
  227. final String s = sm.getSendMsg();
  228. final String r = sm.getReceiveMsg();
  229. if (StringUtils.isBlank(s) || StringUtils.isBlank(r)) {
  230. return AjaxResult.error("空报文!");
  231. }
  232. if (StringUtils.equals(CommParseUtils.subSimNum(s), "00")) {
  233. } else {
  234. if (!StringUtils.equals(CommParseUtils.subSimNum(s), CommParseUtils.subSimNum(r))) {
  235. return AjaxResult.error("subSimNum不对应!");
  236. }
  237. }
  238. if (!StringUtils.equals(CommParseUtils.subCmd(s), CommParseUtils.subCmd(r))) {
  239. return AjaxResult.error("subCmd不对应!");
  240. }
  241. if (!StringUtils.equals(CommParseUtils.subCmdId(s), CommParseUtils.subCmdId(r))) {
  242. return AjaxResult.error("subCmdId不对应!");
  243. }
  244. return AjaxResult.success("接收报文匹配正确!");
  245. }
  246. }