From 6202f02ecbe59729de11c41a69b62f05669b076b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=A2=A6=E8=BF=9C?= <1063331231@qq.com> Date: Thu, 31 Jul 2025 15:40:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=8E=B7=E5=8F=96position?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/command/control/PumpRotateStartCommand.java | 19 ++++++++++++++----- .../app/core/command/DeviceCommandGenerator.java | 16 ++++++++-------- .../handacid/app/core/state/RemoteControlState.java | 2 +- .../app/scheduled/BleGamepadStateScheduledTask.java | 5 ++--- .../hardware/command/handlers/BleGamepadHandler.java | 11 ++++------- .../hardware/command/handlers/IOCtrlHandler.java | 14 +++++--------- .../hardware/command/handlers/MotorHandler.java | 11 +++++------ 7 files changed, 39 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/iflytop/handacid/app/command/control/PumpRotateStartCommand.java b/src/main/java/com/iflytop/handacid/app/command/control/PumpRotateStartCommand.java index 0de86cd..6f737f4 100644 --- a/src/main/java/com/iflytop/handacid/app/command/control/PumpRotateStartCommand.java +++ b/src/main/java/com/iflytop/handacid/app/command/control/PumpRotateStartCommand.java @@ -1,5 +1,6 @@ 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.enums.ChannelCode; import com.iflytop.handacid.app.common.enums.Direction; @@ -46,14 +47,22 @@ public class PumpRotateStartCommand extends BaseCommandHandler { } return runAsync(() -> { 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); CommandFuture commandFuture = deviceCommandService.sendCommand(deviceCommand); 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); } else { DeviceCommand deviceCommand = channelCtrlService.getPumpBackwardRotateCommandByChannel(channelCode); diff --git a/src/main/java/com/iflytop/handacid/app/core/command/DeviceCommandGenerator.java b/src/main/java/com/iflytop/handacid/app/core/command/DeviceCommandGenerator.java index c33eeed..3d35817 100644 --- a/src/main/java/com/iflytop/handacid/app/core/command/DeviceCommandGenerator.java +++ b/src/main/java/com/iflytop/handacid/app/core/command/DeviceCommandGenerator.java @@ -46,7 +46,7 @@ public class DeviceCommandGenerator { public static DeviceCommand pump1MoveBy(double position) { DeviceCommandParams params = new DeviceCommandParams(); 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当前转数 */ 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) { DeviceCommandParams params = new DeviceCommandParams(); 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当前转数 */ 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) { DeviceCommandParams params = new DeviceCommandParams(); 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当前转数 */ 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) { DeviceCommandParams params = new DeviceCommandParams(); 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当前转数 */ public static DeviceCommand pump4GetPosition() { - return controlCmd(Device.PUMP_4, Action.GET, null); + return getInfoCmd(Device.PUMP_4); } //=========================================== 私有方法 ============================================================ diff --git a/src/main/java/com/iflytop/handacid/app/core/state/RemoteControlState.java b/src/main/java/com/iflytop/handacid/app/core/state/RemoteControlState.java index 17c55bc..0b0bd41 100644 --- a/src/main/java/com/iflytop/handacid/app/core/state/RemoteControlState.java +++ b/src/main/java/com/iflytop/handacid/app/core/state/RemoteControlState.java @@ -19,7 +19,7 @@ public class RemoteControlState { private volatile boolean connected = false; @Schema(description = "当前电量(0-100%)") - private volatile int batteryLevel = 0; + private volatile double batteryLevel = 0; @Schema(description = "是否正在充电") private volatile boolean charging = false; diff --git a/src/main/java/com/iflytop/handacid/app/scheduled/BleGamepadStateScheduledTask.java b/src/main/java/com/iflytop/handacid/app/scheduled/BleGamepadStateScheduledTask.java index ab31fd1..69d67d0 100644 --- a/src/main/java/com/iflytop/handacid/app/scheduled/BleGamepadStateScheduledTask.java +++ b/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); deviceState.getRemoteControlState().setConnected(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) { diff --git a/src/main/java/com/iflytop/handacid/hardware/command/handlers/BleGamepadHandler.java b/src/main/java/com/iflytop/handacid/hardware/command/handlers/BleGamepadHandler.java index 1ce6cab..fc48611 100644 --- a/src/main/java/com/iflytop/handacid/hardware/command/handlers/BleGamepadHandler.java +++ b/src/main/java/com/iflytop/handacid/hardware/command/handlers/BleGamepadHandler.java @@ -32,11 +32,10 @@ public class BleGamepadHandler extends CommandHandler { private final Map supportCmdDeviceMIdMap = supportCmdDeviceBletMap.entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().mid)); - private final Set supportActions = Set.of(Action.OPEN, Action.CLOSE); + private final Set supportActions = Set.of(Action.OPEN, Action.CLOSE, Action.GET); @Override - protected Map getSupportCmdDeviceMIdMap() - { + protected Map getSupportCmdDeviceMIdMap() { return supportCmdDeviceMIdMap; } @@ -55,12 +54,10 @@ public class BleGamepadHandler extends CommandHandler { } BleGamepadMid bleGamepadMid = supportCmdDeviceBletMap.get(device); - if (command.getAction() == Action.GET) - { + if (command.getAction() == Action.GET) { Double power = driver_.get_power_state(bleGamepadMid); Boolean is_connected = driver_.is_connected(bleGamepadMid); - result = new HashMap() - {{ + result = new HashMap() {{ put("power", power); put("is_connected", is_connected); }}; diff --git a/src/main/java/com/iflytop/handacid/hardware/command/handlers/IOCtrlHandler.java b/src/main/java/com/iflytop/handacid/hardware/command/handlers/IOCtrlHandler.java index d06f02a..5ec3ae3 100644 --- a/src/main/java/com/iflytop/handacid/hardware/command/handlers/IOCtrlHandler.java +++ b/src/main/java/com/iflytop/handacid/hardware/command/handlers/IOCtrlHandler.java @@ -23,7 +23,7 @@ import java.util.stream.Collectors; @RequiredArgsConstructor public class IOCtrlHandler extends CommandHandler { private final OutputIOCtrlDriver driver_; - private final Map supportCmdDeviceIOOutputMap = Map.ofEntries( + private final Map supportCmdDeviceIOOutputMap = Map.ofEntries( Map.entry(Device.VALVE_1, OutputIOMId.DO_VALVE1), Map.entry(Device.VALVE_2, OutputIOMId.DO_VALVE2), Map.entry(Device.VALVE_3, OutputIOMId.DO_VALVE3), @@ -33,11 +33,10 @@ public class IOCtrlHandler extends CommandHandler { private final Map supportCmdDeviceMIdMap = supportCmdDeviceIOOutputMap.entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().mid)); - private final Set supportActions = Set.of(Action.OPEN, Action.CLOSE); + private final Set supportActions = Set.of(Action.OPEN, Action.CLOSE, Action.GET); @Override - protected Map getSupportCmdDeviceMIdMap() - { + protected Map getSupportCmdDeviceMIdMap() { return supportCmdDeviceMIdMap; } @@ -62,12 +61,9 @@ public class IOCtrlHandler extends CommandHandler { } else if (command.getAction() == Action.CLOSE) { log.info("close {}", device); driver_.close(outputIOMId); - } - else if (command.getAction() == Action.GET) - { + } else if (command.getAction() == Action.GET) { Boolean state = driver_.getIOState(outputIOMId); - result = new HashMap() - {{ + result = new HashMap() {{ put("state", state); }}; } diff --git a/src/main/java/com/iflytop/handacid/hardware/command/handlers/MotorHandler.java b/src/main/java/com/iflytop/handacid/hardware/command/handlers/MotorHandler.java index 70fd586..532ca87 100644 --- a/src/main/java/com/iflytop/handacid/hardware/command/handlers/MotorHandler.java +++ b/src/main/java/com/iflytop/handacid/hardware/command/handlers/MotorHandler.java @@ -45,7 +45,8 @@ public class MotorHandler extends CommandHandler { Action.MOVE_BY, Action.ROTATE, Action.STOP, - Action.MOVE_END); + Action.MOVE_END, + Action.GET); @Override protected Map getSupportCmdDeviceMIdMap() { @@ -57,10 +58,9 @@ public class MotorHandler extends CommandHandler { return supportActions; } - public MotorDirect getMotorDirect(MotorDirection cmdDirection) - { + public MotorDirect getMotorDirect(MotorDirection cmdDirection) { return switch (cmdDirection) { - case FORWARD -> MotorDirect.FORWARD; + case FORWARD -> MotorDirect.FORWARD; case BACKWARD -> MotorDirect.BACKWARD; }; } @@ -126,8 +126,7 @@ public class MotorHandler extends CommandHandler { case GET -> { log.info("Motor {} Get Position", stepMotorMId.mid.getDescription()); double position = driver_.getPosition(stepMotorMId); - result = new HashMap() - {{ + result = new HashMap() {{ put("position", position); }}; }