Browse Source

fix:bug修复

master
guoapeng 5 months ago
parent
commit
825da4bcbb
  1. BIN
      matrix-spray.db
  2. 3
      sql/init.sql
  3. 25
      src/main/java/com/qyft/ms/app/controller/CMDController.java
  4. 3
      src/main/java/com/qyft/ms/app/controller/MatrixController.java
  5. 3
      src/main/java/com/qyft/ms/app/controller/MatrixCraftController.java
  6. 8
      src/main/java/com/qyft/ms/app/model/dto/MatrixCraftDTO.java
  7. 79
      src/main/java/com/qyft/ms/app/service/CMDService.java
  8. 3
      src/main/java/com/qyft/ms/app/service/ISysSettingsService.java
  9. 11
      src/main/java/com/qyft/ms/app/service/impl/ISysSettingsServiceImpl.java
  10. 3
      src/main/java/com/qyft/ms/app/service/impl/MatrixCraftServiceImpl.java
  11. 1
      src/main/java/com/qyft/ms/device/client/TcpClient.java
  12. 13
      src/main/java/com/qyft/ms/device/common/constant/DeviceCommands.java
  13. 95
      src/main/java/com/qyft/ms/device/service/DeviceTcpCMDService.java
  14. 3
      src/main/resources/application.yml
  15. 300
      src/test/java/com/qyft/ms/CMDServiceTest.java
  16. 9
      src/test/java/com/qyft/ms/SprayTest.java

BIN
matrix-spray.db

3
sql/init.sql

@ -99,4 +99,5 @@ VALUES (1, NULL, '玻片位置', 'slide_position', NULL),
(10, NULL, '电机运行速度', 'speed', NULL),
(11, 10, 'x轴速度', 'x_speed', 10),
(12, 10, 'y轴速度', 'y_speed', 10),
(13, 10, 'z轴速度', 'z_speed', 10);
(13, 10, 'z轴速度', 'z_speed', 10),
(14, NULL, '废液桶位置','waste_liquor', '173.08,75,70' );

25
src/main/java/com/qyft/ms/app/controller/CMDController.java

