核查系统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

package com.iflytop.nuclear.service.impl;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.iflytop.nuclear.entity.CheckInfo;
import com.iflytop.nuclear.entity.CheckResult;
import com.iflytop.nuclear.entity.DetectionMessage;
import com.iflytop.nuclear.model.Task;
import com.iflytop.nuclear.service.CheckService;
import com.iflytop.nuclear.service.TaskService;
import com.iflytop.nuclear.websocket.SocketClient;
import com.iflytop.nuclear.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.Date;
/**
* @author cool
* @date 2023/7/2 09:44
*/
@Service
public class CheckServiceImpl implements CheckService {
@Autowired
TaskService taskService;
@Autowired
WebSocketServer webSocketServer;
@Autowired
SocketClient socketClient;
private static boolean messageReceived = false;
private static boolean breakOff = false;
// !!! 记录规则路径上的路过但是不检测的坐标 进行排除即可
public void changeMessageReceived(boolean flag) {
messageReceived = flag;
}
public void changeBreakOff(boolean flag) {
breakOff = flag;
}
@Override
public CheckInfo getCheckInfoByTaskId(int taskId) {
Task byId = taskService.getById(taskId);
CheckInfo checkInfo = CheckInfo.builder()
.order(byId.getCheckOrder())
.status(byId.getStatus())
.currentCoord(byId.getCurrentCoord())
.build();
return checkInfo;
}
public boolean updateCoordAndStatus(int taskId, String currentCoord, int status, boolean updateStartTime, boolean updateEndTime, Integer order) throws IOException {
UpdateWrapper<Task> taskUpdateWrapper = new UpdateWrapper<>();
taskUpdateWrapper.eq("id",taskId);
Task task = new Task();
task.setEndTime(null);
task.setStartTime(null);
if (currentCoord != null){
task.setCurrentCoord(currentCoord);
}
if (updateStartTime) {
task.setStartTime(new Date());
task.setEndTime(null);
}
if (updateEndTime) {
task.setEndTime(new Date());
}
task.setStatus(status);
if (order != null){
task.setCheckOrder(order);
}
boolean update = taskService.update(task, taskUpdateWrapper);
this.sendMessageToPage();
return update;
}
public boolean isMessageReceived() {
return messageReceived;
}
public void sendMessageToDevice(String nextCoord, int taskId) throws IOException {
DetectionMessage startMessage = DetectionMessage.builder()
.messageId(IdUtil.randomUUID())
.coord(nextCoord)
.taskId(taskId)
.build();
socketClient.sendMessage(startMessage.toString());
}
public void sendMessageToPage() throws IOException {
webSocketServer.sendMessage("update");
}
public String initCheck(String initNextCoord,int order, int taskId) throws IOException {
// 指定初始化index为1-6
String nextCoord = initNextCoord;
this.updateCoordAndStatus(taskId, nextCoord, 1, true, false, order);
// 开始检测
this.sendMessageToDevice(nextCoord, taskId);
// 如果中间终止 则退出返回 与messagehandler 处理逻辑相同 外部控制while的终止
// 下一次进入后则进入其他流程
while (!"finish".equals(nextCoord)) {
// 需要一个中断的接口, 中途暂停本次流程
// 用messagehandler处理检测完毕的消息 从而启动以下流程
boolean breakFirst = false;
while (true) {
if (this.breakOff) {
breakFirst = true;
this.updateCoordAndStatus(taskId, nextCoord, 2, false, false, order);
break;
}
if (this.isMessageReceived()) {
messageReceived = false;
break; // 跳出循环
}
}
if (breakFirst) {
break;
}
// 检测完毕后获取下一个坐标
nextCoord = this.getNextCoord(nextCoord, order);
this.updateCoordAndStatus(taskId, nextCoord, 1, false, false, order);
this.sendMessageToDevice(nextCoord, taskId);
if ("finish".equals(nextCoord)) {
this.updateCoordAndStatus(taskId, nextCoord, 3, false, true, order);
}
}
return nextCoord;
}
public String continueCheck(int taskId, int order) throws IOException {
// 被中断过,需要从中断的currentCoord坐标继续
// 首先需要读取数据库,获得当前taskId的信息
Task task = taskService.getById(taskId);
String nextCoord = task.getCurrentCoord();
this.sendMessageToDevice(nextCoord, taskId);
// 得到的currentCoord已经检测过,直接进入下一流程
while (!"finish".equals(nextCoord)) {
// 需要一个中断的接口, 中途暂停本次流程
// 用messagehandler处理检测完毕的消息 从而启动以下流程
boolean breakFirst = false;
while (true) {
if (this.breakOff) {
breakFirst = true;
this.updateCoordAndStatus(taskId, nextCoord, 2, false, false, order);
break;
}
if (this.isMessageReceived()) {
messageReceived = false;
break; // 跳出循环
}
}
if (breakFirst){
break;
}
// 检测完毕后获取下一个坐标
nextCoord = this.getNextCoord(nextCoord, order);
this.updateCoordAndStatus(taskId, nextCoord, 1, false, false, order);
this.sendMessageToDevice(nextCoord, taskId);
if ("finish".equals(nextCoord)) {
this.updateCoordAndStatus(taskId, nextCoord, 3, false, true, order);
}
}
return nextCoord;
}
@Override
public CheckResult autoCheck(int order, String startIndex, int taskId) throws IOException {
// 检测是否有正在进行的任务
QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("status", 1);
Task one = taskService.getOne(queryWrapper);
if (one != null) {
CheckResult checkResult = new CheckResult();
checkResult.setError("当前已有一个任务正在进行中,请暂停该任务后重试。");
return checkResult;
}
this.changeBreakOff(false);
this.updateCoordAndStatus(taskId, null, 1, false, false, order);
String nextCoord = "";
if (order == 0) {
if (startIndex == null || "".equals(startIndex)) {
nextCoord = this.initCheck("1-6", order, taskId);
}else {
nextCoord = this.continueCheck(taskId, order);
}
}else if (order == 1) {
if (startIndex == null || "".equals(startIndex)) {
nextCoord = this.initCheck("6-1", order, taskId);
}else {
nextCoord = this.continueCheck(taskId, order);
}
}
CheckResult checkResult = new CheckResult();
checkResult.setCurrentTestCoord(nextCoord);
return checkResult;
}
@Override
public void breakOffByTaskId(int taskId) throws IOException {
this.updateCoordAndStatus(taskId, null, 2,false, false, null);
this.changeBreakOff(true);
}
/**
* 传入当前检查的坐标点
* @return 获取下一个坐标点
*/
public String getNextCoord(String current, int order) {
if (current == null ) {
return null;
}
String[] split = current.split("-");
int mainLine = Integer.parseInt(split[0]);
int minorLine = Integer.parseInt(split[1]);
// 横向一套规则 纵向一套规则
if (order == 0) {
if (mainLine == 1) {
if (minorLine < 10){
return mainLine + "-" + (minorLine + 1);
}else {
return (mainLine + 1) + "-" + minorLine;
}
}
if (mainLine == 2) {
if (minorLine > 3){
return mainLine + "-" + (minorLine - 1);
}else {
return (mainLine + 1) + "-" + minorLine;
}
}
if (mainLine == 3) {
if (minorLine < 12){
return mainLine + "-" + (minorLine + 1);
}else {
return (mainLine + 1) + "-" + minorLine;
}
}
if (mainLine == 4) {
if (minorLine > 2){
return mainLine + "-" + (minorLine - 1);
}else {
return (mainLine + 1) + "-" + minorLine;
}
}
if (mainLine == 5) {
if (minorLine < 13){
return mainLine + "-" + (minorLine + 1);
}else {
return (mainLine + 1) + "-" + minorLine;
}
}
if (mainLine == 6) {
if (minorLine > 1){
return mainLine + "-" + (minorLine - 1);
}else {
return (mainLine + 1) + "-" + minorLine;
}
}
if (mainLine == 7) {
if (minorLine < 13){
return mainLine + "-" + (minorLine + 1);
}else {
return (mainLine + 1) + "-" + minorLine;
}
}
if (mainLine == 8) {
if (minorLine > 1){
return mainLine + "-" + (minorLine - 1);
}else {
return (mainLine + 1) + "-" + minorLine;
}
}
if (mainLine == 9) {
if (minorLine < 12){
return mainLine + "-" + (minorLine + 1);
}else {
return (mainLine + 1) + "-" + minorLine;
}
}
if (mainLine == 10) {
if (minorLine > 2){
return mainLine + "-" + (minorLine - 1);
}else {
return (mainLine + 1) + "-" + minorLine;
}
}
if (mainLine == 11) {
if (minorLine < 11){
return mainLine + "-" + (minorLine + 1);
}else {
return (mainLine + 1) + "-" + minorLine;
}
}
if (mainLine == 12) {
if (minorLine > 4){
return mainLine + "-" + (minorLine - 1);
}else {
return (mainLine + 1) + "-" + minorLine;
}
}
if (mainLine == 13) {
if (minorLine < 8){
return mainLine + "-" + (minorLine + 1);
}else {
return "finish";
}
}
}
// 纵向
if (order == 1) {
if(minorLine == 1) {
if (mainLine < 10){
return (mainLine + 1) + "-" + minorLine;
}else {
return mainLine + "-" + (minorLine + 1);
}
}
if(minorLine == 2) {
if (mainLine > 3){
return (mainLine - 1) + "-" + minorLine;
}else {
return mainLine + "-" + (minorLine + 1);
}
}
if(minorLine == 3) {
if (mainLine < 12){
return (mainLine + 1) + "-" + minorLine;
}else {
return mainLine + "-" + (minorLine + 1);
}
}
if(minorLine == 4) {
if (mainLine > 2){
return (mainLine- 1) + "-" + minorLine;
}else {
return mainLine + "-" + (minorLine + 1);
}
}
if(minorLine == 5) {
if (mainLine < 13){
return (mainLine + 1) + "-" + minorLine;
}else {
return mainLine + "-" + (minorLine + 1);
}
}
if(minorLine == 6) {
if (mainLine > 1){
return (mainLine - 1) + "-" + minorLine;
}else {
return mainLine + "-" + (minorLine + 1);
}
}
if(minorLine == 7) {
if (mainLine < 13){
return (mainLine + 1) + "-" + minorLine;
}else {
return mainLine + "-" + (minorLine + 1);
}
}
if(minorLine == 8) {
if (mainLine > 1){
return (mainLine - 1) + "-" + minorLine;
}else {
return mainLine + "-" + (minorLine + 1);
}
}
if(minorLine == 9) {
if (mainLine < 12){
return (mainLine + 1) + "-" + minorLine;
}else {
return mainLine + "-" + (minorLine + 1);
}
}
if(minorLine == 10) {
if (mainLine > 2){
return (mainLine - 1) + "-" + minorLine;
}else {
return mainLine + "-" + (minorLine + 1);
}
}
if(minorLine == 11) {
if (mainLine < 11){
return (mainLine + 1) + "-" + minorLine;
}else {
return mainLine + "-" + (minorLine + 1);
}
}
if(minorLine == 12) {
if (mainLine > 4){
return (mainLine - 1) + "-" + minorLine;
}else {
return mainLine + "-" + (minorLine + 1);
}
}
if(minorLine == 13) {
if (mainLine < 8){
return (mainLine + 1) + "-" + minorLine;
}else {
return "finish";
}
}
}
return "";
}
}