加酸仪(java版本)
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.

206 lines
8.8 KiB

  1. package com.iflytop.handacid.app.service;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.iflytop.handacid.app.common.enums.ChannelCode;
  4. import com.iflytop.handacid.app.common.enums.ChannelStateCode;
  5. import com.iflytop.handacid.app.common.enums.SolutionAddMode;
  6. import com.iflytop.handacid.app.common.utils.CommandUtil;
  7. import com.iflytop.handacid.app.core.command.CommandFuture;
  8. import com.iflytop.handacid.app.core.command.DeviceCommand;
  9. import com.iflytop.handacid.app.core.command.DeviceCommandGenerator;
  10. import com.iflytop.handacid.app.core.state.ChannelState;
  11. import com.iflytop.handacid.app.core.state.DeviceState;
  12. import com.iflytop.handacid.common.model.entity.Formulation;
  13. import com.iflytop.handacid.common.service.FormulationService;
  14. import lombok.RequiredArgsConstructor;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.springframework.stereotype.Service;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.concurrent.CompletableFuture;
  20. /**
  21. * 通道控制
  22. */
  23. @Slf4j
  24. @Service
  25. @RequiredArgsConstructor
  26. public class ChannelCtrlService {
  27. private final FormulationService formulationService;
  28. private final DeviceCommandService deviceCommandService;
  29. private final DeviceState deviceState;
  30. /**
  31. * 开始加液
  32. */
  33. public void solutionAddStart() {
  34. CompletableFuture.runAsync(() -> {
  35. List<ChannelState> channelStateList = deviceState.filterChannelStatesIsSelected();
  36. try {
  37. for (ChannelState channelState : channelStateList) {
  38. channelState.setStateCode(ChannelStateCode.ADD);
  39. }
  40. if (SolutionAddMode.AUTO.equals(deviceState.getMode())) {//自动模式
  41. while (!deviceState.isSolutionAddStop()) {
  42. List<CommandFuture> commandFutureList = new ArrayList<>();
  43. for (ChannelState channelState : channelStateList) {
  44. Formulation formulation = formulationService.getOne(new LambdaQueryWrapper<Formulation>().eq(Formulation::getSolutionId, channelState.getSolutionId()));
  45. DeviceCommand deviceCommand = getPumpMoveByCommandByChannel(channelState.getChannelCode(), formulation.getRevolutions());
  46. commandFutureList.add(deviceCommandService.sendCommand(deviceCommand));
  47. }
  48. CommandUtil.wait(commandFutureList);
  49. Thread.sleep(deviceState.getDelay() * 1000L);
  50. }
  51. } else if (SolutionAddMode.CLICK.equals(deviceState.getMode())) {//点动模式
  52. List<CommandFuture> commandFutureList = new ArrayList<>();
  53. for (ChannelState channelState : channelStateList) {
  54. Formulation formulation = formulationService.getOne(new LambdaQueryWrapper<Formulation>().eq(Formulation::getSolutionId, channelState.getSolutionId()));
  55. DeviceCommand deviceCommand = getPumpMoveByCommandByChannel(channelState.getChannelCode(), formulation.getRevolutions());
  56. commandFutureList.add(deviceCommandService.sendCommand(deviceCommand));
  57. }
  58. CommandUtil.wait(commandFutureList);
  59. }
  60. } catch (Exception e) {
  61. throw new RuntimeException(e);
  62. } finally {
  63. for (ChannelState channelState : channelStateList) {
  64. channelState.setStateCode(ChannelStateCode.IDLE);
  65. }
  66. deviceState.setSolutionAddStop(false);
  67. }
  68. });
  69. }
  70. /**
  71. * 停止加液
  72. */
  73. public void solutionAddStop() {
  74. List<ChannelState> channelCodeList = deviceState.filterChannelStatesByState(ChannelStateCode.ADD);
  75. deviceState.setSolutionAddStop(true);
  76. try {
  77. List<CommandFuture> commandFutureList = new ArrayList<>();
  78. for (ChannelState channelState : channelCodeList) {
  79. DeviceCommand deviceCommand = getPumpStopCommandByChannel(channelState.getChannelCode());
  80. commandFutureList.add(deviceCommandService.sendCommand(deviceCommand));
  81. }
  82. CommandUtil.wait(commandFutureList);
  83. } catch (Exception e) {
  84. throw new RuntimeException(e);
  85. } finally {
  86. for (ChannelState channelCode : channelCodeList) {
  87. channelCode.setStateCode(ChannelStateCode.IDLE);
  88. }
  89. }
  90. }
  91. /**
  92. * 开始预充
  93. */
  94. public void solutionPreFillStart() {
  95. CompletableFuture.runAsync(() -> {
  96. List<ChannelState> channelStateList = deviceState.filterChannelStatesIsSelected();
  97. try {
  98. for (ChannelState channelState : channelStateList) {
  99. channelState.setStateCode(ChannelStateCode.PRE);
  100. }
  101. List<CommandFuture> commandFutureList = new ArrayList<>();
  102. for (ChannelState channelState : channelStateList) {
  103. DeviceCommand deviceCommand = getPumpForwardRotateCommandByChannel(channelState.getChannelCode());
  104. commandFutureList.add(deviceCommandService.sendCommand(deviceCommand));
  105. }
  106. CommandUtil.wait(commandFutureList);
  107. } catch (Exception e) {
  108. throw new RuntimeException(e);
  109. }
  110. });
  111. }
  112. /**
  113. * 结束预充
  114. */
  115. public void solutionPreFillStop() {
  116. CompletableFuture.runAsync(() -> {
  117. List<ChannelState> channelCodeList = deviceState.filterChannelStatesByState(ChannelStateCode.PRE);
  118. deviceState.setSolutionAddStop(true);
  119. try {
  120. List<CommandFuture> commandFutureList = new ArrayList<>();
  121. for (ChannelState channelState : channelCodeList) {
  122. DeviceCommand deviceCommand = getPumpStopCommandByChannel(channelState.getChannelCode());
  123. commandFutureList.add(deviceCommandService.sendCommand(deviceCommand));
  124. }
  125. CommandUtil.wait(commandFutureList);
  126. } catch (Exception e) {
  127. throw new RuntimeException(e);
  128. } finally {
  129. for (ChannelState channelCode : channelCodeList) {
  130. channelCode.setPre(true);
  131. channelCode.setStateCode(ChannelStateCode.IDLE);
  132. }
  133. }
  134. });
  135. }
  136. /**
  137. * 根据通道code获取泵相对移动指令
  138. */
  139. public DeviceCommand getPumpMoveByCommandByChannel(ChannelCode channelCode, Double position) {
  140. return switch (channelCode) {
  141. case CHANNEL_1 -> DeviceCommandGenerator.pump1MoveBy(position);
  142. case CHANNEL_2 -> DeviceCommandGenerator.pump2MoveBy(position);
  143. case CHANNEL_3 -> DeviceCommandGenerator.pump3MoveBy(position);
  144. case CHANNEL_4 -> DeviceCommandGenerator.pump4MoveBy(position);
  145. };
  146. }
  147. /**
  148. * 根据通道code获取泵正转指令
  149. */
  150. public DeviceCommand getPumpForwardRotateCommandByChannel(ChannelCode channelCode) {
  151. return switch (channelCode) {
  152. case CHANNEL_1 -> DeviceCommandGenerator.pump1ForwardRotate();
  153. case CHANNEL_2 -> DeviceCommandGenerator.pump2ForwardRotate();
  154. case CHANNEL_3 -> DeviceCommandGenerator.pump3ForwardRotate();
  155. case CHANNEL_4 -> DeviceCommandGenerator.pump4ForwardRotate();
  156. };
  157. }
  158. /**
  159. * 根据通道code获取泵转数指令
  160. */
  161. public DeviceCommand getPumpPositionCommandByChannel(ChannelCode channelCode) {
  162. return switch (channelCode) {
  163. case CHANNEL_1 -> DeviceCommandGenerator.pump1GetPosition();
  164. case CHANNEL_2 -> DeviceCommandGenerator.pump2GetPosition();
  165. case CHANNEL_3 -> DeviceCommandGenerator.pump3GetPosition();
  166. case CHANNEL_4 -> DeviceCommandGenerator.pump4GetPosition();
  167. };
  168. }
  169. /**
  170. * 根据通道code获取泵反转指令
  171. */
  172. public DeviceCommand getPumpBackwardRotateCommandByChannel(ChannelCode channelCode) {
  173. return switch (channelCode) {
  174. case CHANNEL_1 -> DeviceCommandGenerator.pump1BackwardRotate();
  175. case CHANNEL_2 -> DeviceCommandGenerator.pump2BackwardRotate();
  176. case CHANNEL_3 -> DeviceCommandGenerator.pump3BackwardRotate();
  177. case CHANNEL_4 -> DeviceCommandGenerator.pump4BackwardRotate();
  178. };
  179. }
  180. /**
  181. * 根据通道code获取停止泵指令
  182. */
  183. public DeviceCommand getPumpStopCommandByChannel(ChannelCode channelCode) {
  184. return switch (channelCode) {
  185. case CHANNEL_1 -> DeviceCommandGenerator.pump1Stop();
  186. case CHANNEL_2 -> DeviceCommandGenerator.pump2Stop();
  187. case CHANNEL_3 -> DeviceCommandGenerator.pump3Stop();
  188. case CHANNEL_4 -> DeviceCommandGenerator.pump4Stop();
  189. };
  190. }
  191. }