diff --git a/src/main/java/com/iflytop/gd/app/cmd/MoveToHeatAreaCommand.java b/src/main/java/com/iflytop/gd/app/cmd/MoveToHeatAreaCommand.java index fdefeb8..6437f85 100644 --- a/src/main/java/com/iflytop/gd/app/cmd/MoveToHeatAreaCommand.java +++ b/src/main/java/com/iflytop/gd/app/cmd/MoveToHeatAreaCommand.java @@ -18,7 +18,6 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.atomic.AtomicBoolean; /** * 移至加热 @@ -32,13 +31,15 @@ public class MoveToHeatAreaCommand extends BaseCommandHandler { private final DevicePositionService devicePositionService; private final GantryArmService gantryArmService; private final DeviceStateService deviceStateService; - private final AtomicBoolean isExecuting = new AtomicBoolean(false); @Override public CompletableFuture handle(CmdDTO cmdDTO) throws Exception { - if (isExecuting.get()) { + if (deviceStateService.getCommandState().get().isMoveToHeatAreaCommandExecuting()) { throw new AppException(ResultCode.COMMAND_ALREADY_EXECUTING); } + if (deviceStateService.getCommandState().get().isMoveToSolutionAreaCommandExecuting()) { + throw new AppException(ResultCode.CMD_BUSY); + } String heatId = cmdDTO.getStringParam("heatId"); HeatModuleCode heatModuleId = HeatModuleCode.valueOf(heatId); double clawCapPick = devicePositionService.getPosition(DevicePositionCode.clawCapPick).getDistance(); //获取拍子夹取距离 @@ -54,57 +55,90 @@ public class MoveToHeatAreaCommand extends BaseCommandHandler { Point3D liquidAreaTrayPoint3D = devicePositionService.getPosition(DevicePositionCode.liquidAreaTrayPoint).getPoint3D();//获取加液模块上方点位 Point3D heatAreaTrayClawPoint3D = deviceCommandUtilService.getHeatAreaTrayClawPoint3D(heatModuleId);//获取指定加热模块上方点位 + Point3D heatArea4TrayClawPoint3D = deviceCommandUtilService.getHeatAreaTrayClawPoint3D(HeatModuleCode.heat_module_04);//获取指定加热模块上方点位 + Point3D heatAreaCapClawPointPoint3D = deviceCommandUtilService.getHeatAreaCapClawPointPoint3D(heatModuleId);//获取指定加热模块拍子上方点位 Point3D capStorageCapClawPoint3D = devicePositionService.getPosition(DevicePositionCode.capStorageCapClawPoint).getPoint3D();//获取拍子存放区上方点位; //校验目标加热位是否有托盘 try { - deviceStateService.setGantryArmStateIdle(false); - isExecuting.set(true); - Boolean heatModuleTray = deviceCommandUtilService.heatModuleSensorState(heatModuleId); - if (heatModuleTray) { - throw new AppException(ResultCode.TARGET_HEAT_MODULE_NO_TRAY); - } - } finally { - deviceStateService.setGantryArmStateIdle(true); - isExecuting.set(false); + deviceStateService.getCommandState().get().setMoveToHeatAreaCommandExecuting(true); +// Boolean heatModuleTray = deviceCommandUtilService.heatModuleSensorState(heatModuleId); +// if (heatModuleTray) { +// throw new AppException(ResultCode.TARGET_HEAT_MODULE_NO_TRAY); +// } + } catch (Exception e) { + deviceStateService.getCommandState().get().setMoveToHeatAreaCommandExecuting(false); + throw e; } return runAsync(() -> { try { TrayState trayState = deviceStateService.getTrayInSolutionModule(); - - deviceCommandUtilService.capUpBalance(cmdDTO.getCommandId(), cmdDTO.getCommand());//提升拍子存放区至拍子夹取的高度 deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), liquidAreaTrayPoint3D); //将机械臂移动至加液模块上方 deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawTrayPick);//将夹爪打开,准备夹取托盘 deviceCommandUtilService.gantryZMove(solutionModuleTrayMoveHeight);//下降z轴,使夹爪落入托盘孔位 deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawTrayGrip);//将夹爪收紧,夹住托盘 deviceCommandUtilService.gantryZMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), 0);//抬升z轴 deviceStateService.setSolutionModuleStateTrayStatus(0);//加液模块是否有托盘 + deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatArea4TrayClawPoint3D);//将携带托盘的机械臂移动至4号加热模块上方 deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatAreaTrayClawPoint3D);//将携带托盘的机械臂移动至加热模块上方 deviceCommandUtilService.heaterMotorMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatModuleId, trayLift);//抬升加热位托盘 deviceCommandUtilService.gantryZMove(heatModuleTrayMoveHeight);//下降z轴,使托盘落入加热模块 deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawTrayPick);//将夹爪打开,释放托盘 deviceStateService.setHeatModuleStateTrayStatus(heatModuleId, 1);//加热模块是否存在托盘 deviceCommandUtilService.gantryZMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), 0);//抬升z轴 + +// deviceCommandUtilService.capUpBalance(cmdDTO.getCommandId(), cmdDTO.getCommand());//提升拍子存放区至拍子夹取的高度 deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), capStorageCapClawPoint3D);//移动机械臂至拍子存放区上方 deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawCapPick);//将夹爪打开,准备夹取拍子 deviceCommandUtilService.gantryZMove(capModuleCapMoveHeight);//下降z轴,使夹爪落入拍子升降模块拍子孔位 deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawCapGrip);//将夹爪收紧,夹住拍子 deviceCommandUtilService.gantryZMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), 0);//抬升z轴 - deviceCommandUtilService.capUpBalance(cmdDTO.getCommandId(), cmdDTO.getCommand()); //提升拍子存放区 - deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatAreaTrayClawPoint3D);//将携带拍子的机械臂移动至加热模块上方 + deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatAreaCapClawPointPoint3D);//将携带拍子的机械臂移动至加热模块拍子上方 deviceCommandUtilService.gantryZMove(heatModuleCapMoveHeight);//下降z轴,使夹拍子落入加热模块 deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawCapPick);//将夹爪打开,释放拍子 deviceStateService.setHeatModuleStateCapExist(heatModuleId, true);//加热模块是否存在拍子 deviceCommandUtilService.gantryZMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), 0);//抬升z轴 - trayState.setHeatModuleId(heatModuleId); - trayState.setInHeatModule(true); +// trayState.setHeatModuleId(heatModuleId); +// trayState.setInHeatModule(true); gantryArmService.setLiquidIdleTrue();//释放加液区等待 deviceCommandUtilService.heaterMotorMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatModuleId, trayLower);//下降加热模块托盘 deviceStateService.setHeatModuleStateTrayUp(heatModuleId, 0);//加热模块托盘升降状态 - deviceCommandUtilService.gantryMoveZero();//将机械臂移动至0点 +// deviceCommandUtilService.capUpBalance(cmdDTO.getCommandId(), cmdDTO.getCommand()); //提升拍子存放区 + deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), capStorageCapClawPoint3D);//移动机械臂至拍子存放区上方 + +// deviceCommandUtilService.capUpBalance(cmdDTO.getCommandId(), cmdDTO.getCommand());//提升拍子存放区至拍子夹取的高度 +// deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), liquidAreaTrayPoint3D); //将机械臂移动至加液模块上方 +// deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawTrayPick);//将夹爪打开,准备夹取托盘 +// deviceCommandUtilService.gantryZMove(solutionModuleTrayMoveHeight);//下降z轴,使夹爪落入托盘孔位 +// deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawTrayGrip);//将夹爪收紧,夹住托盘 +// deviceCommandUtilService.gantryZMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), 0);//抬升z轴 +// deviceStateService.setSolutionModuleStateTrayStatus(0);//加液模块是否有托盘 +// deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatAreaTrayClawPoint3D);//将携带托盘的机械臂移动至加热模块上方 +// deviceCommandUtilService.heaterMotorMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatModuleId, trayLift);//抬升加热位托盘 +// deviceCommandUtilService.gantryZMove(heatModuleTrayMoveHeight);//下降z轴,使托盘落入加热模块 +// deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawTrayPick);//将夹爪打开,释放托盘 +// deviceStateService.setHeatModuleStateTrayStatus(heatModuleId, 1);//加热模块是否存在托盘 +// deviceCommandUtilService.gantryZMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), 0);//抬升z轴 +// deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), capStorageCapClawPoint3D);//移动机械臂至拍子存放区上方 +// deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawCapPick);//将夹爪打开,准备夹取拍子 +// deviceCommandUtilService.gantryZMove(capModuleCapMoveHeight);//下降z轴,使夹爪落入拍子升降模块拍子孔位 +// deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawCapGrip);//将夹爪收紧,夹住拍子 +// deviceCommandUtilService.gantryZMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), 0);//抬升z轴 +// deviceCommandUtilService.capUpBalance(cmdDTO.getCommandId(), cmdDTO.getCommand()); //提升拍子存放区 +// deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatAreaTrayClawPoint3D);//将携带拍子的机械臂移动至加热模块上方 +// deviceCommandUtilService.gantryZMove(heatModuleCapMoveHeight);//下降z轴,使夹拍子落入加热模块 +// deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawCapPick);//将夹爪打开,释放拍子 +// deviceStateService.setHeatModuleStateCapExist(heatModuleId, true);//加热模块是否存在拍子 +// deviceCommandUtilService.gantryZMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), 0);//抬升z轴 +// trayState.setHeatModuleId(heatModuleId); +// trayState.setInHeatModule(true); +// gantryArmService.setLiquidIdleTrue();//释放加液区等待 +// deviceCommandUtilService.heaterMotorMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatModuleId, trayLower);//下降加热模块托盘 +// deviceStateService.setHeatModuleStateTrayUp(heatModuleId, 0);//加热模块托盘升降状态 +// deviceCommandUtilService.gantryMoveZero();//将机械臂移动至0点 } finally { deviceStateService.setGantryArmStateIdle(true); - isExecuting.set(false); + deviceStateService.getCommandState().get().setMoveToHeatAreaCommandExecuting(false); } }); } diff --git a/src/main/java/com/iflytop/gd/app/cmd/MoveToSolutionAreaCommand.java b/src/main/java/com/iflytop/gd/app/cmd/MoveToSolutionAreaCommand.java index 33bbf52..5997ac1 100644 --- a/src/main/java/com/iflytop/gd/app/cmd/MoveToSolutionAreaCommand.java +++ b/src/main/java/com/iflytop/gd/app/cmd/MoveToSolutionAreaCommand.java @@ -7,7 +7,6 @@ import com.iflytop.gd.app.model.dto.CmdDTO; import com.iflytop.gd.app.service.DeviceCommandUtilService; import com.iflytop.gd.app.service.DevicePositionService; import com.iflytop.gd.app.service.DeviceStateService; -import com.iflytop.gd.app.service.GantryArmService; import com.iflytop.gd.common.annotation.CommandMapping; import com.iflytop.gd.common.enums.HeatModuleCode; import com.iflytop.gd.common.enums.data.DevicePositionCode; @@ -18,7 +17,6 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import java.util.concurrent.CompletableFuture; -import java.util.concurrent.atomic.AtomicBoolean; /** * 移至加液 @@ -30,15 +28,16 @@ import java.util.concurrent.atomic.AtomicBoolean; public class MoveToSolutionAreaCommand extends BaseCommandHandler { private final DeviceCommandUtilService deviceCommandUtilService; private final DevicePositionService devicePositionService; - private final GantryArmService gantryArmService; private final DeviceStateService deviceStateService; - private final AtomicBoolean isExecuting = new AtomicBoolean(false); @Override public CompletableFuture handle(CmdDTO cmdDTO) throws Exception { - if (isExecuting.get()) { + if (deviceStateService.getCommandState().get().isMoveToSolutionAreaCommandExecuting()) { throw new AppException(ResultCode.COMMAND_ALREADY_EXECUTING); } + if (deviceStateService.getCommandState().get().isMoveToHeatAreaCommandExecuting()) { + throw new AppException(ResultCode.CMD_BUSY); + } String heatId = cmdDTO.getStringParam("heatId"); HeatModuleCode heatModuleId = HeatModuleCode.valueOf(heatId); double clawCapPick = devicePositionService.getPosition(DevicePositionCode.clawCapPick).getDistance(); //获取拍子夹取距离 @@ -54,58 +53,94 @@ public class MoveToSolutionAreaCommand extends BaseCommandHandler { Point3D liquidAreaTrayPoint3D = devicePositionService.getPosition(DevicePositionCode.liquidAreaTrayPoint).getPoint3D();//获取加液模块上方点位 Point3D heatAreaTrayClawPoint3D = deviceCommandUtilService.getHeatAreaTrayClawPoint3D(heatModuleId);//获取指定加热模块上方点位 + Point3D heatArea4TrayClawPoint3D = deviceCommandUtilService.getHeatAreaTrayClawPoint3D(HeatModuleCode.heat_module_04);//获取指定加热模块上方点位 + Point3D heatAreaCapClawPointPoint3D = deviceCommandUtilService.getHeatAreaCapClawPointPoint3D(heatModuleId);//获取指定加热模块拍子上方点位 Point3D capStorageCapClawPoint3D = devicePositionService.getPosition(DevicePositionCode.capStorageCapClawPoint).getPoint3D();//获取拍子存放区上方点位; //校验目标加热位是否有托盘 try { - deviceStateService.setGantryArmStateIdle(false); - isExecuting.set(true); - Boolean heatModuleTray = deviceCommandUtilService.heatModuleSensorState(heatModuleId); - if (!heatModuleTray) { - throw new AppException(ResultCode.TARGET_HEAT_MODULE_OCCUPIED); - } - } finally { - deviceStateService.setGantryArmStateIdle(true); - isExecuting.set(false); + deviceStateService.getCommandState().get().setMoveToSolutionAreaCommandExecuting(true); +// Boolean heatModuleTray = deviceCommandUtilService.heatModuleSensorState(heatModuleId); +// if (!heatModuleTray) { +// throw new AppException(ResultCode.TARGET_HEAT_MODULE_OCCUPIED); +// } + } catch (Exception e) { + deviceStateService.getCommandState().get().setMoveToSolutionAreaCommandExecuting(false); + throw e; } return runAsync(() -> { try { TrayState trayState = deviceStateService.getTrayInSolutionModule(); // gantryArmService.waitLiquidIdle();//等待加液区空闲 // gantryArmService.setLiquidIdleFalse();//锁定加液区 - deviceCommandUtilService.capUpBalance(cmdDTO.getCommandId(), cmdDTO.getCommand()); //提升拍子存放区至拍子夹取的高度 + +// deviceCommandUtilService.capUpBalance(cmdDTO.getCommandId(), cmdDTO.getCommand()); //提升拍子存放区至拍子夹取的高度 + deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatAreaCapClawPointPoint3D);//将机械臂移动至加热模块拍子上方 deviceCommandUtilService.heaterMotorMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatModuleId, trayLift);//抬升指定加热位托盘 - deviceStateService.setHeatModuleStateTrayUp(heatModuleId, 1);//将加热模块托盘状态改为抬起 - deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatAreaTrayClawPoint3D);//将机械臂移动至加热模块上方 deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawCapPick);//将夹爪打开,准备夹取拍子 deviceCommandUtilService.gantryZMove(heatModuleCapMoveHeight);//下降z轴,使夹爪落入加热模块拍子孔位 deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawCapGrip);//将夹爪收紧,夹住拍子 deviceStateService.setHeatModuleStateCapExist(heatModuleId, false);//加热模块是否存在拍子 deviceCommandUtilService.gantryZMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), 0);//抬升z轴 +// deviceCommandUtilService.capMotorMoveByNum(-1);//拍子存放模块下降1个拍子位置 deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), capStorageCapClawPoint3D);//将携带拍子的机械臂移动至存放区上方 - deviceCommandUtilService.capMotorMoveByNum(-1);//拍子存放模块下降1个拍子位置 deviceCommandUtilService.gantryZMove(capModuleCapMoveHeight);//下降z轴,使夹拍子落入存放区 deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawCapPick);//将夹爪打开,释放夹取的拍子 deviceCommandUtilService.gantryZMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), 0);//抬升z轴 + deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatAreaTrayClawPoint3D);//将机械臂移动至加热模块上方 + deviceStateService.setHeatModuleStateTrayUp(heatModuleId, 1);//将加热模块托盘状态改为抬起 deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawTrayPick);//将夹爪打开,准备夹取托盘 deviceCommandUtilService.gantryZMove(heatModuleTrayMoveHeight);//下降z轴,使夹爪落入托盘孔位 deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawTrayGrip);//将夹爪收紧,夹住托盘 deviceStateService.setHeatModuleStateTrayStatus(heatModuleId, 0);//加热模块是否存在托盘 - trayState.setInHeatModule(false);//托盘是否在加热模块中 +// trayState.setInHeatModule(false);//托盘是否在加热模块中 deviceCommandUtilService.gantryZMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), 0);//抬升z轴 + deviceCommandUtilService.heaterMotorMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatModuleId, trayLower);//下降加热模块托盘 + deviceStateService.setHeatModuleStateTrayUp(heatModuleId, 0);//加热模块托盘升降状态 + deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatArea4TrayClawPoint3D);//将携带托盘的机械臂移动至4号加热模块上方 deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), liquidAreaTrayPoint3D); //将携带托盘的机械臂移动至加液模块上方 deviceCommandUtilService.gantryZMove(solutionModuleTrayMoveHeight);//下降z轴,使托盘落入加液模块 - deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawTrayPick);//将夹爪打开,释放托盘 - trayState.setInSolutionModule(true);//托盘是否在加液模块中 +// trayState.setInSolutionModule(true);//托盘是否在加液模块中 deviceStateService.setSolutionModuleStateTrayStatus(1);//加液模块是否存在托盘 + deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawTrayPick);//将夹爪打开,释放托盘 deviceCommandUtilService.gantryZMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), 0);//抬升z轴 - deviceCommandUtilService.heaterMotorMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatModuleId, trayLower);//下降加热模块托盘 - deviceCommandUtilService.gantryMoveZero();//将机械臂移动至0点 + deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), capStorageCapClawPoint3D);//移动机械臂至拍子存放区上方 + + +// deviceCommandUtilService.capUpBalance(cmdDTO.getCommandId(), cmdDTO.getCommand()); //提升拍子存放区至拍子夹取的高度 +// deviceCommandUtilService.heaterMotorMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatModuleId, trayLift);//抬升指定加热位托盘 +// deviceStateService.setHeatModuleStateTrayUp(heatModuleId, 1);//将加热模块托盘状态改为抬起 +// deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatAreaTrayClawPoint3D);//将机械臂移动至加热模块上方 +// deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawCapPick);//将夹爪打开,准备夹取拍子 +// deviceCommandUtilService.gantryZMove(heatModuleCapMoveHeight);//下降z轴,使夹爪落入加热模块拍子孔位 +// deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawCapGrip);//将夹爪收紧,夹住拍子 +// deviceStateService.setHeatModuleStateCapExist(heatModuleId, false);//加热模块是否存在拍子 +// deviceCommandUtilService.gantryZMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), 0);//抬升z轴 +// deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), capStorageCapClawPoint3D);//将携带拍子的机械臂移动至存放区上方 +// deviceCommandUtilService.capMotorMoveByNum(-1);//拍子存放模块下降1个拍子位置 +// deviceCommandUtilService.gantryZMove(capModuleCapMoveHeight);//下降z轴,使夹拍子落入存放区 +// deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawCapPick);//将夹爪打开,释放夹取的拍子 +// deviceCommandUtilService.gantryZMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), 0);//抬升z轴 +// deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatAreaTrayClawPoint3D);//将机械臂移动至加热模块上方 +// deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawTrayPick);//将夹爪打开,准备夹取托盘 +// deviceCommandUtilService.gantryZMove(heatModuleTrayMoveHeight);//下降z轴,使夹爪落入托盘孔位 +// deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawTrayGrip);//将夹爪收紧,夹住托盘 +// deviceStateService.setHeatModuleStateTrayStatus(heatModuleId, 0);//加热模块是否存在托盘 +// trayState.setInHeatModule(false);//托盘是否在加热模块中 +// deviceCommandUtilService.gantryZMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), 0);//抬升z轴 +// deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), liquidAreaTrayPoint3D); //将携带托盘的机械臂移动至加液模块上方 +// deviceCommandUtilService.gantryZMove(solutionModuleTrayMoveHeight);//下降z轴,使托盘落入加液模块 +// deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawTrayPick);//将夹爪打开,释放托盘 +// trayState.setInSolutionModule(true);//托盘是否在加液模块中 +// deviceStateService.setSolutionModuleStateTrayStatus(1);//加液模块是否存在托盘 +// deviceCommandUtilService.gantryZMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), 0);//抬升z轴 +// deviceCommandUtilService.heaterMotorMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatModuleId, trayLower);//下降加热模块托盘 +// deviceCommandUtilService.gantryMoveZero();//将机械臂移动至0点 } finally { deviceStateService.setGantryArmStateIdle(true); - isExecuting.set(false); + deviceStateService.getCommandState().get().setMoveToSolutionAreaCommandExecuting(false); } }); } diff --git a/src/main/java/com/iflytop/gd/app/cmd/ShakeStartCommand.java b/src/main/java/com/iflytop/gd/app/cmd/ShakeStartCommand.java index edbea6b..408ead8 100644 --- a/src/main/java/com/iflytop/gd/app/cmd/ShakeStartCommand.java +++ b/src/main/java/com/iflytop/gd/app/cmd/ShakeStartCommand.java @@ -5,6 +5,8 @@ import com.iflytop.gd.app.model.dto.CmdDTO; import com.iflytop.gd.app.service.DeviceCommandUtilService; import com.iflytop.gd.app.service.DeviceStateService; import com.iflytop.gd.common.annotation.CommandMapping; +import com.iflytop.gd.common.exception.AppException; +import com.iflytop.gd.common.result.ResultCode; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -24,6 +26,10 @@ public class ShakeStartCommand extends BaseCommandHandler { @Override public CompletableFuture handle(CmdDTO cmdDTO) { + if (deviceStateService.getCommandState().get().isMoveToHeatAreaCommandExecuting() + || deviceStateService.getCommandState().get().isMoveToSolutionAreaCommandExecuting()) { + throw new AppException(ResultCode.CMD_BUSY); + } return runAsync(() -> { deviceCommandUtilService.shakeStart(cmdDTO.getCommandId(), cmdDTO.getCommand());//开始摇匀 deviceStateService.setSolutionModuleStateShaking(true); diff --git a/src/main/java/com/iflytop/gd/app/cmd/ShakeStopCommand.java b/src/main/java/com/iflytop/gd/app/cmd/ShakeStopCommand.java index dc40777..65fef10 100644 --- a/src/main/java/com/iflytop/gd/app/cmd/ShakeStopCommand.java +++ b/src/main/java/com/iflytop/gd/app/cmd/ShakeStopCommand.java @@ -5,6 +5,8 @@ import com.iflytop.gd.app.model.dto.CmdDTO; import com.iflytop.gd.app.service.DeviceCommandUtilService; import com.iflytop.gd.app.service.DeviceStateService; import com.iflytop.gd.common.annotation.CommandMapping; +import com.iflytop.gd.common.exception.AppException; +import com.iflytop.gd.common.result.ResultCode; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -25,7 +27,12 @@ public class ShakeStopCommand extends BaseCommandHandler { @Override public CompletableFuture handle(CmdDTO cmdDTO) { return runAsync(() -> { + if (deviceStateService.getCommandState().get().isMoveToHeatAreaCommandExecuting() + || deviceStateService.getCommandState().get().isMoveToSolutionAreaCommandExecuting()) { + throw new AppException(ResultCode.CMD_BUSY); + } deviceCommandUtilService.shakeStop(cmdDTO.getCommandId(), cmdDTO.getCommand());//停止摇匀 + deviceCommandUtilService.shakeOrigin(cmdDTO.getCommandId(), cmdDTO.getCommand());//摇匀回原点 deviceStateService.setSolutionModuleStateShaking(false); }); } diff --git a/src/main/java/com/iflytop/gd/app/cmd/TrayDownCommand.java b/src/main/java/com/iflytop/gd/app/cmd/TrayDownCommand.java index 951cf60..b81ff28 100644 --- a/src/main/java/com/iflytop/gd/app/cmd/TrayDownCommand.java +++ b/src/main/java/com/iflytop/gd/app/cmd/TrayDownCommand.java @@ -9,6 +9,8 @@ import com.iflytop.gd.app.service.DeviceStateService; import com.iflytop.gd.common.annotation.CommandMapping; import com.iflytop.gd.common.enums.HeatModuleCode; import com.iflytop.gd.common.enums.data.DevicePositionCode; +import com.iflytop.gd.common.exception.AppException; +import com.iflytop.gd.common.result.ResultCode; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -29,16 +31,26 @@ public class TrayDownCommand extends BaseCommandHandler { @Override public CompletableFuture handle(CmdDTO cmdDTO) { + if (deviceStateService.getCommandState().get().isMoveToHeatAreaCommandExecuting() + || deviceStateService.getCommandState().get().isMoveToSolutionAreaCommandExecuting()) { + throw new AppException(ResultCode.CMD_BUSY); + } JSONArray heatIdJsonArray = cmdDTO.getJSONArrayParam("heatId"); + //从数据库获取加热位下降托盘位置 + double trayLower = devicePositionService.getPosition(DevicePositionCode.trayLower).getDistance(); return runAsync(() -> { for (int i = 0; i < heatIdJsonArray.size(); i++) { String heatId = heatIdJsonArray.getStr(i); HeatModuleCode heatModuleId = HeatModuleCode.valueOf(heatId); - //从数据库获取加热位下降托盘位置 - double trayLower = devicePositionService.getPosition(DevicePositionCode.trayLower).getDistance(); - //下降加热位托盘 - deviceCommandUtilService.heaterMotorMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatModuleId, trayLower); - deviceStateService.setHeatModuleStateTrayUp(heatModuleId, 0); + CompletableFuture.runAsync(() -> { + try { + //下降加热位托盘 + deviceCommandUtilService.heaterMotorMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatModuleId, trayLower); + deviceStateService.setHeatModuleStateTrayUp(heatModuleId, 0); + } catch (Exception e) { + log.error("降下托盘失败", e); + } + }); } }); } diff --git a/src/main/java/com/iflytop/gd/app/cmd/TrayUpCommand.java b/src/main/java/com/iflytop/gd/app/cmd/TrayUpCommand.java index c8f477a..afe8c4b 100644 --- a/src/main/java/com/iflytop/gd/app/cmd/TrayUpCommand.java +++ b/src/main/java/com/iflytop/gd/app/cmd/TrayUpCommand.java @@ -7,8 +7,11 @@ import com.iflytop.gd.app.service.DeviceCommandUtilService; import com.iflytop.gd.app.service.DevicePositionService; import com.iflytop.gd.app.service.DeviceStateService; import com.iflytop.gd.common.annotation.CommandMapping; +import com.iflytop.gd.common.enums.AcidPumpDeviceCode; import com.iflytop.gd.common.enums.HeatModuleCode; import com.iflytop.gd.common.enums.data.DevicePositionCode; +import com.iflytop.gd.common.exception.AppException; +import com.iflytop.gd.common.result.ResultCode; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -26,19 +29,29 @@ public class TrayUpCommand extends BaseCommandHandler { private final DeviceCommandUtilService deviceCommandUtilService; private final DevicePositionService devicePositionService; private final DeviceStateService deviceStateService; - @Override public CompletableFuture handle(CmdDTO cmdDTO) { + if (deviceStateService.getCommandState().get().isMoveToHeatAreaCommandExecuting() + || deviceStateService.getCommandState().get().isMoveToSolutionAreaCommandExecuting()) { + throw new AppException(ResultCode.CMD_BUSY); + } JSONArray heatIdJsonArray = cmdDTO.getJSONArrayParam("heatId"); + //获取加热位抬升托盘位置 + double trayLift = devicePositionService.getPosition(DevicePositionCode.trayLift).getDistance(); return runAsync(() -> { for (int i = 0; i < heatIdJsonArray.size(); i++) { String heatId = heatIdJsonArray.getStr(i); HeatModuleCode heatModuleId = HeatModuleCode.valueOf(heatId); - //获取加热位抬升托盘位置 - double trayLift = devicePositionService.getPosition(DevicePositionCode.trayLift).getDistance(); - //抬升加热位托盘 - deviceCommandUtilService.heaterMotorMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatModuleId, trayLift); - deviceStateService.setHeatModuleStateTrayUp(heatModuleId, 1); + + CompletableFuture.runAsync(() -> { + try { + //抬升加热位托盘 + deviceCommandUtilService.heaterMotorMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatModuleId, trayLift); + deviceStateService.setHeatModuleStateTrayUp(heatModuleId, 1); + } catch (Exception e) { + log.error("抬起托盘失败", e); + } + }); } }); } diff --git a/src/main/java/com/iflytop/gd/app/cmd/debug/step/DebugCapInHeatAreaCommand.java b/src/main/java/com/iflytop/gd/app/cmd/debug/step/DebugCapInHeatAreaCommand.java index 5d180c1..0ece5d1 100644 --- a/src/main/java/com/iflytop/gd/app/cmd/debug/step/DebugCapInHeatAreaCommand.java +++ b/src/main/java/com/iflytop/gd/app/cmd/debug/step/DebugCapInHeatAreaCommand.java @@ -40,19 +40,20 @@ public class DebugCapInHeatAreaCommand extends BaseCommandHandler { Point3D capStorageCapClawPoint3D = devicePositionService.getPosition(DevicePositionCode.capStorageCapClawPoint).getPoint3D();//获取拍子存放区上方点位; return runAsync(() -> { - deviceCommandUtilService.capUpBalance(cmdDTO.getCommandId(), cmdDTO.getCommand());//提升拍子存放区至拍子夹取的高度 - deviceCommandUtilService.heaterMotorMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatModuleId, trayLift);//抬升加热模块托盘 +// deviceCommandUtilService.capUpBalance(cmdDTO.getCommandId(), cmdDTO.getCommand());//提升拍子存放区至拍子夹取的高度 deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), capStorageCapClawPoint3D);//移动机械臂至拍子存放区上方 + deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawCapPick);//将夹爪打开,准备夹取拍子 deviceCommandUtilService.gantryZMove(capModuleCapMoveHeight);//下降z轴,使夹爪落入拍子升降模块拍子孔位 deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawCapGrip);//将夹爪收紧,夹住拍子 deviceCommandUtilService.gantryZMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), 0);//抬升z轴 deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatAreaCapClawPointPoint3D);//将携带拍子的机械臂移动至加热模块拍子上方 + deviceCommandUtilService.heaterMotorMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatModuleId, trayLift);//抬升加热模块托盘 deviceCommandUtilService.gantryZMove(heatModuleCapMoveHeight);//下降z轴,使夹拍子落入加热模块 deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawCapPick);//将夹爪打开,释放拍子 deviceCommandUtilService.gantryZMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), 0);//抬升z轴 deviceCommandUtilService.heaterMotorMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatModuleId, trayLower);//下降加热模块托盘 - deviceCommandUtilService.capUpBalance(cmdDTO.getCommandId(), cmdDTO.getCommand()); //提升拍子存放区 +// deviceCommandUtilService.capUpBalance(cmdDTO.getCommandId(), cmdDTO.getCommand()); //提升拍子存放区 }); } } diff --git a/src/main/java/com/iflytop/gd/app/cmd/debug/step/DebugCapOutHeatAreaCommand.java b/src/main/java/com/iflytop/gd/app/cmd/debug/step/DebugCapOutHeatAreaCommand.java index 8e8827a..d8db3e1 100644 --- a/src/main/java/com/iflytop/gd/app/cmd/debug/step/DebugCapOutHeatAreaCommand.java +++ b/src/main/java/com/iflytop/gd/app/cmd/debug/step/DebugCapOutHeatAreaCommand.java @@ -40,14 +40,14 @@ public class DebugCapOutHeatAreaCommand extends BaseCommandHandler { Point3D capStorageCapClawPoint3D = devicePositionService.getPosition(DevicePositionCode.capStorageCapClawPoint).getPoint3D();//获取拍子存放区上方点位; return runAsync(() -> { - deviceCommandUtilService.capUpBalance(cmdDTO.getCommandId(), cmdDTO.getCommand()); //提升拍子存放区至拍子夹取的高度 - deviceCommandUtilService.heaterMotorMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatModuleId, trayLift);//抬升指定加热位托盘 +// deviceCommandUtilService.capUpBalance(cmdDTO.getCommandId(), cmdDTO.getCommand()); //提升拍子存放区至拍子夹取的高度 deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatAreaCapClawPointPoint3D);//将机械臂移动至加热模块拍子上方 + deviceCommandUtilService.heaterMotorMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatModuleId, trayLift);//抬升指定加热位托盘 deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawCapPick);//将夹爪打开,准备夹取拍子 deviceCommandUtilService.gantryZMove(heatModuleCapMoveHeight);//下降z轴,使夹爪落入加热模块拍子孔位 deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawCapGrip);//将夹爪收紧,夹住拍子 deviceCommandUtilService.gantryZMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), 0);//抬升z轴 - deviceCommandUtilService.capMotorMoveByNum(-1);//拍子存放模块下降1个拍子位置 +// deviceCommandUtilService.capMotorMoveByNum(-1);//拍子存放模块下降1个拍子位置 deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), capStorageCapClawPoint3D);//将携带拍子的机械臂移动至存放区上方 deviceCommandUtilService.gantryZMove(capModuleCapMoveHeight);//下降z轴,使夹拍子落入存放区 deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawCapPick);//将夹爪打开,释放夹取的拍子 diff --git a/src/main/java/com/iflytop/gd/app/cmd/debug/step/DebugMoveTrayToHeatAreaCommand.java b/src/main/java/com/iflytop/gd/app/cmd/debug/step/DebugMoveTrayToHeatAreaCommand.java index f4d1b12..f9d7965 100644 --- a/src/main/java/com/iflytop/gd/app/cmd/debug/step/DebugMoveTrayToHeatAreaCommand.java +++ b/src/main/java/com/iflytop/gd/app/cmd/debug/step/DebugMoveTrayToHeatAreaCommand.java @@ -41,7 +41,6 @@ public class DebugMoveTrayToHeatAreaCommand extends BaseCommandHandler { Point3D heatArea4TrayClawPoint3D = deviceCommandUtilService.getHeatAreaTrayClawPoint3D(HeatModuleCode.heat_module_04);//获取指定加热模块上方点位 return runAsync(() -> { - deviceCommandUtilService.heaterMotorMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatModuleId, trayLift);//抬升指定加热位托盘 deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), liquidAreaTrayPoint3D); //将机械臂移动至加液模块上方 deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawTrayPick);//将夹爪打开,准备夹取托盘 deviceCommandUtilService.gantryZMove(solutionModuleTrayMoveHeight);//下降z轴,使夹爪落入托盘孔位 diff --git a/src/main/java/com/iflytop/gd/app/cmd/debug/step/DebugMoveTrayToSolutionAreaCommand.java b/src/main/java/com/iflytop/gd/app/cmd/debug/step/DebugMoveTrayToSolutionAreaCommand.java index 41833dd..0a8cad5 100644 --- a/src/main/java/com/iflytop/gd/app/cmd/debug/step/DebugMoveTrayToSolutionAreaCommand.java +++ b/src/main/java/com/iflytop/gd/app/cmd/debug/step/DebugMoveTrayToSolutionAreaCommand.java @@ -41,8 +41,8 @@ public class DebugMoveTrayToSolutionAreaCommand extends BaseCommandHandler { Point3D heatArea4TrayClawPoint3D = deviceCommandUtilService.getHeatAreaTrayClawPoint3D(HeatModuleCode.heat_module_04);//获取指定加热模块上方点位 return runAsync(() -> { - deviceCommandUtilService.heaterMotorMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatModuleId, trayLift);//抬升指定加热位托盘 deviceCommandUtilService.gantryMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatAreaTrayClawPoint3D);//将机械臂移动至加热模块上方 + deviceCommandUtilService.heaterMotorMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), heatModuleId, trayLift);//抬升指定加热位托盘 deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawTrayPick);//将夹爪打开,准备夹取托盘 deviceCommandUtilService.gantryZMove(heatModuleTrayMoveHeight);//下降z轴,使夹爪落入托盘孔位 deviceCommandUtilService.clawMove(cmdDTO.getCommandId(), cmdDTO.getCommand(), clawTrayGrip);//将夹爪收紧,夹住托盘 diff --git a/src/main/java/com/iflytop/gd/app/config/AsyncConfig.java b/src/main/java/com/iflytop/gd/app/config/AsyncConfig.java new file mode 100644 index 0000000..b410ba7 --- /dev/null +++ b/src/main/java/com/iflytop/gd/app/config/AsyncConfig.java @@ -0,0 +1,21 @@ +package com.iflytop.gd.app.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +@Configuration +public class AsyncConfig { + @Bean("commandTaskExecutor") + public ThreadPoolTaskExecutor taskExecutor() { + ThreadPoolTaskExecutor exec = new ThreadPoolTaskExecutor(); + exec.setCorePoolSize(10); + exec.setMaxPoolSize(20); + exec.setQueueCapacity(50); + exec.setThreadNamePrefix("cmd-exec-"); + exec.setWaitForTasksToCompleteOnShutdown(true); + exec.setAwaitTerminationSeconds(30); + exec.initialize(); + return exec; + } +} diff --git a/src/main/java/com/iflytop/gd/app/controller/CmdController.java b/src/main/java/com/iflytop/gd/app/controller/CmdController.java index 6274eb8..84f0abc 100644 --- a/src/main/java/com/iflytop/gd/app/controller/CmdController.java +++ b/src/main/java/com/iflytop/gd/app/controller/CmdController.java @@ -4,10 +4,13 @@ import cn.hutool.json.JSONUtil; import com.iflytop.gd.app.core.CommandHandlerRegistry; import com.iflytop.gd.app.core.DebugGenerator; import com.iflytop.gd.app.model.dto.CmdDTO; +import com.iflytop.gd.app.service.DeviceStateService; import com.iflytop.gd.app.service.WebSocketService; import com.iflytop.gd.common.cmd.CommandHandler; import com.iflytop.gd.common.constant.CommandStatus; +import com.iflytop.gd.common.exception.AppException; import com.iflytop.gd.common.result.Result; +import com.iflytop.gd.common.result.ResultCode; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; @@ -28,6 +31,7 @@ import java.util.concurrent.CompletableFuture; public class CmdController { private final CommandHandlerRegistry registry; private final WebSocketService webSocketService; + private final DeviceStateService deviceStateService; @Operation(summary = "前端统一调用一个接口") @PostMapping @@ -36,6 +40,9 @@ public class CmdController { String command = cmdDTO.getCommand(); try { webSocketService.pushDebug(DebugGenerator.generateJson(commandId, command, CommandStatus.RECEIVE, "已收到业务指令请求,开始处理")); + if (deviceStateService.getDeviceState().isEmergencyStop()) { + throw new AppException(ResultCode.EMERGENCY_STOP); + } CommandHandler commandHandler = registry.getCommandHandler(command); if (commandHandler == null) { webSocketService.pushDebug(DebugGenerator.generateJson(commandId, command, CommandStatus.DEVICE_ERROR, "未找到对应的业务指令")); diff --git a/src/main/java/com/iflytop/gd/app/controller/DevicePointController.java b/src/main/java/com/iflytop/gd/app/controller/DevicePointController.java index d537955..79c7673 100644 --- a/src/main/java/com/iflytop/gd/app/controller/DevicePointController.java +++ b/src/main/java/com/iflytop/gd/app/controller/DevicePointController.java @@ -1,5 +1,6 @@ package com.iflytop.gd.app.controller; +import com.iflytop.gd.app.model.dto.AddDevicePositionDTO; import com.iflytop.gd.app.model.entity.DevicePosition; import com.iflytop.gd.app.model.vo.DevicePositionVO; import com.iflytop.gd.app.service.DevicePositionService; @@ -42,9 +43,8 @@ public class DevicePointController { @Operation(summary = "修改") @PutMapping("") - public Result update(@Valid @RequestBody DevicePosition devicePoint) { - devicePointService.updateById(devicePoint); - return Result.success(); + public Result update(@Valid @RequestBody AddDevicePositionDTO addDevicePositionDTO) { + return Result.success(devicePointService.updatePosition(addDevicePositionDTO)); } @Operation(summary = "删除") diff --git a/src/main/java/com/iflytop/gd/app/controller/TasksController.java b/src/main/java/com/iflytop/gd/app/controller/TasksController.java index b405e99..20fa18b 100644 --- a/src/main/java/com/iflytop/gd/app/controller/TasksController.java +++ b/src/main/java/com/iflytop/gd/app/controller/TasksController.java @@ -57,7 +57,7 @@ public class TasksController { @PostMapping("/") public Result start(@RequestBody TaskDTO dto) { if (tasksService.getCurrent() != null) { - return Result.failed("存在正在运行的实验,请先停止"); + return Result.failed("存在正在运行的实验,请先终止"); } return Result.success(tasksService.start(dto.getName())); } diff --git a/src/main/java/com/iflytop/gd/app/core/BaseCommandHandler.java b/src/main/java/com/iflytop/gd/app/core/BaseCommandHandler.java index a05ab61..4b25cdc 100644 --- a/src/main/java/com/iflytop/gd/app/core/BaseCommandHandler.java +++ b/src/main/java/com/iflytop/gd/app/core/BaseCommandHandler.java @@ -5,15 +5,22 @@ import com.iflytop.gd.common.annotation.CheckedRunnable; import com.iflytop.gd.common.cmd.CommandFuture; import com.iflytop.gd.common.cmd.CommandHandler; import com.iflytop.gd.common.utils.LambdaUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import java.util.Arrays; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; public abstract class BaseCommandHandler implements CommandHandler { + @Autowired + @Qualifier("commandTaskExecutor") + private Executor commandExecutor; + protected CompletableFuture runAsync(CheckedRunnable task) { - return CompletableFuture.runAsync(LambdaUtil.unchecked(task)); + return CompletableFuture.runAsync(LambdaUtil.unchecked(task), commandExecutor); } protected void commandWait(CommandFuture... futures) throws Exception { diff --git a/src/main/java/com/iflytop/gd/app/core/CommandPoolManager.java b/src/main/java/com/iflytop/gd/app/core/CommandPoolManager.java new file mode 100644 index 0000000..20a3181 --- /dev/null +++ b/src/main/java/com/iflytop/gd/app/core/CommandPoolManager.java @@ -0,0 +1,31 @@ +package com.iflytop.gd.app.core; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; +import org.springframework.stereotype.Component; + +import java.util.concurrent.ThreadPoolExecutor; + +@Slf4j +@Component +public class CommandPoolManager { + private final ThreadPoolTaskExecutor executor; + + public CommandPoolManager(@Qualifier("commandTaskExecutor") ThreadPoolTaskExecutor executor) { + this.executor = executor; + } + + /** + * 强制中断所有正在执行的任务 + */ + public void forceShutdownAll() { + try { + ThreadPoolExecutor nativeExecutor = executor.getThreadPoolExecutor(); + nativeExecutor.shutdownNow(); + } catch (Exception e) { + log.error("强制关闭所有cmd线程失败"); + } + } + +} diff --git a/src/main/java/com/iflytop/gd/app/core/device/CommandState.java b/src/main/java/com/iflytop/gd/app/core/device/CommandState.java new file mode 100644 index 0000000..05bb407 --- /dev/null +++ b/src/main/java/com/iflytop/gd/app/core/device/CommandState.java @@ -0,0 +1,35 @@ +package com.iflytop.gd.app.core.device; + +import lombok.Data; + +/** + * 指令互斥关系状态 + */ +@Data +public class CommandState { + private boolean capFreeCommandExecuting = false; + private boolean capLiftingOriginCommandExecuting = false; + private boolean capUsedCommandExecuting = false; + private boolean doorCloseCommandExecuting = false; + private boolean doorOpenCommandExecuting = false; + private boolean doorOriginCommandExecuting = false; + private boolean dualRobotJointOriginCommandExecuting = false; + private boolean fanStartCommandExecuting = false; + private boolean fanStopCommandExecuting = false; + private boolean filledSolutionStartCommandExecuting = false; + private boolean filledSolutionStopCommandExecuting = false; + private boolean gantryXOriginCommandExecuting = false; + private boolean gantryYOriginCommandExecuting = false; + private boolean gantryZOriginCommandExecuting = false; + private boolean heatStartCommandExecuting = false; + private boolean heatStopCommandExecuting = false; + private boolean moveToHeatAreaCommandExecuting = false; + private boolean moveToSolutionAreaCommandExecuting = false; + private boolean shakeStartCommandExecuting = false; + private boolean shakeStopCommandExecuting = false; + private boolean solutionAddCommandExecuting = false; + private boolean takePhotoCommandExecuting = false; + private boolean trayDownCommandExecuting = false; + private boolean trayLiftingOriginCommandExecuting = false; + private boolean trayUpCommandExecuting = false; +} diff --git a/src/main/java/com/iflytop/gd/app/core/device/DeviceState.java b/src/main/java/com/iflytop/gd/app/core/device/DeviceState.java index c41f8ab..2d70235 100644 --- a/src/main/java/com/iflytop/gd/app/core/device/DeviceState.java +++ b/src/main/java/com/iflytop/gd/app/core/device/DeviceState.java @@ -7,6 +7,7 @@ import lombok.Data; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.atomic.AtomicReference; @Schema(description = "当前设备") @Data diff --git a/src/main/java/com/iflytop/gd/app/model/dto/AddDevicePositionDTO.java b/src/main/java/com/iflytop/gd/app/model/dto/AddDevicePositionDTO.java new file mode 100644 index 0000000..e70d634 --- /dev/null +++ b/src/main/java/com/iflytop/gd/app/model/dto/AddDevicePositionDTO.java @@ -0,0 +1,20 @@ +package com.iflytop.gd.app.model.dto; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import lombok.Data; + + +@Data +public class AddDevicePositionDTO { + + private Long id; + + @NotNull + @NotBlank + @Schema(description = "位置数据") + private String position; + + +} \ No newline at end of file diff --git a/src/main/java/com/iflytop/gd/app/service/DevicePositionService.java b/src/main/java/com/iflytop/gd/app/service/DevicePositionService.java index d77804e..af77931 100644 --- a/src/main/java/com/iflytop/gd/app/service/DevicePositionService.java +++ b/src/main/java/com/iflytop/gd/app/service/DevicePositionService.java @@ -3,6 +3,7 @@ package com.iflytop.gd.app.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.iflytop.gd.app.mapper.DevicePositionMapper; +import com.iflytop.gd.app.model.dto.AddDevicePositionDTO; import com.iflytop.gd.app.model.entity.DevicePosition; import com.iflytop.gd.app.model.vo.DevicePositionVO; import com.iflytop.gd.common.enums.data.DevicePositionCode; @@ -36,6 +37,15 @@ public class DevicePositionService extends ServiceImpl getList() { List devicePositions = super.list(); return devicePositions.stream() diff --git a/src/main/java/com/iflytop/gd/app/service/DeviceStateService.java b/src/main/java/com/iflytop/gd/app/service/DeviceStateService.java index 802ba5d..85c6127 100644 --- a/src/main/java/com/iflytop/gd/app/service/DeviceStateService.java +++ b/src/main/java/com/iflytop/gd/app/service/DeviceStateService.java @@ -6,11 +6,14 @@ import com.iflytop.gd.app.model.entity.Tasks; import com.iflytop.gd.app.model.entity.User; import com.iflytop.gd.common.enums.ContainerCode; import com.iflytop.gd.common.enums.HeatModuleCode; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Getter; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.beans.PropertyChangeSupport; import java.util.List; +import java.util.concurrent.atomic.AtomicReference; /** * 设备状态服务 @@ -22,6 +25,9 @@ public class DeviceStateService { private final DeviceState deviceState = new DeviceState(); + @Getter + private final AtomicReference commandState = new AtomicReference<>(new CommandState()); + public synchronized DeviceState getDeviceState() { diff --git a/src/main/java/com/iflytop/gd/app/service/TasksService.java b/src/main/java/com/iflytop/gd/app/service/TasksService.java index fd499b6..dbd183c 100644 --- a/src/main/java/com/iflytop/gd/app/service/TasksService.java +++ b/src/main/java/com/iflytop/gd/app/service/TasksService.java @@ -6,12 +6,14 @@ import com.iflytop.gd.app.mapper.TasksMapper; import com.iflytop.gd.app.model.entity.TaskSteps; import com.iflytop.gd.app.model.entity.Tasks; import com.iflytop.gd.app.model.vo.TaskStepsVO; +import com.iflytop.gd.common.exception.AppException; +import com.iflytop.gd.common.result.ResultCode; +import jakarta.annotation.PostConstruct; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.util.Arrays; -import java.util.Date; import java.util.List; import java.util.stream.Collectors; @@ -24,6 +26,14 @@ public class TasksService extends ServiceImpl { private final TaskStepsService taskStepsService; private final DeviceStateService deviceStateService; + @PostConstruct + public void init() { + List tasksList = this.list(new LambdaQueryWrapper().eq(Tasks::getStatus, 1)); + if(!tasksList.isEmpty()){ + deviceStateService.getDeviceState().setCurrentTasks(tasksList.getFirst()); + } + } + public TaskStepsVO detail(Long id) { Tasks tasks = this.getById(id); List taskSteps = taskStepsService.list(new LambdaQueryWrapper<>(TaskSteps.class).eq(TaskSteps::getTaskId, id)); @@ -40,7 +50,11 @@ public class TasksService extends ServiceImpl { } public Tasks start(String taskName) { - Tasks tasks = new Tasks(); + Tasks tasks = this.getOne(new LambdaQueryWrapper().eq(Tasks::getName, taskName)); + if (tasks != null) { + throw new AppException(ResultCode.DATA_ALREADY_EXISTS); + } + tasks = new Tasks(); tasks.setName(taskName); tasks.setCreateUser(deviceStateService.getDeviceState().getCurrentUser().getId()); tasks.setStatus(1); diff --git a/src/main/java/com/iflytop/gd/app/service/WebSocketService.java b/src/main/java/com/iflytop/gd/app/service/WebSocketService.java index 269f574..9a9f30c 100644 --- a/src/main/java/com/iflytop/gd/app/service/WebSocketService.java +++ b/src/main/java/com/iflytop/gd/app/service/WebSocketService.java @@ -1,7 +1,7 @@ package com.iflytop.gd.app.service; import cn.hutool.json.JSONUtil; -import com.iflytop.gd.app.config.WebSocketServer; +import com.iflytop.gd.app.ws.server.WebSocketServer; import com.iflytop.gd.app.model.bo.Notification; import com.iflytop.gd.app.model.dto.WebsocketResult; import com.iflytop.gd.common.constant.WebSocketMessageType; diff --git a/src/main/java/com/iflytop/gd/app/ws/client/DeviceEmergencyStopClient.java b/src/main/java/com/iflytop/gd/app/ws/client/DeviceEmergencyStopClient.java new file mode 100644 index 0000000..b861ac3 --- /dev/null +++ b/src/main/java/com/iflytop/gd/app/ws/client/DeviceEmergencyStopClient.java @@ -0,0 +1,61 @@ +package com.iflytop.gd.app.ws.client; + +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import lombok.extern.slf4j.Slf4j; +import org.java_websocket.client.WebSocketClient; +import org.java_websocket.enums.ReadyState; +import org.java_websocket.handshake.ServerHandshake; +import org.springframework.scheduling.annotation.Scheduled; + +import java.net.URI; +import java.util.function.Consumer; + +/** + * WebsocketCliet监听设备发送的急停消息,实现设备的急停 + */ +@Slf4j +public class DeviceEmergencyStopClient extends WebSocketClient { + private Consumer consumer; + + public DeviceEmergencyStopClient(URI serverUri, Consumer consumer) { + super(serverUri); + this.consumer = consumer; + } + + @Override + public void onOpen(ServerHandshake handshakedata) { + log.info("device_status_listener_websocket connect success"); + } + + @Override + public void onMessage(String message) { + JSONObject jsonObject = JSONUtil.parseObj(message); + consumer.accept(jsonObject); + } + + @Override + public void onClose(int code, String reason, boolean remote) { + log.info("device_status_listener_websocket lost connection..."); + } + + @Override + public void onError(Exception ex) { + log.info("device_status_listener_websocket on error"); + } + + //失去连接后重新连接 10s轮询 + @Scheduled(fixedRate = 10000) + private void autoConnect() { + if (!isOpen()) { + if (getReadyState().equals(ReadyState.NOT_YET_CONNECTED)) { + try { + connect(); + } catch (IllegalStateException ignored) { + } + } else if (getReadyState().equals(ReadyState.CLOSED)) { + reconnect(); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/com/iflytop/gd/app/ws/client/DeviceEmergencyStopConfig.java b/src/main/java/com/iflytop/gd/app/ws/client/DeviceEmergencyStopConfig.java new file mode 100644 index 0000000..6d0f997 --- /dev/null +++ b/src/main/java/com/iflytop/gd/app/ws/client/DeviceEmergencyStopConfig.java @@ -0,0 +1,50 @@ +package com.iflytop.gd.app.ws.client; + +import com.iflytop.gd.app.core.CommandPoolManager; +import com.iflytop.gd.app.service.DeviceStateService; +import com.iflytop.gd.app.service.StepCommandService; +import jakarta.annotation.Resource; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.net.URI; + +@Slf4j +@Configuration +@RequiredArgsConstructor +public class DeviceEmergencyStopConfig { + private final StepCommandService stepCommandService; + private final DeviceStateService deviceStateService; + private final CommandPoolManager commandPoolManager; + + @Value("${iflytophald.ip}") + String ip; + @Value("${iflytophald.datach.port}") + Integer port; + + + @Bean + DeviceEmergencyStopClient stopAllClientService() { + URI uri = URI.create(String.format("ws://%s:%d/%s", ip, port, "key")); + return new DeviceEmergencyStopClient(uri, s -> { + if (s.get("event").equals("press")) { + log.info("触发急停{}", s); + try { + commandPoolManager.forceShutdownAll();//强制终止现在运行的所有指令 + stepCommandService.stopAll(); + deviceStateService.setEmergencyStop(true); + } catch (Exception e) { + log.error("设备急停失败:{}", e.getMessage()); + throw new RuntimeException(e); + } + } else if (s.get("event").equals("release")) { + deviceStateService.setEmergencyStop(false); + log.info("解除急停{}", s); + } + }); + + } +} diff --git a/src/main/java/com/iflytop/gd/app/config/WebSocketServer.java b/src/main/java/com/iflytop/gd/app/ws/server/WebSocketServer.java similarity index 97% rename from src/main/java/com/iflytop/gd/app/config/WebSocketServer.java rename to src/main/java/com/iflytop/gd/app/ws/server/WebSocketServer.java index a188d69..2281004 100644 --- a/src/main/java/com/iflytop/gd/app/config/WebSocketServer.java +++ b/src/main/java/com/iflytop/gd/app/ws/server/WebSocketServer.java @@ -1,4 +1,4 @@ -package com.iflytop.gd.app.config; +package com.iflytop.gd.app.ws.server; import jakarta.websocket.*; import jakarta.websocket.server.ServerEndpoint; diff --git a/src/main/java/com/iflytop/gd/app/config/WebSocketConfig.java b/src/main/java/com/iflytop/gd/app/ws/server/WebSocketServerConfig.java similarity index 81% rename from src/main/java/com/iflytop/gd/app/config/WebSocketConfig.java rename to src/main/java/com/iflytop/gd/app/ws/server/WebSocketServerConfig.java index 9b9d910..d6c412e 100644 --- a/src/main/java/com/iflytop/gd/app/config/WebSocketConfig.java +++ b/src/main/java/com/iflytop/gd/app/ws/server/WebSocketServerConfig.java @@ -1,11 +1,11 @@ -package com.iflytop.gd.app.config; +package com.iflytop.gd.app.ws.server; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter; @Configuration -public class WebSocketConfig { +public class WebSocketServerConfig { @Bean public ServerEndpointExporter serverEndpointExporter() { diff --git a/src/main/java/com/iflytop/gd/common/result/ResultCode.java b/src/main/java/com/iflytop/gd/common/result/ResultCode.java index 3755cb1..b036aa6 100644 --- a/src/main/java/com/iflytop/gd/common/result/ResultCode.java +++ b/src/main/java/com/iflytop/gd/common/result/ResultCode.java @@ -46,13 +46,15 @@ public enum ResultCode implements IResultCode, Serializable { EXTERNAL_API_ERROR("5002", "第三方服务调用失败"), COMMAND_EXEC_TIMEOUT("5003", "命令执行超时"), HARDWARE_ERROR("5004", "硬件错误"), + EMERGENCY_STOP("5555", "设备急停中"), //============================ 6xxx:指令相关 ============================ COMMAND_NOT_FOUND("6000", "指令未找到"), COMMAND_ALREADY_EXECUTING("6001", "指令正在执行,无法重复执行"), SENSOR_STATUS_FAILED("6010", "获取传感器状态失败"), TARGET_HEAT_MODULE_OCCUPIED("6021", "目标加热模块被占用"), TARGET_HEAT_MODULE_NO_TRAY("6022", "目标加热模块无托盘"), - CAP_LIFT_ERROR("6023", "拍子升降错误"), // 新增 cap 升降错误 + CAP_LIFT_ERROR("6023", "拍子升降错误"), + CMD_BUSY("6024", "设备忙,请稍后"), ; /** 状态码 */ private final String code; diff --git a/src/main/java/com/iflytop/gd/hardware/config/StepMotorConfig.java b/src/main/java/com/iflytop/gd/hardware/config/StepMotorConfig.java index 897b5a0..2b41fc1 100644 --- a/src/main/java/com/iflytop/gd/hardware/config/StepMotorConfig.java +++ b/src/main/java/com/iflytop/gd/hardware/config/StepMotorConfig.java @@ -83,7 +83,7 @@ public class StepMotorConfig { static final double HEATER_MOTOR_DEFAULT_MAX_LIMIT = 100.0; static final double ACID_PUMP_MOTOR_DEFAULT_MAX_LIMIT = 1000000; final static Map stepMotorMaxLimitMap = Map.ofEntries( - Map.entry(StepMotorMId.DOOR_MOTOR_MID, 400.0), + Map.entry(StepMotorMId.DOOR_MOTOR_MID, 500.0), Map.entry(StepMotorMId.SHAKE_MOTOR_MID, 20000.0), Map.entry(StepMotorMId.TRAY_MOTOR_MID, 385.0), Map.entry(StepMotorMId.HBOT_X_MOTOR_MID, 1400.0), diff --git a/src/main/resources/sql/init.sql b/src/main/resources/sql/init.sql index b95c36b..1b168a7 100644 --- a/src/main/resources/sql/init.sql +++ b/src/main/resources/sql/init.sql @@ -13,7 +13,8 @@ CREATE TABLE IF NOT EXISTS user ); INSERT OR IGNORE INTO user (username, nickname, password, role, fixed_user, deleted) -VALUES ('admin', 'Admin', '123456', 'ADMIN', 'ENABLE', 'DISABLE'); +VALUES ('admin', 'Admin', '123456', 'ADMIN', 'ENABLE', 'DISABLE'), + ('test', 'test', '123456', 'ADMIN', 'ENABLE', 'DISABLE'); -- ores 矿石 表