Browse Source

fix:指令传递溶液id,通过溶液匹配通道

master
白凤吉 3 days ago
parent
commit
0048dc5be5
  1. 24
      src/main/java/com/iflytop/handacid/app/command/control/PumpRotateStartCommand.java
  2. 19
      src/main/java/com/iflytop/handacid/app/command/control/SolutionAddStartCommand.java
  3. 17
      src/main/java/com/iflytop/handacid/app/command/control/SolutionPreFillStartCommand.java
  4. 16
      src/main/java/com/iflytop/handacid/app/core/state/DeviceState.java

24
src/main/java/com/iflytop/handacid/app/command/control/PumpRotateStartCommand.java

@ -8,6 +8,7 @@ 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;
@ -30,12 +31,21 @@ public class PumpRotateStartCommand extends BaseCommandHandler {
private final DeviceCommandService deviceCommandService;
private final ChannelCtrlService channelCtrlService;
private final WebSocketSender webSocketSender;
private final DeviceState deviceState;
@Override
public CompletableFuture<Void> handle(CommandDTO commandDTO) {
ChannelCode channelCode = commandDTO.getEnumParam("channelCode", ChannelCode.class);
if (channelCode == null) {
throw new IllegalArgumentException("参数 channelCode 不能为空");
Long solutionId = commandDTO.getLongParam("solutionId");
if (solutionId == null) {
throw new IllegalArgumentException("参数 solutionId 不能为空");
}
Double concentration = commandDTO.getDoubleParam("concentration");
if (concentration == null) {
throw new IllegalArgumentException("参数 concentration 不能为空");
}
ChannelState channelState = deviceState.getChannelStateBySolution(solutionId, concentration);
if (channelState == null) {
throw new IllegalArgumentException("该溶液与浓度未找到对应通道");
}
Direction direction = commandDTO.getEnumParam("direction", Direction.class);
if (direction == null) {
@ -47,25 +57,25 @@ public class PumpRotateStartCommand extends BaseCommandHandler {
}
return runAsync(() -> {
if (Direction.FORWARD.equals(direction)) {
DeviceCommand currentPositionDeviceCommand = channelCtrlService.getPumpPositionCommandByChannel(channelCode);
DeviceCommand currentPositionDeviceCommand = channelCtrlService.getPumpPositionCommandByChannel(channelState.getChannelCode());
CommandFuture currentPositionCommandFuture = deviceCommandService.sendCommand(currentPositionDeviceCommand);
CommandUtil.wait(currentPositionCommandFuture);
Double currentPosition = currentPositionCommandFuture.getResponseResult().getJSONObject("data").getDouble("position");
DeviceCommand deviceCommand = channelCtrlService.getPumpMoveCommandByChannel(channelCode, position);
DeviceCommand deviceCommand = channelCtrlService.getPumpMoveCommandByChannel(channelState.getChannelCode(), position);
CommandFuture commandFuture = deviceCommandService.sendCommand(deviceCommand);
CommandUtil.wait(commandFuture);
DeviceCommand newPositionDeviceCommand = channelCtrlService.getPumpPositionCommandByChannel(channelCode);
DeviceCommand newPositionDeviceCommand = channelCtrlService.getPumpPositionCommandByChannel(channelState.getChannelCode());
CommandFuture newPositionCommandFuture = deviceCommandService.sendCommand(newPositionDeviceCommand);
CommandUtil.wait(newPositionCommandFuture);
Double newPosition = (Double) newPositionCommandFuture.getResponseResult().getJSONObject("data").get("position");
log.info("currentPosition: {}, newPosition: {}", currentPosition, newPosition);
webSocketSender.pushPumpPosition((newPosition - currentPosition) / 100);
} else {
DeviceCommand deviceCommand = channelCtrlService.getPumpBackwardRotateCommandByChannel(channelCode);
DeviceCommand deviceCommand = channelCtrlService.getPumpBackwardRotateCommandByChannel(channelState.getChannelCode());
CommandFuture commandFuture = deviceCommandService.sendCommand(deviceCommand);
CommandUtil.wait(commandFuture);
}

19
src/main/java/com/iflytop/handacid/app/command/control/SolutionAddStartCommand.java

@ -1,9 +1,6 @@
package com.iflytop.handacid.app.command.control;
import com.iflytop.handacid.app.common.annotation.CommandMapping;
import com.iflytop.handacid.app.common.enums.ChannelCode;
import com.iflytop.handacid.app.common.enums.ChannelStateCode;
import com.iflytop.handacid.app.common.enums.Direction;
import com.iflytop.handacid.app.common.utils.CommandUtil;
import com.iflytop.handacid.app.core.command.BaseCommandHandler;
import com.iflytop.handacid.app.core.command.CommandFuture;
@ -33,16 +30,24 @@ public class SolutionAddStartCommand extends BaseCommandHandler {
@Override
public CompletableFuture<Void> handle(CommandDTO commandDTO) {
ChannelCode channelCode = commandDTO.getEnumParam("channelCode", ChannelCode.class);
if (channelCode == null) {
throw new IllegalArgumentException("参数 channelCode 不能为空");
Long solutionId = commandDTO.getLongParam("solutionId");
if (solutionId == null) {
throw new IllegalArgumentException("参数 solutionId 不能为空");
}
Double concentration = commandDTO.getDoubleParam("concentration");
if (concentration == null) {
throw new IllegalArgumentException("参数 concentration 不能为空");
}
ChannelState channelState = deviceState.getChannelStateBySolution(solutionId, concentration);
if (channelState == null) {
throw new IllegalArgumentException("该溶液与浓度未找到对应通道");
}
Double position = commandDTO.getDoubleParam("position");
if (position == null) {
throw new IllegalArgumentException("参数 position 不能为空");
}
return runAsync(() -> {
DeviceCommand deviceCommand = channelCtrlService.getPumpMoveCommandByChannel(channelCode, position);
DeviceCommand deviceCommand = channelCtrlService.getPumpMoveCommandByChannel(channelState.getChannelCode(), position);
CommandFuture commandFuture = deviceCommandService.sendCommand(deviceCommand);
CommandUtil.wait(commandFuture);
});

17
src/main/java/com/iflytop/handacid/app/command/control/SolutionPreFillStartCommand.java

@ -6,6 +6,7 @@ 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;
@ -30,12 +31,20 @@ public class SolutionPreFillStartCommand extends BaseCommandHandler {
@Override
public CompletableFuture<Void> handle(CommandDTO commandDTO) {
ChannelCode channelCode = commandDTO.getEnumParam("channelCode", ChannelCode.class);
if (channelCode == null) {
throw new IllegalArgumentException("参数 channelCode 不能为空");
Long solutionId = commandDTO.getLongParam("solutionId");
if (solutionId == null) {
throw new IllegalArgumentException("参数 solutionId 不能为空");
}
Double concentration = commandDTO.getDoubleParam("concentration");
if (concentration == null) {
throw new IllegalArgumentException("参数 concentration 不能为空");
}
ChannelState channelState = deviceState.getChannelStateBySolution(solutionId, concentration);
if (channelState == null) {
throw new IllegalArgumentException("该溶液与浓度未找到对应通道");
}
return runAsync(() -> {
DeviceCommand deviceCommand = channelCtrlService.getPumpForwardRotateCommandByChannel(channelCode);
DeviceCommand deviceCommand = channelCtrlService.getPumpForwardRotateCommandByChannel(channelState.getChannelCode());
CommandFuture commandFuture = deviceCommandService.sendCommand(deviceCommand);
CommandUtil.wait(commandFuture);
});

16
src/main/java/com/iflytop/handacid/app/core/state/DeviceState.java

@ -9,10 +9,7 @@ import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
@Schema(description = "设备当前状态")
@ -106,4 +103,15 @@ public class DeviceState {
return channelStateMap.values().stream()
.anyMatch(cs -> cs.getStateCode() == ChannelStateCode.ADD);
}
/**
* 根据溶液ID和浓度获取匹配的通道状态
*/
public ChannelState getChannelStateBySolution(Long solutionId, Double concentration) {
return channelStateMap.values().stream()
.filter(cs -> Objects.equals(cs.getSolutionId(), solutionId)
&& Objects.equals(cs.getConcentration(), concentration))
.findFirst()
.orElse(null);
}
}
Loading…
Cancel
Save