diff --git a/src/main/java/com/iflytop/handacid/app/command/control/SolutionDrainStartCommand.java b/src/main/java/com/iflytop/handacid/app/command/control/SolutionDrainStartCommand.java new file mode 100644 index 0000000..5aef560 --- /dev/null +++ b/src/main/java/com/iflytop/handacid/app/command/control/SolutionDrainStartCommand.java @@ -0,0 +1,48 @@ +package com.iflytop.handacid.app.command.control; + +import com.iflytop.handacid.app.common.annotation.CommandMapping; +import com.iflytop.handacid.app.common.enums.ChannelStateCode; +import com.iflytop.handacid.app.common.utils.CommandUtil; +import com.iflytop.handacid.app.core.command.BaseCommandHandler; +import com.iflytop.handacid.app.core.command.CommandFuture; +import com.iflytop.handacid.app.core.command.DeviceCommand; +import com.iflytop.handacid.app.core.state.ChannelState; +import com.iflytop.handacid.app.core.state.DeviceState; +import com.iflytop.handacid.app.model.dto.CommandDTO; +import com.iflytop.handacid.app.service.ChannelCtrlService; +import com.iflytop.handacid.app.service.DeviceCommandService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +/** + * 开始排空 + */ +@Slf4j +@Component +@RequiredArgsConstructor +@CommandMapping("solution_drain_start") +public class SolutionDrainStartCommand extends BaseCommandHandler { + private final DeviceCommandService deviceCommandService; + private final ChannelCtrlService channelCtrlService; + private final DeviceState deviceState; + + @Override + public CompletableFuture handle(CommandDTO commandDTO) { + List channelCodeList = deviceState.filterChannelStatesIsPre(); + return runAsync(() -> { + List commandFutureList = new ArrayList<>(); + for (ChannelState channelState : channelCodeList) { + channelState.setStateCode(ChannelStateCode.DRAIN); + DeviceCommand deviceCommand = channelCtrlService.getPumpBackwardRotateCommandByChannel(channelState.getChannelCode()); + commandFutureList.add(deviceCommandService.sendCommand(deviceCommand)); + } + CommandUtil.wait(commandFutureList); + }); + } +} + diff --git a/src/main/java/com/iflytop/handacid/app/command/control/SolutionDrainStopCommand.java b/src/main/java/com/iflytop/handacid/app/command/control/SolutionDrainStopCommand.java new file mode 100644 index 0000000..e2a7dcc --- /dev/null +++ b/src/main/java/com/iflytop/handacid/app/command/control/SolutionDrainStopCommand.java @@ -0,0 +1,48 @@ +package com.iflytop.handacid.app.command.control; + +import com.iflytop.handacid.app.common.annotation.CommandMapping; +import com.iflytop.handacid.app.common.enums.ChannelStateCode; +import com.iflytop.handacid.app.common.utils.CommandUtil; +import com.iflytop.handacid.app.core.command.BaseCommandHandler; +import com.iflytop.handacid.app.core.command.CommandFuture; +import com.iflytop.handacid.app.core.command.DeviceCommand; +import com.iflytop.handacid.app.core.state.ChannelState; +import com.iflytop.handacid.app.core.state.DeviceState; +import com.iflytop.handacid.app.model.dto.CommandDTO; +import com.iflytop.handacid.app.service.ChannelCtrlService; +import com.iflytop.handacid.app.service.DeviceCommandService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +/** + * 停止排空 + */ +@Slf4j +@Component +@RequiredArgsConstructor +@CommandMapping("solution_drain_stop") +public class SolutionDrainStopCommand extends BaseCommandHandler { + private final DeviceCommandService deviceCommandService; + private final ChannelCtrlService channelCtrlService; + private final DeviceState deviceState; + + @Override + public CompletableFuture handle(CommandDTO commandDTO) { + List channelCodeList = deviceState.filterChannelStatesByState(ChannelStateCode.DRAIN); + return runAsync(() -> { + List commandFutureList = new ArrayList<>(); + for (ChannelState channelState : channelCodeList) { + channelState.setStateCode(ChannelStateCode.DRAIN); + DeviceCommand deviceCommand = channelCtrlService.getPumpStopCommandByChannel(channelState.getChannelCode()); + commandFutureList.add(deviceCommandService.sendCommand(deviceCommand)); + } + CommandUtil.wait(commandFutureList); + }); + } +} + diff --git a/src/main/java/com/iflytop/handacid/app/command/control/SolutionPreFillStopCommand.java b/src/main/java/com/iflytop/handacid/app/command/control/SolutionPreFillStopCommand.java index 2e2a15e..3b328c9 100644 --- a/src/main/java/com/iflytop/handacid/app/command/control/SolutionPreFillStopCommand.java +++ b/src/main/java/com/iflytop/handacid/app/command/control/SolutionPreFillStopCommand.java @@ -46,6 +46,7 @@ public class SolutionPreFillStopCommand extends BaseCommandHandler { CommandUtil.wait(commandFutureList); } finally { for (ChannelState channelCode : channelCodeList) { + channelCode.setPre(true); channelCode.setStateCode(ChannelStateCode.IDLE); } } diff --git a/src/main/java/com/iflytop/handacid/app/core/state/ChannelState.java b/src/main/java/com/iflytop/handacid/app/core/state/ChannelState.java index 1f73976..36626f9 100644 --- a/src/main/java/com/iflytop/handacid/app/core/state/ChannelState.java +++ b/src/main/java/com/iflytop/handacid/app/core/state/ChannelState.java @@ -3,7 +3,6 @@ package com.iflytop.handacid.app.core.state; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.iflytop.handacid.app.common.enums.ChannelCode; import com.iflytop.handacid.app.common.enums.ChannelStateCode; -import com.iflytop.handacid.app.model.bo.ChannelSolution; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; import lombok.RequiredArgsConstructor; @@ -23,6 +22,9 @@ public class ChannelState { @Schema(description = "当前通道状态") private ChannelStateCode stateCode = ChannelStateCode.IDLE; + @Schema(description = "是否已预充") + private volatile boolean pre = false; + @Schema(description = "剩余容量(单位:mL)") private Double remainingVolume; diff --git a/src/main/java/com/iflytop/handacid/app/core/state/DeviceState.java b/src/main/java/com/iflytop/handacid/app/core/state/DeviceState.java index 5b9bf7d..1acad1d 100644 --- a/src/main/java/com/iflytop/handacid/app/core/state/DeviceState.java +++ b/src/main/java/com/iflytop/handacid/app/core/state/DeviceState.java @@ -50,4 +50,13 @@ public class DeviceState { .filter(cs -> cs.getStateCode() == stateCode) .collect(Collectors.toList()); } + + /** + * 获取已经预充过的通道 + */ + public List filterChannelStatesIsPre() { + return channelStateMap.values().stream() + .filter(ChannelState::isPre) + .collect(Collectors.toList()); + } } \ No newline at end of file diff --git a/src/main/java/com/iflytop/handacid/app/service/ChannelCtrlService.java b/src/main/java/com/iflytop/handacid/app/service/ChannelCtrlService.java index 47b548c..9c75284 100644 --- a/src/main/java/com/iflytop/handacid/app/service/ChannelCtrlService.java +++ b/src/main/java/com/iflytop/handacid/app/service/ChannelCtrlService.java @@ -45,6 +45,18 @@ public class ChannelCtrlService { } /** + * 根据通道code获取泵反转指令 + */ + public DeviceCommand getPumpBackwardRotateCommandByChannel(ChannelCode channelCode) { + return switch (channelCode) { + case CHANNEL_1 -> DeviceCommandGenerator.pump1BackwardRotate(); + case CHANNEL_2 -> DeviceCommandGenerator.pump2BackwardRotate(); + case CHANNEL_3 -> DeviceCommandGenerator.pump3BackwardRotate(); + case CHANNEL_4 -> DeviceCommandGenerator.pump4BackwardRotate(); + }; + } + + /** * 根据通道code获取停止泵指令 */ public DeviceCommand getPumpStopCommandByChannel(ChannelCode channelCode) {