Browse Source

修改获取position

master
王梦远 4 days ago
parent
commit
6202f02ecb
  1. 19
      src/main/java/com/iflytop/handacid/app/command/control/PumpRotateStartCommand.java
  2. 16
      src/main/java/com/iflytop/handacid/app/core/command/DeviceCommandGenerator.java
  3. 2
      src/main/java/com/iflytop/handacid/app/core/state/RemoteControlState.java
  4. 5
      src/main/java/com/iflytop/handacid/app/scheduled/BleGamepadStateScheduledTask.java
  5. 11
      src/main/java/com/iflytop/handacid/hardware/command/handlers/BleGamepadHandler.java
  6. 14
      src/main/java/com/iflytop/handacid/hardware/command/handlers/IOCtrlHandler.java
  7. 11
      src/main/java/com/iflytop/handacid/hardware/command/handlers/MotorHandler.java

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

@ -1,5 +1,6 @@
package com.iflytop.handacid.app.command.control; package com.iflytop.handacid.app.command.control;
import cn.hutool.json.JSONObject;
import com.iflytop.handacid.app.common.annotation.CommandMapping; import com.iflytop.handacid.app.common.annotation.CommandMapping;
import com.iflytop.handacid.app.common.enums.ChannelCode; import com.iflytop.handacid.app.common.enums.ChannelCode;
import com.iflytop.handacid.app.common.enums.Direction; import com.iflytop.handacid.app.common.enums.Direction;
@ -46,14 +47,22 @@ public class PumpRotateStartCommand extends BaseCommandHandler {
} }
return runAsync(() -> { return runAsync(() -> {
if (Direction.FORWARD.equals(direction)) { if (Direction.FORWARD.equals(direction)) {
DeviceCommand getPositionDeviceCommand = channelCtrlService.getPumpForwardRotateCommandByChannel(channelCode);
CommandFuture positionCommandFuture = deviceCommandService.sendCommand(getPositionDeviceCommand);
CommandUtil.wait(positionCommandFuture);
Integer currentPosition = (Integer) positionCommandFuture.getResponseResult().get("position");
DeviceCommand currentPositionDeviceCommand = channelCtrlService.getPumpPositionCommandByChannel(channelCode);
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(channelCode, position);
CommandFuture commandFuture = deviceCommandService.sendCommand(deviceCommand); CommandFuture commandFuture = deviceCommandService.sendCommand(deviceCommand);
CommandUtil.wait(commandFuture); CommandUtil.wait(commandFuture);
Integer newPosition = (Integer) commandFuture.getResponseResult().get("position");
DeviceCommand newPositionDeviceCommand = channelCtrlService.getPumpPositionCommandByChannel(channelCode);
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); webSocketSender.pushPumpPosition(newPosition - currentPosition);
} else { } else {
DeviceCommand deviceCommand = channelCtrlService.getPumpBackwardRotateCommandByChannel(channelCode); DeviceCommand deviceCommand = channelCtrlService.getPumpBackwardRotateCommandByChannel(channelCode);

16
src/main/java/com/iflytop/handacid/app/core/command/DeviceCommandGenerator.java

@ -46,7 +46,7 @@ public class DeviceCommandGenerator {
public static DeviceCommand pump1MoveBy(double position) { public static DeviceCommand pump1MoveBy(double position) {
DeviceCommandParams params = new DeviceCommandParams(); DeviceCommandParams params = new DeviceCommandParams();
params.setPosition(position); params.setPosition(position);
return controlCmd(Device.PUMP_1, Action.MOVE_BY, null);
return controlCmd(Device.PUMP_1, Action.MOVE_BY, params);
} }
/** /**
@ -67,7 +67,7 @@ public class DeviceCommandGenerator {
* 1当前转数 * 1当前转数
*/ */
public static DeviceCommand pump1GetPosition() { public static DeviceCommand pump1GetPosition() {
return controlCmd(Device.PUMP_1, Action.GET, null);
return getInfoCmd(Device.PUMP_1);
} }
/** /**
@ -103,7 +103,7 @@ public class DeviceCommandGenerator {
public static DeviceCommand pump2MoveBy(double position) { public static DeviceCommand pump2MoveBy(double position) {
DeviceCommandParams params = new DeviceCommandParams(); DeviceCommandParams params = new DeviceCommandParams();
params.setPosition(position); params.setPosition(position);
return controlCmd(Device.PUMP_2, Action.MOVE_BY, null);
return controlCmd(Device.PUMP_2, Action.MOVE_BY, params);
} }
/** /**
@ -124,7 +124,7 @@ public class DeviceCommandGenerator {
* 2当前转数 * 2当前转数
*/ */
public static DeviceCommand pump2GetPosition() { public static DeviceCommand pump2GetPosition() {
return controlCmd(Device.PUMP_2, Action.GET, null);
return getInfoCmd(Device.PUMP_2);
} }
/** /**
@ -160,7 +160,7 @@ public class DeviceCommandGenerator {
public static DeviceCommand pump3MoveBy(double position) { public static DeviceCommand pump3MoveBy(double position) {
DeviceCommandParams params = new DeviceCommandParams(); DeviceCommandParams params = new DeviceCommandParams();
params.setPosition(position); params.setPosition(position);
return controlCmd(Device.PUMP_3, Action.MOVE_BY, null);
return controlCmd(Device.PUMP_3, Action.MOVE_BY, params);
} }
/** /**
@ -181,7 +181,7 @@ public class DeviceCommandGenerator {
* 3当前转数 * 3当前转数
*/ */
public static DeviceCommand pump3GetPosition() { public static DeviceCommand pump3GetPosition() {
return controlCmd(Device.PUMP_3, Action.GET, null);
return getInfoCmd(Device.PUMP_3);
} }
/** /**
@ -217,7 +217,7 @@ public class DeviceCommandGenerator {
public static DeviceCommand pump4MoveBy(double position) { public static DeviceCommand pump4MoveBy(double position) {
DeviceCommandParams params = new DeviceCommandParams(); DeviceCommandParams params = new DeviceCommandParams();
params.setPosition(position); params.setPosition(position);
return controlCmd(Device.PUMP_4, Action.MOVE_BY, null);
return controlCmd(Device.PUMP_4, Action.MOVE_BY, params);
} }
/** /**
@ -238,7 +238,7 @@ public class DeviceCommandGenerator {
* 4当前转数 * 4当前转数
*/ */
public static DeviceCommand pump4GetPosition() { public static DeviceCommand pump4GetPosition() {
return controlCmd(Device.PUMP_4, Action.GET, null);
return getInfoCmd(Device.PUMP_4);
} }
//=========================================== 私有方法 ============================================================ //=========================================== 私有方法 ============================================================

2
src/main/java/com/iflytop/handacid/app/core/state/RemoteControlState.java

@ -19,7 +19,7 @@ public class RemoteControlState {
private volatile boolean connected = false; private volatile boolean connected = false;
@Schema(description = "当前电量(0-100%)") @Schema(description = "当前电量(0-100%)")
private volatile int batteryLevel = 0;
private volatile double batteryLevel = 0;
@Schema(description = "是否正在充电") @Schema(description = "是否正在充电")
private volatile boolean charging = false; private volatile boolean charging = false;

5
src/main/java/com/iflytop/handacid/app/scheduled/BleGamepadStateScheduledTask.java

@ -26,9 +26,8 @@ public class BleGamepadStateScheduledTask {
boolean is_connect = bleGamepadDriver.is_connected(BleGamepadMid.BleGamePad); boolean is_connect = bleGamepadDriver.is_connected(BleGamepadMid.BleGamePad);
deviceState.getRemoteControlState().setConnected(is_connect); deviceState.getRemoteControlState().setConnected(is_connect);
if (is_connect) { if (is_connect) {
Integer[] power_state = bleGamepadDriver.get_power_state(BleGamepadMid.BleGamePad);
deviceState.getRemoteControlState().setBatteryLevel(power_state[0]);
deviceState.getRemoteControlState().setCharging(power_state[1] != 0);
Double power_state = bleGamepadDriver.get_power_state(BleGamepadMid.BleGamePad);
deviceState.getRemoteControlState().setBatteryLevel(power_state);
} }
} }
} catch (Exception e) { } catch (Exception e) {

11
src/main/java/com/iflytop/handacid/hardware/command/handlers/BleGamepadHandler.java

@ -32,11 +32,10 @@ public class BleGamepadHandler extends CommandHandler {
private final Map<Device, MId> supportCmdDeviceMIdMap = supportCmdDeviceBletMap.entrySet().stream() private final Map<Device, MId> supportCmdDeviceMIdMap = supportCmdDeviceBletMap.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().mid)); .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().mid));
private final Set<Action> supportActions = Set.of(Action.OPEN, Action.CLOSE);
private final Set<Action> supportActions = Set.of(Action.OPEN, Action.CLOSE, Action.GET);
@Override @Override
protected Map<Device, MId> getSupportCmdDeviceMIdMap()
{
protected Map<Device, MId> getSupportCmdDeviceMIdMap() {
return supportCmdDeviceMIdMap; return supportCmdDeviceMIdMap;
} }
@ -55,12 +54,10 @@ public class BleGamepadHandler extends CommandHandler {
} }
BleGamepadMid bleGamepadMid = supportCmdDeviceBletMap.get(device); BleGamepadMid bleGamepadMid = supportCmdDeviceBletMap.get(device);
if (command.getAction() == Action.GET)
{
if (command.getAction() == Action.GET) {
Double power = driver_.get_power_state(bleGamepadMid); Double power = driver_.get_power_state(bleGamepadMid);
Boolean is_connected = driver_.is_connected(bleGamepadMid); Boolean is_connected = driver_.is_connected(bleGamepadMid);
result = new HashMap<String, Object>()
{{
result = new HashMap<String, Object>() {{
put("power", power); put("power", power);
put("is_connected", is_connected); put("is_connected", is_connected);
}}; }};

14
src/main/java/com/iflytop/handacid/hardware/command/handlers/IOCtrlHandler.java

@ -23,7 +23,7 @@ import java.util.stream.Collectors;
@RequiredArgsConstructor @RequiredArgsConstructor
public class IOCtrlHandler extends CommandHandler { public class IOCtrlHandler extends CommandHandler {
private final OutputIOCtrlDriver driver_; private final OutputIOCtrlDriver driver_;
private final Map<Device, OutputIOMId> supportCmdDeviceIOOutputMap = Map.ofEntries(
private final Map<Device, OutputIOMId> supportCmdDeviceIOOutputMap = Map.ofEntries(
Map.entry(Device.VALVE_1, OutputIOMId.DO_VALVE1), Map.entry(Device.VALVE_1, OutputIOMId.DO_VALVE1),
Map.entry(Device.VALVE_2, OutputIOMId.DO_VALVE2), Map.entry(Device.VALVE_2, OutputIOMId.DO_VALVE2),
Map.entry(Device.VALVE_3, OutputIOMId.DO_VALVE3), Map.entry(Device.VALVE_3, OutputIOMId.DO_VALVE3),
@ -33,11 +33,10 @@ public class IOCtrlHandler extends CommandHandler {
private final Map<Device, MId> supportCmdDeviceMIdMap = supportCmdDeviceIOOutputMap.entrySet().stream() private final Map<Device, MId> supportCmdDeviceMIdMap = supportCmdDeviceIOOutputMap.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().mid)); .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().mid));
private final Set<Action> supportActions = Set.of(Action.OPEN, Action.CLOSE);
private final Set<Action> supportActions = Set.of(Action.OPEN, Action.CLOSE, Action.GET);
@Override @Override
protected Map<Device, MId> getSupportCmdDeviceMIdMap()
{
protected Map<Device, MId> getSupportCmdDeviceMIdMap() {
return supportCmdDeviceMIdMap; return supportCmdDeviceMIdMap;
} }
@ -62,12 +61,9 @@ public class IOCtrlHandler extends CommandHandler {
} else if (command.getAction() == Action.CLOSE) { } else if (command.getAction() == Action.CLOSE) {
log.info("close {}", device); log.info("close {}", device);
driver_.close(outputIOMId); driver_.close(outputIOMId);
}
else if (command.getAction() == Action.GET)
{
} else if (command.getAction() == Action.GET) {
Boolean state = driver_.getIOState(outputIOMId); Boolean state = driver_.getIOState(outputIOMId);
result = new HashMap<String, Object>()
{{
result = new HashMap<String, Object>() {{
put("state", state); put("state", state);
}}; }};
} }

11
src/main/java/com/iflytop/handacid/hardware/command/handlers/MotorHandler.java

@ -45,7 +45,8 @@ public class MotorHandler extends CommandHandler {
Action.MOVE_BY, Action.MOVE_BY,
Action.ROTATE, Action.ROTATE,
Action.STOP, Action.STOP,
Action.MOVE_END);
Action.MOVE_END,
Action.GET);
@Override @Override
protected Map<Device, MId> getSupportCmdDeviceMIdMap() { protected Map<Device, MId> getSupportCmdDeviceMIdMap() {
@ -57,10 +58,9 @@ public class MotorHandler extends CommandHandler {
return supportActions; return supportActions;
} }
public MotorDirect getMotorDirect(MotorDirection cmdDirection)
{
public MotorDirect getMotorDirect(MotorDirection cmdDirection) {
return switch (cmdDirection) { return switch (cmdDirection) {
case FORWARD -> MotorDirect.FORWARD;
case FORWARD -> MotorDirect.FORWARD;
case BACKWARD -> MotorDirect.BACKWARD; case BACKWARD -> MotorDirect.BACKWARD;
}; };
} }
@ -126,8 +126,7 @@ public class MotorHandler extends CommandHandler {
case GET -> { case GET -> {
log.info("Motor {} Get Position", stepMotorMId.mid.getDescription()); log.info("Motor {} Get Position", stepMotorMId.mid.getDescription());
double position = driver_.getPosition(stepMotorMId); double position = driver_.getPosition(stepMotorMId);
result = new HashMap<String, Object>()
{{
result = new HashMap<String, Object>() {{
put("position", position); put("position", position);
}}; }};
} }

Loading…
Cancel
Save