@ -281,29 +281,6 @@ public class CMDController {
}
}
@Operation(summary = "测试: 以指定转速运行指定时间")
@PostMapping("/rotate")
public Result<String> rotate(@RequestBody CMDForm cmdForm) {
try {
if (cmdForm.getCommandId() == null || cmdForm.getCommandId().isEmpty()) {
String commandId = UUID.randomUUID().toString();
cmdForm.setCommandId(commandId);
}
if (cmdForm.getCommandName() == null || cmdForm.getCommandName().isEmpty()) {
cmdForm.setCommandName("rotate");
}
log.info("接收到指令: {}", JSONUtil.toJsonStr(cmdForm));
if (cmdService.rotate(cmdForm)) {
return Result.success(cmdForm.getCommandId());
} else {
return Result.failed("参数错误");
}
} catch (Exception e) {
log.error("指令执行异常: {}", JSONUtil.toJsonStr(cmdForm), e);
return Result.failed("执行失败");
}
}
@Operation(summary = "设定指定轴的速度")
@PostMapping("/setMotorSpeed")
public Result<String> setMotorSpeed(@RequestBody CMDForm cmdForm) {
@ -373,7 +350,7 @@ public class CMDController {
}
}
@Operation(summary = "以指定亮度开启照明灯板")
@Operation(summary = "开启照明灯板")
@PostMapping("/turnOnLightPanel")
public Result<String> turnOnLightPanel(@RequestBody CMDForm cmdForm) {
try {

3
src/main/java/com/qyft/ms/app/controller/MatrixController.java

@ -1,15 +1,12 @@
package com.qyft.ms.app.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qyft.ms.app.model.dto.MatrixDTO;
import com.qyft.ms.app.model.entity.Matrix;
import com.qyft.ms.app.model.vo.DeleteMatrixResult;
import com.qyft.ms.app.service.MatrixService;
import com.qyft.ms.device.model.entity.CtrlFuncStep;
import com.qyft.ms.system.common.base.BasePageQuery;
import com.qyft.ms.system.common.result.PageResult;
import com.qyft.ms.system.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;

3
src/main/java/com/qyft/ms/app/controller/MatrixCraftController.java

@ -15,6 +15,7 @@ import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@ -32,7 +33,7 @@ public class MatrixCraftController {
@Operation(summary = "新增基质工艺")
@PostMapping("/add")
public Result<Integer> add(@RequestBody MatrixCraftDTO dto) {
public Result<Integer> add(@RequestBody @Validated MatrixCraftDTO dto) {
User user = iUserService.currentUser();
if (user != null) {
dto.setCreateUser(user.getId());

8
src/main/java/com/qyft/ms/app/model/dto/MatrixCraftDTO.java

@ -7,15 +7,19 @@ import lombok.Data;
@Data
public class MatrixCraftDTO {
@NotBlank
@Schema(description = "名称")
private String name;
@NotBlank
@Schema(description = "基质id")
private Long matrixId;
@NotBlank
@Schema(description = "路径类型 0 竖向 1 横向")
private Integer routeType;
@NotBlank
@Schema(description = "喷涂高度")
private Integer height;
@ -25,15 +29,19 @@ public class MatrixCraftDTO {
@Schema(description = "氮气气压")
private Integer nitrogenAirPressure;
@NotBlank
@Schema(description = "基质流速")
private Integer matrixFlowVelocity;
@NotBlank
@Schema(description = "电压")
private Integer voltage;
@NotBlank
@Schema(description = "移速")
private Integer movementSpeed;
@NotBlank
@Schema(description = "行间距")
private Integer space;

79
src/main/java/com/qyft/ms/app/service/CMDService.java

@ -9,7 +9,6 @@ import com.qyft.ms.app.model.entity.OperationLog;
import com.qyft.ms.app.model.entity.SysSettings;
import com.qyft.ms.app.model.form.CMDForm;
import com.qyft.ms.app.model.vo.ExecutionResult;
import com.qyft.ms.device.controller.DeviceController;
import com.qyft.ms.device.model.bo.DeviceStatus;
import com.qyft.ms.device.service.DeviceStatusService;
import com.qyft.ms.device.service.DeviceTcpCMDService;
@ -18,7 +17,6 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.List;
import java.util.function.Supplier;
import static com.qyft.ms.app.common.generator.PathGenerator.generatePathPoints;
@ -31,7 +29,6 @@ public class CMDService {
private final WebSocketService webSocketService;
private final DeviceTcpCMDService deviceTcpCMDService;
private final OperationLogService operationLogService;
private final MatrixCraftService matrixCraftService;
private final ISysSettingsService sysSettingsService;
private final DeviceStatusService deviceStatusService;
private volatile boolean running = true;
@ -67,13 +64,7 @@ public class CMDService {
}
List<Supplier<Boolean>> cmdList = new ArrayList<>();
String type = (String) params.get("type");
if (type.equals("clean")) {
cmdList.add(deviceTcpCMDService::switchThreeWayValveToSubstrate);
} else if (type.equals("spray")) {
cmdList.add(deviceTcpCMDService::switchThreeWayValveToSpray);
}else {
return false;
}
cmdList.add(() -> deviceTcpCMDService.switchThreeWayValve(type));
initExecutorThread(cmdList, form);
return true;
}
@ -81,14 +72,14 @@ public class CMDService {
// 除湿阀清洗阀喷嘴阀控制方法
public boolean controlValve(CMDForm form) {
Map<String, Object> params = form.getParams();
if (params == null || !params.containsKey("valveType") || !params.containsKey("isOpen")) {
if (params == null || !params.containsKey("type") || !params.containsKey("isOpen")) {
return false;
}
List<Supplier<Boolean>> cmdList = new ArrayList<>();
if(params.get("valveType") !=null && params.get("isOpen") !=null) {
String valveType = (String) params.get("valveType");
if(params.get("type") !=null && params.get("isOpen") !=null) {
String type = (String) params.get("type");
boolean isOpen = (boolean) params.get("isOpen");
cmdList.add(() -> deviceTcpCMDService.controlValve(valveType,isOpen));
cmdList.add(() -> deviceTcpCMDService.controlValve(type,isOpen));
}
initExecutorThread(cmdList, form);
return true;
@ -175,7 +166,8 @@ public class CMDService {
!params.containsKey("routeType") ||
!params.containsKey("movementSpeed") ||
!params.containsKey("height") ||
!params.containsKey("matrixFlowVelocity")
!params.containsKey("matrixFlowVelocity") ||
!params.containsKey("nitrogenFlowVelocity")
) {
return "参数错误";
}
@ -193,9 +185,9 @@ public class CMDService {
// 设置指定轴的电机的运行速度
int movementSpeed = (Integer) params.get("movementSpeed");
cmdList.add(() -> deviceTcpCMDService.setMotorSpeed("x", movementSpeed));
cmdList.add(() -> deviceTcpCMDService.setMotorSpeed("y", movementSpeed));
cmdList.add(() -> deviceTcpCMDService.setMotorSpeed("z", 10));
cmdList.add(() -> deviceTcpCMDService.setMotorSpeed("X", movementSpeed));
cmdList.add(() -> deviceTcpCMDService.setMotorSpeed("Y", movementSpeed));
cmdList.add(() -> deviceTcpCMDService.setMotorSpeed("Z", 10));
// 回到原点
cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("Z", 5));
@ -244,7 +236,7 @@ public class CMDService {
// 移动到指定高度(位置)
int height = (Integer) params.get("height");
if(z - height < 15) {
if(height < 15) {
return "高度设置太低,有撞针的风险";
}
cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("Z",z - height ));
@ -290,7 +282,7 @@ public class CMDService {
cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("Y", nextY));
}
// 停止喷涂
cmdList.add(deviceTcpCMDService::syringePumpStop);
cmdList.add(deviceTcpCMDService::turnOffSyringePump);
// 关闭喷嘴阀
cmdList.add(() -> deviceTcpCMDService.controlValve("Nozzle", true));
}
@ -338,13 +330,13 @@ public class CMDService {
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }
if (!result) {
log.error("指令执行异常: {}", JSONUtil.toJsonStr(form));
executionResult.setStatus(CMDResultCode.FAILURE.getCode());
executionResult.setMessage(CMDResultCode.FAILURE.getMsg());
webSocketService.pushMsg(WebSocketMessageType.CMD, executionResult);
return;
}
// if (!result) {
// log.error("指令执行异常: {}", JSONUtil.toJsonStr(form));
// executionResult.setStatus(CMDResultCode.FAILURE.getCode());
// executionResult.setMessage(CMDResultCode.FAILURE.getMsg());
// webSocketService.pushMsg(WebSocketMessageType.CMD, executionResult);
// return;
// }
}
log.info("指令执行成功: {}", JSONUtil.toJsonStr(form));
executionResult.setStatus(CMDResultCode.SUCCESS.getCode());
@ -386,21 +378,6 @@ public class CMDService {
return true;
}
// 测试轴转动
public boolean rotate(CMDForm form) {
Map<String, Object> params = form.getParams();
List<Supplier<Boolean>> cmdList = new ArrayList<>();
String axis = (String) params.get("axis");
double rotationSpeed = Optional.ofNullable(params.get("rotationSpeed"))
.filter(Number.class::isInstance)
.map(Number.class::cast)
.map(Number::doubleValue)
.orElse(0.0);
int time = (Integer) params.get("time");
cmdList.add(() -> deviceTcpCMDService.rotateMotor(axis, rotationSpeed, time));
initExecutorThread(cmdList, form);
return true;
}
// 轴停止转动
public boolean stopMotor(CMDForm form) {
@ -423,9 +400,10 @@ public class CMDService {
cmdList.add(() -> deviceTcpCMDService.switchThreeWayValve("clear_spray"));
cmdList.add(() -> deviceTcpCMDService.controlValve("Cleaning", true));
} else if (Objects.equals(type, "nozzle")) {
cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("X", 173.08));
cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("Y", 75.2));
cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("Z", 70));
Map<String, Double> wasteLiquorPosition = sysSettingsService.getWasteLiquorPosition();
cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("X", wasteLiquorPosition.get("x")));
cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("Y", wasteLiquorPosition.get("y")));
cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("Z", wasteLiquorPosition.get("z")));
cmdList.add(() -> deviceTcpCMDService.switchThreeWayValve("clear_nozzle"));
cmdList.add(() -> deviceTcpCMDService.controlValve("Nozzle", true));
}
@ -460,8 +438,7 @@ public class CMDService {
public boolean turnOnLightPanel(CMDForm form) {
Map<String, Object> params = form.getParams();
List<Supplier<Boolean>> cmdList = new ArrayList<>();
int brightness = (int) params.get("brightness");
cmdList.add(() -> deviceTcpCMDService.turnOnLightPanel(brightness));
cmdList.add(() -> deviceTcpCMDService.turnOnLightPanel());
initExecutorThread(cmdList, form);
return true;
}
@ -492,7 +469,7 @@ public class CMDService {
initExecutorThread(cmdList, form);
return true;
}
// 停止预充
// 停止预充
public boolean stopPrefill(CMDForm form) {
List<Supplier<Boolean>> cmdList = new ArrayList<>();
cmdList.add(deviceTcpCMDService::turnOffSyringePump);
@ -529,7 +506,7 @@ public class CMDService {
.map(Number.class::cast)
.map(Number::doubleValue)
.orElse(0.0);
cmdList.add(() -> deviceTcpCMDService.startDehumidify(humidity));
cmdList.add(() -> deviceTcpCMDService.controlValve("Dehumidification", true));
cmdList.add(() -> {
sysSettingsService.updateTargetHumidity(humidity);
while (true) {
@ -579,6 +556,7 @@ public class CMDService {
return true;
}
// 托盘推入
public boolean trayOut(CMDForm form) {
List<Supplier<Boolean>> cmdList = new ArrayList<>();
cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("Y", 150));
@ -586,6 +564,7 @@ public class CMDService {
return true;
}
// 托盘推出
public boolean trayIn(CMDForm form) {
List<Supplier<Boolean>> cmdList = new ArrayList<>();
cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("Y", 0));
@ -622,6 +601,4 @@ public class CMDService {
webSocketService.pushMsg(WebSocketMessageType.CMD, executionResult);
}
}

3
src/main/java/com/qyft/ms/app/service/ISysSettingsService.java

@ -8,6 +8,7 @@ import com.qyft.ms.app.model.vo.SysSettingResult;
import com.qyft.ms.app.model.vo.SysSettingVO;
import java.util.List;
import java.util.Map;
/**
* 系统配置接口层
@ -23,4 +24,6 @@ public interface ISysSettingsService extends IService<SysSettings> {
Double getTargetHumidity();
void updateTargetHumidity(Double humidity);
Map<String, Double> getWasteLiquorPosition();
}

11
src/main/java/com/qyft/ms/app/service/impl/ISysSettingsServiceImpl.java

@ -11,6 +11,7 @@ import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* 系统配置实现类
@ -27,6 +28,9 @@ public class ISysSettingsServiceImpl extends ServiceImpl<SysSettingsMapper, SysS
List<SysSettings> motorSpeedList;
@Getter
Map<String, Double> wasteLiquorPosition;
@Getter
String workStatus = "idle";
@Getter
@ -39,6 +43,13 @@ public class ISysSettingsServiceImpl extends ServiceImpl<SysSettingsMapper, SysS
Long parentId = sysSettingsMapper.selectOne(queryWrapper).getId();
slidePositionList = sysSettingsMapper.selectList(new QueryWrapper<SysSettings>().eq("parent_id", parentId));
motorSpeedList = sysSettingsMapper.selectList(new QueryWrapper<SysSettings>().eq("parent_id", new QueryWrapper<SysSettings>().eq("code", "speed")));
String position = sysSettingsMapper.selectOne(new QueryWrapper<SysSettings>().eq("code", "waste_liquor")).getValue();
String [] positionList = position.split(",");
wasteLiquorPosition = Map.of(
"x", Double.parseDouble(positionList[0]),
"y", Double.parseDouble(positionList[0]),
"z", Double.parseDouble(positionList[0])
);
}
public void updateWorkStatus(String status) {

3
src/main/java/com/qyft/ms/app/service/impl/MatrixCraftServiceImpl.java

@ -77,7 +77,8 @@ public class MatrixCraftServiceImpl extends ServiceImpl<MatrixCraftMapper, Matri
Matrix matrix = matrixMapper.selectOne(new QueryWrapper<Matrix>().eq("id", matrixId));
MatrixCraftResult matrixCraftResult = new MatrixCraftResult();
BeanUtils.copyProperties(matrixCraft, matrixCraftResult);
matrixCraftResult.setMatrixName(matrix.getName());
matrixCraftResult.setMatrixName(matrix != null ? matrix.getName() : "");
matrixCraftResultList.add((matrixCraftResult));
}
page.setRecords(matrixCraftResultList);

1
src/main/java/com/qyft/ms/device/client/TcpClient.java

@ -122,7 +122,6 @@ public class TcpClient {
if (request.getParams() == null) {
request.setParams(new HashMap<>());
}
request.getParams().put("class", "test");
String requestJsonStr = JSONUtil.toJsonStr(request);
log.info("发送TCP指令(同步) {}", requestJsonStr);
if (this.send(requestJsonStr)) {

13
src/main/java/com/qyft/ms/device/common/constant/DeviceCommands.java

@ -12,10 +12,7 @@ public class DeviceCommands {
public static final String MOVE_MOTOR_TO_POSITION = "moveMotorToPosition";
// 设置指定轴的电机的运行速度
public static final String SET_MOTOR_SPEED = "setMotorSpeed";
// 切换三通阀到基质状态
public static final String SWITCH_THREE_WAY_VALVE_TO_SUBSTRATE = "switchThreeWayValveToSubstrate";
// 切换三通阀到喷涂状态
public static final String SWITCH_THREE_WAY_VALVE_TO_SPRAY = "switchThreeWayValveToSpray";
// 控制指定阀的开启或关闭
public static final String CONTROL_VALVE = "controlValve";
// 以指定亮度开启照明灯板
@ -30,15 +27,13 @@ public class DeviceCommands {
public static final String TURN_ON_SYRINGE_PUMP = "turnOnSyringePump";
// 停止注射泵
public static final String TURN_OFF_SYRINGE_PUMP = "turnOffSyringePump";
// 设置注射泵的容量时间比例参数
public static final String SET_SYRINGE_PUMP_PARAMETERS = "setSyringePumpParameters";
// 推送指定容量的液体
public static final String PUSH_VOLUME = "pushVolume";
public static final String ROTATE_MOTOR = "rotateMotor";
public static final String SYRINGE_PUMP_MOVE_AT_SPEED = "syringePumpMoveAtSpeed";
public static final String SYRINGE_PUMP_STOP = "syringePumpStop";
public static final String SYRINGE_PUMP_START = "syringePumpStart";
public static final String STOP_MOTOR = "stopMotor";
public static final String START_DEHUMIDIFY = "startDehumidify";
public static final String SWITCH_THREE_WAY_VALVE = "switchThreeWayValve";
}

95
src/main/java/com/qyft/ms/device/service/DeviceTcpCMDService.java

@ -140,33 +140,16 @@ public class DeviceTcpCMDService {
return true;
}
// 三通阀控制方法
/**
* 切换三通阀到清洗状态
* @return 操作是否成功
*/
public boolean switchThreeWayValveToSubstrate() {
return this.putTask(DeviceCommands.SWITCH_THREE_WAY_VALVE_TO_SUBSTRATE);
}
/**
* 切换三通阀到喷涂状态
* @return 操作是否成功
*/
public boolean switchThreeWayValveToSpray() {
return this.putTask(DeviceCommands.SWITCH_THREE_WAY_VALVE_TO_SPRAY);
}
// 除湿阀清洗阀喷嘴阀控制方法
/**
* 控制指定阀的开启或关闭
* valveType 阀类型可选值为 "Dehumidification", "Cleaning", "Nozzle"
* type 阀类型可选值为 "Dehumidification", "Cleaning", "Nozzle"
* isOpen 是否开启true 为开启false 为关闭
* @return 操作是否成功
*/
public boolean controlValve(String valveType, boolean isOpen) {
public boolean controlValve(String type, boolean isOpen) {
Map<String, Object> params = new HashMap<>();
params.put("valveType", valveType);
params.put("type", type);
params.put("isOpen", isOpen);
return this.putTask(DeviceCommands.CONTROL_VALVE, params);
@ -175,12 +158,10 @@ public class DeviceTcpCMDService {
// 照明灯板控制方法
/**
* 以指定亮度开启照明灯板
* @param brightness 亮度值范围 0 - 100
* @return 操作是否成功
*/
public boolean turnOnLightPanel(int brightness) {
public boolean turnOnLightPanel() {
Map<String, Object> params = new HashMap<>();
params.put("brightness", brightness);
return this.putTask(DeviceCommands.TURN_ON_LIGHT_PANEL, params);
}
@ -232,40 +213,7 @@ public class DeviceTcpCMDService {
return this.putTask(DeviceCommands.TURN_OFF_SYRINGE_PUMP);
}
/**
* 设置注射泵的容量时间比例参数
* @param speed 容量
* @return 操作是否成功
*/
public boolean setSyringePumpParameters(int speed) {
Map<String, Object> params = new HashMap<>();
params.put("capacity", speed);
return this.putTask(DeviceCommands.SET_SYRINGE_PUMP_PARAMETERS, params);
}
/**
* 推送指定容量的液体
* @param volume 容量单位ml
* @return 操作是否成功
*/
public boolean pushVolume(double volume) {
Map<String, Object> params = new HashMap<>();
params.put("volume", volume);
return this.putTask(DeviceCommands.PUSH_VOLUME, params);
}
// 以指定转速运行指定时间
// axis 轴标识,可选值为"X","Y", "Z"
// rotationSpeed转速 mm/s(毫米/)正负代表方向 X[-3009, 300] Y[-750, 750] Z[-750, 750]
// time 时间 ms
public boolean rotateMotor(String axis, double rotationSpeed, int time) {
Map<String, Object> params = new HashMap<>();
params.put("axis", axis);
params.put("rotationSpeed", rotationSpeed);
params.put("time", time);
return this.putTask(DeviceCommands.ROTATE_MOTOR, params);
}
// 停止指定轴的电机运动
public boolean stopMotor(String axis) {
@ -274,45 +222,12 @@ public class DeviceTcpCMDService {
return this.putTask(DeviceCommands.STOP_MOTOR, params);
}
/**
* 注射泵指定速度运动
* @param speed mm/s 正值负值 代表方向
*/
public boolean syringePumpMoveAtSpeed(int speed) {
Map<String, Object> params = new HashMap<>();
params.put("speed", speed);
return this.putTask(DeviceCommands.SYRINGE_PUMP_MOVE_AT_SPEED, params);
}
/**
* 注射泵停止移动
*
*/
public boolean syringePumpStop() {
return this.putTask(DeviceCommands.SYRINGE_PUMP_STOP);
}
/**
* 注射泵移动
*
*/
public boolean syringePumpStart() {
return this.putTask(DeviceCommands.SYRINGE_PUMP_START);
}
// 除湿
public boolean startDehumidify(double humidity) {
Map<String, Object> params = new HashMap<>();
params.put("humidity", humidity);
return this.putTask(DeviceCommands.START_DEHUMIDIFY, params);
}
// 切换三通阀
public boolean switchThreeWayValve(String type) {
Map<String, Object> params = new HashMap<>();
params.put("type", type);
return this.putTask(DeviceCommands.START_DEHUMIDIFY, params);
return this.putTask(DeviceCommands.SWITCH_THREE_WAY_VALVE, params);
}
}

3
src/main/resources/application.yml

@ -35,7 +35,8 @@ jwt:
#与设备TCP链接
tcp:
enable: true # 是否开启 TCP 连接
host: 192.168.1.130
host: 127.0.0.1
# host: 192.168.1.140
port: 9080
reconnect: 5000 # 断线重连间隔(单位:毫秒)
timeout: 10000 # 连接超时时间(单位:毫秒)

300
src/test/java/com/qyft/ms/CMDServiceTest.java

@ -1,10 +1,12 @@
package com.qyft.ms;
import com.qyft.ms.app.model.entity.SysSettings;
import com.qyft.ms.app.model.form.CMDForm;
import com.qyft.ms.app.model.vo.ExecutionResult;
import com.qyft.ms.app.service.CMDService;
import com.qyft.ms.app.service.ISysSettingsService;
import com.qyft.ms.app.service.OperationLogService;
import com.qyft.ms.app.service.WebSocketService;
import com.qyft.ms.device.service.DeviceStatusService;
import com.qyft.ms.device.service.DeviceTcpCMDService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
@ -13,137 +15,245 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
@DisplayName("指令测试")
@ActiveProfiles("test")
@SpringBootTest
public class CMDServiceTest {
@Transactional // 测试完成后回滚数据
@DisplayName("指令单元测试")
@SpringBootTest(classes = MatrixSprayApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class CMDServiceTest {
@Mock
private DeviceTcpCMDService deviceTcpCMDService;
@InjectMocks
private CMDService cmdService;
@Mock
private WebSocketService webSocketService;
@Mock
private DeviceTcpCMDService deviceTcpCMDService;
@Mock
private OperationLogService operationLogService;
@InjectMocks
private CMDService cmdService;
private CMDForm form;
@Mock
private ISysSettingsService sysSettingsService;
@Mock
private DeviceStatusService deviceStatusService;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
form = new CMDForm();
form.setCommandId("test-command");
form.setCommandName("test-command");
}
@DisplayName("测试轴移动")
@DisplayName("电机移动")
@Test
void testMoveMotorToPosition_Success() {
// 准备参数
Map<String, Object> params = new HashMap<>();
params.put("axis", "x");
params.put("position", 100.0);
form.setParams(params);
void moveMotorToPosition() {
CMDForm form = createForm(Map.of("axis", "X", "position", 100.0));
when(deviceTcpCMDService.moveMotorToPosition("X", 100.0)).thenReturn(true);
// 模拟设备服务调用
when(deviceTcpCMDService.moveMotorToPosition(anyString(), anyDouble())).thenReturn(true);
assertTrue(cmdService.moveMotorToPosition(form));
}
// 执行测试
boolean result = cmdService.moveMotorToPosition(form);
@DisplayName("三通阀")
@Test
void switchThreeWayValve() {
CMDForm form = createForm(Map.of("type", "clear_nozzle"));
when(deviceTcpCMDService.switchThreeWayValve("clear_nozzle")).thenReturn(true);
// 验证结果
assertTrue(result);
verify(deviceTcpCMDService, times(1)).moveMotorToPosition("x", 100.0);
assertTrue(cmdService.switchThreeWayValve(form));
}
@DisplayName("测试开始喷涂")
@DisplayName("阀门控制")
@Test
void testStartWork_InvalidPosition() {
// 准备无效参数
Map<String, Object> params = new HashMap<>();
params.put("position", new ArrayList<>()); // 空位置列表
form.setParams(params);
void controlValve() {
CMDForm form = createForm(Map.of("type", "Nozzle", "isOpen", true));
when(deviceTcpCMDService.controlValve("Nozzle", true)).thenReturn(true);
// 执行测试
String result = cmdService.startWork(form);
assertTrue(cmdService.controlValve(form));
}
// 验证返回错误信息
assertEquals("position参数错误", result);
}
@DisplayName("测试开始喷涂路径规划")
@Test
void testStartWork_PathGeneration() {
// 准备有效参数
Map<String, Object> params = new HashMap<>();
List<Map<String, Integer>> positions = new ArrayList<>();
Map<String, Integer> pos = new HashMap<>();
pos.put("x1", 10);
pos.put("y1", 20);
pos.put("x2", 30);
pos.put("y2", 40);
pos.put("index", 0);
positions.add(pos);
params.put("position", positions);
params.put("space", 5);
params.put("routeType", 1);
params.put("height", 50);
params.put("movementSpeed", 100);
form.setParams(params);
@DisplayName("以指定电压值开启高压电")
@Test
void turnOnHighVoltage() {
CMDForm form = createForm(Map.of("voltage", 4500));
when(deviceTcpCMDService.turnOnHighVoltage(4500)).thenReturn(true);
// 模拟依赖项
when(deviceTcpCMDService.switchThreeWayValveToSpray()).thenReturn(true);
when(deviceTcpCMDService.setMotorSpeed(anyString(), anyInt())).thenReturn(true);
when(deviceTcpCMDService.moveMotorToPosition(anyString(), anyDouble())).thenReturn(true);
assertTrue(cmdService.turnOnHighVoltage(form));
}
// 执行测试
String result = cmdService.startWork(form);
@DisplayName("关闭高压电")
@Test
void turnOffHighVoltage() {
CMDForm form = new CMDForm();
when(deviceTcpCMDService.turnOffHighVoltage()).thenReturn(true);
// 验证命令链长度
assertNotNull(result);
verify(deviceTcpCMDService, atLeastOnce()).switchThreeWayValveToSpray();
assertTrue(cmdService.turnOffHighVoltage(form));
}
@DisplayName("注射泵开启")
@Test
void testControlValve_NozzleOpen() {
Map<String, Object> params = new HashMap<>();
params.put("valveType", "Nozzle");
params.put("isOpen", true);
form.setParams(params);
void turnOnSyringePump() {
CMDForm form = createForm(Map.of("rotationSpeed", 30.0));
when(deviceTcpCMDService.turnOnSyringePump(30.0)).thenReturn(true);
assertTrue(cmdService.turnOnSyringePump(form));
}
@DisplayName("注射泵关闭")
@Test
void turnOffSyringePump() {
CMDForm form =createForm(Map.of());
when(deviceTcpCMDService.turnOffSyringePump()).thenReturn(true);
System.out.println("参数===========" + form);
assertTrue(cmdService.turnOffSyringePump(form));
}
when(deviceTcpCMDService.controlValve(anyString(), anyBoolean())).thenReturn(true);
@DisplayName("设置电机电流")
@Test
void setMotorRunningCurrent() {
CMDForm form = createForm(Map.of("axis", "X", "current", 1.5));
when(deviceTcpCMDService.setMotorRunningCurrent("X", 1.5)).thenReturn(true);
assertTrue(cmdService.setMotorRunningCurrent(form));
}
@DisplayName("托盘推出")
@Test
void trayOut() {
CMDForm form = new CMDForm();
when(deviceTcpCMDService.moveMotorToPosition("Y", 150)).thenReturn(true);
boolean result = cmdService.controlValve(form);
assertTrue(result);
verify(deviceTcpCMDService).controlValve("Nozzle", true);
assertTrue(cmdService.trayOut(form));
}
@DisplayName("托盘推入")
@Test
void testRun_CommandFailure() {
// 模拟一个失败的指令
List<Supplier<Boolean>> cmdList = new ArrayList<>();
cmdList.add(() -> false); // 直接返回失败
void trayIn() {
CMDForm form = new CMDForm();
when(deviceTcpCMDService.moveMotorToPosition("Y", 0)).thenReturn(true);
// 执行内部方法
cmdService.run(cmdList, form);
assertTrue(cmdService.trayIn(form));
}
@DisplayName("电机停止")
@Test
void stopMotor() {
CMDForm form = createForm(Map.of("axis", "Z"));
when(deviceTcpCMDService.stopMotor("Z")).thenReturn(true);
assertTrue(cmdService.stopMotor(form));
}
// 验证WebSocket错误消息推送
verify(webSocketService).pushMsg(any(), any(ExecutionResult.class));
@DisplayName("照明灯开启")
@Test
void turnOnLightPanel() {
CMDForm form = new CMDForm();
when(deviceTcpCMDService.turnOnLightPanel()).thenReturn(true);
assertTrue(cmdService.turnOnLightPanel(form));
}
@DisplayName("照明灯关闭")
@Test
void turnOffLightPanel() {
CMDForm form = new CMDForm();
when(deviceTcpCMDService.turnOffLightPanel()).thenReturn(true);
assertTrue(cmdService.turnOffLightPanel(form));
}
@DisplayName("电机回原点")
@Test
void motorMoveToHome() {
CMDForm form = createForm(Map.of("axis", "Y"));
when(deviceTcpCMDService.motorMoveToHome("Y")).thenReturn(true);
assertTrue(cmdService.motorMoveToHome(form));
}
@DisplayName("预充")
@Test
void startPrefill() {
CMDForm form = createForm(Map.of("rotationSpeed", 50.0));
when(deviceTcpCMDService.switchThreeWayValve("clear_spray")).thenReturn(true);
when(deviceTcpCMDService.controlValve("Nozzle", true)).thenReturn(true);
when(deviceTcpCMDService.turnOnSyringePump(50.0)).thenReturn(true);
assertTrue(cmdService.startPrefill(form));
}
@DisplayName("停止预充")
@Test
void stopPrefill() {
CMDForm form = new CMDForm();
when(deviceTcpCMDService.turnOffSyringePump()).thenReturn(true);
when(deviceTcpCMDService.controlValve("Nozzle", false)).thenReturn(true);
assertTrue(cmdService.stopPrefill(form));
}
@DisplayName("开始喷头清洗")
@Test
void startWash_nozzleType() {
CMDForm form = createForm(Map.of("type", "nozzle"));
when(deviceTcpCMDService.moveMotorToPosition("X", 173.08)).thenReturn(true);
when(deviceTcpCMDService.moveMotorToPosition("Y", 75.2)).thenReturn(true);
when(deviceTcpCMDService.moveMotorToPosition("Z", 70)).thenReturn(true);
when(deviceTcpCMDService.switchThreeWayValve("clear_nozzle")).thenReturn(true);
when(deviceTcpCMDService.controlValve("Nozzle", true)).thenReturn(true);
assertTrue(cmdService.startWash(form));
}
@DisplayName("结束清洗")
@Test
void stopWash() {
CMDForm form = new CMDForm();
when(deviceTcpCMDService.switchThreeWayValve("close_all")).thenReturn(true);
when(deviceTcpCMDService.controlValve("Cleaning", false)).thenReturn(true);
when(deviceTcpCMDService.controlValve("Nozzle", false)).thenReturn(true);
assertTrue(cmdService.stopWash(form));
}
@DisplayName("设置点击运行速度")
@Test
void setMotorSpeed() {
CMDForm form = createForm(Map.of("axis", "X", "speed", 100.0));
when(deviceTcpCMDService.setMotorSpeed("X", 100.0)).thenReturn(true);
assertTrue(cmdService.setMotorSpeed(form));
}
@DisplayName("开始喷涂")
@Test
void startWork_pathGeneration() {
CMDForm form = createForm(Map.of(
"position", List.of(Map.of("x1", 0, "y1", 0, "x2", 20, "y2", 50, "index", 0)),
"space", 5,
"routeType", 1
));
SysSettings setting = new SysSettings();
setting.setValue("10,20,30");
when(sysSettingsService.getSlidePositionList()).thenReturn(Collections.singletonList(setting));
String result = cmdService.startWork(form);
System.out.println(result);
assertNotEquals("路径规划失败", result);
}
// 工具方法简化Form构造
private CMDForm createForm(Map<String, Object> params) {
CMDForm form = new CMDForm();
form.setParams(params);
return form;
}
}
}

9
src/test/java/com/qyft/ms/SprayTest.java

@ -1,17 +1,17 @@
package com.qyft.ms;
import com.qyft.ms.app.model.entity.OperationLog;
import com.qyft.ms.app.model.entity.SysSettings;
import com.qyft.ms.app.model.form.CMDForm;
import com.qyft.ms.app.service.*;
import com.qyft.ms.device.service.DeviceTcpCMDService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.*;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.*;
import static com.qyft.ms.app.common.generator.PathGenerator.generatePathPoints;
import static org.mockito.Mockito.*;
public class SprayTest {
@ -68,12 +68,10 @@ public class SprayTest {
mockSetting.setValue("100,200,300"); // 模拟托盘坐标
when(sysSettingsService.getSlidePositionList()).thenReturn(Collections.singletonList(mockSetting));
when(deviceTcpCMDService.switchThreeWayValveToSpray()).thenReturn(true);
when(deviceTcpCMDService.setMotorSpeed(anyString(), anyInt())).thenReturn(true);
when(deviceTcpCMDService.moveMotorToPosition(anyString(), anyDouble())).thenReturn(true);
when(deviceTcpCMDService.turnOnHighVoltage((int) anyDouble())).thenReturn(true);
when(deviceTcpCMDService.controlValve(anyString(), anyBoolean())).thenReturn(true);
when(deviceTcpCMDService.syringePumpMoveAtSpeed(anyInt())).thenReturn(true);
when(deviceTcpCMDService.turnOffSyringePump()).thenReturn(true);
when(deviceTcpCMDService.motorMoveToHome(anyString())).thenReturn(true);
@ -86,7 +84,6 @@ public class SprayTest {
String result = cmdService.startWork(form);
// 4. 验证
verify(deviceTcpCMDService, times(1)).switchThreeWayValveToSpray();
verify(deviceTcpCMDService, times(1)).setMotorSpeed("x", 100);
verify(deviceTcpCMDService, times(1)).setMotorSpeed("y", 100);
verify(deviceTcpCMDService, times(1)).setMotorSpeed("z", 100);

Loading…
Cancel
Save