From 4e7c555d50793a5748b8af8bd327937498adab63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=A2=A6=E8=BF=9C?= <1063331231@qq.com> Date: Wed, 23 Jul 2025 11:28:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=B0=83=E8=AF=95=E6=8C=87?= =?UTF-8?q?=E4=BB=A4=E8=9C=82=E9=B8=A3=E5=99=A8=20=E6=8C=87=E7=A4=BA?= =?UTF-8?q?=E7=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/command/debug/BeepCloseCommand.java | 30 +++++++++++++++ .../app/command/debug/BeepOpenCommand.java | 44 +++++++++++++++++++++ .../app/command/debug/LightCloseCommand.java | 30 +++++++++++++++ .../app/command/debug/LightOpenCommand.java | 45 ++++++++++++++++++++++ .../app/core/command/DeviceCommandGenerator.java | 24 ++++++++++-- .../colortitration/common/base/IBaseEnum.java | 13 +++++++ .../common/command/DeviceCommandParams.java | 2 + .../colortitration/common/enums/BeepMode.java | 19 +++++++++ .../colortitration/common/enums/Device.java | 4 +- .../common/enums/TricolorLightColor.java | 16 +++++++- 10 files changed, 220 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/iflytop/colortitration/app/command/debug/BeepCloseCommand.java create mode 100644 src/main/java/com/iflytop/colortitration/app/command/debug/BeepOpenCommand.java create mode 100644 src/main/java/com/iflytop/colortitration/app/command/debug/LightCloseCommand.java create mode 100644 src/main/java/com/iflytop/colortitration/app/command/debug/LightOpenCommand.java create mode 100644 src/main/java/com/iflytop/colortitration/common/enums/BeepMode.java diff --git a/src/main/java/com/iflytop/colortitration/app/command/debug/BeepCloseCommand.java b/src/main/java/com/iflytop/colortitration/app/command/debug/BeepCloseCommand.java new file mode 100644 index 0000000..5783523 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/command/debug/BeepCloseCommand.java @@ -0,0 +1,30 @@ +package com.iflytop.colortitration.app.command.debug; + +import com.iflytop.colortitration.app.core.command.BaseCommandHandler; +import com.iflytop.colortitration.app.core.command.DeviceCommand; +import com.iflytop.colortitration.app.core.command.DeviceCommandGenerator; +import com.iflytop.colortitration.app.model.dto.CommandDTO; +import com.iflytop.colortitration.app.service.DeviceCommandService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.util.concurrent.CompletableFuture; + +/** + * 蜂鸣器关闭 + */ +@Slf4j +@Component +@RequiredArgsConstructor +public class BeepCloseCommand extends BaseCommandHandler { + private final DeviceCommandService deviceCommandService; + @Override + public CompletableFuture handle(CommandDTO commandDTO) { + return runAsync(() -> { + DeviceCommand deviceCommand = DeviceCommandGenerator.beepClose(); + deviceCommandService.sendCommand(commandDTO.getCommandId(), commandDTO.getCommand(), deviceCommand); + }); + } +} + diff --git a/src/main/java/com/iflytop/colortitration/app/command/debug/BeepOpenCommand.java b/src/main/java/com/iflytop/colortitration/app/command/debug/BeepOpenCommand.java new file mode 100644 index 0000000..9410165 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/command/debug/BeepOpenCommand.java @@ -0,0 +1,44 @@ +package com.iflytop.colortitration.app.command.debug; + +import com.iflytop.colortitration.app.core.command.BaseCommandHandler; +import com.iflytop.colortitration.app.core.command.DeviceCommand; +import com.iflytop.colortitration.app.core.command.DeviceCommandGenerator; +import com.iflytop.colortitration.app.model.dto.CommandDTO; +import com.iflytop.colortitration.app.service.DeviceCommandService; +import com.iflytop.colortitration.common.base.IBaseEnum; +import com.iflytop.colortitration.common.enums.BeepMode; +import com.iflytop.colortitration.common.exception.AppException; +import com.iflytop.colortitration.common.result.ResultCode; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Component; + +import java.util.concurrent.CompletableFuture; + +/** + * 蜂鸣器开启 + */ +@Slf4j +@Component +@RequiredArgsConstructor +public class BeepOpenCommand extends BaseCommandHandler { + private final DeviceCommandService deviceCommandService; + + @Override + public CompletableFuture handle(CommandDTO commandDTO) { + String modeStr = commandDTO.getStringParam("mode"); + if (StringUtils.isEmpty(modeStr)) { + throw new AppException(ResultCode.INVALID_PARAMETER);//参数缺失 + } + if (!IBaseEnum.contains(BeepMode.class, modeStr)) { + throw new AppException(ResultCode.PARAMETER_TYPE_MISMATCH);//参数类型不匹配 + } + BeepMode mode = BeepMode.valueOf(modeStr); + return runAsync(() -> { + DeviceCommand deviceCommand = DeviceCommandGenerator.beepOpen(mode); + deviceCommandService.sendCommand(commandDTO.getCommandId(), commandDTO.getCommand(), deviceCommand); + }); + } +} + diff --git a/src/main/java/com/iflytop/colortitration/app/command/debug/LightCloseCommand.java b/src/main/java/com/iflytop/colortitration/app/command/debug/LightCloseCommand.java new file mode 100644 index 0000000..c9cad9d --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/command/debug/LightCloseCommand.java @@ -0,0 +1,30 @@ +package com.iflytop.colortitration.app.command.debug; + +import com.iflytop.colortitration.app.core.command.BaseCommandHandler; +import com.iflytop.colortitration.app.core.command.DeviceCommand; +import com.iflytop.colortitration.app.core.command.DeviceCommandGenerator; +import com.iflytop.colortitration.app.model.dto.CommandDTO; +import com.iflytop.colortitration.app.service.DeviceCommandService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.util.concurrent.CompletableFuture; + +/** + * 指示灯关闭 + */ +@Slf4j +@Component +@RequiredArgsConstructor +public class LightCloseCommand extends BaseCommandHandler { + private final DeviceCommandService deviceCommandService; + @Override + public CompletableFuture handle(CommandDTO commandDTO) { + return runAsync(() -> { + DeviceCommand deviceCommand = DeviceCommandGenerator.tricolorLightClose(); + deviceCommandService.sendCommand(commandDTO.getCommandId(), commandDTO.getCommand(), deviceCommand); + }); + } +} + diff --git a/src/main/java/com/iflytop/colortitration/app/command/debug/LightOpenCommand.java b/src/main/java/com/iflytop/colortitration/app/command/debug/LightOpenCommand.java new file mode 100644 index 0000000..931d82d --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/command/debug/LightOpenCommand.java @@ -0,0 +1,45 @@ +package com.iflytop.colortitration.app.command.debug; + +import com.iflytop.colortitration.app.core.command.BaseCommandHandler; +import com.iflytop.colortitration.app.core.command.DeviceCommand; +import com.iflytop.colortitration.app.core.command.DeviceCommandGenerator; +import com.iflytop.colortitration.app.model.dto.CommandDTO; +import com.iflytop.colortitration.app.service.DeviceCommandService; +import com.iflytop.colortitration.common.base.IBaseEnum; +import com.iflytop.colortitration.common.enums.BeepMode; +import com.iflytop.colortitration.common.enums.TricolorLightColor; +import com.iflytop.colortitration.common.exception.AppException; +import com.iflytop.colortitration.common.result.ResultCode; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Component; + +import java.util.concurrent.CompletableFuture; + +/** + * 指示灯开启 + */ +@Slf4j +@Component +@RequiredArgsConstructor +public class LightOpenCommand extends BaseCommandHandler { + private final DeviceCommandService deviceCommandService; + + @Override + public CompletableFuture handle(CommandDTO commandDTO) { + String colorStr = commandDTO.getStringParam("color"); + if (StringUtils.isEmpty(colorStr)) { + throw new AppException(ResultCode.INVALID_PARAMETER);//参数缺失 + } + if (!IBaseEnum.contains(TricolorLightColor.class, colorStr)) { + throw new AppException(ResultCode.PARAMETER_TYPE_MISMATCH);//参数类型不匹配 + } + TricolorLightColor color = TricolorLightColor.valueOf(colorStr); + return runAsync(() -> { + DeviceCommand deviceCommand = DeviceCommandGenerator.tricolorLightOpen(color); + deviceCommandService.sendCommand(commandDTO.getCommandId(), commandDTO.getCommand(), deviceCommand); + }); + } +} + diff --git a/src/main/java/com/iflytop/colortitration/app/core/command/DeviceCommandGenerator.java b/src/main/java/com/iflytop/colortitration/app/core/command/DeviceCommandGenerator.java index 5c35a0d..1bc3da3 100644 --- a/src/main/java/com/iflytop/colortitration/app/core/command/DeviceCommandGenerator.java +++ b/src/main/java/com/iflytop/colortitration/app/core/command/DeviceCommandGenerator.java @@ -1,10 +1,7 @@ package com.iflytop.colortitration.app.core.command; import com.iflytop.colortitration.common.command.DeviceCommandParams; -import com.iflytop.colortitration.common.enums.Action; -import com.iflytop.colortitration.common.enums.Device; -import com.iflytop.colortitration.common.enums.MotorDirection; -import com.iflytop.colortitration.common.enums.TricolorLightColor; +import com.iflytop.colortitration.common.enums.*; /** * 生成给设备发送的指令 @@ -29,6 +26,25 @@ public class DeviceCommandGenerator { public static DeviceCommand tricolorLightClose() { return controlCmd(Device.TRICOLOR_LIGHT, Action.CLOSE, null); } + // =========================================== 蜂鸣器 ==================================================== + + /** + * 蜂鸣器开启 + * + * @param mode alarm | info + */ + public static DeviceCommand beepOpen(BeepMode mode) { + DeviceCommandParams params = new DeviceCommandParams(); + params.setMode(mode); + return controlCmd(Device.BEEP, Action.OPEN, params); + } + + /** + * 蜂鸣器关闭 + */ + public static DeviceCommand beepClose() { + return controlCmd(Device.BEEP, Action.CLOSE, null); + } // =========================================== 加热棒 ==================================================== /** diff --git a/src/main/java/com/iflytop/colortitration/common/base/IBaseEnum.java b/src/main/java/com/iflytop/colortitration/common/base/IBaseEnum.java index d6961a5..a8064a0 100644 --- a/src/main/java/com/iflytop/colortitration/common/base/IBaseEnum.java +++ b/src/main/java/com/iflytop/colortitration/common/base/IBaseEnum.java @@ -3,6 +3,7 @@ package com.iflytop.colortitration.common.base; import cn.hutool.core.util.ObjectUtil; +import java.util.Arrays; import java.util.EnumSet; import java.util.Objects; @@ -75,6 +76,18 @@ public interface IBaseEnum { } return value; } + /** + * 根据name值比对是否包含 + * + * @param enumClass + * @param name + * @return + */ + static > boolean contains(Class enumClass, String name) { + return Arrays.stream(enumClass.getEnumConstants()) + .map(Enum::name) + .anyMatch(name::equals); + } T getValue(); diff --git a/src/main/java/com/iflytop/colortitration/common/command/DeviceCommandParams.java b/src/main/java/com/iflytop/colortitration/common/command/DeviceCommandParams.java index f357b1f..0f7ee88 100644 --- a/src/main/java/com/iflytop/colortitration/common/command/DeviceCommandParams.java +++ b/src/main/java/com/iflytop/colortitration/common/command/DeviceCommandParams.java @@ -1,6 +1,7 @@ package com.iflytop.colortitration.common.command; +import com.iflytop.colortitration.common.enums.BeepMode; import com.iflytop.colortitration.common.enums.MotorDirection; import com.iflytop.colortitration.common.enums.TricolorLightColor; import lombok.Data; @@ -16,6 +17,7 @@ public class DeviceCommandParams { private Double distance; private Double temperature; private TricolorLightColor color; + private BeepMode mode; private Double x; private Double y; private Double z; diff --git a/src/main/java/com/iflytop/colortitration/common/enums/BeepMode.java b/src/main/java/com/iflytop/colortitration/common/enums/BeepMode.java new file mode 100644 index 0000000..7a2d525 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/enums/BeepMode.java @@ -0,0 +1,19 @@ +package com.iflytop.colortitration.common.enums; + + +import com.iflytop.colortitration.common.base.IBaseEnum; + +public enum BeepMode implements IBaseEnum { + alarm, // 报警音 + info; //提示音 + + @Override + public Object getValue() { + return null; + } + + @Override + public String getLabel() { + return ""; + } +} diff --git a/src/main/java/com/iflytop/colortitration/common/enums/Device.java b/src/main/java/com/iflytop/colortitration/common/enums/Device.java index 3cb88c1..599dace 100644 --- a/src/main/java/com/iflytop/colortitration/common/enums/Device.java +++ b/src/main/java/com/iflytop/colortitration/common/enums/Device.java @@ -22,10 +22,12 @@ public enum Device { STIR_MOTOR_1(HardwareType.STEPPER_MOTOR, "搅拌电机 1"), STIR_MOTOR_2(HardwareType.STEPPER_MOTOR, "搅拌电机 2"), CLAW(HardwareType.SERVO_MOTOR, "夹爪"), - DUAL_ROBOT(HardwareType.STEPPER_MOTOR, "双轴机械臂"), + DUAL_ROBOT_1(HardwareType.STEPPER_MOTOR, "双轴机械臂大臂"), + DUAL_ROBOT_2(HardwareType.STEPPER_MOTOR, "双轴机械臂小臂"), HEAT_ROD_1(HardwareType.IO_DEVICE, "加热棒 1"), HEAT_ROD_2(HardwareType.IO_DEVICE, "加热棒 2"), TRICOLOR_LIGHT(HardwareType.IO_DEVICE, "三色灯"), + BEEP(HardwareType.IO_DEVICE, "蜂鸣器"), ; /** diff --git a/src/main/java/com/iflytop/colortitration/common/enums/TricolorLightColor.java b/src/main/java/com/iflytop/colortitration/common/enums/TricolorLightColor.java index 791070f..b0f7d09 100644 --- a/src/main/java/com/iflytop/colortitration/common/enums/TricolorLightColor.java +++ b/src/main/java/com/iflytop/colortitration/common/enums/TricolorLightColor.java @@ -1,5 +1,17 @@ package com.iflytop.colortitration.common.enums; -public enum TricolorLightColor { - RED, GREEN, BLUE +import com.iflytop.colortitration.common.base.IBaseEnum; + +public enum TricolorLightColor implements IBaseEnum { + RED, GREEN, BLUE; + + @Override + public Object getValue() { + return null; + } + + @Override + public String getLabel() { + return ""; + } }