核查系统api
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

413 lines
15 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. package com.iflytop.nuclear.service.impl;
  2. import cn.hutool.core.util.IdUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  5. import com.iflytop.nuclear.entity.CheckInfo;
  6. import com.iflytop.nuclear.entity.CheckResult;
  7. import com.iflytop.nuclear.entity.DetectionMessage;
  8. import com.iflytop.nuclear.model.Task;
  9. import com.iflytop.nuclear.service.CheckService;
  10. import com.iflytop.nuclear.service.TaskService;
  11. import com.iflytop.nuclear.websocket.SocketClient;
  12. import com.iflytop.nuclear.websocket.WebSocketServer;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Service;
  15. import java.io.IOException;
  16. import java.util.Date;
  17. /**
  18. * @author cool
  19. * @date 2023/7/2 09:44
  20. */
  21. @Service
  22. public class CheckServiceImpl implements CheckService {
  23. @Autowired
  24. TaskService taskService;
  25. @Autowired
  26. WebSocketServer webSocketServer;
  27. @Autowired
  28. SocketClient socketClient;
  29. private static boolean messageReceived = false;
  30. private static boolean breakOff = false;
  31. // !!! 记录规则路径上的路过但是不检测的坐标 进行排除即可
  32. public void changeMessageReceived(boolean flag) {
  33. messageReceived = flag;
  34. }
  35. public void changeBreakOff(boolean flag) {
  36. breakOff = flag;
  37. }
  38. @Override
  39. public CheckInfo getCheckInfoByTaskId(int taskId) {
  40. Task byId = taskService.getById(taskId);
  41. CheckInfo checkInfo = CheckInfo.builder()
  42. .order(byId.getCheckOrder())
  43. .status(byId.getStatus())
  44. .currentCoord(byId.getCurrentCoord())
  45. .build();
  46. return checkInfo;
  47. }
  48. public boolean updateCoordAndStatus(int taskId, String currentCoord, int status, boolean updateStartTime, boolean updateEndTime, Integer order) throws IOException {
  49. UpdateWrapper<Task> taskUpdateWrapper = new UpdateWrapper<>();
  50. taskUpdateWrapper.eq("id",taskId);
  51. Task task = new Task();
  52. task.setEndTime(null);
  53. task.setStartTime(null);
  54. if (currentCoord != null){
  55. task.setCurrentCoord(currentCoord);
  56. }
  57. if (updateStartTime) {
  58. task.setStartTime(new Date());
  59. task.setEndTime(null);
  60. }
  61. if (updateEndTime) {
  62. task.setEndTime(new Date());
  63. }
  64. task.setStatus(status);
  65. if (order != null){
  66. task.setCheckOrder(order);
  67. }
  68. boolean update = taskService.update(task, taskUpdateWrapper);
  69. this.sendMessageToPage();
  70. return update;
  71. }
  72. public boolean isMessageReceived() {
  73. return messageReceived;
  74. }
  75. public void sendMessageToDevice(String nextCoord, int taskId) throws IOException {
  76. DetectionMessage startMessage = DetectionMessage.builder()
  77. .messageId(IdUtil.randomUUID())
  78. .coord(nextCoord)
  79. .taskId(taskId)
  80. .build();
  81. socketClient.sendMessage(startMessage.toString());
  82. }
  83. public void sendMessageToPage() throws IOException {
  84. webSocketServer.sendMessage("update");
  85. }
  86. public String initCheck(String initNextCoord,int order, int taskId) throws IOException {
  87. // 指定初始化index为1-6
  88. String nextCoord = initNextCoord;
  89. this.updateCoordAndStatus(taskId, nextCoord, 1, true, false, order);
  90. // 开始检测
  91. this.sendMessageToDevice(nextCoord, taskId);
  92. // 如果中间终止 则退出返回 与messagehandler 处理逻辑相同 外部控制while的终止
  93. // 下一次进入后则进入其他流程
  94. while (!"finish".equals(nextCoord)) {
  95. // 需要一个中断的接口, 中途暂停本次流程
  96. // 用messagehandler处理检测完毕的消息 从而启动以下流程
  97. boolean breakFirst = false;
  98. while (true) {
  99. if (this.breakOff) {
  100. breakFirst = true;
  101. this.updateCoordAndStatus(taskId, nextCoord, 2, false, false, order);
  102. break;
  103. }
  104. if (this.isMessageReceived()) {
  105. messageReceived = false;
  106. break; // 跳出循环
  107. }
  108. }
  109. if (breakFirst) {
  110. break;
  111. }
  112. // 检测完毕后获取下一个坐标
  113. nextCoord = this.getNextCoord(nextCoord, order);
  114. this.updateCoordAndStatus(taskId, nextCoord, 1, false, false, order);
  115. this.sendMessageToDevice(nextCoord, taskId);
  116. if ("finish".equals(nextCoord)) {
  117. this.updateCoordAndStatus(taskId, nextCoord, 3, false, true, order);
  118. }
  119. }
  120. return nextCoord;
  121. }
  122. public String continueCheck(int taskId, int order) throws IOException {
  123. // 被中断过,需要从中断的currentCoord坐标继续
  124. // 首先需要读取数据库,获得当前taskId的信息
  125. Task task = taskService.getById(taskId);
  126. String nextCoord = task.getCurrentCoord();
  127. this.sendMessageToDevice(nextCoord, taskId);
  128. // 得到的currentCoord已经检测过,直接进入下一流程
  129. while (!"finish".equals(nextCoord)) {
  130. // 需要一个中断的接口, 中途暂停本次流程
  131. // 用messagehandler处理检测完毕的消息 从而启动以下流程
  132. boolean breakFirst = false;
  133. while (true) {
  134. if (this.breakOff) {
  135. breakFirst = true;
  136. this.updateCoordAndStatus(taskId, nextCoord, 2, false, false, order);
  137. break;
  138. }
  139. if (this.isMessageReceived()) {
  140. messageReceived = false;
  141. break; // 跳出循环
  142. }
  143. }
  144. if (breakFirst){
  145. break;
  146. }
  147. // 检测完毕后获取下一个坐标
  148. nextCoord = this.getNextCoord(nextCoord, order);
  149. this.updateCoordAndStatus(taskId, nextCoord, 1, false, false, order);
  150. this.sendMessageToDevice(nextCoord, taskId);
  151. if ("finish".equals(nextCoord)) {
  152. this.updateCoordAndStatus(taskId, nextCoord, 3, false, true, order);
  153. }
  154. }
  155. return nextCoord;
  156. }
  157. @Override
  158. public CheckResult autoCheck(int order, String startIndex, int taskId) throws IOException {
  159. // 检测是否有正在进行的任务
  160. QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
  161. queryWrapper.eq("status", 1);
  162. Task one = taskService.getOne(queryWrapper);
  163. if (one != null) {
  164. CheckResult checkResult = new CheckResult();
  165. checkResult.setError("当前已有一个任务正在进行中,请暂停该任务后重试。");
  166. return checkResult;
  167. }
  168. this.changeBreakOff(false);
  169. this.updateCoordAndStatus(taskId, null, 1, false, false, order);
  170. String nextCoord = "";
  171. if (order == 0) {
  172. if (startIndex == null || "".equals(startIndex)) {
  173. nextCoord = this.initCheck("1-6", order, taskId);
  174. }else {
  175. nextCoord = this.continueCheck(taskId, order);
  176. }
  177. }else if (order == 1) {
  178. if (startIndex == null || "".equals(startIndex)) {
  179. nextCoord = this.initCheck("6-1", order, taskId);
  180. }else {
  181. nextCoord = this.continueCheck(taskId, order);
  182. }
  183. }
  184. CheckResult checkResult = new CheckResult();
  185. checkResult.setCurrentTestCoord(nextCoord);
  186. return checkResult;
  187. }
  188. @Override
  189. public void breakOffByTaskId(int taskId) throws IOException {
  190. this.updateCoordAndStatus(taskId, null, 2,false, false, null);
  191. this.changeBreakOff(true);
  192. }
  193. /**
  194. * 传入当前检查的坐标点
  195. * @return 获取下一个坐标点
  196. */
  197. public String getNextCoord(String current, int order) {
  198. if (current == null ) {
  199. return null;
  200. }
  201. String[] split = current.split("-");
  202. int mainLine = Integer.parseInt(split[0]);
  203. int minorLine = Integer.parseInt(split[1]);
  204. // 横向一套规则 纵向一套规则
  205. if (order == 0) {
  206. if (mainLine == 1) {
  207. if (minorLine < 10){
  208. return mainLine + "-" + (minorLine + 1);
  209. }else {
  210. return (mainLine + 1) + "-" + minorLine;
  211. }
  212. }
  213. if (mainLine == 2) {
  214. if (minorLine > 3){
  215. return mainLine + "-" + (minorLine - 1);
  216. }else {
  217. return (mainLine + 1) + "-" + minorLine;
  218. }
  219. }
  220. if (mainLine == 3) {
  221. if (minorLine < 12){
  222. return mainLine + "-" + (minorLine + 1);
  223. }else {
  224. return (mainLine + 1) + "-" + minorLine;
  225. }
  226. }
  227. if (mainLine == 4) {
  228. if (minorLine > 2){
  229. return mainLine + "-" + (minorLine - 1);
  230. }else {
  231. return (mainLine + 1) + "-" + minorLine;
  232. }
  233. }
  234. if (mainLine == 5) {
  235. if (minorLine < 13){
  236. return mainLine + "-" + (minorLine + 1);
  237. }else {
  238. return (mainLine + 1) + "-" + minorLine;
  239. }
  240. }
  241. if (mainLine == 6) {
  242. if (minorLine > 1){
  243. return mainLine + "-" + (minorLine - 1);
  244. }else {
  245. return (mainLine + 1) + "-" + minorLine;
  246. }
  247. }
  248. if (mainLine == 7) {
  249. if (minorLine < 13){
  250. return mainLine + "-" + (minorLine + 1);
  251. }else {
  252. return (mainLine + 1) + "-" + minorLine;
  253. }
  254. }
  255. if (mainLine == 8) {
  256. if (minorLine > 1){
  257. return mainLine + "-" + (minorLine - 1);
  258. }else {
  259. return (mainLine + 1) + "-" + minorLine;
  260. }
  261. }
  262. if (mainLine == 9) {
  263. if (minorLine < 12){
  264. return mainLine + "-" + (minorLine + 1);
  265. }else {
  266. return (mainLine + 1) + "-" + minorLine;
  267. }
  268. }
  269. if (mainLine == 10) {
  270. if (minorLine > 2){
  271. return mainLine + "-" + (minorLine - 1);
  272. }else {
  273. return (mainLine + 1) + "-" + minorLine;
  274. }
  275. }
  276. if (mainLine == 11) {
  277. if (minorLine < 11){
  278. return mainLine + "-" + (minorLine + 1);
  279. }else {
  280. return (mainLine + 1) + "-" + minorLine;
  281. }
  282. }
  283. if (mainLine == 12) {
  284. if (minorLine > 4){
  285. return mainLine + "-" + (minorLine - 1);
  286. }else {
  287. return (mainLine + 1) + "-" + minorLine;
  288. }
  289. }
  290. if (mainLine == 13) {
  291. if (minorLine < 8){
  292. return mainLine + "-" + (minorLine + 1);
  293. }else {
  294. return "finish";
  295. }
  296. }
  297. }
  298. // 纵向
  299. if (order == 1) {
  300. if(minorLine == 1) {
  301. if (mainLine < 10){
  302. return (mainLine + 1) + "-" + minorLine;
  303. }else {
  304. return mainLine + "-" + (minorLine + 1);
  305. }
  306. }
  307. if(minorLine == 2) {
  308. if (mainLine > 3){
  309. return (mainLine - 1) + "-" + minorLine;
  310. }else {
  311. return mainLine + "-" + (minorLine + 1);
  312. }
  313. }
  314. if(minorLine == 3) {
  315. if (mainLine < 12){
  316. return (mainLine + 1) + "-" + minorLine;
  317. }else {
  318. return mainLine + "-" + (minorLine + 1);
  319. }
  320. }
  321. if(minorLine == 4) {
  322. if (mainLine > 2){
  323. return (mainLine- 1) + "-" + minorLine;
  324. }else {
  325. return mainLine + "-" + (minorLine + 1);
  326. }
  327. }
  328. if(minorLine == 5) {
  329. if (mainLine < 13){
  330. return (mainLine + 1) + "-" + minorLine;
  331. }else {
  332. return mainLine + "-" + (minorLine + 1);
  333. }
  334. }
  335. if(minorLine == 6) {
  336. if (mainLine > 1){
  337. return (mainLine - 1) + "-" + minorLine;
  338. }else {
  339. return mainLine + "-" + (minorLine + 1);
  340. }
  341. }
  342. if(minorLine == 7) {
  343. if (mainLine < 13){
  344. return (mainLine + 1) + "-" + minorLine;
  345. }else {
  346. return mainLine + "-" + (minorLine + 1);
  347. }
  348. }
  349. if(minorLine == 8) {
  350. if (mainLine > 1){
  351. return (mainLine - 1) + "-" + minorLine;
  352. }else {
  353. return mainLine + "-" + (minorLine + 1);
  354. }
  355. }
  356. if(minorLine == 9) {
  357. if (mainLine < 12){
  358. return (mainLine + 1) + "-" + minorLine;
  359. }else {
  360. return mainLine + "-" + (minorLine + 1);
  361. }
  362. }
  363. if(minorLine == 10) {
  364. if (mainLine > 2){
  365. return (mainLine - 1) + "-" + minorLine;
  366. }else {
  367. return mainLine + "-" + (minorLine + 1);
  368. }
  369. }
  370. if(minorLine == 11) {
  371. if (mainLine < 11){
  372. return (mainLine + 1) + "-" + minorLine;
  373. }else {
  374. return mainLine + "-" + (minorLine + 1);
  375. }
  376. }
  377. if(minorLine == 12) {
  378. if (mainLine > 4){
  379. return (mainLine - 1) + "-" + minorLine;
  380. }else {
  381. return mainLine + "-" + (minorLine + 1);
  382. }
  383. }
  384. if(minorLine == 13) {
  385. if (mainLine < 8){
  386. return (mainLine + 1) + "-" + minorLine;
  387. }else {
  388. return "finish";
  389. }
  390. }
  391. }
  392. return "";
  393. }
  394. }