8 changed files with 268 additions and 28 deletions
-
100src/main/java/com/iflytop/handacid/app/command/control/SolutionAddStartCommand.java
-
55src/main/java/com/iflytop/handacid/app/command/control/SolutionAddStopCommand.java
-
28src/main/java/com/iflytop/handacid/app/command/control/StopAllMotorCommand.java
-
5src/main/java/com/iflytop/handacid/app/common/enums/SolutionAddMode.java
-
5src/main/java/com/iflytop/handacid/app/common/utils/CommandUtil.java
-
15src/main/java/com/iflytop/handacid/app/core/state/DeviceState.java
-
23src/main/java/com/iflytop/handacid/app/model/dto/CommandDTO.java
-
65src/main/java/com/iflytop/handacid/app/service/ChannelCtrlService.java
@ -0,0 +1,100 @@ |
|||||
|
package com.iflytop.handacid.app.command.control; |
||||
|
|
||||
|
import cn.hutool.json.JSONArray; |
||||
|
import cn.hutool.json.JSONObject; |
||||
|
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.SolutionAddMode; |
||||
|
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.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_add_start") |
||||
|
public class SolutionAddStartCommand extends BaseCommandHandler { |
||||
|
private final DeviceCommandService deviceCommandService; |
||||
|
private final ChannelCtrlService channelCtrlService; |
||||
|
private final DeviceState deviceState; |
||||
|
|
||||
|
@Override |
||||
|
public CompletableFuture<Void> handle(CommandDTO commandDTO) { |
||||
|
JSONArray jsonArray = commandDTO.getJSONArrayParam("channelArray"); |
||||
|
List<ChannelCode> channelCodeList = new ArrayList<>(); |
||||
|
for (int i = 0; i < jsonArray.size(); i++) { |
||||
|
JSONObject jsonObject = jsonArray.getJSONObject(i); |
||||
|
String channelCodeStr = jsonObject.getStr("channelCode"); |
||||
|
if (channelCodeStr == null) { |
||||
|
throw new IllegalArgumentException("参数 channelCode 不能为空"); |
||||
|
} |
||||
|
ChannelCode channelCode = ChannelCode.valueOf(channelCodeStr); |
||||
|
channelCodeList.add(channelCode); |
||||
|
Double volume = jsonObject.getDouble("volume"); |
||||
|
if (volume == null) { |
||||
|
throw new IllegalArgumentException("参数 volume 不能为空"); |
||||
|
} |
||||
|
} |
||||
|
SolutionAddMode mode = commandDTO.getEnumParam("mode", SolutionAddMode.class); |
||||
|
Integer delay = commandDTO.getIntegerParam("delay"); |
||||
|
if (mode == null) { |
||||
|
throw new IllegalArgumentException("参数 mode 不能为空"); |
||||
|
} |
||||
|
return runAsync(() -> { |
||||
|
try { |
||||
|
for (ChannelCode channelCode : channelCodeList) { |
||||
|
deviceState.getChannelStateMap().get(channelCode).setStateCode(ChannelStateCode.ADD); |
||||
|
} |
||||
|
if (SolutionAddMode.AUTO.equals(mode)) {//自动模式 |
||||
|
while (!deviceState.isSolutionAddStop()) { |
||||
|
List<CommandFuture> commandFutureList = new ArrayList<>(); |
||||
|
for (int i = 0; i < jsonArray.size(); i++) { |
||||
|
JSONObject jsonObject = jsonArray.getJSONObject(i); |
||||
|
String channelCodeStr = jsonObject.getStr("channelCode"); |
||||
|
ChannelCode channelCode = ChannelCode.valueOf(channelCodeStr); |
||||
|
|
||||
|
Double volume = jsonObject.getDouble("volume"); |
||||
|
DeviceCommand deviceCommand = channelCtrlService.getPumpMoveByCommandByChannel(channelCode, volume); |
||||
|
commandFutureList.add(deviceCommandService.sendCommand(deviceCommand)); |
||||
|
} |
||||
|
CommandUtil.wait(commandFutureList); |
||||
|
Thread.sleep(delay * 1000L); |
||||
|
} |
||||
|
} else if (SolutionAddMode.CLICK.equals(mode)) {//点动模式 |
||||
|
List<CommandFuture> commandFutureList = new ArrayList<>(); |
||||
|
for (int i = 0; i < jsonArray.size(); i++) { |
||||
|
JSONObject jsonObject = jsonArray.getJSONObject(i); |
||||
|
String channelCodeStr = jsonObject.getStr("channelCode"); |
||||
|
ChannelCode channelCode = ChannelCode.valueOf(channelCodeStr); |
||||
|
deviceState.getChannelStateMap().get(channelCode).setStateCode(ChannelStateCode.ADD); |
||||
|
Double volume = jsonObject.getDouble("volume"); |
||||
|
DeviceCommand deviceCommand = channelCtrlService.getPumpMoveByCommandByChannel(channelCode, volume); |
||||
|
commandFutureList.add(deviceCommandService.sendCommand(deviceCommand)); |
||||
|
} |
||||
|
CommandUtil.wait(commandFutureList); |
||||
|
} |
||||
|
} finally { |
||||
|
for (ChannelCode channelCode : channelCodeList) { |
||||
|
deviceState.getChannelStateMap().get(channelCode).setStateCode(ChannelStateCode.IDLE); |
||||
|
} |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
|
@ -0,0 +1,55 @@ |
|||||
|
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_add_stop") |
||||
|
public class SolutionAddStopCommand extends BaseCommandHandler { |
||||
|
private final DeviceCommandService deviceCommandService; |
||||
|
private final ChannelCtrlService channelCtrlService; |
||||
|
private final DeviceState deviceState; |
||||
|
|
||||
|
@Override |
||||
|
public CompletableFuture<Void> handle(CommandDTO commandDTO) { |
||||
|
|
||||
|
return runAsync(() -> { |
||||
|
List<ChannelState> channelCodeList = deviceState.filterChannelStatesByState(ChannelStateCode.ADD); |
||||
|
deviceState.setSolutionAddStop(true); |
||||
|
try { |
||||
|
List<CommandFuture> commandFutureList = new ArrayList<>(); |
||||
|
for (ChannelState channelState : channelCodeList) { |
||||
|
DeviceCommand deviceCommand = channelCtrlService.getPumpStopCommandByChannel(channelState.getChannelCode()); |
||||
|
commandFutureList.add(deviceCommandService.sendCommand(deviceCommand)); |
||||
|
} |
||||
|
CommandUtil.wait(commandFutureList); |
||||
|
} finally { |
||||
|
for (ChannelState channelCode : channelCodeList) { |
||||
|
channelCode.setStateCode(ChannelStateCode.IDLE); |
||||
|
} |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
|
} |
||||
|
|
@ -1,28 +0,0 @@ |
|||||
package com.iflytop.handacid.app.command.control; |
|
||||
|
|
||||
import com.iflytop.handacid.app.common.annotation.CommandMapping; |
|
||||
import com.iflytop.handacid.app.core.command.BaseCommandHandler; |
|
||||
import com.iflytop.handacid.app.model.dto.CommandDTO; |
|
||||
import lombok.RequiredArgsConstructor; |
|
||||
import lombok.extern.slf4j.Slf4j; |
|
||||
import org.springframework.stereotype.Component; |
|
||||
|
|
||||
import java.util.concurrent.CompletableFuture; |
|
||||
|
|
||||
/** |
|
||||
* 停止所有电机 |
|
||||
*/ |
|
||||
@Slf4j |
|
||||
@Component |
|
||||
@RequiredArgsConstructor |
|
||||
@CommandMapping("stop_all_motor") |
|
||||
public class StopAllMotorCommand extends BaseCommandHandler { |
|
||||
|
|
||||
@Override |
|
||||
public CompletableFuture<Void> handle(CommandDTO commandDTO) { |
|
||||
|
|
||||
return runAsync(() -> { |
|
||||
}); |
|
||||
} |
|
||||
} |
|
||||
|
|
@ -0,0 +1,5 @@ |
|||||
|
package com.iflytop.handacid.app.common.enums; |
||||
|
|
||||
|
public enum SolutionAddMode { |
||||
|
AUTO, CLICK |
||||
|
} |
@ -0,0 +1,65 @@ |
|||||
|
package com.iflytop.handacid.app.service; |
||||
|
|
||||
|
import com.iflytop.handacid.app.common.enums.ChannelCode; |
||||
|
import com.iflytop.handacid.app.common.enums.ChannelStateCode; |
||||
|
import com.iflytop.handacid.app.core.command.DeviceCommand; |
||||
|
import com.iflytop.handacid.app.core.command.DeviceCommandGenerator; |
||||
|
import com.iflytop.handacid.app.core.state.DeviceState; |
||||
|
import lombok.RequiredArgsConstructor; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
/** |
||||
|
* 通道控制 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@Service |
||||
|
@RequiredArgsConstructor |
||||
|
public class ChannelCtrlService { |
||||
|
private final DeviceCommandService deviceCommandService; |
||||
|
private final DeviceState deviceState; |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 根据通道code获取泵相对移动指令 |
||||
|
*/ |
||||
|
public DeviceCommand getPumpMoveByCommandByChannel(ChannelCode channelCode, Double position) { |
||||
|
return switch (channelCode) { |
||||
|
case CHANNEL_1 -> DeviceCommandGenerator.pump1MoveBy(position); |
||||
|
case CHANNEL_2 -> DeviceCommandGenerator.pump2MoveBy(position); |
||||
|
case CHANNEL_3 -> DeviceCommandGenerator.pump3MoveBy(position); |
||||
|
case CHANNEL_4 -> DeviceCommandGenerator.pump4MoveBy(position); |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 根据通道code获取停止泵指令 |
||||
|
*/ |
||||
|
public DeviceCommand getPumpStopCommandByChannel(ChannelCode channelCode) { |
||||
|
return switch (channelCode) { |
||||
|
case CHANNEL_1 -> DeviceCommandGenerator.pump1Stop(); |
||||
|
case CHANNEL_2 -> DeviceCommandGenerator.pump2Stop(); |
||||
|
case CHANNEL_3 -> DeviceCommandGenerator.pump3Stop(); |
||||
|
case CHANNEL_4 -> DeviceCommandGenerator.pump4Stop(); |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 加液 |
||||
|
* |
||||
|
* @param channelCode 通道code |
||||
|
* @param volume 加液量 |
||||
|
*/ |
||||
|
public void solutionAddStart(ChannelCode channelCode, Double volume) { |
||||
|
|
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 停止加液 |
||||
|
* @param channelCode 通道code |
||||
|
*/ |
||||
|
public void solutionAddStop(ChannelCode channelCode) { |
||||
|
deviceState.getChannelStateMap().get(channelCode).setStateCode(ChannelStateCode.IDLE); |
||||
|
|
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue