From bcd1f9b1a7c0d9a7a1df559183187d51d3ee682d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E5=87=A4=E5=90=89?= Date: Fri, 28 Feb 2025 10:24:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=AE=9E=E7=8E=B0=E5=8F=AF=E7=BC=96?= =?UTF-8?q?=E8=BE=91=E7=9A=84=E8=AE=BE=E5=A4=87=E6=8E=A7=E5=88=B6=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sql/demo.sql | 8 +- .../java/com/qyft/gd/controller/CMDController.java | 27 +- .../gd/device/common/constant/DeviceCommands.java | 5 + .../gd/device/controller/DeviceController.java | 38 ++ .../gd/device/controller/DeviceCtrlController.java | 19 +- .../qyft/gd/device/model/bo/CtrlFuncDeviceCMD.java | 23 - .../qyft/gd/device/model/bo/DeviceCtrlFuncCMD.java | 23 + .../device/model/bo/DeviceOperationalStatus.java | 38 ++ .../com/qyft/gd/device/model/bo/DeviceStatus.java | 2 +- .../qyft/gd/device/model/entity/CtrlFuncStep.java | 4 +- .../qyft/gd/device/model/vo/DeviceCtrlFuncVO.java | 26 + .../qyft/gd/device/service/DeviceCtrlService.java | 49 +- .../com/qyft/gd/device/service/DeviceService.java | 14 + .../gd/device/service/DeviceStatusService.java | 13 +- .../qyft/gd/device/service/DeviceStepService.java | 86 ++-- .../gd/device/service/DeviceTcpCMDService.java | 15 + .../qyft/gd/device/service/ICtrlFuncService.java | 8 +- .../gd/device/service/ICtrlFuncStepService.java | 4 +- .../device/service/impl/CtrlFuncServiceImpl.java | 51 +- .../service/impl/CtrlFuncStepServiceImpl.java | 14 +- src/main/java/com/qyft/gd/service/CMDService.java | 558 ++++----------------- 21 files changed, 390 insertions(+), 635 deletions(-) create mode 100644 src/main/java/com/qyft/gd/device/controller/DeviceController.java delete mode 100644 src/main/java/com/qyft/gd/device/model/bo/CtrlFuncDeviceCMD.java create mode 100644 src/main/java/com/qyft/gd/device/model/bo/DeviceCtrlFuncCMD.java create mode 100644 src/main/java/com/qyft/gd/device/model/bo/DeviceOperationalStatus.java create mode 100644 src/main/java/com/qyft/gd/device/model/vo/DeviceCtrlFuncVO.java create mode 100644 src/main/java/com/qyft/gd/device/service/DeviceService.java diff --git a/sql/demo.sql b/sql/demo.sql index 8f98c0c..4c0b971 100644 --- a/sql/demo.sql +++ b/sql/demo.sql @@ -220,9 +220,13 @@ VALUES ('开门', 'openDoor'), CREATE TABLE ctrl_func_step ( id INTEGER PRIMARY KEY AUTOINCREMENT, - func_id INTEGER NOT NULL, - device_cmd INTEGER NOT NULL, + func_cmd TEXT NOT NULL, + device_cmd TEXT NOT NULL, params TEXT, create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); +INSERT INTO ctrl_func_step (func_cmd, device_cmd, params) +VALUES ('openDoor', 'openDoor', null), + ('openDoor', 'openDoor', null), + ('openDoor', 'openDoor', null); diff --git a/src/main/java/com/qyft/gd/controller/CMDController.java b/src/main/java/com/qyft/gd/controller/CMDController.java index 91b3696..a9439f5 100644 --- a/src/main/java/com/qyft/gd/controller/CMDController.java +++ b/src/main/java/com/qyft/gd/controller/CMDController.java @@ -1,11 +1,13 @@ package com.qyft.gd.controller; import cn.hutool.json.JSONUtil; +import com.qyft.gd.device.service.DeviceCtrlService; import com.qyft.gd.model.dto.CmdInjectFluidDTO; import com.qyft.gd.model.dto.StartHeatDTO; import com.qyft.gd.model.entity.Crafts; import com.qyft.gd.model.form.CMDForm; import com.qyft.gd.service.CMDService; +import com.qyft.gd.service.CraftsService; import com.qyft.gd.system.common.result.Result; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -28,8 +30,6 @@ public class CMDController { private final CMDService cmdService; -// public Result> - @Operation(summary = "执行指令") @PostMapping("/execute") public Result execute(@RequestBody CMDForm cmdForm) { @@ -50,27 +50,4 @@ public class CMDController { } } - @Operation(summary = "批量加热") - @PostMapping("/startHeat") - public Result startHeat(@RequestBody StartHeatDTO dto) { - cmdService.startHeats(dto); - return Result.success("操作成功"); -// for (int i : list) { -// CMDForm cmdForm = new CMDForm(); -// cmdForm.setCommand("startHeat"); -// cmdForm.setCommandId(UUID.randomUUID().toString()); -// Map params = new HashMap<>(); -// params.put("heatId", i); -// cmdForm.setParams(params); -// cmdService.executeCommand(cmdForm); -// } -// return Result.success("操作成功"); - } - - @Operation(summary = "批量加液") - @PostMapping("/injectFluid") - public Result injectFluid(@RequestBody CmdInjectFluidDTO dto) { - cmdService.injectFluids(dto); - return Result.success("操作成功"); - } } diff --git a/src/main/java/com/qyft/gd/device/common/constant/DeviceCommands.java b/src/main/java/com/qyft/gd/device/common/constant/DeviceCommands.java index c944ceb..07736d2 100644 --- a/src/main/java/com/qyft/gd/device/common/constant/DeviceCommands.java +++ b/src/main/java/com/qyft/gd/device/common/constant/DeviceCommands.java @@ -18,6 +18,11 @@ public class DeviceCommands { public static final String MOVE_RAIL_ARM_TO_POINT = "moveRailArmToPoint"; /** + * 导轨机械臂相对运动 + */ + public static final String MOVE_RAIL_ARM_RELATIVE = "moveRailArmRelative"; + + /** * 设置导轨机械臂的速度 */ public static final String SET_RAIL_ARM_SPEED = "setRailArmSpeed"; diff --git a/src/main/java/com/qyft/gd/device/controller/DeviceController.java b/src/main/java/com/qyft/gd/device/controller/DeviceController.java new file mode 100644 index 0000000..0c365b7 --- /dev/null +++ b/src/main/java/com/qyft/gd/device/controller/DeviceController.java @@ -0,0 +1,38 @@ +package com.qyft.gd.device.controller; + +import com.qyft.gd.device.model.bo.DeviceOperationalStatus; +import com.qyft.gd.device.model.bo.DeviceStatus; +import com.qyft.gd.device.service.DeviceService; +import com.qyft.gd.device.service.DeviceStatusService; +import com.qyft.gd.system.common.result.Result; +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.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Tag(name = "设备控制") +@RestController +@RequestMapping("/api/device") +@RequiredArgsConstructor +@Slf4j +public class DeviceController { + private final DeviceStatusService deviceStatusService; + + @Operation(summary = "获取设备业务操作状态") + @GetMapping("/operational/status") + public Result getDeviceOperationalStatus() { + DeviceOperationalStatus deviceOperationalStatus = deviceStatusService.getDeviceOperationalStatus(); + return Result.success(deviceOperationalStatus); + } + + + @Operation(summary = "获取当前设备状态") + @GetMapping("/status") + public Result getDeviceStatus() { + DeviceStatus deviceStatus = deviceStatusService.getDeviceStatus(); + return Result.success(deviceStatus); + } +} diff --git a/src/main/java/com/qyft/gd/device/controller/DeviceCtrlController.java b/src/main/java/com/qyft/gd/device/controller/DeviceCtrlController.java index a41d575..b59e013 100644 --- a/src/main/java/com/qyft/gd/device/controller/DeviceCtrlController.java +++ b/src/main/java/com/qyft/gd/device/controller/DeviceCtrlController.java @@ -1,10 +1,10 @@ package com.qyft.gd.device.controller; -import com.qyft.gd.device.model.bo.CtrlFuncDeviceCMD; +import com.qyft.gd.device.model.bo.DeviceCtrlFuncCMD; import com.qyft.gd.device.model.bo.DeviceStatus; import com.qyft.gd.device.model.entity.CtrlFunc; import com.qyft.gd.device.model.form.CtrlFuncForm; -import com.qyft.gd.device.model.vo.CtrlFuncVO; +import com.qyft.gd.device.model.vo.DeviceCtrlFuncVO; import com.qyft.gd.device.service.DeviceStatusService; import com.qyft.gd.device.service.ICtrlFuncService; import com.qyft.gd.system.common.result.Result; @@ -24,13 +24,12 @@ import java.util.List; @RequiredArgsConstructor @Slf4j public class DeviceCtrlController { - private final DeviceStatusService deviceStatusService; private final ICtrlFuncService ctrlFuncService; @Operation(summary = "获取所有设备控制方法步骤") @GetMapping("/ctrl/step") - public Result> getAllCtrlFuncStep() { - List ctrlFuncList = ctrlFuncService.findAllStepCMD(); + public Result> getAllCtrlFuncStep() { + List ctrlFuncList = ctrlFuncService.findAllStepCMD(); return Result.success(ctrlFuncList); } @@ -43,8 +42,8 @@ public class DeviceCtrlController { @Operation(summary = "根据id获取设备控制方法与步骤") @GetMapping("/ctrl/{id}") - public Result getById(@PathVariable Long id) { - CtrlFuncVO ctrlFuncVO = ctrlFuncService.findById(id); + public Result getById(@PathVariable Long id) { + DeviceCtrlFuncVO ctrlFuncVO = ctrlFuncService.findById(id); return Result.success(ctrlFuncVO); } @@ -88,12 +87,6 @@ public class DeviceCtrlController { return Result.failed(); } - @Operation(summary = "获取当前设备状态") - @GetMapping("/status") - public Result getDeviceStatus() { - DeviceStatus deviceStatus = deviceStatusService.getDeviceStatus(); - return Result.success(deviceStatus); - } } diff --git a/src/main/java/com/qyft/gd/device/model/bo/CtrlFuncDeviceCMD.java b/src/main/java/com/qyft/gd/device/model/bo/CtrlFuncDeviceCMD.java deleted file mode 100644 index 646d307..0000000 --- a/src/main/java/com/qyft/gd/device/model/bo/CtrlFuncDeviceCMD.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.qyft.gd.device.model.bo; - -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.Data; -import lombok.EqualsAndHashCode; - -@EqualsAndHashCode(callSuper = false) -@Schema(description = "设备控制方法步骤指令") -@Data -public class CtrlFuncDeviceCMD { - - public CtrlFuncDeviceCMD(String name, String deviceCmd) { - this.name = name; - this.deviceCmd = deviceCmd; - } - - @Schema(description = "指令名称") - private String name; - - @Schema(description = "设备指令") - private String deviceCmd; - -} diff --git a/src/main/java/com/qyft/gd/device/model/bo/DeviceCtrlFuncCMD.java b/src/main/java/com/qyft/gd/device/model/bo/DeviceCtrlFuncCMD.java new file mode 100644 index 0000000..2a30bba --- /dev/null +++ b/src/main/java/com/qyft/gd/device/model/bo/DeviceCtrlFuncCMD.java @@ -0,0 +1,23 @@ +package com.qyft.gd.device.model.bo; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@EqualsAndHashCode(callSuper = false) +@Schema(description = "设备控制方法步骤指令") +@Data +public class DeviceCtrlFuncCMD { + + public DeviceCtrlFuncCMD(String name, String deviceCmd) { + this.name = name; + this.deviceCmd = deviceCmd; + } + + @Schema(description = "指令名称") + private String name; + + @Schema(description = "设备指令") + private String deviceCmd; + +} diff --git a/src/main/java/com/qyft/gd/device/model/bo/DeviceOperationalStatus.java b/src/main/java/com/qyft/gd/device/model/bo/DeviceOperationalStatus.java new file mode 100644 index 0000000..3551875 --- /dev/null +++ b/src/main/java/com/qyft/gd/device/model/bo/DeviceOperationalStatus.java @@ -0,0 +1,38 @@ +package com.qyft.gd.device.model.bo; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.util.ArrayList; +import java.util.List; + +@Schema(description = "设备业务操作状态") +@Data +public class DeviceOperationalStatus { + @Schema(description = "托盘列表") + private List trayList = new ArrayList<>(); + + /** + * 托盘 + */ + @Data + static class Tray { + @Schema(description = "所在加热区id") + private Long heatId; + @Schema(description = "是否在加液区") + private boolean isSolutionArea; + @Schema(description = "试管列表") + private List tubeList; + } + + /** + * 试管 + */ + @Data + static class Tube { + @Schema(description = "试管编号") + private Integer tubeNum; + @Schema(description = "试管内是否有样品") + private boolean isSample; + } +} diff --git a/src/main/java/com/qyft/gd/device/model/bo/DeviceStatus.java b/src/main/java/com/qyft/gd/device/model/bo/DeviceStatus.java index 51e50dd..293ea3e 100644 --- a/src/main/java/com/qyft/gd/device/model/bo/DeviceStatus.java +++ b/src/main/java/com/qyft/gd/device/model/bo/DeviceStatus.java @@ -8,7 +8,7 @@ import java.util.List; /** * 设备当前状态 */ -@Schema(description = "设备当前状态") +@Schema(description = "设备当前传感器状态") @Data public class DeviceStatus { diff --git a/src/main/java/com/qyft/gd/device/model/entity/CtrlFuncStep.java b/src/main/java/com/qyft/gd/device/model/entity/CtrlFuncStep.java index 0870760..b83f858 100644 --- a/src/main/java/com/qyft/gd/device/model/entity/CtrlFuncStep.java +++ b/src/main/java/com/qyft/gd/device/model/entity/CtrlFuncStep.java @@ -12,8 +12,8 @@ import lombok.EqualsAndHashCode; @Data public class CtrlFuncStep extends BaseEntity { - @Schema(description = "控制方法的id") - private Long funcId; + @Schema(description = "控制方法的指令") + private String funcCmd; @Schema(description = "设备指令") private String deviceCmd; diff --git a/src/main/java/com/qyft/gd/device/model/vo/DeviceCtrlFuncVO.java b/src/main/java/com/qyft/gd/device/model/vo/DeviceCtrlFuncVO.java new file mode 100644 index 0000000..4712c47 --- /dev/null +++ b/src/main/java/com/qyft/gd/device/model/vo/DeviceCtrlFuncVO.java @@ -0,0 +1,26 @@ +package com.qyft.gd.device.model.vo; + +import com.qyft.gd.device.model.entity.CtrlFuncStep; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.util.List; + +@EqualsAndHashCode(callSuper = false) +@Schema(description = "获取所有设备控制方法返回数据") +@Data +public class DeviceCtrlFuncVO { + + private Long id; + + @Schema(description = "控制方法的名称") + private String name; + + @Schema(description = "控制方法的指令") + private String funcCmd; + + @Schema(description = "设备控制方法步骤列表") + private List ctrlFuncStepList; + +} \ No newline at end of file diff --git a/src/main/java/com/qyft/gd/device/service/DeviceCtrlService.java b/src/main/java/com/qyft/gd/device/service/DeviceCtrlService.java index cd61841..a97c095 100644 --- a/src/main/java/com/qyft/gd/device/service/DeviceCtrlService.java +++ b/src/main/java/com/qyft/gd/device/service/DeviceCtrlService.java @@ -23,14 +23,15 @@ public class DeviceCtrlService { /** * 开门 */ - public boolean openDoor() { - return deviceTcpCMDService.openDoor(); + public boolean openDoor(Map params) { + return true; +// return deviceTcpCMDService.openDoor(); } /** * 关门 */ - public boolean closeDoor() { + public boolean closeDoor(Map params) { return deviceTcpCMDService.closeDoor(); } @@ -57,16 +58,38 @@ public class DeviceCtrlService { } /** + * 导轨机械臂相对运动 + * + * @param params 应当包含int类型的x,y,z相对移动量 + */ + public boolean moveRailArmRelative(Map params) { + try { + Integer x = (Integer) params.get("x"); + Integer y = (Integer) params.get("y"); + Integer z = (Integer) params.get("z"); + if (x == null || y == null || z == null) { + log.info("导轨机械臂相对运动,传入参数错误:{}", JSONUtil.toJsonStr(params)); + return false; + } + return deviceTcpCMDService.moveRailArmRelative(x, y, z); + } catch (Exception e) { + log.info("导轨机械臂相对运动,执行异常,传入参数:{}", JSONUtil.toJsonStr(params), e); + return false; + } + } + + + /** * 打开导轨机械臂夹爪 */ - public boolean openClaw() { + public boolean openClaw(Map params) { return deviceTcpCMDService.openClaw(); } /** * 收合导轨机械臂夹爪 */ - public boolean closeClaw() { + public boolean closeClaw(Map params) { return deviceTcpCMDService.closeClaw(); } @@ -98,7 +121,7 @@ public class DeviceCtrlService { /** * 导轨机械臂移至加液区上方 */ - public boolean moveToActionArea() { + public boolean moveToActionArea(Map params) { try { String solutionAreaPosition = baseDataService.getSolutionAreaPosition(); String[] heatAreaPositionArr = solutionAreaPosition.split(","); @@ -196,10 +219,12 @@ public class DeviceCtrlService { /** * 依次添加溶液 * - * @param tubeSolList tubeNum需要添加溶液的试管编号,solId溶液id,volume加液量 + * @param params tubeSolList tubeNum需要添加溶液的试管编号,solId溶液id,volume加液量 */ - public synchronized boolean addLiquid(List tubeSolList) { + @SuppressWarnings("unchecked") + public synchronized boolean addLiquid(Map params) { try { + List tubeSolList = (List) params.get("tubeSolList"); for (TubeSol tubeSol : tubeSolList) { String tubePosition = baseDataService.getTubePositionBySolutionArea(tubeSol.getTubeNum()); String[] tubePositionArr = tubePosition.split(","); @@ -213,7 +238,7 @@ public class DeviceCtrlService { } return true; } catch (Exception e) { - log.info("依次添加溶液,执行异常,传入参数:{}", JSONUtil.toJsonStr(tubeSolList), e); + log.info("依次添加溶液,执行异常,传入参数:{}", JSONUtil.toJsonStr(params), e); return false; } } @@ -221,14 +246,14 @@ public class DeviceCtrlService { /** * 开始摇匀 */ - public boolean startShaking() { + public boolean startShaking(Map params) { return deviceTcpCMDService.startShaking(); } /** * 停止摇匀 */ - public boolean stopShaking() { + public boolean stopShaking(Map params) { return deviceTcpCMDService.stopShaking(); } @@ -236,7 +261,7 @@ public class DeviceCtrlService { /** * 拍照 */ - public boolean takePhoto() { + public boolean takePhoto(Map params) { return deviceTcpCMDService.takePhoto(); } } diff --git a/src/main/java/com/qyft/gd/device/service/DeviceService.java b/src/main/java/com/qyft/gd/device/service/DeviceService.java new file mode 100644 index 0000000..818e477 --- /dev/null +++ b/src/main/java/com/qyft/gd/device/service/DeviceService.java @@ -0,0 +1,14 @@ +package com.qyft.gd.device.service; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +/** + * 设备操作 + */ +@Service +@RequiredArgsConstructor +public class DeviceService { + + +} diff --git a/src/main/java/com/qyft/gd/device/service/DeviceStatusService.java b/src/main/java/com/qyft/gd/device/service/DeviceStatusService.java index bb5972d..6c1aa9f 100644 --- a/src/main/java/com/qyft/gd/device/service/DeviceStatusService.java +++ b/src/main/java/com/qyft/gd/device/service/DeviceStatusService.java @@ -1,7 +1,9 @@ package com.qyft.gd.device.service; import cn.hutool.core.bean.BeanUtil; +import com.qyft.gd.device.model.bo.DeviceOperationalStatus; import com.qyft.gd.device.model.bo.DeviceStatus; +import lombok.Getter; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -12,9 +14,16 @@ import org.springframework.stereotype.Service; @RequiredArgsConstructor public class DeviceStatusService { /** - * 设备状态实体 + * 设备运行状态 */ private final DeviceStatus deviceStatus = new DeviceStatus(); + /** + * 设备业务状态 + * -- GETTER -- + * 更新设备业务操作状态 + */ + @Getter + private final DeviceOperationalStatus deviceOperationalStatus = new DeviceOperationalStatus(); /** * 更新设备状态 @@ -33,4 +42,6 @@ public class DeviceStatusService { return BeanUtil.copyProperties(deviceStatus, DeviceStatus.class); } } + + } diff --git a/src/main/java/com/qyft/gd/device/service/DeviceStepService.java b/src/main/java/com/qyft/gd/device/service/DeviceStepService.java index 54ab4a8..966b9d1 100644 --- a/src/main/java/com/qyft/gd/device/service/DeviceStepService.java +++ b/src/main/java/com/qyft/gd/device/service/DeviceStepService.java @@ -26,9 +26,10 @@ public class DeviceStepService { * @param heatId 加热区id */ public boolean upTray(String heatId) { - Map params = Map.of("heatId", heatId); - List> cmdList = cmdService.upTray(params); - return cmdList.stream().allMatch(Supplier::get); +// Map params = Map.of("heatId", heatId); +// List> cmdList = cmdService.upTray(params); +// return cmdList.stream().allMatch(Supplier::get); + return true; } /** @@ -37,9 +38,10 @@ public class DeviceStepService { * @param heatId 加热区id */ public boolean downTray(String heatId) { - Map params = Map.of("heatId", heatId); - List> cmdList = cmdService.downTray(params); - return cmdList.stream().allMatch(Supplier::get); +// Map params = Map.of("heatId", heatId); +// List> cmdList = cmdService.downTray(params); +// return cmdList.stream().allMatch(Supplier::get); + return true; } /** @@ -48,14 +50,14 @@ public class DeviceStepService { * @param tubeSolList 需要添加溶液的试管与溶液 */ public boolean addLiquid(List tubeSolList) { - for (TubeSol tubeSol : tubeSolList) { - Map params = Map.of("tubeNum", tubeSol.getTubeNum(), "solutionId", tubeSol.getSolId(), "volume", tubeSol.getVolume()); - List> cmdList = cmdService.downTray(params); - boolean result = cmdList.stream().allMatch(Supplier::get); - if (!result) { - return false; - } - } +// for (TubeSol tubeSol : tubeSolList) { +// Map params = Map.of("tubeNum", tubeSol.getTubeNum(), "solutionId", tubeSol.getSolId(), "volume", tubeSol.getVolume()); +// List> cmdList = cmdService.downTray(params); +// boolean result = cmdList.stream().allMatch(Supplier::get); +// if (!result) { +// return false; +// } +// } return true; } @@ -65,9 +67,10 @@ public class DeviceStepService { * @param heatId 加热区id */ public boolean moveToSol(String heatId) { - Map params = Map.of("heatId", heatId); - List> cmdList = cmdService.moveToActionArea(params); - return cmdList.stream().allMatch(Supplier::get); +// Map params = Map.of("heatId", heatId); +// List> cmdList = cmdService.moveToActionArea(params); +// return cmdList.stream().allMatch(Supplier::get); + return true; } /** @@ -76,9 +79,10 @@ public class DeviceStepService { * @param heatId 加热区id */ public boolean moveToHeat(String heatId) { - Map params = Map.of("heatId", heatId); - List> cmdList = cmdService.moveToHeatArea(params); - return cmdList.stream().allMatch(Supplier::get); +// Map params = Map.of("heatId", heatId); +// List> cmdList = cmdService.moveToHeatArea(params); +// return cmdList.stream().allMatch(Supplier::get); + return true; } /** @@ -87,16 +91,17 @@ public class DeviceStepService { * @param second 摇匀时间 */ public boolean shaking(int second) { - Map params = Map.of(); - List> cmdList = cmdService.startShakeUp(params); - boolean result = cmdList.stream().allMatch(Supplier::get); - if (result) { - this.delay(second); - cmdList = cmdService.stopShakeUp(params); - result = cmdList.stream().allMatch(Supplier::get); - return result; - } - return false; +// Map params = Map.of(); +// List> cmdList = cmdService.startShakeUp(params); +// boolean result = cmdList.stream().allMatch(Supplier::get); +// if (result) { +// this.delay(second); +// cmdList = cmdService.stopShakeUp(params); +// result = cmdList.stream().allMatch(Supplier::get); +// return result; +// } +// return false; + return true; } /** @@ -106,9 +111,10 @@ public class DeviceStepService { * @param temperature 目标温度 */ public boolean startHeating(String heatId, double temperature) { - Map params = Map.of("heatId", heatId, "temperature", temperature); - List> cmdList = cmdService.startHeat(params); - return cmdList.stream().allMatch(Supplier::get); +// Map params = Map.of("heatId", heatId, "temperature", temperature); +// List> cmdList = cmdService.startHeat(params); +// return cmdList.stream().allMatch(Supplier::get); + return true; } /** @@ -117,18 +123,20 @@ public class DeviceStepService { * @param heatId 加热区id */ public boolean stopHeating(String heatId) { - Map params = Map.of("heatId", heatId); - List> cmdList = cmdService.stopHeat(params); - return cmdList.stream().allMatch(Supplier::get); +// Map params = Map.of("heatId", heatId); +// List> cmdList = cmdService.stopHeat(params); +// return cmdList.stream().allMatch(Supplier::get); + return true; } /** * 停止加热 */ public boolean takePhoto() { - Map params = Map.of(); - List> cmdList = cmdService.takePhoto(params); - return cmdList.stream().allMatch(Supplier::get); +// Map params = Map.of(); +// List> cmdList = cmdService.takePhoto(params); +// return cmdList.stream().allMatch(Supplier::get); + return true; } //移至异常 diff --git a/src/main/java/com/qyft/gd/device/service/DeviceTcpCMDService.java b/src/main/java/com/qyft/gd/device/service/DeviceTcpCMDService.java index 431b0e7..da96798 100644 --- a/src/main/java/com/qyft/gd/device/service/DeviceTcpCMDService.java +++ b/src/main/java/com/qyft/gd/device/service/DeviceTcpCMDService.java @@ -119,6 +119,21 @@ public class DeviceTcpCMDService { } /** + * 导轨机械臂相对运动 + * + * @param x 相对坐标x + * @param y 相对坐标y + * @param z 相对坐标z + */ + public boolean moveRailArmRelative(int x, int y, int z) { + Map params = new HashMap<>(); + params.put("x", x); + params.put("y", y); + params.put("z", z); + return this.putTask(DeviceCommands.MOVE_RAIL_ARM_RELATIVE, params); + } + + /** * 设置导轨机械臂的速度 * * @param speed 速度值 diff --git a/src/main/java/com/qyft/gd/device/service/ICtrlFuncService.java b/src/main/java/com/qyft/gd/device/service/ICtrlFuncService.java index a1ffa7a..42045bb 100644 --- a/src/main/java/com/qyft/gd/device/service/ICtrlFuncService.java +++ b/src/main/java/com/qyft/gd/device/service/ICtrlFuncService.java @@ -1,21 +1,21 @@ package com.qyft.gd.device.service; import com.baomidou.mybatisplus.extension.service.IService; -import com.qyft.gd.device.model.bo.CtrlFuncDeviceCMD; +import com.qyft.gd.device.model.bo.DeviceCtrlFuncCMD; import com.qyft.gd.device.model.entity.CtrlFunc; import com.qyft.gd.device.model.form.CtrlFuncForm; -import com.qyft.gd.device.model.vo.CtrlFuncVO; +import com.qyft.gd.device.model.vo.DeviceCtrlFuncVO; import java.util.List; public interface ICtrlFuncService extends IService { - List findAllStepCMD(); + List findAllStepCMD(); List findAll(); - CtrlFuncVO findById(Long id); + DeviceCtrlFuncVO findById(Long id); long countFuncCmdByFuncCmd(String funcCmd); diff --git a/src/main/java/com/qyft/gd/device/service/ICtrlFuncStepService.java b/src/main/java/com/qyft/gd/device/service/ICtrlFuncStepService.java index 076f8d1..14d31a8 100644 --- a/src/main/java/com/qyft/gd/device/service/ICtrlFuncStepService.java +++ b/src/main/java/com/qyft/gd/device/service/ICtrlFuncStepService.java @@ -6,7 +6,7 @@ import com.qyft.gd.device.model.entity.CtrlFuncStep; import java.util.List; public interface ICtrlFuncStepService extends IService { - List selectListByFuncId(Long funcId); + List selectListByFuncCmd(String funcCmd); + - boolean saveList(List ctrlFuncStepList); } diff --git a/src/main/java/com/qyft/gd/device/service/impl/CtrlFuncServiceImpl.java b/src/main/java/com/qyft/gd/device/service/impl/CtrlFuncServiceImpl.java index 32183fe..226d951 100644 --- a/src/main/java/com/qyft/gd/device/service/impl/CtrlFuncServiceImpl.java +++ b/src/main/java/com/qyft/gd/device/service/impl/CtrlFuncServiceImpl.java @@ -3,11 +3,11 @@ package com.qyft.gd.device.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.qyft.gd.device.mapper.CtrlFuncMapper; -import com.qyft.gd.device.model.bo.CtrlFuncDeviceCMD; +import com.qyft.gd.device.model.bo.DeviceCtrlFuncCMD; import com.qyft.gd.device.model.entity.CtrlFunc; import com.qyft.gd.device.model.entity.CtrlFuncStep; import com.qyft.gd.device.model.form.CtrlFuncForm; -import com.qyft.gd.device.model.vo.CtrlFuncVO; +import com.qyft.gd.device.model.vo.DeviceCtrlFuncVO; import com.qyft.gd.device.service.ICtrlFuncService; import com.qyft.gd.device.service.ICtrlFuncStepService; import lombok.RequiredArgsConstructor; @@ -24,28 +24,29 @@ public class CtrlFuncServiceImpl extends ServiceImpl i private final ICtrlFuncStepService ctrlFuncStepService; - private static final List ctrlFuncDeviceCMDList = new ArrayList<>(); + private static final List ctrlFuncDeviceCMDList = new ArrayList<>(); static { - ctrlFuncDeviceCMDList.add(new CtrlFuncDeviceCMD("开门", "openDoor")); - ctrlFuncDeviceCMDList.add(new CtrlFuncDeviceCMD("关门", "closeDoor")); - ctrlFuncDeviceCMDList.add(new CtrlFuncDeviceCMD("导轨机械臂运动到指定点位", "moveRailArmToPoint")); - ctrlFuncDeviceCMDList.add(new CtrlFuncDeviceCMD("打开导轨机械臂夹爪", "openClaw")); - ctrlFuncDeviceCMDList.add(new CtrlFuncDeviceCMD("收合导轨机械臂夹爪", "closeClaw")); - ctrlFuncDeviceCMDList.add(new CtrlFuncDeviceCMD("导轨机械臂移至指定加热区上方", "moveToHeatArea")); - ctrlFuncDeviceCMDList.add(new CtrlFuncDeviceCMD("导轨机械臂移至加液区上方", "moveToActionArea")); - ctrlFuncDeviceCMDList.add(new CtrlFuncDeviceCMD("指定加热区开始加热", "startHeating")); - ctrlFuncDeviceCMDList.add(new CtrlFuncDeviceCMD("指定加热区停止加热", "stopHeating")); - ctrlFuncDeviceCMDList.add(new CtrlFuncDeviceCMD("指定加热区抬起托盘", "raiseTray")); - ctrlFuncDeviceCMDList.add(new CtrlFuncDeviceCMD("指定加热区放下托盘", "lowerTray")); - ctrlFuncDeviceCMDList.add(new CtrlFuncDeviceCMD("依次添加溶液", "addLiquid")); - ctrlFuncDeviceCMDList.add(new CtrlFuncDeviceCMD("开始摇匀", "startShaking")); - ctrlFuncDeviceCMDList.add(new CtrlFuncDeviceCMD("停止摇匀", "stopShaking")); - ctrlFuncDeviceCMDList.add(new CtrlFuncDeviceCMD("拍照", "takePhoto")); + ctrlFuncDeviceCMDList.add(new DeviceCtrlFuncCMD("开门", "openDoor")); + ctrlFuncDeviceCMDList.add(new DeviceCtrlFuncCMD("关门", "closeDoor")); + ctrlFuncDeviceCMDList.add(new DeviceCtrlFuncCMD("导轨机械臂运动到指定点位", "moveRailArmToPoint")); + ctrlFuncDeviceCMDList.add(new DeviceCtrlFuncCMD("导轨机械臂相对运动", "moveRailArmRelative")); + ctrlFuncDeviceCMDList.add(new DeviceCtrlFuncCMD("导轨机械臂移至指定加热区上方", "moveToHeatArea")); + ctrlFuncDeviceCMDList.add(new DeviceCtrlFuncCMD("导轨机械臂移至加液区上方", "moveToActionArea")); + ctrlFuncDeviceCMDList.add(new DeviceCtrlFuncCMD("打开导轨机械臂夹爪", "openClaw")); + ctrlFuncDeviceCMDList.add(new DeviceCtrlFuncCMD("收合导轨机械臂夹爪", "closeClaw")); + ctrlFuncDeviceCMDList.add(new DeviceCtrlFuncCMD("指定加热区开始加热", "startHeating")); + ctrlFuncDeviceCMDList.add(new DeviceCtrlFuncCMD("指定加热区停止加热", "stopHeating")); + ctrlFuncDeviceCMDList.add(new DeviceCtrlFuncCMD("指定加热区抬起托盘", "raiseTray")); + ctrlFuncDeviceCMDList.add(new DeviceCtrlFuncCMD("指定加热区放下托盘", "lowerTray")); + ctrlFuncDeviceCMDList.add(new DeviceCtrlFuncCMD("依次添加溶液", "addLiquid")); + ctrlFuncDeviceCMDList.add(new DeviceCtrlFuncCMD("开始摇匀", "startShaking")); + ctrlFuncDeviceCMDList.add(new DeviceCtrlFuncCMD("停止摇匀", "stopShaking")); + ctrlFuncDeviceCMDList.add(new DeviceCtrlFuncCMD("拍照", "takePhoto")); } @Override - public List findAllStepCMD() { + public List findAllStepCMD() { return ctrlFuncDeviceCMDList; } @@ -55,11 +56,11 @@ public class CtrlFuncServiceImpl extends ServiceImpl i } @Override - public CtrlFuncVO findById(Long id) { + public DeviceCtrlFuncVO findById(Long id) { CtrlFunc ctrlFunc = this.getById(id); - List ctrlFuncStepList = ctrlFuncStepService.selectListByFuncId(id); - CtrlFuncVO ctrlFuncVO = new CtrlFuncVO(); - ctrlFuncVO.setId(id); + List ctrlFuncStepList = ctrlFuncStepService.selectListByFuncCmd(ctrlFunc.getFuncCmd()); + DeviceCtrlFuncVO ctrlFuncVO = new DeviceCtrlFuncVO(); + ctrlFuncVO.setId(ctrlFunc.getId()); ctrlFuncVO.setName(ctrlFunc.getName()); ctrlFuncVO.setFuncCmd(ctrlFunc.getFuncCmd()); ctrlFuncVO.setCtrlFuncStepList(ctrlFuncStepList); @@ -101,7 +102,7 @@ public class CtrlFuncServiceImpl extends ServiceImpl i .map(Long::parseLong) .collect(Collectors.toList()); for (Long id : ids) { - ctrlFuncStepService.remove(new LambdaQueryWrapper().eq(CtrlFuncStep::getFuncId, id)); + ctrlFuncStepService.remove(new LambdaQueryWrapper().eq(CtrlFuncStep::getFuncCmd, id)); } return this.removeByIds(ids); } @@ -111,7 +112,7 @@ public class CtrlFuncServiceImpl extends ServiceImpl i List ctrlFuncStepList = ctrlFuncStepFormList.stream() .map(ctrlFuncStepForm -> { CtrlFuncStep ctrlFuncStep = new CtrlFuncStep(); - ctrlFuncStep.setFuncId(ctrlFunc.getId()); + ctrlFuncStep.setFuncCmd(ctrlFunc.getFuncCmd()); ctrlFuncStep.setDeviceCmd(ctrlFuncStepForm.getDeviceCmd()); ctrlFuncStep.setParams(ctrlFuncStepForm.getParams()); return ctrlFuncStep; diff --git a/src/main/java/com/qyft/gd/device/service/impl/CtrlFuncStepServiceImpl.java b/src/main/java/com/qyft/gd/device/service/impl/CtrlFuncStepServiceImpl.java index 36610e3..a59c20a 100644 --- a/src/main/java/com/qyft/gd/device/service/impl/CtrlFuncStepServiceImpl.java +++ b/src/main/java/com/qyft/gd/device/service/impl/CtrlFuncStepServiceImpl.java @@ -13,18 +13,8 @@ import java.util.List; public class CtrlFuncStepServiceImpl extends ServiceImpl implements ICtrlFuncStepService { @Override - public List selectListByFuncId(Long funcId) { - return this.list(new LambdaQueryWrapper().eq(CtrlFuncStep::getFuncId, funcId)); + public List selectListByFuncCmd(String funcCmd) { + return this.list(new LambdaQueryWrapper().eq(CtrlFuncStep::getFuncCmd, funcCmd)); } - @Override - public boolean saveList(List ctrlFuncStepList) { - for (CtrlFuncStep ctrlFuncStep : ctrlFuncStepList) { - this.save(ctrlFuncStep); - } - - -// this.saveBatch(list); - return false; - } } diff --git a/src/main/java/com/qyft/gd/service/CMDService.java b/src/main/java/com/qyft/gd/service/CMDService.java index 2152a29..5a44a9e 100644 --- a/src/main/java/com/qyft/gd/service/CMDService.java +++ b/src/main/java/com/qyft/gd/service/CMDService.java @@ -1,513 +1,123 @@ package com.qyft.gd.service; import cn.hutool.json.JSONUtil; -import com.qyft.gd.common.constant.Commands; import com.qyft.gd.common.constant.WebSocketMessageType; import com.qyft.gd.common.result.CMDResultCode; -import com.qyft.gd.device.service.DeviceTcpCMDService; -import com.qyft.gd.model.dto.CmdInjectFluidDTO; -import com.qyft.gd.model.dto.InjectFluid; -import com.qyft.gd.model.dto.StartHeatDTO; +import com.qyft.gd.device.model.bo.DeviceCtrlFuncCMD; +import com.qyft.gd.device.model.entity.CtrlFuncStep; +import com.qyft.gd.device.service.DeviceCtrlService; +import com.qyft.gd.device.service.ICtrlFuncService; +import com.qyft.gd.device.service.ICtrlFuncStepService; +import com.qyft.gd.model.entity.TaskSteps; +import com.qyft.gd.model.entity.Tasks; import com.qyft.gd.model.form.CMDForm; -import com.qyft.gd.model.vo.ContainerListVO; import com.qyft.gd.model.vo.ExecutionResult; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.HashMap; +import java.lang.reflect.Method; import java.util.List; import java.util.Map; -import java.util.function.Function; -import java.util.function.Supplier; @Slf4j +@RequiredArgsConstructor @Service public class CMDService { private final ITasksService tasksService; private final ITaskStepsService taskStepsService; - DeviceTcpCMDService deviceTcpCMDService; - WebSocketService webSocketService; - BaseDataService baseDataService; - IContainerService containerService; - Map, List>>> commandMap; - CMDForm form; - Map cmdMap; - - @Autowired - public CMDService(DeviceTcpCMDService deviceTcpCMDService, WebSocketService webSocketService, BaseDataService baseDataService, ITasksService tasksService, ITaskStepsService taskStepsService, IContainerService containerService) { - this.deviceTcpCMDService = deviceTcpCMDService; - this.webSocketService = webSocketService; - this.baseDataService = baseDataService; - this.tasksService = tasksService; - this.taskStepsService = taskStepsService; - this.containerService = containerService; - this.cmdMap = new HashMap<>(); - cmdMap.put("openDoor", "开门"); - cmdMap.put("closeDoor", "关门"); - cmdMap.put("upTray", "抬起托盘"); - cmdMap.put("downTray", "降下托盘"); - cmdMap.put("injectFluid", "添加溶液"); - cmdMap.put("moveToActionArea", "移至加液"); - cmdMap.put("startShakeUp", "摇匀试管架"); - cmdMap.put("stopShakeUp", "停止摇匀试管架"); - cmdMap.put("startHeat", "开始加热"); - cmdMap.put("stopHeat", "停止加热"); - cmdMap.put("takePhoto", "拍照"); - cmdMap.put("takeOffCap", "取试管架盖"); - cmdMap.put("putBackCap", "装回试管架盖"); - cmdMap.put("moveMachineArm", "机械臂移动至指定坐标"); - cmdMap.put("moveTube", "移动单个试管"); - cmdMap.put("moveToHeatArea", "移至加热区"); - cmdMap.put("openClaw", "机械臂爪子开启"); - cmdMap.put("closeClaw", "机械臂爪子关闭"); - // 初始化命令映射 - this.commandMap = new HashMap<>(); - commandMap.put(Commands.OPEN_DOOR, this::openDoor); - commandMap.put(Commands.CLOSE_DOOR, this::closeDoor); - commandMap.put(Commands.UP_TRAY, this::upTray); - commandMap.put(Commands.DOWN_TRAY, this::downTray); - commandMap.put(Commands.INJECT_FLUID, this::injectFluid); - commandMap.put(Commands.MOVE_TO_ACTION_AREA, this::moveToActionArea); - commandMap.put(Commands.START_SHAKE_UP, this::startShakeUp); - commandMap.put(Commands.STOP_SHAKE_UP, this::stopShakeUp); - commandMap.put(Commands.START_HEAT, this::startHeat); - commandMap.put(Commands.STOP_HEAT, this::stopHeat); - commandMap.put(Commands.TAKE_PHOTO, this::takePhoto); - commandMap.put(Commands.TAKE_OFF_CAP, this::takeOffCap); - commandMap.put(Commands.PUT_BACK_CAP, this::putBackCap); - commandMap.put(Commands.MOVE_MACHINE_ARM, this::moveMachineArm); - commandMap.put(Commands.MOVE_TUBE, this::moveTube); - commandMap.put(Commands.MOVE_TO_HEAT_AREA, this::moveToHeatArea); - commandMap.put(Commands.OPEN_CLAW, this::openClaw); - commandMap.put(Commands.CLOSE_CLAW, this::closeClaw); - - } - - // 开门 - public List> openDoor(Map params) { - List> cmdList = new ArrayList<>(); - cmdList.add(() -> deviceTcpCMDService.openDoor()); - return cmdList; - } - - // 关门 - public List> closeDoor(Map params) { - List> cmdList = new ArrayList<>(); - cmdList.add(() -> deviceTcpCMDService.closeDoor()); - return cmdList; - } - - // 机械臂爪子开启 - public List> openClaw(Map params) { - List> cmdList = new ArrayList<>(); - cmdList.add(() -> deviceTcpCMDService.openClaw()); - return cmdList; - } - - // 机械臂爪子闭合 - public List> closeClaw(Map params) { - List> cmdList = new ArrayList<>(); - cmdList.add(() -> deviceTcpCMDService.closeClaw()); - return cmdList; - } - - // 移至加热 - public List> moveToHeatArea(Map params) { - List> cmdList = new ArrayList<>(); - Map map = baseDataService.getOffsetMap(); - String heatAreaPosition = baseDataService.getHeatAreaPositionById((Integer) params.get("heatId")); - String[] heatAreaPositionArr = heatAreaPosition.split(","); - int x1 = Integer.parseInt(heatAreaPositionArr[0]); - int y1 = Integer.parseInt(heatAreaPositionArr[1]); - int z1 = Integer.parseInt(heatAreaPositionArr[2]); - - String solutionAreaPosition = baseDataService.getSolutionAreaPosition(); - String[] solutionAreaPositionArr = solutionAreaPosition.split(","); - int x2 = Integer.parseInt(solutionAreaPositionArr[0]); - int y2 = Integer.parseInt(solutionAreaPositionArr[1]); - int z2 = Integer.parseInt(solutionAreaPositionArr[2]); - // 机械臂移动到加液位 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x2, y2, z2)); - // 打开钩子 - cmdList.add(() -> deviceTcpCMDService.openClaw()); - // 下降高度 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x2, y2, z2 - Integer.parseInt((String) map.get("sys_offset_tube_rack_take_height")))); - // 闭合钩子 - cmdList.add(() -> deviceTcpCMDService.closeClaw()); - // 机械臂抬起高度 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x2, y2, z2 + Integer.parseInt((String) map.get("sys_offset_tube_rack_take_height")) + Integer.parseInt((String) map.get("sys_offset_tube_height")))); - // 机械臂移动到指定加热位 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x1, y1, z2 + Integer.parseInt((String) map.get("sys_offset_tube_rack_take_height")) + Integer.parseInt((String) map.get("sys_offset_tube_height")))); - // 机械臂下降高度 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x1, y1, z1)); - // 机械臂打开钩子 - cmdList.add(() -> deviceTcpCMDService.openClaw()); - // 机械臂上升高度 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x1, y1, z1 + Integer.parseInt((String) map.get("sys_offset_tube_rack_take_height")))); - // 关闭钩子 - cmdList.add(() -> deviceTcpCMDService.closeClaw()); - - return cmdList; - } - - // 移动单个试管 - public List> moveTube(Map params) { - List> cmdList = new ArrayList<>(); - - return cmdList; - } - - // 机械臂 - public List> moveMachineArm(Map params) { - List> cmdList = new ArrayList<>(); - String position = (String) params.get("position"); - String[] positionArr = position.split(","); - int x = Integer.parseInt(positionArr[0]); - int y = Integer.parseInt(positionArr[1]); - int z = Integer.parseInt(positionArr[2]); - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x, y, z)); - return cmdList; - } - - // 装回盖子 - public List> putBackCap(Map params) { - List> cmdList = new ArrayList<>(); - // 加热位机器代码 - String heatId = baseDataService.getHeatAreaHardwareIdById((Integer) params.get("heatId")); - String lidPosition = baseDataService.getLidPosition(); - String[] lidPositionArr = lidPosition.split(","); - int x1 = Integer.parseInt(lidPositionArr[0]); - int y1 = Integer.parseInt(lidPositionArr[1]); - int z1 = Integer.parseInt(lidPositionArr[2]); - - String position = baseDataService.getHeatAreaLidPositionById((Integer) params.get("heatId")); - Map map = baseDataService.getOffsetMap(); - String[] positionArr = position.split(","); - int x2 = Integer.parseInt(positionArr[0]); - int y2 = Integer.parseInt(positionArr[1]); - int z2 = Integer.parseInt(positionArr[2]); - //机械臂移动到拍子存放位坐标 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x1, y1, z1)); - // 打开钩子 - cmdList.add(() -> deviceTcpCMDService.openClaw()); - // 机械臂下移位置 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x1, y1, z1 - (Integer) map.get("sys_offset_lid_take_height"))); - // 闭合钩子 - cmdList.add(() -> deviceTcpCMDService.closeClaw()); - // 机械臂抬起指定高度 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x1, y1, z1)); - // 机械臂移至指定试管架 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x2, y2, z2)); - // 打开钩子 - cmdList.add(() -> deviceTcpCMDService.openClaw()); - // 机械臂上移指定位置 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x1, y1, z1 + (Integer) map.get("sys_offset_lid_take_height"))); - // 关闭钩子 - cmdList.add(() -> deviceTcpCMDService.closeClaw()); - // 密封 - // 解除密封 - cmdList.add(() -> deviceTcpCMDService.setSealLid(Integer.parseInt(heatId), true)); - // 机械臂复位 - return cmdList; - } - - // 取试管架盖 - public List> takeOffCap(Map params) { - List> cmdList = new ArrayList<>(); - // 拍子坐标 - String position = baseDataService.getHeatAreaLidPositionById((Integer) params.get("heatId")); - // 加热位机器代码 - String hardwareId = baseDataService.getHeatAreaHardwareIdById((Integer) params.get("heatId")); - - Map map = baseDataService.getOffsetMap(); - String[] positionArr = position.split(","); - int x1 = Integer.parseInt(positionArr[0]); - int y1 = Integer.parseInt(positionArr[1]); - int z1 = Integer.parseInt(positionArr[2]); - - String lidPosition = baseDataService.getLidPosition(); - String[] lidPositionArr = lidPosition.split(","); - int x2 = Integer.parseInt(lidPositionArr[0]); - int y2 = Integer.parseInt(lidPositionArr[1]); - int z2 = Integer.parseInt(lidPositionArr[2]); - - //机械臂移动到拍子坐标 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x1, y1, z1)); - // 解除密封 - cmdList.add(() -> deviceTcpCMDService.setSealLid(Integer.parseInt(hardwareId), false)); - // 打开钩子 - cmdList.add(() -> deviceTcpCMDService.openClaw()); - // 机械臂下移位置 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x1, y1, z1 - (Integer) map.get("sys_offset_lid_take_height"))); - // 闭合钩子 - cmdList.add(() -> deviceTcpCMDService.closeClaw()); - // 机械臂抬起指定高度 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x1, y1, z1)); - // 机械臂移至拍子存放区 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x2, y2, z2)); - // 打开钩子 - cmdList.add(() -> deviceTcpCMDService.openClaw()); - // 机械臂上移指定位置 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x2, y2, z2 + (Integer) map.get("sys_offset_lid_take_height"))); - // 关闭钩子 - cmdList.add(() -> deviceTcpCMDService.closeClaw()); - // 机械臂复位 - return cmdList; - } - - // 拍照 - public List> takePhoto(Map params) { - List> cmdList = new ArrayList<>(); - cmdList.add(() -> deviceTcpCMDService.takePhoto()); - return cmdList; - } - - // 停止加热 - public List> stopHeat(Map params) { - List> cmdList = new ArrayList<>(); - String hardwareId = baseDataService.getHeatAreaHardwareIdById((Integer) params.get("heatId")); - cmdList.add(() -> deviceTcpCMDService.stopHeating(hardwareId)); - return cmdList; - } + private final WebSocketService webSocketService; + private final DeviceCtrlService deviceCtrlService; + private final ICtrlFuncStepService ctrlFuncStepService; + private final ICtrlFuncService ctrlFuncService; + + @SuppressWarnings("unchecked") + private void run(CMDForm cmdForm) { + String commandName = cmdForm.getCommand(); + ExecutionResult executionResult = new ExecutionResult(); + executionResult.setCommandId(cmdForm.getCommandId()); + executionResult.setCommandName(commandName); + + try { + List paramsList = cmdForm.getParams(); + List ctrlFuncStepList = ctrlFuncStepService.selectListByFuncCmd(commandName); + + int index = 0; + for (CtrlFuncStep ctrlFuncStep : ctrlFuncStepList) { + Map params = null; + if (ctrlFuncStep.getParams() == null) { // 如果没有参数定义,使用传入的参数 + try { + params = (Map) paramsList.get(index); + } catch (IndexOutOfBoundsException e) { + log.error("指令执行错误,传参错误: {}", JSONUtil.toJsonStr(cmdForm)); + executionResult.setStatus(CMDResultCode.FAILURE.getCode()); + executionResult.setMessage(CMDResultCode.FAILURE.getMsg()); + webSocketService.pushMsg(WebSocketMessageType.CMD, executionResult); + return; + } + index++; // 移动到下一个参数 + } - // 批量加热 - public void startHeats(StartHeatDTO dto) { - List> cmdList = new ArrayList<>(); - for (Integer i : dto.getHeatIds()) { - Map map = new HashMap<>(); - map.put("heatId", i); - cmdList.addAll(startHeat(map)); - } - new Thread(() -> { - ExecutionResult executionResult = new ExecutionResult(); - executionResult.setCommandId(dto.getCommandId()); - // 执行所有命令 - for (Supplier command : cmdList) { - boolean result = command.get(); - if (!result) { - log.error("指令执行异常: {}", JSONUtil.toJsonStr(dto)); - executionResult.setStatus(CMDResultCode.FAILURE.getCode()); - executionResult.setMessage(CMDResultCode.FAILURE.getMsg()); - webSocketService.pushMsg(WebSocketMessageType.CMD, executionResult); - return; + String deviceCmd = ctrlFuncStep.getDeviceCmd(); + Method method = getMethodByName(deviceCmd); + if (method != null) { + boolean success = (boolean) method.invoke(deviceCtrlService, params); + if (!success) { + log.error("指令执行错误,执行{}返回false: {}", deviceCmd, JSONUtil.toJsonStr(cmdForm)); + executionResult.setStatus(CMDResultCode.FAILURE.getCode()); + executionResult.setMessage(CMDResultCode.FAILURE.getMsg()); + webSocketService.pushMsg(WebSocketMessageType.CMD, executionResult); + return; + } } } executionResult.setStatus(CMDResultCode.SUCCESS.getCode()); executionResult.setMessage(CMDResultCode.SUCCESS.getMsg()); webSocketService.pushMsg(WebSocketMessageType.CMD, executionResult); - }).start(); - } - - // 开始加热 - public List> startHeat(Map params) { - List> cmdList = new ArrayList<>(); - String hardwareId = baseDataService.getHeatAreaHardwareIdById((Integer) params.get("heatId")); - Double temperature; - if (params.get("temperature") == null) { - temperature = Double.parseDouble(baseDataService.getHeatAreaTemperatureById((Integer) params.get("heatId"))); - } else { - temperature = Double.parseDouble((String) params.get("temperature")); - } - cmdList.add(() -> deviceTcpCMDService.startHeating(hardwareId, temperature)); - return cmdList; - } - - // 开始摇匀 - public List> startShakeUp(Map params) { - List> cmdList = new ArrayList<>(); - if (params.get("speed") != null) { - cmdList.add(() -> deviceTcpCMDService.setShakingSpeed((Integer) params.get("speed"))); + return; + } catch (Exception e) { + log.error("指令执行错误: {}", JSONUtil.toJsonStr(cmdForm), e); } - cmdList.add(() -> deviceTcpCMDService.startShaking()); - return cmdList; - } - - // 结束摇匀 - public List> stopShakeUp(Map params) { - List> cmdList = new ArrayList<>(); - cmdList.add(() -> deviceTcpCMDService.stopShaking()); - return cmdList; - } - - // 移至加液 - public List> moveToActionArea(Map params) { - List> cmdList = new ArrayList<>(); - Map map = baseDataService.getOffsetMap(); - String heatAreaPosition = baseDataService.getHeatAreaPositionById((Integer) params.get("heatId")); - String[] heatAreaPositionArr = heatAreaPosition.split(","); - int x1 = Integer.parseInt(heatAreaPositionArr[0]); - int y1 = Integer.parseInt(heatAreaPositionArr[1]); - int z1 = Integer.parseInt(heatAreaPositionArr[2]); - - String solutionAreaPosition = baseDataService.getSolutionAreaPosition(); - String[] solutionAreaPositionArr = solutionAreaPosition.split(","); - int x2 = Integer.parseInt(solutionAreaPositionArr[0]); - int y2 = Integer.parseInt(solutionAreaPositionArr[1]); - int z2 = Integer.parseInt(solutionAreaPositionArr[2]); - // 机械臂移动到指定的加热位坐标 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x1, y1, z1)); - // 打开钩子 - cmdList.add(() -> deviceTcpCMDService.openClaw()); - // 下降高度 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x1, y1, z1 - Integer.parseInt((String) map.get("sys_offset_tube_rack_take_height")))); - // 闭合钩子 - cmdList.add(() -> deviceTcpCMDService.closeClaw()); - // 机械臂抬起高度 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x1, y1, z1 + Integer.parseInt((String) map.get("sys_offset_tube_rack_take_height")) + Integer.parseInt((String) map.get("sys_offset_tube_height")))); - // 机械臂移动到加液位高度 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x2, y2, z1 + Integer.parseInt((String) map.get("sys_offset_tube_rack_take_height")) + Integer.parseInt((String) map.get("sys_offset_tube_height")))); - // 机械臂下降高度 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x2, y2, z2)); - // 机械臂打开钩子 - cmdList.add(() -> deviceTcpCMDService.openClaw()); - // 机械臂上升高度 - cmdList.add(() -> deviceTcpCMDService.moveRailArmToPoint(x2, y2, z2 + Integer.parseInt((String) map.get("sys_offset_tube_rack_take_height")))); - // 关闭钩子 - cmdList.add(() -> deviceTcpCMDService.closeClaw()); - - return cmdList; + // 如果执行到这里,说明没有成功的步骤,返回失败 + log.error("指令执行错误: {}", JSONUtil.toJsonStr(cmdForm)); + executionResult.setStatus(CMDResultCode.FAILURE.getCode()); + executionResult.setMessage(CMDResultCode.FAILURE.getMsg()); + webSocketService.pushMsg(WebSocketMessageType.CMD, executionResult); } - // 批量加液 - public void injectFluids(CmdInjectFluidDTO dto) { - List> cmdList = new ArrayList<>(); - for (InjectFluid injectFluid : dto.getInjectFluids()) { - Map map = new HashMap<>(); - map.put("tubeNum", injectFluid.getTubeNum()); - map.put("solutionId", injectFluid.getSolutionId()); - map.put("volume", injectFluid.getVolume()); - cmdList.addAll(injectFluid(map)); - } - - new Thread(() -> { - ExecutionResult executionResult = new ExecutionResult(); - executionResult.setCommandId(dto.getCommandId()); - // 执行所有命令 - for (Supplier command : cmdList) { - boolean result = command.get(); - if (!result) { - log.error("指令执行异常: {}", JSONUtil.toJsonStr(dto)); - executionResult.setStatus(CMDResultCode.FAILURE.getCode()); - executionResult.setMessage(CMDResultCode.FAILURE.getMsg()); - webSocketService.pushMsg(WebSocketMessageType.CMD, executionResult); - return; + public boolean executeCommand(CMDForm cmdForm) { + String commandName = cmdForm.getCommand(); + List deviceCtrlFuncCMDList = ctrlFuncService.findAllStepCMD(); + for (DeviceCtrlFuncCMD deviceCtrlFuncCMD : deviceCtrlFuncCMDList) { + if (deviceCtrlFuncCMD.getDeviceCmd().equals(commandName)) { + Tasks tasks = tasksService.getIngTask(); + if (tasks != null) { + TaskSteps taskSteps = new TaskSteps(); + taskSteps.setTaskId(tasks.getId()); + taskSteps.setStepDescription("执行指令:" + commandName); + taskStepsService.addTaskSteps(taskSteps); } + new Thread(() -> run(cmdForm)).start(); + return true; } - executionResult.setStatus(CMDResultCode.SUCCESS.getCode()); - executionResult.setMessage(CMDResultCode.SUCCESS.getMsg()); - webSocketService.pushMsg(WebSocketMessageType.CMD, executionResult); - }).start(); - } - - // 加液 - public List> injectFluid(Map params) { - List> cmdList = new ArrayList<>(); - // 获取容器信息 - ContainerListVO container = baseDataService.getContainerBySolutionId((Integer) params.get("solutionId")); - if (container.getCapacityTotal() - container.getCapacityUsed() < (Integer) params.get("volume")) { - cmdList.add(() -> false); - } - - // 试管的坐标 - String tubePosition = baseDataService.getTubePositionBySolutionArea((Integer) params.get("tubeNum")); - String[] tubePositionArr = tubePosition.split(","); - int x = Integer.parseInt(tubePositionArr[0]); - int y = Integer.parseInt(tubePositionArr[1]); - int z = Integer.parseInt(tubePositionArr[2]); - // 泵id - String pumpId = baseDataService.getPumpIdBySolutionId((Long) params.get("solutionId")); - if (params.get("flowRate") != null) { - cmdList.add(() -> deviceTcpCMDService.setFlowRate(pumpId, (Integer) params.get("flowRate"))); } - cmdList.add(() -> deviceTcpCMDService.moveLiquidArmToPoint(x, y, z)); - cmdList.add(() -> deviceTcpCMDService.addLiquid(pumpId, ((Integer) params.get("volume")))); - cmdList.add(() -> updateVolume(params)); - - return cmdList; - } - - // 放下托盘 - public List> downTray(Map params) { - List> cmdList = new ArrayList<>(); - String hardwareId = baseDataService.getHeatAreaHardwareIdById((Integer) params.get("heatId")); - if (params.get("height") != null && params.get("speed") != null) { - cmdList.add(() -> deviceTcpCMDService.setTrayParams(hardwareId, (Integer) params.get("speed"), (Integer) params.get("height"))); - } - cmdList.add(() -> deviceTcpCMDService.lowerTray(hardwareId)); - return cmdList; - } - - // 抬起托盘 - public List> upTray(Map params) { - List> cmdList = new ArrayList<>(); - String hardwareId = baseDataService.getHeatAreaHardwareIdById((Integer) params.get("heatId")); - if (params.get("height") != null && params.get("speed") != null) { - cmdList.add(() -> deviceTcpCMDService.setTrayParams(hardwareId, (Integer) params.get("speed"), (Integer) params.get("height"))); - } - cmdList.add(() -> deviceTcpCMDService.raiseTray(hardwareId)); - - return cmdList; - } - - private boolean updateVolume(Map params) { - // 获取容器信息 - ContainerListVO container = baseDataService.getContainerBySolutionId((Integer) params.get("solutionId")); - container.setCapacityUsed(container.getCapacityUsed() + (Integer) params.get("volume")); - containerService.updateById(container); - baseDataService.updateConfig(); - List list = containerService.getAllContainer(); - Map containerMap = new HashMap<>(); - containerMap.put("containerList", list); - webSocketService.pushMsg(WebSocketMessageType.CONTAINER, containerMap); - return true; + return false; } - private void initExecutorThread(List> cmdList) { - new Thread(() -> run(cmdList)).start(); - } - - private void run(List> cmdList) { - ExecutionResult executionResult = new ExecutionResult(); - executionResult.setCommandId(form.getCommandId()); - executionResult.setCommandName(form.getCommand()); - - // 执行所有命令 - for (Supplier command : cmdList) { - boolean result = command.get(); - if (!result) { - log.error("指令执行异常: {}", JSONUtil.toJsonStr(form)); - executionResult.setStatus(CMDResultCode.FAILURE.getCode()); - executionResult.setMessage(CMDResultCode.FAILURE.getMsg()); - webSocketService.pushMsg(WebSocketMessageType.CMD, executionResult); - return; + private Method getMethodByName(String methodName) { + try { + Class clazz = deviceCtrlService.getClass(); + for (Method method : clazz.getDeclaredMethods()) { + if (method.getName().equals(methodName)) { + return method; + } } + } catch (Exception e) { + log.error("执行指令没有找到对应的方法:{}", methodName, e); } - executionResult.setStatus(CMDResultCode.SUCCESS.getCode()); - executionResult.setMessage(CMDResultCode.SUCCESS.getMsg()); - webSocketService.pushMsg(WebSocketMessageType.CMD, executionResult); - } - - public boolean executeCommand(CMDForm cmdForm) { -// form = cmdForm; -// String commandName = cmdForm.getCommand(); -// Function, List>> command = commandMap.get(commandName); -// if (command == null) { -// return false; -// } -// List> cmdList = command.apply(form.getParams()); -// Tasks tasks = tasksService.getIngTask(); -// if (tasks != null) { -// TaskSteps taskSteps = new TaskSteps(); -// taskSteps.setTaskId(tasks.getId()); -// taskSteps.setStepDescription("执行指令:" + cmdMap.get(commandName)); -// taskStepsService.addTaskSteps(taskSteps); -// } -// initExecutorThread(cmdList); - return true; + return null; }