package com.iflytop.gd.app.service; import com.iflytop.gd.app.core.device.HeatModuleState; import com.iflytop.gd.common.cmd.CommandFuture; import com.iflytop.gd.common.cmd.DeviceCommandBundle; import com.iflytop.gd.common.cmd.DeviceCommandGenerator; import com.iflytop.gd.common.enums.HeatModuleCode; import com.iflytop.gd.common.enums.data.DevicePositionCode; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; /** * 临时方法 以后应该会废弃 */ @Slf4j @Service @RequiredArgsConstructor public class DeviceCommandTempUtilService { private final DeviceCommandService deviceCommandService; private final DevicePositionService devicePositionService; private final DeviceStateService deviceStateService; private List moveTrayHeatModuleAvoidUpStateList = Collections.synchronizedList(new ArrayList<>()); /** * 抬升 * 临时移动托盘的时候,加热模块升降避让 * 该方法会恢复之前升起的状态 */ public void moveTrayHeatModuleAvoidUp(String cmdId, String cmdCode, HeatModuleCode targetHeatModuleCode) throws Exception { double trayLift = devicePositionService.getPosition(DevicePositionCode.trayLift).getDistance(); //托盘升降抬升距离 List futuresList = new ArrayList<>(); for (HeatModuleCode heatModuleCode : moveTrayHeatModuleAvoidUpStateList) { DeviceCommandBundle deviceCommand = getHeaterMotorMoveDeviceCommand(heatModuleCode, trayLift); CommandFuture deviceCommandFuture = deviceCommandService.sendCommand(cmdId, cmdCode, deviceCommand); futuresList.add(deviceCommandFuture); } if(targetHeatModuleCode != null) { DeviceCommandBundle deviceCommand = getHeaterMotorMoveDeviceCommand(targetHeatModuleCode, trayLift); CommandFuture deviceCommandFuture = deviceCommandService.sendCommand(cmdId, cmdCode, deviceCommand); futuresList.add(deviceCommandFuture); } commandWait(futuresList.toArray(new CommandFuture[0])); for (HeatModuleCode heatModuleCode : moveTrayHeatModuleAvoidUpStateList) { deviceStateService.setHeatModuleStateTrayUp(heatModuleCode, 1);//加热模块托盘升降状态 } } /** * 下降 * 临时移动托盘的时候,加热模块升降避让 * 该方法会下降所有升起的加热模块并且记录 */ public void moveTrayHeatModuleAvoidDown(String cmdId, String cmdCode, HeatModuleCode targetHeatModuleCode) throws Exception { moveTrayHeatModuleAvoidUpStateList.clear(); List heatModuleStateList = deviceStateService.getDeviceState().getHeatModule(); double trayLower = devicePositionService.getPosition(DevicePositionCode.trayLower).getDistance(); //获取加热位下降托盘位置 List futuresList = new ArrayList<>(); for (HeatModuleState heatModuleState : heatModuleStateList) { if (heatModuleState.getTrayUp() == 1) { HeatModuleCode heatModuleCode = heatModuleState.getModuleCode(); DeviceCommandBundle deviceCommand = getHeaterMotorMoveDeviceCommand(heatModuleCode, trayLower); CommandFuture deviceCommandFuture = deviceCommandService.sendCommand(cmdId, cmdCode, deviceCommand); moveTrayHeatModuleAvoidUpStateList.add(heatModuleCode); futuresList.add(deviceCommandFuture); } } if(targetHeatModuleCode != null) { DeviceCommandBundle deviceCommand = getHeaterMotorMoveDeviceCommand(targetHeatModuleCode, trayLower); CommandFuture deviceCommandFuture = deviceCommandService.sendCommand(cmdId, cmdCode, deviceCommand); futuresList.add(deviceCommandFuture); } commandWait(futuresList.toArray(new CommandFuture[0])); for (HeatModuleCode heatModuleCode : moveTrayHeatModuleAvoidUpStateList) { deviceStateService.setHeatModuleStateTrayUp(heatModuleCode, 0);//加热模块托盘升降状态 } } public DeviceCommandBundle getHeaterMotorMoveDeviceCommand(HeatModuleCode heatModuleCode, Double position) { return switch (heatModuleCode) { case heat_module_01 -> DeviceCommandGenerator.heaterMotor1Move(position); case heat_module_02 -> DeviceCommandGenerator.heaterMotor2Move(position); case heat_module_03 -> DeviceCommandGenerator.heaterMotor3Move(position); case heat_module_04 -> DeviceCommandGenerator.heaterMotor4Move(position); case heat_module_05 -> DeviceCommandGenerator.heaterMotor5Move(position); case heat_module_06 -> DeviceCommandGenerator.heaterMotor6Move(position); }; } protected void commandWait(CommandFuture... futures) throws Exception { CompletableFuture[] responseFutures = Arrays.stream(futures) .map(CommandFuture::getResponseFuture) .toArray(CompletableFuture[]::new); CompletableFuture.allOf(responseFutures) .get(120, TimeUnit.SECONDS); } }