Browse Source

feat:手柄控制是否开始预充、加液

master
白凤吉 4 days ago
parent
commit
d08f9091cc
  1. 17
      src/main/java/com/iflytop/handacid/app/command/control/SolutionAddStopCommand.java
  2. 18
      src/main/java/com/iflytop/handacid/app/command/control/SolutionPreFillStopCommand.java
  3. 26
      src/main/java/com/iflytop/handacid/app/core/listener/BleGamepadEventListener.java
  4. 22
      src/main/java/com/iflytop/handacid/app/core/state/DeviceState.java
  5. 65
      src/main/java/com/iflytop/handacid/app/service/ChannelCtrlService.java

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

@ -27,28 +27,13 @@ import java.util.concurrent.CompletableFuture;
@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);
}
}
channelCtrlService.solutionAddStop();
});
}
}

18
src/main/java/com/iflytop/handacid/app/command/control/SolutionPreFillStopCommand.java

@ -27,29 +27,13 @@ import java.util.concurrent.CompletableFuture;
@RequiredArgsConstructor
@CommandMapping("solution_pre_fill_stop")
public class SolutionPreFillStopCommand 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.PRE);
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.setPre(true);
channelCode.setStateCode(ChannelStateCode.IDLE);
}
}
channelCtrlService.solutionPreFillStop();
});
}
}

26
src/main/java/com/iflytop/handacid/app/core/listener/BleGamepadEventListener.java

@ -1,5 +1,7 @@
package com.iflytop.handacid.app.core.listener;
import com.iflytop.handacid.app.core.state.DeviceState;
import com.iflytop.handacid.app.service.ChannelCtrlService;
import com.iflytop.handacid.hardware.service.AppEventBusService;
import com.iflytop.handacid.hardware.type.A8kPacket;
import com.iflytop.handacid.hardware.type.CmdId;
@ -12,7 +14,6 @@ import org.springframework.stereotype.Component;
/**
* 蓝牙手柄按钮事件监听
*
*/
@Component
@ -20,6 +21,8 @@ import org.springframework.stereotype.Component;
@RequiredArgsConstructor
public class BleGamepadEventListener {
private final AppEventBusService eventBus;
private final ChannelCtrlService channelCtrlService;
private final DeviceState deviceState;
@PostConstruct
synchronized public void init() {
@ -28,23 +31,34 @@ public class BleGamepadEventListener {
/**
* 事件总线事件处理
*
* @param event e
*/
private void onAppEvent(AppEvent event) {
if (event instanceof A8kHardwareReport canPacket) {
A8kPacket packet = canPacket.getReportPacket();
CmdId cmdId = CmdId.valueOf(packet.getCmdId());
CmdId cmdId = CmdId.valueOf(packet.getCmdId());
if (CmdId.event_ble_gamepad_liquid_acid.equals(cmdId)) {
//判断当前的设备状态
log.info("蓝牙手柄 加酸按钮 按下");
if(deviceState.isSolutionAdding()){
channelCtrlService.solutionAddStop();
}else{
channelCtrlService.solutionAddStart();
}
} else if (CmdId.event_ble_gamepad_liquid_acid_prefilling.equals(cmdId)) {
log.info("蓝牙手柄 预充按钮 按下");
}
else if (CmdId.event_ble_gamepad_connected.equals(cmdId)) {
if(deviceState.isSolutionPreFillStart()){
channelCtrlService.solutionPreFillStart();
}else{
channelCtrlService.solutionPreFillStop();
}
} else if (CmdId.event_ble_gamepad_connected.equals(cmdId)) {
log.info("蓝牙手柄 连接成功");
}
else if (CmdId.event_ble_gamepad_disconnected.equals(cmdId)) {
deviceState.setBluetoothConnected(true);
} else if (CmdId.event_ble_gamepad_disconnected.equals(cmdId)) {
log.info("蓝牙手柄 连接断开");
deviceState.setBluetoothConnected(false);
}
}

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

@ -40,6 +40,9 @@ public class DeviceState {
@Schema(description = "是否是急停状态,true为急停")
private volatile boolean emergencyStop = false;
@Schema(description = "蓝牙是否连接")
private volatile boolean bluetoothConnected = false;
@Schema(description = "当前登录用户")
private volatile User currentUser;
@ -51,6 +54,7 @@ public class DeviceState {
json.putOnce("virtual", virtual);
json.putOnce("initComplete", initComplete);
json.putOnce("emergencyStop", emergencyStop);
json.putOnce("bluetoothConnected", bluetoothConnected);
json.putOnce("currentUser", currentUser);
return json;
}
@ -81,4 +85,22 @@ public class DeviceState {
.filter(ChannelState::isSelected)
.collect(Collectors.toList());
}
/**
* 是否正在加液
* 如果有任何通道处于 ADD 状态
*/
public boolean isSolutionAdding() {
return channelStateMap.values().stream()
.anyMatch(cs -> cs.getStateCode() == ChannelStateCode.ADD);
}
/**
* 是否正在预充
* 如果有任何通道处于 PRE 状态
*/
public boolean isSolutionPreFillStart() {
return channelStateMap.values().stream()
.anyMatch(cs -> cs.getStateCode() == ChannelStateCode.ADD);
}
}

65
src/main/java/com/iflytop/handacid/app/service/ChannelCtrlService.java

@ -74,6 +74,28 @@ public class ChannelCtrlService {
}
/**
* 停止加液
*/
public void solutionAddStop() {
List<ChannelState> channelCodeList = deviceState.filterChannelStatesByState(ChannelStateCode.ADD);
deviceState.setSolutionAddStop(true);
try {
List<CommandFuture> commandFutureList = new ArrayList<>();
for (ChannelState channelState : channelCodeList) {
DeviceCommand deviceCommand = getPumpStopCommandByChannel(channelState.getChannelCode());
commandFutureList.add(deviceCommandService.sendCommand(deviceCommand));
}
CommandUtil.wait(commandFutureList);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
for (ChannelState channelCode : channelCodeList) {
channelCode.setStateCode(ChannelStateCode.IDLE);
}
}
}
/**
* 开始预充
*/
public void solutionPreFillStart() {
@ -96,6 +118,31 @@ public class ChannelCtrlService {
}
/**
* 结束预充
*/
public void solutionPreFillStop() {
CompletableFuture.runAsync(() -> {
List<ChannelState> channelCodeList = deviceState.filterChannelStatesByState(ChannelStateCode.PRE);
deviceState.setSolutionAddStop(true);
try {
List<CommandFuture> commandFutureList = new ArrayList<>();
for (ChannelState channelState : channelCodeList) {
DeviceCommand deviceCommand = getPumpStopCommandByChannel(channelState.getChannelCode());
commandFutureList.add(deviceCommandService.sendCommand(deviceCommand));
}
CommandUtil.wait(commandFutureList);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
for (ChannelState channelCode : channelCodeList) {
channelCode.setPre(true);
channelCode.setStateCode(ChannelStateCode.IDLE);
}
}
});
}
/**
* 根据通道code获取泵相对移动指令
*/
public DeviceCommand getPumpMoveByCommandByChannel(ChannelCode channelCode, Double position) {
@ -143,23 +190,5 @@ public class ChannelCtrlService {
};
}
/**
* 加液
*
* @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);
}
}
Loading…
Cancel
Save