diff --git a/src/main/java/com/qyft/ms/app/device/spray/SprayTaskExecutor_bak.java b/src/main/java/com/qyft/ms/app/device/spray/SprayTaskExecutor_bak.java index c9d6775..a4e5f01 100644 --- a/src/main/java/com/qyft/ms/app/device/spray/SprayTaskExecutor_bak.java +++ b/src/main/java/com/qyft/ms/app/device/spray/SprayTaskExecutor_bak.java @@ -2,10 +2,12 @@ // //import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; //import com.qyft.ms.app.common.constant.WebSocketMessageType; -//import com.qyft.ms.app.common.generator.PathGenerator; //import com.qyft.ms.app.device.status.DeviceStatus; //import com.qyft.ms.app.device.status.SprayTask; -//import com.qyft.ms.app.model.bo.*; +//import com.qyft.ms.app.model.bo.Point2D; +//import com.qyft.ms.app.model.bo.SprayTaskPointCollectorPushBO; +//import com.qyft.ms.app.model.bo.SprayTaskSprayed; +//import com.qyft.ms.app.model.bo.SprayTaskStep; //import com.qyft.ms.app.model.entity.Position; //import com.qyft.ms.app.model.entity.SysSettings; //import com.qyft.ms.app.service.PositionService; @@ -27,14 +29,13 @@ //import java.util.Arrays; //import java.util.List; //import java.util.concurrent.CompletableFuture; -//import java.util.concurrent.CopyOnWriteArrayList; //import java.util.concurrent.ExecutionException; //import java.util.concurrent.TimeUnit; // //@Slf4j //@Component //@RequiredArgsConstructor -//public class SprayTaskExecutor_bak { +//public class SprayTaskExecutor { // private final WebSocketService webSocketService; // private final DeviceCommandService deviceCommandService; // private final SysSettingsService sysSettingsService; @@ -71,6 +72,7 @@ // // taskThread = new Thread(() -> { // try { +// webSocketService.pushCMDResponseMsg(FrontResponseGenerator.generateJson(sprayTask.getCmdId(), sprayTask.getCmdCode(), CommandStatus.START, "喷涂任务开始执行")); // int reCurrentStep = sprayTask.getCurrentStep(); // int currentStep = 0; //当前喷涂步骤 // int sprayCount = 0; @@ -81,6 +83,8 @@ // //先移动到玻片位置 // DeviceCommand motorXPositionSetCommand = DeviceCommandGenerator.motorXPositionSet(slideArr[sprayTaskStep.getIndex()][0], 20.0); // DeviceCommand motorYPositionSetCommand = DeviceCommandGenerator.motorYPositionSet(75.5 - slideArr[sprayTaskStep.getIndex()][1], 20.0); +// CommandFuture motorXPositionSetCommandFuture = deviceCommandService.sendCommand(sprayTask.getCmdId(), sprayTask.getCmdCode(), motorXPositionSetCommand); +// CommandFuture motorYPositionSetCommandFuture = deviceCommandService.sendCommand(sprayTask.getCmdId(), sprayTask.getCmdCode(), motorYPositionSetCommand); // CommandFuture motorXPositionSetCommandFuture = deviceCommandService.sendCommandSprayTask(sprayTask.getCmdId(), sprayTask.getCmdCode(), motorXPositionSetCommand); // CommandFuture motorYPositionSetCommandFuture = deviceCommandService.sendCommandSprayTask(sprayTask.getCmdId(), sprayTask.getCmdCode(), motorYPositionSetCommand); // @@ -88,6 +92,7 @@ // Double slideHeight = Double.parseDouble(slideHeightSysSettings.getValue()); // Double height = slideHeight - sprayTask.getSprayParams().getMotorZHeight();//下降z轴高度 // DeviceCommand motorZPositionSetAboveSlideCommand = DeviceCommandGenerator.motorZPositionSet(height, 15.0); +// CommandFuture motorZPositionSetAboveSlideCommandFuture = deviceCommandService.sendCommand(sprayTask.getCmdId(), sprayTask.getCmdCode(), motorZPositionSetAboveSlideCommand); // CommandFuture motorZPositionSetAboveSlideCommandFuture = deviceCommandService.sendCommandSprayTask(sprayTask.getCmdId(), sprayTask.getCmdCode(), motorZPositionSetAboveSlideCommand); // commandWait(motorXPositionSetCommandFuture, motorYPositionSetCommandFuture, motorZPositionSetAboveSlideCommandFuture); // } @@ -97,7 +102,7 @@ // double cacheXPoint = -1; // double cacheYPoint = -1; // DecimalFormat df = new DecimalFormat("#.##"); -// for (int i = 0; i < sprayTaskStep.getSprayPathPointList().size(); i++) {//单次喷涂 +// for (int i = 0; i < sprayTaskStep.getSpraySteps().size(); i++) {//单次喷涂 // if (currentIndex != sprayTaskStep.getIndex()) { // sprayNum = 0; // currentIndex = sprayTaskStep.getIndex(); @@ -106,13 +111,14 @@ // currentStep++; // continue; // } -// if (i == 1) { +// if(i == 1){ // DeviceCommand syringePumpForwardCommand = DeviceCommandGenerator.syringePumpForward(sprayTask.getSprayParams().getVolume());//推动移动注射泵 +// CommandFuture syringePumpForwardCommandFuture = deviceCommandService.sendCommand(sprayTask.getCmdId(), sprayTask.getCmdCode(), syringePumpForwardCommand); // CommandFuture syringePumpForwardCommandFuture = deviceCommandService.sendCommandSprayTask(sprayTask.getCmdId(), sprayTask.getCmdCode(), syringePumpForwardCommand); // commandWait(syringePumpForwardCommandFuture); // } // sprayTask.setFirstImmobility(true); -// List sprayStepCommands = sprayTaskStep.getSprayPathPointList().get(i); +// List sprayStepCommands = sprayTaskStep.getSpraySteps().get(i); // DeviceCommand xSprayStepCommands = sprayStepCommands.get(0); // xSprayStepCommands.getParam().put("speed", sprayTask.getSprayParams().getMovingSpeed());//防止修改了移动速度这里重新设置移动速度 // DeviceCommand ySprayStepCommands = sprayStepCommands.get(1); @@ -125,11 +131,13 @@ // ySprayStepCommands.getParam().put("position", Double.parseDouble(df.format(aYPoint))); // // if (cacheXPoint != aXPoint) { +// CommandFuture commandFuture = deviceCommandService.sendCommand(sprayTask.getCmdId(), sprayTask.getCmdCode(), xSprayStepCommands); // CommandFuture commandFuture = deviceCommandService.sendCommandSprayTask(sprayTask.getCmdId(), sprayTask.getCmdCode(), xSprayStepCommands); // commandFutureList.add(commandFuture); // cacheXPoint = aXPoint; // } // if (cacheYPoint != aYPoint) { +// CommandFuture commandFuture = deviceCommandService.sendCommand(sprayTask.getCmdId(), sprayTask.getCmdCode(), ySprayStepCommands); // CommandFuture commandFuture = deviceCommandService.sendCommandSprayTask(sprayTask.getCmdId(), sprayTask.getCmdCode(), ySprayStepCommands); // commandFutureList.add(commandFuture); // cacheYPoint = aYPoint; @@ -158,6 +166,7 @@ // deviceStatus.setSuspendable(false); // if (currentStep >= reCurrentStep) { // DeviceCommand syringePumpStopCommand = DeviceCommandGenerator.syringePumpStop();//停止推动注射泵 +// CommandFuture syringePumpStopCommandFuture = deviceCommandService.sendCommand(sprayTask.getCmdId(), sprayTask.getCmdCode(), syringePumpStopCommand); // CommandFuture syringePumpStopCommandFuture = deviceCommandService.sendCommandSprayTask(sprayTask.getCmdId(), sprayTask.getCmdCode(), syringePumpStopCommand); // commandWait(syringePumpStopCommandFuture); // } @@ -166,10 +175,12 @@ // } // // DeviceCommand highVoltageCloseCommand = DeviceCommandGenerator.highVoltageClose();//关闭高压 +// CommandFuture highVoltageCloseCommandFuture = deviceCommandService.sendCommand(sprayTask.getCmdId(), sprayTask.getCmdCode(), highVoltageCloseCommand); // CommandFuture highVoltageCloseCommandFuture = deviceCommandService.sendCommandSprayTask(sprayTask.getCmdId(), sprayTask.getCmdCode(), highVoltageCloseCommand); // commandWait(highVoltageCloseCommandFuture); // Thread.sleep(500); // DeviceCommand nozzleValveCloseCommand = DeviceCommandGenerator.nozzleValveClose();//关闭喷嘴阀 +// CommandFuture nozzleValveCloseCommandFuture = deviceCommandService.sendCommand(sprayTask.getCmdId(), sprayTask.getCmdCode(), nozzleValveCloseCommand); // CommandFuture nozzleValveCloseCommandFuture = deviceCommandService.sendCommandSprayTask(sprayTask.getCmdId(), sprayTask.getCmdCode(), nozzleValveCloseCommand); // commandWait(nozzleValveCloseCommandFuture); // @@ -177,6 +188,9 @@ // DeviceCommand motorXOriginCommand = DeviceCommandGenerator.motorXOrigin(); // DeviceCommand motorYOriginCommand = DeviceCommandGenerator.motorYOrigin(); // DeviceCommand motorZOriginCommand = DeviceCommandGenerator.motorZOrigin(); +// CommandFuture motorXOriginCommandFuture = deviceCommandService.sendCommand(sprayTask.getCmdId(), sprayTask.getCmdCode(), motorXOriginCommand); +// CommandFuture motorYOriginCommandFuture = deviceCommandService.sendCommand(sprayTask.getCmdId(), sprayTask.getCmdCode(), motorYOriginCommand); +// CommandFuture motorZOriginCommandFuture = deviceCommandService.sendCommand(sprayTask.getCmdId(), sprayTask.getCmdCode(), motorZOriginCommand); // CommandFuture motorXOriginCommandFuture = deviceCommandService.sendCommandSprayTask(sprayTask.getCmdId(), sprayTask.getCmdCode(), motorXOriginCommand); // CommandFuture motorYOriginCommandFuture = deviceCommandService.sendCommandSprayTask(sprayTask.getCmdId(), sprayTask.getCmdCode(), motorYOriginCommand); // CommandFuture motorZOriginCommandFuture = deviceCommandService.sendCommandSprayTask(sprayTask.getCmdId(), sprayTask.getCmdCode(), motorZOriginCommand); @@ -214,6 +228,9 @@ // // /** // * 停止任务线程: +// * 1. 直接中断线程,不再关心 CompletableFuture 的等待和结果; +// * 2. 可选择等待一定时间让线程自行退出(如 join 超时 2 秒); +// * 3. 清空线程引用,防止内存泄露。 // */ // public synchronized void stopTask() { // if (taskThread != null && taskThread.isAlive()) { @@ -231,70 +248,5 @@ // .orTimeout(120, TimeUnit.SECONDS) // .get(); // } -// -// -// private List getSprayPath(Integer index, SprayTimes sprayTimes) { -// String matrixPathType = sprayTimes.getMatrixPathType(); -// Position slidePosition1 = positionService.getOne(new LambdaQueryWrapper().eq(Position::getPointCode, "slide_position1")); -// Position slidePosition2 = positionService.getOne(new LambdaQueryWrapper().eq(Position::getPointCode, "slide_position2")); -// Position slidePosition3 = positionService.getOne(new LambdaQueryWrapper().eq(Position::getPointCode, "slide_position3")); -// Position slidePosition4 = positionService.getOne(new LambdaQueryWrapper().eq(Position::getPointCode, "slide_position4")); -// Double[][] slideArr = { -// {slidePosition1.getX(), slidePosition1.getY()}, -// {slidePosition2.getX(), slidePosition2.getY()}, -// {slidePosition3.getX(), slidePosition3.getY()}, -// {slidePosition4.getX(), slidePosition4.getY()} -// }; -// Double[] upperLeft = {sprayTimes.getX1(), sprayTimes.getY1()}; //范围左上角 x1,y1 -// Double[] lowerRight = {sprayTimes.getX2(), sprayTimes.getY2()}; //范围左上角 x1,y1 -// Double[] slide = slideArr[index];//获取玻片的坐标 -// DecimalFormat df = new DecimalFormat("#.##"); -// double left = Double.parseDouble(df.format(slide[0] + upperLeft[0])); -// double right = Double.parseDouble(df.format(slide[0] + lowerRight[0])); -// double top = Double.parseDouble(df.format(slide[1] + upperLeft[1])); -// double bottom = Double.parseDouble(df.format(slide[1] + lowerRight[1])); -// Double spacing = sprayTimes.getSpacing(); -// List sprayTaskStepList = new CopyOnWriteArrayList<>(); -// if ("horizontal".equals(matrixPathType)) {//喷涂路径类型 horizontal 横向 | vertical 纵向 | grid 网格先横向后纵向 -// List pathList = PathGenerator.generatePathPoints( -// left, right, top, bottom, -// spacing, -// PathGenerator.MoveMode.HORIZONTAL_ZIGZAG_TOP_DOWN -// ); -// SprayTaskStep sprayTaskStep = new SprayTaskStep(); -// sprayTaskStep.setIndex(index); -// sprayTaskStep.setSprayPathPointList(pathList); -// sprayTaskStepList.add(sprayTaskStep); -// } else if ("vertical".equals(matrixPathType)) { -// List pathList = PathGenerator.generatePathPoints( -// left, right, top, bottom, -// spacing, -// PathGenerator.MoveMode.VERTICAL_ZIGZAG_LEFT_RIGHT -// ); -// SprayTaskStep sprayTaskStep = new SprayTaskStep(); -// sprayTaskStep.setIndex(index); -// sprayTaskStep.setSprayPathPointList(pathList); -// sprayTaskStepList.add(sprayTaskStep); -// } else if ("grid".equals(matrixPathType)) { -// List pathList1 = PathGenerator.generatePathPoints( -// left, right, top, bottom, -// spacing, -// PathGenerator.MoveMode.HORIZONTAL_ZIGZAG_TOP_DOWN -// ); -// SprayTaskStep sprayTaskStep1 = new SprayTaskStep(); -// sprayTaskStep1.setIndex(index); -// sprayTaskStep1.setSprayPathPointList(pathList1); -// sprayTaskStepList.add(sprayTaskStep1); -// List pathList2 = PathGenerator.generatePathPoints( -// left, right, top, bottom, -// spacing, -// PathGenerator.MoveMode.VERTICAL_ZIGZAG_LEFT_RIGHT -// ); -// SprayTaskStep sprayTaskStep2 = new SprayTaskStep(); -// sprayTaskStep2.setIndex(index); -// sprayTaskStep2.setSprayPathPointList(pathList2); -// sprayTaskStepList.add(sprayTaskStep2); -// } -// return sprayTaskStepList; -// } //} +// diff --git a/src/main/java/com/qyft/ms/app/device/status/SprayTask.java b/src/main/java/com/qyft/ms/app/device/status/SprayTask.java index 7104194..d596ec4 100644 --- a/src/main/java/com/qyft/ms/app/device/status/SprayTask.java +++ b/src/main/java/com/qyft/ms/app/device/status/SprayTask.java @@ -41,6 +41,9 @@ public class SprayTask { */ private volatile boolean spraying = false; + private final Object pauseLock = new Object(); + private final Object closeLock = new Object(); + private SprayTask() { } diff --git a/src/main/java/com/qyft/ms/app/front/cmd/business/MatrixSprayStart.java b/src/main/java/com/qyft/ms/app/front/cmd/business/MatrixSprayStart.java index 49b7bda..6ad9d2d 100644 --- a/src/main/java/com/qyft/ms/app/front/cmd/business/MatrixSprayStart.java +++ b/src/main/java/com/qyft/ms/app/front/cmd/business/MatrixSprayStart.java @@ -71,6 +71,7 @@ public class MatrixSprayStart extends BaseCommandHandler { } sprayTask.setCmdId(form.getCmdId()); sprayTask.setCmdCode(form.getCmdCode()); + sprayTask.setClose(false); sprayTask.setSpraying(true);//正在进行喷涂 deviceStatus.setSpraying(true); diff --git a/src/main/java/com/qyft/ms/app/front/cmd/business/MatrixSprayStop.java b/src/main/java/com/qyft/ms/app/front/cmd/business/MatrixSprayStop.java index d195252..4b56874 100644 --- a/src/main/java/com/qyft/ms/app/front/cmd/business/MatrixSprayStop.java +++ b/src/main/java/com/qyft/ms/app/front/cmd/business/MatrixSprayStop.java @@ -47,42 +47,37 @@ public class MatrixSprayStop extends BaseCommandHandler { return runAsync(() -> { sprayTask.setClose(true); - try { - sprayTaskExecutor.stopTask();//终止喷涂任务线程 + sprayTaskExecutor.stopTask();//终止喷涂任务线程 - DeviceCommand syringePumpStopCommand = DeviceCommandGenerator.syringePumpStop(); //停止推动注射泵 - CommandFuture syringePumpStopCommandFuture = deviceCommandService.sendCommand(form.getCmdId(), form.getCmdCode(), syringePumpStopCommand); - DeviceCommand nozzleValveCloseCommand = DeviceCommandGenerator.nozzleValveClose(); //关闭喷嘴阀 - CommandFuture nozzleValveCloseCommandFuture = deviceCommandService.sendCommand(form.getCmdId(), form.getCmdCode(), nozzleValveCloseCommand); - DeviceCommand highVoltageCloseCommand = DeviceCommandGenerator.highVoltageClose(); //关闭高压 - CommandFuture highVoltageCloseCommandFuture = deviceCommandService.sendCommand(form.getCmdId(), form.getCmdCode(), highVoltageCloseCommand); - commandWait(syringePumpStopCommandFuture, nozzleValveCloseCommandFuture, highVoltageCloseCommandFuture); + DeviceCommand syringePumpStopCommand = DeviceCommandGenerator.syringePumpStop(); //停止推动注射泵 + CommandFuture syringePumpStopCommandFuture = deviceCommandService.sendCommand(form.getCmdId(), form.getCmdCode(), syringePumpStopCommand); + DeviceCommand nozzleValveCloseCommand = DeviceCommandGenerator.nozzleValveClose(); //关闭喷嘴阀 + CommandFuture nozzleValveCloseCommandFuture = deviceCommandService.sendCommand(form.getCmdId(), form.getCmdCode(), nozzleValveCloseCommand); + DeviceCommand highVoltageCloseCommand = DeviceCommandGenerator.highVoltageClose(); //关闭高压 + CommandFuture highVoltageCloseCommandFuture = deviceCommandService.sendCommand(form.getCmdId(), form.getCmdCode(), highVoltageCloseCommand); + commandWait(syringePumpStopCommandFuture, nozzleValveCloseCommandFuture, highVoltageCloseCommandFuture); - DeviceCommand motorXStopCommand = DeviceCommandGenerator.motorXStop(); //x轴停止移动 - DeviceCommand motorYStopCommand = DeviceCommandGenerator.motorYStop(); //y轴停止移动 - DeviceCommand motorZStopCommand = DeviceCommandGenerator.motorZStop(); //z轴停止移动 - CommandFuture motorXStopCommandFuture = deviceCommandService.sendCommand(form.getCmdId(), form.getCmdCode(), motorXStopCommand); - CommandFuture motorYStopCommandFuture = deviceCommandService.sendCommand(form.getCmdId(), form.getCmdCode(), motorYStopCommand); - CommandFuture motorZStopCommandFuture = deviceCommandService.sendCommand(form.getCmdId(), form.getCmdCode(), motorZStopCommand); - commandWait(motorXStopCommandFuture, motorYStopCommandFuture, motorZStopCommandFuture); + DeviceCommand motorXStopCommand = DeviceCommandGenerator.motorXStop(); //x轴停止移动 + DeviceCommand motorYStopCommand = DeviceCommandGenerator.motorYStop(); //y轴停止移动 + DeviceCommand motorZStopCommand = DeviceCommandGenerator.motorZStop(); //z轴停止移动 + CommandFuture motorXStopCommandFuture = deviceCommandService.sendCommand(form.getCmdId(), form.getCmdCode(), motorXStopCommand); + CommandFuture motorYStopCommandFuture = deviceCommandService.sendCommand(form.getCmdId(), form.getCmdCode(), motorYStopCommand); + CommandFuture motorZStopCommandFuture = deviceCommandService.sendCommand(form.getCmdId(), form.getCmdCode(), motorZStopCommand); + commandWait(motorXStopCommandFuture, motorYStopCommandFuture, motorZStopCommandFuture); - //XYZ回原点 - DeviceCommand motorXOriginCommand = DeviceCommandGenerator.motorXOrigin(); - DeviceCommand motorYOriginCommand = DeviceCommandGenerator.motorYOrigin(); - DeviceCommand motorZOriginCommand = DeviceCommandGenerator.motorZOrigin(); - CommandFuture motorXOriginCommandFuture = deviceCommandService.sendCommand(form.getCmdId(), form.getCmdCode(), motorXOriginCommand); - CommandFuture motorYOriginCommandFuture = deviceCommandService.sendCommand(form.getCmdId(), form.getCmdCode(), motorYOriginCommand); - CommandFuture motorZOriginCommandFuture = deviceCommandService.sendCommand(form.getCmdId(), form.getCmdCode(), motorZOriginCommand); - commandWait(motorXOriginCommandFuture, motorYOriginCommandFuture, motorZOriginCommandFuture); - - SprayTask.getInstance().clear(); - deviceStatus.setSpraying(false); - deviceStatus.setPaused(false); - deviceStatus.setSuspendable(false); - } finally { - sprayTask.setClose(false); - } + //XYZ回原点 + DeviceCommand motorXOriginCommand = DeviceCommandGenerator.motorXOrigin(); + DeviceCommand motorYOriginCommand = DeviceCommandGenerator.motorYOrigin(); + DeviceCommand motorZOriginCommand = DeviceCommandGenerator.motorZOrigin(); + CommandFuture motorXOriginCommandFuture = deviceCommandService.sendCommand(form.getCmdId(), form.getCmdCode(), motorXOriginCommand); + CommandFuture motorYOriginCommandFuture = deviceCommandService.sendCommand(form.getCmdId(), form.getCmdCode(), motorYOriginCommand); + CommandFuture motorZOriginCommandFuture = deviceCommandService.sendCommand(form.getCmdId(), form.getCmdCode(), motorZOriginCommand); + commandWait(motorXOriginCommandFuture, motorYOriginCommandFuture, motorZOriginCommandFuture); + SprayTask.getInstance().clear(); + deviceStatus.setSpraying(false); + deviceStatus.setPaused(false); + deviceStatus.setSuspendable(false); }); } } diff --git a/src/main/java/com/qyft/ms/system/service/device/DeviceCommandService.java b/src/main/java/com/qyft/ms/system/service/device/DeviceCommandService.java index 3d11996..b09fbfa 100644 --- a/src/main/java/com/qyft/ms/system/service/device/DeviceCommandService.java +++ b/src/main/java/com/qyft/ms/system/service/device/DeviceCommandService.java @@ -30,7 +30,6 @@ public class DeviceCommandService { private final WebSocketService webSocketService; private final DeviceStatus deviceStatus; private final ApplicationEventPublisher publisher; - private final Object pauseLock = new Object(); public CommandFuture executeCommand(DeviceCommand cmdToDevice) { int cmdId = CyclicNumberGenerator.getInstance().generateNumber(); @@ -76,13 +75,15 @@ public class DeviceCommandService { public CommandFuture sendCommandSprayTask(String cmdId, String cmdCode, DeviceCommand deviceCommand) throws Exception { SprayTask sprayTask = SprayTask.getInstance(); - synchronized (pauseLock) { + synchronized (sprayTask.getPauseLock()) { while (sprayTask.isPaused()) { - pauseLock.wait(); + sprayTask.getPauseLock().wait(); } } - if (sprayTask.isClose() || Thread.currentThread().isInterrupted()) { - throw new InterruptedException(); + synchronized (sprayTask.getCloseLock()) { + if (sprayTask.isClose() || Thread.currentThread().isInterrupted()) { + throw new InterruptedException(); + } } return sendCommand(cmdId, cmdCode, deviceCommand); }