石墨消解仪后端服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
5.3 KiB

  1. package com.iflytop.gd.app.service;
  2. import com.iflytop.gd.app.core.device.HeatModuleState;
  3. import com.iflytop.gd.common.cmd.CommandFuture;
  4. import com.iflytop.gd.common.cmd.DeviceCommandBundle;
  5. import com.iflytop.gd.common.cmd.DeviceCommandGenerator;
  6. import com.iflytop.gd.common.enums.HeatModuleCode;
  7. import com.iflytop.gd.common.enums.data.DevicePositionCode;
  8. import lombok.RequiredArgsConstructor;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.stereotype.Service;
  11. import java.util.ArrayList;
  12. import java.util.Arrays;
  13. import java.util.Collections;
  14. import java.util.List;
  15. import java.util.concurrent.CompletableFuture;
  16. import java.util.concurrent.TimeUnit;
  17. /**
  18. * 临时方法 以后应该会废弃
  19. */
  20. @Slf4j
  21. @Service
  22. @RequiredArgsConstructor
  23. public class DeviceCommandTempUtilService {
  24. private final DeviceCommandService deviceCommandService;
  25. private final DevicePositionService devicePositionService;
  26. private final DeviceStateService deviceStateService;
  27. private List<HeatModuleCode> moveTrayHeatModuleAvoidUpStateList = Collections.synchronizedList(new ArrayList<>());
  28. /**
  29. * 抬升
  30. * 临时移动托盘的时候加热模块升降避让
  31. * 该方法会恢复之前升起的状态
  32. */
  33. public void moveTrayHeatModuleAvoidUp(String cmdId, String cmdCode, HeatModuleCode targetHeatModuleCode) throws Exception {
  34. double trayLift = devicePositionService.getPosition(DevicePositionCode.trayLift).getDistance(); //托盘升降抬升距离
  35. List<CommandFuture> futuresList = new ArrayList<>();
  36. for (HeatModuleCode heatModuleCode : moveTrayHeatModuleAvoidUpStateList) {
  37. DeviceCommandBundle deviceCommand = getHeaterMotorMoveDeviceCommand(heatModuleCode, trayLift);
  38. CommandFuture deviceCommandFuture = deviceCommandService.sendCommand(cmdId, cmdCode, deviceCommand);
  39. futuresList.add(deviceCommandFuture);
  40. }
  41. if(targetHeatModuleCode != null) {
  42. DeviceCommandBundle deviceCommand = getHeaterMotorMoveDeviceCommand(targetHeatModuleCode, trayLift);
  43. CommandFuture deviceCommandFuture = deviceCommandService.sendCommand(cmdId, cmdCode, deviceCommand);
  44. futuresList.add(deviceCommandFuture);
  45. }
  46. commandWait(futuresList.toArray(new CommandFuture[0]));
  47. for (HeatModuleCode heatModuleCode : moveTrayHeatModuleAvoidUpStateList) {
  48. deviceStateService.setHeatModuleStateTrayUp(heatModuleCode, 1);//加热模块托盘升降状态
  49. }
  50. }
  51. /**
  52. * 下降
  53. * 临时移动托盘的时候加热模块升降避让
  54. * 该方法会下降所有升起的加热模块并且记录
  55. */
  56. public void moveTrayHeatModuleAvoidDown(String cmdId, String cmdCode, HeatModuleCode targetHeatModuleCode) throws Exception {
  57. moveTrayHeatModuleAvoidUpStateList.clear();
  58. List<HeatModuleState> heatModuleStateList = deviceStateService.getDeviceState().getHeatModule();
  59. double trayLower = devicePositionService.getPosition(DevicePositionCode.trayLower).getDistance(); //获取加热位下降托盘位置
  60. List<CommandFuture> futuresList = new ArrayList<>();
  61. for (HeatModuleState heatModuleState : heatModuleStateList) {
  62. if (heatModuleState.getTrayUp() == 1) {
  63. HeatModuleCode heatModuleCode = heatModuleState.getModuleCode();
  64. DeviceCommandBundle deviceCommand = getHeaterMotorMoveDeviceCommand(heatModuleCode, trayLower);
  65. CommandFuture deviceCommandFuture = deviceCommandService.sendCommand(cmdId, cmdCode, deviceCommand);
  66. moveTrayHeatModuleAvoidUpStateList.add(heatModuleCode);
  67. futuresList.add(deviceCommandFuture);
  68. }
  69. }
  70. if(targetHeatModuleCode != null) {
  71. DeviceCommandBundle deviceCommand = getHeaterMotorMoveDeviceCommand(targetHeatModuleCode, trayLower);
  72. CommandFuture deviceCommandFuture = deviceCommandService.sendCommand(cmdId, cmdCode, deviceCommand);
  73. futuresList.add(deviceCommandFuture);
  74. }
  75. commandWait(futuresList.toArray(new CommandFuture[0]));
  76. for (HeatModuleCode heatModuleCode : moveTrayHeatModuleAvoidUpStateList) {
  77. deviceStateService.setHeatModuleStateTrayUp(heatModuleCode, 0);//加热模块托盘升降状态
  78. }
  79. }
  80. public DeviceCommandBundle getHeaterMotorMoveDeviceCommand(HeatModuleCode heatModuleCode, Double position) {
  81. return switch (heatModuleCode) {
  82. case heat_module_01 -> DeviceCommandGenerator.heaterMotor1Move(position);
  83. case heat_module_02 -> DeviceCommandGenerator.heaterMotor2Move(position);
  84. case heat_module_03 -> DeviceCommandGenerator.heaterMotor3Move(position);
  85. case heat_module_04 -> DeviceCommandGenerator.heaterMotor4Move(position);
  86. case heat_module_05 -> DeviceCommandGenerator.heaterMotor5Move(position);
  87. case heat_module_06 -> DeviceCommandGenerator.heaterMotor6Move(position);
  88. };
  89. }
  90. protected void commandWait(CommandFuture... futures) throws Exception {
  91. CompletableFuture<?>[] responseFutures = Arrays.stream(futures)
  92. .map(CommandFuture::getResponseFuture)
  93. .toArray(CompletableFuture[]::new);
  94. CompletableFuture.allOf(responseFutures)
  95. .get(120, TimeUnit.SECONDS);
  96. }
  97. }