diff --git a/src/main/java/com/iflytop/gd/app/service/api/CraftsService.java b/src/main/java/com/iflytop/gd/app/service/api/CraftsService.java index 54f9c1d..c17fdac 100644 --- a/src/main/java/com/iflytop/gd/app/service/api/CraftsService.java +++ b/src/main/java/com/iflytop/gd/app/service/api/CraftsService.java @@ -19,6 +19,7 @@ import com.iflytop.gd.common.exception.AppException; import com.iflytop.gd.common.result.ResultCode; import jakarta.annotation.PostConstruct; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.statemachine.config.StateMachineFactory; import org.springframework.stereotype.Service; @@ -34,6 +35,7 @@ import java.util.stream.Collectors; /** * 工艺接口服务 */ +@Slf4j @Service @RequiredArgsConstructor public class CraftsService extends ServiceImpl { @@ -89,27 +91,30 @@ public class CraftsService extends ServiceImpl { */ public synchronized SetCraftsVO startCrafts(Long craftId, HeatModuleCode heatModuleCode) { Crafts craft = this.getById(craftId); - if(craft == null) { + if (craft == null) { throw new AppException(ResultCode.INVALID_PARAMETER); } Ores ores = oresService.getById(craft.getOresId()); - if(ores == null) { + if (ores == null) { throw new AppException(ResultCode.INVALID_PARAMETER); } //判断是否指定加热区id if (heatModuleCode == null) { //如果没有指定加热区id,则自动获取一个 HeatModuleState heatModuleState = heatModuleService.getIdleHeatModule(); - if(heatModuleState == null){ + if (heatModuleState == null) { + log.error("自动分配工艺失败,无空闲加热模块。"); throw new AppException(ResultCode.HEAT_MODULE_NO_IDLE); - }else{ + } else { heatModuleCode = heatModuleState.getModuleCode(); } - }else{ + } else { if (futureMap.containsKey(heatModuleCode)) {//校验指定加热模块是否正在执行工艺 + log.error("工艺正在执行中,不可重复开始执行。"); throw new AppException(ResultCode.CRAFT_RUNNING); } } + log.info("准备开始工艺,加热模块{},工艺{}", heatModuleCode, craft.getName()); //TODO 传感器校验heatModuleCode是否被占用 // 校验已有上下文状态,仅允许在 READY、STOPPED 或 FINISHED 状态下重置 diff --git a/src/main/java/com/iflytop/gd/app/service/crafts/CraftsStepService.java b/src/main/java/com/iflytop/gd/app/service/crafts/CraftsStepService.java index 6a2c719..a7260b7 100644 --- a/src/main/java/com/iflytop/gd/app/service/crafts/CraftsStepService.java +++ b/src/main/java/com/iflytop/gd/app/service/crafts/CraftsStepService.java @@ -65,7 +65,9 @@ public class CraftsStepService { */ private boolean addLiquid(HeatModuleCode heatModuleCode, JSONObject params) throws Exception { //TODO 判断加液区是否有托盘,如果没有托盘的话,判断加热区是否有托盘,然后将其移动至加液区 - moveToSolutionModule(heatModuleCode); + if(deviceStateService.getDeviceState().getSolutionModule().getTrayStatus() == 0){ + moveToSolutionModule(heatModuleCode); + } JSONArray tubeSolList = params.getJSONArray("tubeSolList"); for (int i = 0; i < tubeSolList.size(); i++) { JSONObject tubeSol = tubeSolList.getJSONObject(i); diff --git a/src/main/java/com/iflytop/gd/app/service/device/DeviceCommandTempUtilService.java b/src/main/java/com/iflytop/gd/app/service/device/DeviceCommandTempUtilService.java index 8788ada..766d0b3 100644 --- a/src/main/java/com/iflytop/gd/app/service/device/DeviceCommandTempUtilService.java +++ b/src/main/java/com/iflytop/gd/app/service/device/DeviceCommandTempUtilService.java @@ -26,6 +26,7 @@ import java.util.concurrent.CompletableFuture; @Service @RequiredArgsConstructor public class DeviceCommandTempUtilService { + private final DeviceCommandService deviceCommandService; private final HeatModuleService heatModuleService; private final DevicePositionService devicePositionService; private final DeviceStateService deviceStateService; @@ -37,7 +38,7 @@ public class DeviceCommandTempUtilService { * 临时移动托盘的时候,加热模块升降避让 * 该方法会恢复之前升起的状态 */ - public void moveTrayHeatModuleAvoidUp(HeatModuleCode targetHeatModuleCode, HeatModuleCode... exceptionHeatModuleCodes) { + public void moveTrayHeatModuleAvoidUp(HeatModuleCode targetHeatModuleCode, HeatModuleCode... exceptionHeatModuleCodes) throws Exception { moveTrayHeatModuleAvoidUp(null, null, targetHeatModuleCode, exceptionHeatModuleCodes); } @@ -46,29 +47,24 @@ public class DeviceCommandTempUtilService { * 临时移动托盘的时候,加热模块升降避让 * 该方法会恢复之前升起的状态 */ - public void moveTrayHeatModuleAvoidUp(String cmdId, String cmdCode, HeatModuleCode targetHeatModuleCode, HeatModuleCode... exceptionHeatModuleCodes) { + public void moveTrayHeatModuleAvoidUp(String cmdId, String cmdCode, HeatModuleCode targetHeatModuleCode, HeatModuleCode... exceptionHeatModuleCodes) throws Exception { double trayLift = devicePositionService.getPosition(DevicePositionCode.trayLift).getDistance(); //托盘升降抬升距离 + List futuresList = new ArrayList<>(); for (HeatModuleCode heatModuleCode : moveTrayHeatModuleAvoidUpStateList) { if (!Arrays.stream(exceptionHeatModuleCodes).toList().contains(heatModuleCode)) { - CompletableFuture.runAsync(() -> { - try { - heatModuleService.heaterMotorMove(cmdId, cmdCode, heatModuleCode, trayLift); - deviceStateService.getDeviceState().getHeatModuleByCode(heatModuleCode).setTrayUp(1);//加热模块托盘升降状态 - } catch (Exception e) { - log.error("避让抬升错误:{}", heatModuleCode, e); - } - }); + DeviceCommandBundle deviceCommand = getHeaterMotorMoveDeviceCommand(heatModuleCode, trayLift); + CommandFuture deviceCommandFuture = deviceCommandService.sendCommand(cmdId, cmdCode, deviceCommand); + futuresList.add(deviceCommandFuture); } } if (targetHeatModuleCode != null) { - CompletableFuture.runAsync(() -> { - try { - heatModuleService.heaterMotorMove(cmdId, cmdCode, targetHeatModuleCode, trayLift); - deviceStateService.getDeviceState().getHeatModuleByCode(targetHeatModuleCode).setTrayUp(1);//加热模块托盘升降状态 - } catch (Exception e) { - log.error("避让抬升错误:{}", targetHeatModuleCode, e); - } - }); + DeviceCommandBundle deviceCommand = getHeaterMotorMoveDeviceCommand(targetHeatModuleCode, trayLift); + CommandFuture deviceCommandFuture = deviceCommandService.sendCommand(cmdId, cmdCode, deviceCommand); + futuresList.add(deviceCommandFuture); + } + CommandUtil.wait(futuresList.toArray(new CommandFuture[0])); + for (HeatModuleCode heatModuleCode : moveTrayHeatModuleAvoidUpStateList) { + deviceStateService.getDeviceState().getHeatModuleByCode(heatModuleCode).setTrayUp(1);//加热模块托盘升降状态 } } @@ -86,7 +82,7 @@ public class DeviceCommandTempUtilService { * 临时移动托盘的时候,加热模块升降避让 * 该方法会下降所有升起的加热模块并且记录 */ - public void moveTrayHeatModuleAvoidDown(String cmdId, String cmdCode, HeatModuleCode targetHeatModuleCode, HeatModuleCode... exceptionHeatModuleCodes) { + public void moveTrayHeatModuleAvoidDown(String cmdId, String cmdCode, HeatModuleCode targetHeatModuleCode, HeatModuleCode... exceptionHeatModuleCodes) throws Exception { moveTrayHeatModuleAvoidUpStateList.clear(); List heatModuleStateList = deviceStateService.getDeviceState().getHeatModule(); double trayLower = devicePositionService.getPosition(DevicePositionCode.trayLower).getDistance(); //获取加热位下降托盘位置 @@ -95,26 +91,21 @@ public class DeviceCommandTempUtilService { if (!Arrays.stream(exceptionHeatModuleCodes).toList().contains(heatModuleState.getModuleCode())) { if (heatModuleState.getTrayUp() == 1) { HeatModuleCode heatModuleCode = heatModuleState.getModuleCode(); - CompletableFuture.runAsync(() -> { - try { - heatModuleService.heaterMotorMove(cmdId, cmdCode, heatModuleCode, trayLower); - deviceStateService.getDeviceState().getHeatModuleByCode(heatModuleCode).setTrayUp(0);//加热模块托盘升降状态 - } catch (Exception e) { - log.error("避让下降错误:{}", heatModuleCode, e); - } - }); + DeviceCommandBundle deviceCommand = getHeaterMotorMoveDeviceCommand(heatModuleCode, trayLower); + CommandFuture deviceCommandFuture = deviceCommandService.sendCommand(cmdId, cmdCode, deviceCommand); + moveTrayHeatModuleAvoidUpStateList.add(heatModuleCode); + futuresList.add(deviceCommandFuture); } } } if (targetHeatModuleCode != null) { - CompletableFuture.runAsync(() -> { - try { - heatModuleService.heaterMotorMove(cmdId, cmdCode, targetHeatModuleCode, trayLower); - deviceStateService.getDeviceState().getHeatModuleByCode(targetHeatModuleCode).setTrayUp(0);//加热模块托盘升降状态 - } catch (Exception e) { - log.error("避让下降错误:{}", targetHeatModuleCode, e); - } - }); + DeviceCommandBundle deviceCommand = getHeaterMotorMoveDeviceCommand(targetHeatModuleCode, trayLower); + CommandFuture deviceCommandFuture = deviceCommandService.sendCommand(cmdId, cmdCode, deviceCommand); + futuresList.add(deviceCommandFuture); + } + CommandUtil.wait(futuresList.toArray(new CommandFuture[0])); + for (HeatModuleCode heatModuleCode : moveTrayHeatModuleAvoidUpStateList) { + deviceStateService.getDeviceState().getHeatModuleByCode(heatModuleCode).setTrayUp(0);//加热模块托盘升降状态 } }