Browse Source

重构代码

main
maochaoying 2 years ago
parent
commit
ca9309de58
  1. 3
      src/main/java/com/iflytop/nuclear/controller/TaskController.java
  2. 126
      src/main/java/com/iflytop/nuclear/service/impl/CheckServiceImpl.java
  3. 6
      src/main/java/com/iflytop/nuclear/websocket/WebSocketServer.java

3
src/main/java/com/iflytop/nuclear/controller/TaskController.java

@ -19,6 +19,7 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -81,7 +82,7 @@ public class TaskController {
} }
@PostMapping("/delete/excel/{id}") @PostMapping("/delete/excel/{id}")
public ResponseData deleteTaskExcel(@PathVariable(name = "id") int id) {
public ResponseData deleteTaskExcel(@PathVariable(name = "id") int id) throws IOException {
QueryWrapper<NuclearCoreConfig> nuclearCoreConfigQueryWrapper = new QueryWrapper<>(); QueryWrapper<NuclearCoreConfig> nuclearCoreConfigQueryWrapper = new QueryWrapper<>();
nuclearCoreConfigQueryWrapper.eq("task_id", id); nuclearCoreConfigQueryWrapper.eq("task_id", id);
boolean remove = nuclearCoreConfigService.remove(nuclearCoreConfigQueryWrapper); boolean remove = nuclearCoreConfigService.remove(nuclearCoreConfigQueryWrapper);

126
src/main/java/com/iflytop/nuclear/service/impl/CheckServiceImpl.java

@ -55,7 +55,7 @@ public class CheckServiceImpl implements CheckService {
return checkInfo; return checkInfo;
} }
public boolean updateCoordAndStatus(int taskId, String currentCoord, int status) {
public boolean updateCoordAndStatus(int taskId, String currentCoord, int status) throws IOException {
UpdateWrapper<Task> taskUpdateWrapper = new UpdateWrapper<>(); UpdateWrapper<Task> taskUpdateWrapper = new UpdateWrapper<>();
taskUpdateWrapper.eq("id",taskId); taskUpdateWrapper.eq("id",taskId);
Task task = new Task(); Task task = new Task();
@ -64,6 +64,7 @@ public class CheckServiceImpl implements CheckService {
} }
task.setStatus(status); task.setStatus(status);
boolean update = taskService.update(task, taskUpdateWrapper); boolean update = taskService.update(task, taskUpdateWrapper);
this.sendMessageToPage();
return update; return update;
} }
@ -71,27 +72,25 @@ public class CheckServiceImpl implements CheckService {
return messageReceived; return messageReceived;
} }
public void sendMessageToClient(String nextCoord, int taskId) throws IOException {
DetectionMessage startMessage = DetectionMessage.builder()
.messageId(IdUtil.randomUUID())
.coord(nextCoord)
.taskId(taskId)
.build();
webSocketServer.sendMessage(startMessage.toString());
public void sendMessageToDevice(String nextCoord, int taskId) throws IOException {
// DetectionMessage startMessage = DetectionMessage.builder()
// .messageId(IdUtil.randomUUID())
// .coord(nextCoord)
// .taskId(taskId)
// .build();
// webSocketServer.sendMessage(startMessage.toString());
} }
@Override
public CheckResult autoCheck(int order, String startIndex, int taskId) throws IOException {
this.changeBreakOff(false);
this.updateCoordAndStatus(taskId, null, 1);
currentTaskId = taskId;
if (order == 0) {
if (startIndex == null || "".equals(startIndex)) {
public void sendMessageToPage() throws IOException {
webSocketServer.sendMessage("update");
}
public void initCheck(String initNextCoord,int order, int taskId) throws IOException {
// 指定初始化index为1-6 // 指定初始化index为1-6
String nextCoord = "1-6";
String nextCoord = initNextCoord;
this.updateCoordAndStatus(taskId, nextCoord, 1); this.updateCoordAndStatus(taskId, nextCoord, 1);
// 开始检测 // 开始检测
this.sendMessageToClient(nextCoord, taskId);
this.sendMessageToDevice(nextCoord, taskId);
// 如果中间终止 则退出返回 与messagehandler 处理逻辑相同 外部控制while的终止 // 如果中间终止 则退出返回 与messagehandler 处理逻辑相同 外部控制while的终止
// 下一次进入后则进入其他流程 // 下一次进入后则进入其他流程
while (!"finish".equals(nextCoord)) { while (!"finish".equals(nextCoord)) {
@ -113,19 +112,21 @@ public class CheckServiceImpl implements CheckService {
break; break;
} }
// 检测完毕后获取下一个坐标 // 检测完毕后获取下一个坐标
nextCoord = this.getNextCoord(nextCoord, 0);
nextCoord = this.getNextCoord(nextCoord, order);
this.updateCoordAndStatus(taskId, nextCoord, 1); this.updateCoordAndStatus(taskId, nextCoord, 1);
this.sendMessageToClient(nextCoord, taskId);
this.sendMessageToDevice(nextCoord, taskId);
if ("finish".equals(nextCoord)) { if ("finish".equals(nextCoord)) {
this.updateCoordAndStatus(taskId, nextCoord, 3); this.updateCoordAndStatus(taskId, nextCoord, 3);
} }
} }
}else {
}
public void continueCheck(int taskId, int order) throws IOException {
// 被中断过需要从中断的currentCoord坐标继续 // 被中断过需要从中断的currentCoord坐标继续
// 首先需要读取数据库获得当前taskId的信息 // 首先需要读取数据库获得当前taskId的信息
Task task = taskService.getById(taskId); Task task = taskService.getById(taskId);
String nextCoord = task.getCurrentCoord(); String nextCoord = task.getCurrentCoord();
this.sendMessageToClient(nextCoord, taskId);
this.sendMessageToDevice(nextCoord, taskId);
// 得到的currentCoord已经检测过直接进入下一流程 // 得到的currentCoord已经检测过直接进入下一流程
while (!"finish".equals(nextCoord)) { while (!"finish".equals(nextCoord)) {
// 需要一个中断的接口, 中途暂停本次流程 // 需要一个中断的接口, 中途暂停本次流程
@ -146,82 +147,31 @@ public class CheckServiceImpl implements CheckService {
break; break;
} }
// 检测完毕后获取下一个坐标 // 检测完毕后获取下一个坐标
nextCoord = this.getNextCoord(nextCoord, 0);
nextCoord = this.getNextCoord(nextCoord, order);
this.updateCoordAndStatus(taskId, nextCoord, 1); this.updateCoordAndStatus(taskId, nextCoord, 1);
this.sendMessageToClient(nextCoord, taskId);
this.sendMessageToDevice(nextCoord, taskId);
if ("finish".equals(nextCoord)) { if ("finish".equals(nextCoord)) {
this.updateCoordAndStatus(taskId, nextCoord, 3); this.updateCoordAndStatus(taskId, nextCoord, 3);
} }
} }
} }
}else if (order == 1) {
@Override
public CheckResult autoCheck(int order, String startIndex, int taskId) throws IOException {
this.changeBreakOff(false);
this.updateCoordAndStatus(taskId, null, 1);
currentTaskId = taskId;
if (order == 0) {
if (startIndex == null || "".equals(startIndex)) { if (startIndex == null || "".equals(startIndex)) {
// 指定初始化index为6-1
String nextCoord = "6-1";
this.updateCoordAndStatus(taskId, nextCoord, 1);
// 开始检测
this.sendMessageToClient(nextCoord, taskId);
// 如果中间终止 则退出返回 与messagehandler 处理逻辑相同 外部控制while的终止
// 下一次进入后则进入其他流程
while (!"finish".equals(nextCoord)) {
// 需要一个中断的接口, 中途暂停本次流程
// 用messagehandler处理检测完毕的消息 从而启动以下流程
boolean breakFirst = false;
while (true) {
if (this.breakOff) {
breakFirst = true;
this.updateCoordAndStatus(taskId, nextCoord, 2);
break;
}
if (this.isMessageReceived()) {
messageReceived = false;
break; // 跳出循环
}
}
if (breakFirst) {
break;
}
// 检测完毕后获取下一个坐标
nextCoord = this.getNextCoord(nextCoord, 1);
this.updateCoordAndStatus(taskId, nextCoord, 1);
this.sendMessageToClient(nextCoord, taskId);
if ("finish".equals(nextCoord)) {
this.updateCoordAndStatus(taskId, nextCoord, 3);
}
}
this.initCheck("1-6", order, taskId);
}else { }else {
// 被中断过需要从中断的currentCoord坐标继续
// 首先需要读取数据库获得当前taskId的信息
Task task = taskService.getById(taskId);
String nextCoord = task.getCurrentCoord();
this.sendMessageToClient(nextCoord, taskId);
// 得到的currentCoord已经检测过直接进入下一流程
while (!"finish".equals(nextCoord)) {
// 需要一个中断的接口, 中途暂停本次流程
// 用messagehandler处理检测完毕的消息 从而启动以下流程
boolean breakFirst = false;
while (true) {
if (this.breakOff) {
breakFirst = true;
this.updateCoordAndStatus(taskId, nextCoord, 2);
break;
}
if (this.isMessageReceived()) {
messageReceived = false;
break; // 跳出循环
}
}
if (breakFirst){
break;
}
// 检测完毕后获取下一个坐标
nextCoord = this.getNextCoord(nextCoord, 1);
this.updateCoordAndStatus(taskId, nextCoord, 1);
this.sendMessageToClient(nextCoord, taskId);
if ("finish".equals(nextCoord)) {
this.updateCoordAndStatus(taskId, nextCoord, 3);
}
this.continueCheck(taskId, order);
} }
}else if (order == 1) {
if (startIndex == null || "".equals(startIndex)) {
this.initCheck("6-1", order, taskId);
}else {
this.continueCheck(taskId, order);
} }
} }
return null; return null;

6
src/main/java/com/iflytop/nuclear/websocket/WebSocketServer.java

@ -109,9 +109,9 @@ public class WebSocketServer {
JSONObject jsonObject = JSON.parseObject(message); JSONObject jsonObject = JSON.parseObject(message);
String command = jsonObject.getString("command"); String command = jsonObject.getString("command");
// 根据command类型分发给不同的handler和detection // 根据command类型分发给不同的handler和detection
if (!command.isEmpty()) {
messageHandler.dispatcher(command, jsonObject, session);
}
// if (!command.isEmpty()) {
// messageHandler.dispatcher(command, jsonObject, session);
// }
} catch (Exception e) { } catch (Exception e) {
JSONObject msg = new JSONObject(); JSONObject msg = new JSONObject();
msg.put("message", "传输数据必须为包含command字段的JSON格式"); msg.put("message", "传输数据必须为包含command字段的JSON格式");

Loading…
Cancel
Save