CommReceiveService.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package com.ruoyi.sim.service.impl;
  2. import com.ruoyi.common.core.domain.AjaxResult;
  3. import com.ruoyi.sim.constant.FaultConst;
  4. import com.ruoyi.sim.domain.*;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Service;
  10. import java.util.HashSet;
  11. import static com.ruoyi.sim.constant.CommConst.*;
  12. @Service
  13. // 多实例
  14. // 异步调用
  15. // @Scope("prototype")
  16. public class CommReceiveService {
  17. private static final Logger l = LoggerFactory.getLogger(CommReceiveService.class);
  18. @Autowired
  19. private RealExamFaultService realExamFaultService;
  20. @Autowired
  21. private SimService simService;
  22. @Autowired
  23. private DebugFaultService debugFaultService;
  24. @Autowired
  25. private FaultService faultService;
  26. /**
  27. * 只要返回信息,即认为在线。
  28. *
  29. * @param sm
  30. * @param s
  31. * @return
  32. */
  33. public AjaxResult checkOneSimState(SimMsg sm, Sim s) {
  34. if (s == null) {
  35. l.warn("s is null");
  36. return AjaxResult.error("sim is null");
  37. }
  38. if (StringUtils.isNotBlank(sm.getReceiveMsg())) {
  39. simService.updateSimStateBySimId(s.getSimId(), Sim.State.ONLINE);
  40. }
  41. return AjaxResult.success();
  42. }
  43. public AjaxResult checkOneSimSn(SimMsg sm, Sim s) {
  44. final String simType = s.getSimType();
  45. final String r = sm.getReceiveMsg();
  46. final String msgError = "连接模拟器类型不正确!";
  47. switch (simType) {
  48. case Sim.TYPE_0001 -> {
  49. if (r.startsWith(Sim.TYPE_0001_SN) && r.endsWith(s.getSimNum())) {
  50. return AjaxResult.success();
  51. } else {
  52. return AjaxResult.error(msgError);
  53. }
  54. }
  55. case Sim.TYPE_0002 -> {
  56. if (r.startsWith(Sim.TYPE_0002_SN) && r.endsWith(s.getSimNum())) {
  57. return AjaxResult.success();
  58. } else {
  59. return AjaxResult.error(msgError);
  60. }
  61. }
  62. case Sim.TYPE_0003 -> {
  63. if (r.startsWith(Sim.TYPE_0003_SN) && r.endsWith(s.getSimNum())) {
  64. return AjaxResult.success();
  65. } else {
  66. return AjaxResult.error(msgError);
  67. }
  68. }
  69. default -> throw new IllegalStateException("Unexpected value: " + simType);
  70. }
  71. }
  72. public void clearOneFault(SimMsg sm, Sim s, RealExamFault reF, Fault f) {
  73. // check
  74. //
  75. //
  76. if (reF != null) {
  77. realExamFaultService.updateRefStateByRefId(reF.getRefId(), RealExamFault.State.CLEARED);
  78. }
  79. }
  80. /**
  81. * 设置出题值。
  82. *
  83. * @param sm
  84. * @param s
  85. * @param reF debug模式下为null。
  86. * @param f
  87. * @param faultIds debug模式下为null。
  88. */
  89. public void setFaultQuestionValue(SimMsg sm, Sim s, RealExamFault reF, Fault f, String[] faultIds) {
  90. // check
  91. //
  92. String faultQuestionValue = subContentData(sm.getReceiveMsg());
  93. // todo:
  94. if (StringUtils.isBlank(faultQuestionValue)) {
  95. l.warn("faultQuestionValue is empty!");
  96. return;
  97. }
  98. l.info("faultQuestionValue = {}", faultQuestionValue);
  99. if (reF != null) {
  100. // 修改关联状态。
  101. reF.setSimFaultQuestionValue(faultQuestionValue);
  102. realExamFaultService.updateRealExamFault(reF);
  103. if (RealExamFault.Flag.YES.equals(reF.getFlag())) {
  104. realExamFaultService.updateRefStateByRefId(reF.getRefId(), RealExamFault.State.WRITTEN);
  105. } else if (RealExamFault.Flag.NO.equals(reF.getFlag())) {
  106. realExamFaultService.updateRefStateByRefId(reF.getRefId(), RealExamFault.State.LOOP_READ);
  107. }
  108. } else {
  109. //
  110. //
  111. HashSet<String> fSet = new HashSet<>();
  112. for (String fId : faultIds) {
  113. fSet.add(fId);
  114. }
  115. l.info("fSet.size() = {}", fSet.size());
  116. String faultId = f.getFaultId();
  117. l.info("faultId = {}", faultId);
  118. DebugFault df = debugFaultService.exist(s.getSimId(), faultId);
  119. if (df == null) {
  120. df = new DebugFault();
  121. df.setSimId(s.getSimId());
  122. df.setFaultId(f.getFaultId());
  123. if (fSet.contains(faultId)) {
  124. df.setFlag(DebugFault.Flag.YES);
  125. } else {
  126. df.setFlag(DebugFault.Flag.NO);
  127. }
  128. df.setSimFaultQuestionValue(faultQuestionValue);
  129. df.setSimFaultAnswerValue("");
  130. df.setAnswerRight(DebugFault.AnswerRight.UNKNOWN);
  131. debugFaultService.insertDebugFault(df);
  132. } else {
  133. if (fSet.contains(faultId)) {
  134. df.setFlag(DebugFault.Flag.YES);
  135. } else {
  136. df.setFlag(DebugFault.Flag.NO);
  137. }
  138. df.setSimFaultQuestionValue(faultQuestionValue);
  139. df.setSimFaultAnswerValue("");
  140. df.setAnswerRight(DebugFault.AnswerRight.UNKNOWN);
  141. debugFaultService.updateDebugFault(df);
  142. }
  143. }
  144. }
  145. /**
  146. * 设置答题值。
  147. *
  148. * @param sm
  149. * @param s
  150. * @param reF debug模式为null
  151. * @param f
  152. * @param refState 轮询时候为null debug模式为null
  153. */
  154. public void setFaultAnswerValue(SimMsg sm, Sim s, RealExamFault reF, Fault f, String refState) {
  155. // check
  156. //
  157. String faultAnswerValue = subContentData(sm.getReceiveMsg());
  158. // todo:
  159. if (StringUtils.isBlank(faultAnswerValue)) {
  160. l.warn("faultAnswerValue is empty!");
  161. return;
  162. }
  163. l.info("faultAnswerValue = {}", faultAnswerValue);
  164. if (reF != null && refState != null) {
  165. if (StringUtils.isNotBlank(refState)) {
  166. reF.setRefState(refState);
  167. }
  168. reF.setSimFaultAnswerValue(faultAnswerValue);
  169. realExamFaultService.updateRealExamFault(reF);
  170. } else {
  171. DebugFault df = debugFaultService.exist(s.getSimId(), f.getFaultId());
  172. if (df == null) {
  173. df = new DebugFault();
  174. df.setSimId(s.getSimId());
  175. df.setFaultId(f.getFaultId());
  176. df.setSimFaultAnswerValue(faultAnswerValue);
  177. debugFaultService.insertDebugFault(df);
  178. } else {
  179. df.setSimFaultAnswerValue(faultAnswerValue);
  180. debugFaultService.updateDebugFault(df);
  181. }
  182. }
  183. }
  184. /**
  185. * 截取 内容报文。
  186. *
  187. * @param receiveMsg
  188. * @return
  189. */
  190. public String subContentData(String receiveMsg) {
  191. if (StringUtils.isEmpty(receiveMsg)) {
  192. return "";
  193. }
  194. return StringUtils.substring(receiveMsg, 10, 18);
  195. }
  196. /**
  197. * 开始考试 检查 故障部位 检查
  198. *
  199. * @param sm
  200. * @param s
  201. * @param f
  202. * @return
  203. */
  204. public AjaxResult getOneFaultCheck(SimMsg sm, Sim s, Fault f) {
  205. String checkValue = subContentData(sm.getReceiveMsg());
  206. if (s == null) {
  207. return AjaxResult.error("没有对应模拟器!");
  208. }
  209. // 是否在 故障部位 跳过检查 白名单中。
  210. if (FaultConst.FAULT_SET_CHECK_PASS.contains(f.getFaultId())) {
  211. // 跳过检查,直接成功。
  212. return AjaxResult.success(f);
  213. }
  214. // 是否是 2型的维护管 或 3型的维护管
  215. if (FaultConst.FAULT_SET_WHG.contains(f.getFaultId())) {
  216. // 判断必须存在
  217. String WHG_EXIST_MSG = checkValue.substring(4, 6);
  218. if (WHG_MSG_EXIST_YES.equals(WHG_EXIST_MSG)) {
  219. return AjaxResult.success(f);
  220. } else {
  221. return AjaxResult.error("故障部位[" + f.getBindHardwareMsg() + "][" + f.getReplaceName() + "]未正确安装;", f);
  222. }
  223. }
  224. if (BLANK_CONTENT.equals(checkValue)) {
  225. l.info("故障部位[" + f.getBindHardwareMsg() + "][" + f.getReplaceName() + "]未正确安装;");
  226. return AjaxResult.error("故障部位[" + f.getBindHardwareMsg() + "][" + f.getReplaceName() + "]未正确安装;", f);
  227. } else {
  228. return AjaxResult.success(f);
  229. }
  230. }
  231. }