|
|
@ -12,13 +12,16 @@ import com.qyft.ms.app.model.entity.Position; |
|
|
|
import com.qyft.ms.app.model.entity.SysSettings; |
|
|
|
import com.qyft.ms.app.service.PositionService; |
|
|
|
import com.qyft.ms.app.service.SysSettingsService; |
|
|
|
import com.qyft.ms.system.common.constant.CommandStatus; |
|
|
|
import com.qyft.ms.system.common.device.command.CommandFuture; |
|
|
|
import com.qyft.ms.system.common.device.command.DeviceCommandGenerator; |
|
|
|
import com.qyft.ms.system.common.device.command.FrontResponseGenerator; |
|
|
|
import com.qyft.ms.system.model.bo.DeviceCommand; |
|
|
|
import com.qyft.ms.system.service.WebSocketService; |
|
|
|
import com.qyft.ms.system.service.device.DeviceCommandService; |
|
|
|
import jakarta.annotation.PostConstruct; |
|
|
|
import lombok.RequiredArgsConstructor; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
@ -28,6 +31,7 @@ import java.util.concurrent.CompletableFuture; |
|
|
|
import java.util.concurrent.ExecutionException; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@Component |
|
|
|
@RequiredArgsConstructor |
|
|
|
public class SprayTaskExecutor { |
|
|
@ -60,14 +64,14 @@ public class SprayTaskExecutor { |
|
|
|
* 启动任务线程,如果线程已存在且正在运行则不重复启动 |
|
|
|
*/ |
|
|
|
public synchronized void startTask() { |
|
|
|
SprayTask sprayTask = SprayTask.getInstance(); |
|
|
|
if (taskThread != null && taskThread.isAlive()) { |
|
|
|
webSocketService.pushMsg(WebSocketMessageType.CMD_DEBUG, "设备正在喷涂,请先停止喷涂"); |
|
|
|
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(sprayTask.getCmdId(), sprayTask.getCmdCode(), CommandStatus.ERROR, "设备正在喷涂,请先停止喷涂")); |
|
|
|
return; |
|
|
|
} |
|
|
|
taskThread = new Thread(() -> { |
|
|
|
try { |
|
|
|
webSocketService.pushMsg(WebSocketMessageType.CMD_DEBUG, "喷涂任务线程启动"); |
|
|
|
SprayTask sprayTask = SprayTask.getInstance(); |
|
|
|
webSocketService.pushCMDResponseMsg(FrontResponseGenerator.generateJson(sprayTask.getCmdId(), sprayTask.getCmdCode(), CommandStatus.START, "喷涂任务开始执行")); |
|
|
|
if (!sprayTask.isSprayPreStepsCompleted()) { |
|
|
|
//XYZ回原点 |
|
|
|
DeviceCommand motorXPositionSetCommand = DeviceCommandGenerator.motorXPositionSet(0.0); |
|
|
@ -98,6 +102,7 @@ public class SprayTaskExecutor { |
|
|
|
|
|
|
|
int reCurrentStep = sprayTask.getCurrentStep(); |
|
|
|
int currentStep = 0; //当前喷涂步骤 |
|
|
|
int sprayNum = 1; |
|
|
|
for (SprayTaskStep sprayTaskStep : sprayTask.getSprayTaskStepList()) {//循环进行多次喷涂 |
|
|
|
|
|
|
|
DeviceCommand nozzleValveOpenCommand = DeviceCommandGenerator.nozzleValveOpen();//开启喷嘴阀 |
|
|
@ -111,7 +116,7 @@ public class SprayTaskExecutor { |
|
|
|
deviceStatus.setSuspendable(true); |
|
|
|
for (int i = 0; i < sprayTaskStep.getSpraySteps().size(); i++) {//单次喷涂 |
|
|
|
if (currentStep < reCurrentStep) { |
|
|
|
break; |
|
|
|
continue; |
|
|
|
} |
|
|
|
List<DeviceCommand> sprayStepCommands = sprayTaskStep.getSpraySteps().get(i); |
|
|
|
List<CommandFuture> commandFutureList = new ArrayList<>(); |
|
|
@ -132,7 +137,7 @@ public class SprayTaskExecutor { |
|
|
|
double nextXPoint = (Double) sprayStepCommands.get(0).getParam().get("position") - slideArr[sprayTaskStep.getIndex()][0]; |
|
|
|
double nextYPoint = 75.5 - (Double) sprayStepCommands.get(1).getParam().get("position"); |
|
|
|
nextPoint = new Point2D(nextXPoint, nextYPoint); |
|
|
|
SprayTaskPointCollectorPushBO sprayTaskPointCollectorPushBO = new SprayTaskPointCollectorPushBO(sprayTaskStep.getIndex(), currentPoint, nextPoint); |
|
|
|
SprayTaskPointCollectorPushBO sprayTaskPointCollectorPushBO = new SprayTaskPointCollectorPushBO(sprayTask.getCmdId(), sprayTask.getCmdCode(), sprayTaskStep.getIndex(), sprayNum, currentPoint, nextPoint); |
|
|
|
webSocketService.pushMsg(WebSocketMessageType.SPRAY_POINT, sprayTaskPointCollectorPushBO); |
|
|
|
} |
|
|
|
commandWait(commandFutureArray); |
|
|
@ -141,22 +146,23 @@ public class SprayTaskExecutor { |
|
|
|
sprayTask.setCurrentStep(currentStep);//将当前的喷涂进度缓存 |
|
|
|
if (nextPoint != null) { |
|
|
|
//将当前点位缓存 |
|
|
|
sprayStepList.add(new Point2D(nextPoint.getX(), nextPoint.getY())); |
|
|
|
SprayTaskSprayed sprayTaskSprayed = new SprayTaskSprayed(); |
|
|
|
sprayTaskSprayed.setNumber(sprayNum); |
|
|
|
sprayTaskSprayed.setIndex(sprayTaskStep.getIndex()); |
|
|
|
sprayTaskSprayed.setSprayedPoints(new Point2D(nextPoint.getX(), nextPoint.getY())); |
|
|
|
sprayTask.getSprayTaskSprayedList().add(sprayTaskSprayed); |
|
|
|
} |
|
|
|
} |
|
|
|
sprayTask.setSuspendable(false);//不可暂停 |
|
|
|
deviceStatus.setSuspendable(false); |
|
|
|
SprayTaskSprayed sprayTaskSprayed = new SprayTaskSprayed(); |
|
|
|
sprayTaskSprayed.setIndex(sprayTaskStep.getIndex()); |
|
|
|
sprayTaskSprayed.setSprayedPoints(sprayStepList); |
|
|
|
|
|
|
|
DeviceCommand nozzleValveCloseCommand = DeviceCommandGenerator.nozzleValveClose();//关闭喷嘴阀 |
|
|
|
DeviceCommand syringePumpStopCommand = DeviceCommandGenerator.syringePumpStop();//停止推动注射泵 |
|
|
|
CommandFuture nozzleValveCloseCommandFuture = deviceCommandService.sendCommand(sprayTask.getCmdId(), sprayTask.getCmdCode(), nozzleValveCloseCommand); |
|
|
|
CommandFuture syringePumpStopCommandFuture = deviceCommandService.sendCommand(sprayTask.getCmdId(), sprayTask.getCmdCode(), syringePumpStopCommand); |
|
|
|
commandWait(nozzleValveCloseCommandFuture, syringePumpStopCommandFuture); |
|
|
|
sprayNum++; |
|
|
|
} |
|
|
|
|
|
|
|
DeviceCommand highVoltageCloseCommand = DeviceCommandGenerator.highVoltageClose();//关闭高压 |
|
|
|
CommandFuture highVoltageCloseCommandFuture = deviceCommandService.sendCommand(sprayTask.getCmdId(), sprayTask.getCmdCode(), highVoltageCloseCommand); |
|
|
|
commandWait(highVoltageCloseCommandFuture); |
|
|
@ -169,21 +175,31 @@ public class SprayTaskExecutor { |
|
|
|
CommandFuture motorYPositionSetCommandFuture = deviceCommandService.sendCommand(sprayTask.getCmdId(), sprayTask.getCmdCode(), motorYPositionSetCommand); |
|
|
|
CommandFuture motorZPositionSetCommandFuture = deviceCommandService.sendCommand(sprayTask.getCmdId(), sprayTask.getCmdCode(), motorZPositionSetCommand); |
|
|
|
commandWait(motorXPositionSetCommandFuture, motorYPositionSetCommandFuture, motorZPositionSetCommandFuture); |
|
|
|
webSocketService.pushMsg(WebSocketMessageType.CMD_DEBUG, "喷涂任务执行完毕"); |
|
|
|
|
|
|
|
SprayTask.getInstance().clear(); |
|
|
|
deviceStatus.setSpraying(false); |
|
|
|
deviceStatus.setSuspendable(false); |
|
|
|
deviceStatus.setSuspendable(false); |
|
|
|
|
|
|
|
webSocketService.pushCMDResponseMsg(FrontResponseGenerator.generateJson(sprayTask.getCmdId(), sprayTask.getCmdCode(), CommandStatus.SUCCESS, "喷涂任务执行成功")); |
|
|
|
webSocketService.pushCMDResponseMsg(FrontResponseGenerator.generateJson(sprayTask.getCmdId(), sprayTask.getCmdCode(), CommandStatus.SPRAY_TASK_FINISH, "喷涂任务结束")); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
webSocketService.pushMsg(WebSocketMessageType.CMD_DEBUG, "喷涂任务线程停止"); |
|
|
|
deviceStatus.setSuspendable(false); |
|
|
|
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(sprayTask.getCmdId(), sprayTask.getCmdCode(), CommandStatus.SEND, "喷涂任务线程停止")); |
|
|
|
Thread.currentThread().interrupt(); |
|
|
|
} catch (Exception e) { |
|
|
|
webSocketService.pushMsg(WebSocketMessageType.CMD_DEBUG, "喷涂任务线程异常:" + e.getMessage()); |
|
|
|
log.info("喷涂任务失败", e); |
|
|
|
SprayTask.getInstance().clear(); |
|
|
|
deviceStatus.setSpraying(false); |
|
|
|
deviceStatus.setSuspendable(false); |
|
|
|
deviceStatus.setSuspendable(false); |
|
|
|
webSocketService.pushCMDResponseMsg(FrontResponseGenerator.generateJson(sprayTask.getCmdId(), sprayTask.getCmdCode(), CommandStatus.FAIL, "喷涂任务执行失败")); |
|
|
|
webSocketService.pushCMDResponseMsg(FrontResponseGenerator.generateJson(sprayTask.getCmdId(), sprayTask.getCmdCode(), CommandStatus.SPRAY_TASK_FINISH, "喷涂任务结束")); |
|
|
|
} finally { |
|
|
|
webSocketService.pushMsg(WebSocketMessageType.CMD_DEBUG, "喷涂任务线程退出"); |
|
|
|
webSocketService.pushDebugMsg(FrontResponseGenerator.generateJson(sprayTask.getCmdId(), sprayTask.getCmdCode(), CommandStatus.SEND, "喷涂任务线程退出")); |
|
|
|
// 在线程结束后将 taskThread 设置为 null,保证状态一致和资源释放 |
|
|
|
synchronized (this) { |
|
|
|
taskThread = null; |
|
|
|
SprayTask.getInstance().clear(); |
|
|
|
deviceStatus.setSpraying(false); |
|
|
|
deviceStatus.setPaused(false); |
|
|
|
deviceStatus.setSuspendable(false); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
@ -198,7 +214,6 @@ public class SprayTaskExecutor { |
|
|
|
*/ |
|
|
|
public synchronized void stopTask() { |
|
|
|
if (taskThread != null && taskThread.isAlive()) { |
|
|
|
webSocketService.pushMsg(WebSocketMessageType.CMD_DEBUG, "停止喷涂任务线程"); |
|
|
|
// 中断线程,使得 future.get() 能够抛出 InterruptedException,从而退出等待 |
|
|
|
taskThread.interrupt(); |
|
|
|
} |
|
|
|