From 825da4bcbb5f6f317a57cc82b187364fba6cb71d Mon Sep 17 00:00:00 2001 From: guoapeng Date: Wed, 5 Mar 2025 19:55:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:bug=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- matrix-spray.db | Bin 36864 -> 36864 bytes sql/init.sql | 3 +- .../com/qyft/ms/app/controller/CMDController.java | 25 +- .../qyft/ms/app/controller/MatrixController.java | 3 - .../ms/app/controller/MatrixCraftController.java | 3 +- .../com/qyft/ms/app/model/dto/MatrixCraftDTO.java | 8 + .../java/com/qyft/ms/app/service/CMDService.java | 79 ++---- .../qyft/ms/app/service/ISysSettingsService.java | 3 + .../app/service/impl/ISysSettingsServiceImpl.java | 11 + .../app/service/impl/MatrixCraftServiceImpl.java | 3 +- .../java/com/qyft/ms/device/client/TcpClient.java | 1 - .../ms/device/common/constant/DeviceCommands.java | 13 +- .../ms/device/service/DeviceTcpCMDService.java | 95 +------ src/main/resources/application.yml | 3 +- src/test/java/com/qyft/ms/CMDServiceTest.java | 300 ++++++++++++++------- src/test/java/com/qyft/ms/SprayTest.java | 9 +- 16 files changed, 276 insertions(+), 283 deletions(-) diff --git a/matrix-spray.db b/matrix-spray.db index c7e534ed5fd965b2191638f872565112da847c6a..d5176e0e185087faa1c2d637c10925ecd7404f82 100644 GIT binary patch delta 1633 zcmd6n-D(p-6vt;%W3y@Yqn6TYy$As>3h8z}c6N70)Kl=Q)80g{0MMF|L-xkw)DyB7n_DO4Kruw5Z zsee_&?~mWNmX$b|%;gBd5Yq)n79d?ijGH!xmVv1y9tXk;#pP751jf-UY#@Y@P3d_h zy5h_qRRI7<+9A*m)g$#t8xw#()z*@(fhnoC5|RW6VT?`0lD*YGER7I1DIYxlETnw~ z_lGJVgrXJJPAWjNU$h%)iK@`Nr*t9Y6AU0A5GLbeSH}}_vY(!Yj1!w9CPI(IhPiv8?sxI7KCgsLmEsr1BrG9^z7i;feicA&`s z?0YiEO`NT4GwI>J9Jwgct=@DO#L9^ws>EzMr`mX|&BL;*bd! z2^8Zw>|#W0*Cs9>h%gSRL70OuWTQZ*A`|~13&M6SdHF!d;TRc&K;$?PhepJ9P5!q) acOosbIH8^n9#h)TZa<)DrG(I?l(gQw zDeE6l?_MN$>)+u?@FIfXLGdoW&04ehPZlnK!@rz2BRolf=RLS$ye&Ffrv0%vYhBdLQ)Hf++~nEM?VR;4 z8+>~BvW4!A&&_cmDW;-;Spo9}OcZsMpblfD5_d}&|J8XXlR^-KF&baS6mu2ljwd z1vk2#i(o_n2r(jyFD@y#3F}e}*Z4Js+S+o`vO-$2Gf^|7dx0(pXS!UcS!rc4KF&US0QXCGz%$zq!6qx0TG>wVj4v^SA4dpZNZ&t=!=C!AvD@ z?-N^*yuC-E!!Ydhhl__@hzk@eK@<}X^LR->K4V~qI1x&G_t%@o%w66&A2`!m48=4y4xuY 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 setMotorSpeed(@RequestBody CMDForm cmdForm) { @@ -373,7 +350,7 @@ public class CMDController { } } - @Operation(summary = "以指定亮度开启照明灯板") + @Operation(summary = "开启照明灯板") @PostMapping("/turnOnLightPanel") public Result turnOnLightPanel(@RequestBody CMDForm cmdForm) { try { diff --git a/src/main/java/com/qyft/ms/app/controller/MatrixController.java b/src/main/java/com/qyft/ms/app/controller/MatrixController.java index b94a446..a538e4d 100644 --- a/src/main/java/com/qyft/ms/app/controller/MatrixController.java +++ b/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; diff --git a/src/main/java/com/qyft/ms/app/controller/MatrixCraftController.java b/src/main/java/com/qyft/ms/app/controller/MatrixCraftController.java index e1abfcf..5a13cf6 100644 --- a/src/main/java/com/qyft/ms/app/controller/MatrixCraftController.java +++ b/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 add(@RequestBody MatrixCraftDTO dto) { + public Result add(@RequestBody @Validated MatrixCraftDTO dto) { User user = iUserService.currentUser(); if (user != null) { dto.setCreateUser(user.getId()); diff --git a/src/main/java/com/qyft/ms/app/model/dto/MatrixCraftDTO.java b/src/main/java/com/qyft/ms/app/model/dto/MatrixCraftDTO.java index 8e499a5..7351d63 100644 --- a/src/main/java/com/qyft/ms/app/model/dto/MatrixCraftDTO.java +++ b/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; diff --git a/src/main/java/com/qyft/ms/app/service/CMDService.java b/src/main/java/com/qyft/ms/app/service/CMDService.java index 4cf4052..25075d2 100644 --- a/src/main/java/com/qyft/ms/app/service/CMDService.java +++ b/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> 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 params = form.getParams(); - if (params == null || !params.containsKey("valveType") || !params.containsKey("isOpen")) { + if (params == null || !params.containsKey("type") || !params.containsKey("isOpen")) { return false; } List> 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 params = form.getParams(); - List> 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 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 params = form.getParams(); List> 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> 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> cmdList = new ArrayList<>(); cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("Y", 150)); @@ -586,6 +564,7 @@ public class CMDService { return true; } + // 托盘推出 public boolean trayIn(CMDForm form) { List> cmdList = new ArrayList<>(); cmdList.add(() -> deviceTcpCMDService.moveMotorToPosition("Y", 0)); @@ -622,6 +601,4 @@ public class CMDService { webSocketService.pushMsg(WebSocketMessageType.CMD, executionResult); } - - } diff --git a/src/main/java/com/qyft/ms/app/service/ISysSettingsService.java b/src/main/java/com/qyft/ms/app/service/ISysSettingsService.java index d15e23e..1c05061 100644 --- a/src/main/java/com/qyft/ms/app/service/ISysSettingsService.java +++ b/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 { Double getTargetHumidity(); void updateTargetHumidity(Double humidity); + + Map getWasteLiquorPosition(); } diff --git a/src/main/java/com/qyft/ms/app/service/impl/ISysSettingsServiceImpl.java b/src/main/java/com/qyft/ms/app/service/impl/ISysSettingsServiceImpl.java index e6a9bf5..9a8bc36 100644 --- a/src/main/java/com/qyft/ms/app/service/impl/ISysSettingsServiceImpl.java +++ b/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 motorSpeedList; @Getter + Map wasteLiquorPosition; + + @Getter String workStatus = "idle"; @Getter @@ -39,6 +43,13 @@ public class ISysSettingsServiceImpl extends ServiceImpl().eq("parent_id", parentId)); motorSpeedList = sysSettingsMapper.selectList(new QueryWrapper().eq("parent_id", new QueryWrapper().eq("code", "speed"))); + String position = sysSettingsMapper.selectOne(new QueryWrapper().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) { diff --git a/src/main/java/com/qyft/ms/app/service/impl/MatrixCraftServiceImpl.java b/src/main/java/com/qyft/ms/app/service/impl/MatrixCraftServiceImpl.java index 0641045..11614af 100644 --- a/src/main/java/com/qyft/ms/app/service/impl/MatrixCraftServiceImpl.java +++ b/src/main/java/com/qyft/ms/app/service/impl/MatrixCraftServiceImpl.java @@ -77,7 +77,8 @@ public class MatrixCraftServiceImpl extends ServiceImpl().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); diff --git a/src/main/java/com/qyft/ms/device/client/TcpClient.java b/src/main/java/com/qyft/ms/device/client/TcpClient.java index f974c4e..b65cd05 100644 --- a/src/main/java/com/qyft/ms/device/client/TcpClient.java +++ b/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)) { diff --git a/src/main/java/com/qyft/ms/device/common/constant/DeviceCommands.java b/src/main/java/com/qyft/ms/device/common/constant/DeviceCommands.java index 11340e1..79fd5fe 100644 --- a/src/main/java/com/qyft/ms/device/common/constant/DeviceCommands.java +++ b/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"; } \ No newline at end of file diff --git a/src/main/java/com/qyft/ms/device/service/DeviceTcpCMDService.java b/src/main/java/com/qyft/ms/device/service/DeviceTcpCMDService.java index c9f4f4d..db0a72b 100644 --- a/src/main/java/com/qyft/ms/device/service/DeviceTcpCMDService.java +++ b/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 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 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 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 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 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 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 params = new HashMap<>(); - params.put("humidity", humidity); - return this.putTask(DeviceCommands.START_DEHUMIDIFY, params); - } // 切换三通阀 public boolean switchThreeWayValve(String type) { Map params = new HashMap<>(); params.put("type", type); - return this.putTask(DeviceCommands.START_DEHUMIDIFY, params); + return this.putTask(DeviceCommands.SWITCH_THREE_WAY_VALVE, params); } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 6d803b8..986da8b 100644 --- a/src/main/resources/application.yml +++ b/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 # 连接超时时间(单位:毫秒) diff --git a/src/test/java/com/qyft/ms/CMDServiceTest.java b/src/test/java/com/qyft/ms/CMDServiceTest.java index 9ffb915..3d3ed80 100644 --- a/src/test/java/com/qyft/ms/CMDServiceTest.java +++ b/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 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 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 params = new HashMap<>(); - List> positions = new ArrayList<>(); - Map 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 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> 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 params) { + CMDForm form = new CMDForm(); + form.setParams(params); + return form; } -} \ No newline at end of file +} diff --git a/src/test/java/com/qyft/ms/SprayTest.java b/src/test/java/com/qyft/ms/SprayTest.java index d6c49ad..b5e52aa 100644 --- a/src/test/java/com/qyft/ms/SprayTest.java +++ b/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);