Browse Source

增加调试指令蜂鸣器 指示灯

master
王梦远 2 weeks ago
parent
commit
4e7c555d50
  1. 30
      src/main/java/com/iflytop/colortitration/app/command/debug/BeepCloseCommand.java
  2. 44
      src/main/java/com/iflytop/colortitration/app/command/debug/BeepOpenCommand.java
  3. 30
      src/main/java/com/iflytop/colortitration/app/command/debug/LightCloseCommand.java
  4. 45
      src/main/java/com/iflytop/colortitration/app/command/debug/LightOpenCommand.java
  5. 24
      src/main/java/com/iflytop/colortitration/app/core/command/DeviceCommandGenerator.java
  6. 13
      src/main/java/com/iflytop/colortitration/common/base/IBaseEnum.java
  7. 2
      src/main/java/com/iflytop/colortitration/common/command/DeviceCommandParams.java
  8. 19
      src/main/java/com/iflytop/colortitration/common/enums/BeepMode.java
  9. 4
      src/main/java/com/iflytop/colortitration/common/enums/Device.java
  10. 16
      src/main/java/com/iflytop/colortitration/common/enums/TricolorLightColor.java

30
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<Void> handle(CommandDTO commandDTO) {
return runAsync(() -> {
DeviceCommand deviceCommand = DeviceCommandGenerator.beepClose();
deviceCommandService.sendCommand(commandDTO.getCommandId(), commandDTO.getCommand(), deviceCommand);
});
}
}

44
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<Void> 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);
});
}
}

30
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<Void> handle(CommandDTO commandDTO) {
return runAsync(() -> {
DeviceCommand deviceCommand = DeviceCommandGenerator.tricolorLightClose();
deviceCommandService.sendCommand(commandDTO.getCommandId(), commandDTO.getCommand(), deviceCommand);
});
}
}

45
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<Void> 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);
});
}
}

24
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);
}
// =========================================== 加热棒 ====================================================
/**

13
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<T> {
}
return value;
}
/**
* 根据name值比对是否包含
*
* @param enumClass
* @param name
* @return
*/
static <T extends Enum<T>> boolean contains(Class<T> enumClass, String name) {
return Arrays.stream(enumClass.getEnumConstants())
.map(Enum::name)
.anyMatch(name::equals);
}
T getValue();

2
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;

19
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 "";
}
}

4
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, "蜂鸣器"),
;
/**

16
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 "";
}
}
Loading…
Cancel
Save