From 5baf5f09bbe50e13d74731ae0728057d8faae2f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E5=87=A4=E5=90=89?= Date: Thu, 20 Feb 2025 15:24:21 +0800 Subject: [PATCH] =?UTF-8?q?fix:tcp=E6=8C=87=E4=BB=A4=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E4=BD=BF=E7=94=A8DeviceCommands?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gd/device/common/constant/DeviceCommands.java | 34 ++++++++-- .../com/qyft/gd/device/service/DeviceService.java | 72 +++++++++++----------- 2 files changed, 64 insertions(+), 42 deletions(-) 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 aa8bad7..416d456 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 @@ -14,12 +14,18 @@ public class DeviceCommands { /** 设置导轨机械臂的速度 */ public static final String SET_RAIL_ARM_SPEED = "setRailArmSpeed"; + /** 轨道机械臂移动停止移动 */ + public static final String STOP_RAIL_ARM = "stopRailArm"; + /** 开门 */ public static final String OPEN_DOOR = "openDoor"; /** 关门 */ public static final String CLOSE_DOOR = "closeDoor"; + /** 设置门参数 */ + public static final String SET_DOOR_PARAMS = "setDoorParams"; + /** 张开夹爪 */ public static final String OPEN_CLAW = "openClaw"; @@ -35,9 +41,18 @@ public class DeviceCommands { /** 设置加液机械臂的速度 */ public static final String SET_LIQUID_ARM_SPEED = "setLiquidArmSpeed"; + /** 加液机械臂移动停止 */ + public static final String STOP_LIQUID_ARM = "stopLiquidArm"; + + /** 设置夹爪 */ + public static final String SET_CLAW_PARAMS = "setClawParams"; + /** 加液 */ public static final String ADD_LIQUID = "addLiquid"; + /** 设置加热位试管架拍子密封状态 */ + public static final String SET_SEAL_LID = "setSealLid"; + /** 设置加液泵流量 */ public static final String SET_FLOW_RATE = "setFlowRate"; @@ -56,15 +71,22 @@ public class DeviceCommands { /** 停止加热 */ public static final String STOP_HEATING = "stopHeating"; - /** 抬起托盘到指定高度 */ - public static final String MOVE_TRAY_TO_HEIGHT = "moveTrayToHeight"; + /** 抬起托盘 */ + public static final String RAISE_TRAY = "raiseTray"; + + /** 放下托盘 */ + public static final String LOWER_TRAY = "lowerTray"; + + /** 设置托盘电机参数 */ + public static final String SET_TRAY_PARAMS = "setTrayParams"; + + /** 调整拍子存放区高度 */ + public static final String SET_CAP_HEIGHT = "setCapHeight"; - /** 设置托盘抬起速度 */ - public static final String SET_TRAY_SPEED = "setTraySpeed"; + /** 调整拍子存放区电机参数 */ + public static final String SET_CAP_PARAMS = "setCapParams"; /** 拍照 */ public static final String TAKE_PHOTO = "takePhoto"; - /** 触发报警 */ - public static final String ALARM_TEST = "alarmTest"; } diff --git a/src/main/java/com/qyft/gd/device/service/DeviceService.java b/src/main/java/com/qyft/gd/device/service/DeviceService.java index 8d71f35..44119c4 100644 --- a/src/main/java/com/qyft/gd/device/service/DeviceService.java +++ b/src/main/java/com/qyft/gd/device/service/DeviceService.java @@ -2,6 +2,7 @@ package com.qyft.gd.device.service; import cn.hutool.json.JSONUtil; import com.qyft.gd.device.client.TcpClient; +import com.qyft.gd.device.common.constant.DeviceCommands; import com.qyft.gd.device.common.jsonrpc.JsonRpcRequest; import com.qyft.gd.device.model.bo.DeviceFeedback; import jakarta.annotation.PostConstruct; @@ -30,27 +31,26 @@ public class DeviceService { new Thread(this::executeTasks).start(); } - private boolean enqueueMoveRailTask(String method) { + private boolean putTask(String method) { JsonRpcRequest request = new JsonRpcRequest(); request.setMethod(method); - return this.enqueueMoveRailTask(request); + return this.putTask(request); } - private boolean enqueueMoveRailTask(String method, Map params) { + private boolean putTask(String method, Map params) { JsonRpcRequest request = new JsonRpcRequest(); request.setMethod(method); request.setParams(params); - return this.enqueueMoveRailTask(request); + return this.putTask(request); } - private boolean enqueueMoveRailTask(JsonRpcRequest request) { + private boolean putTask(JsonRpcRequest request) { Callable task = () -> { DeviceFeedback deviceFeedback = tcpClient.sendCommand(request); if (deviceFeedback == null || deviceFeedback.getError() != null) { log.error("TCP 指令执行错误 request:{} feedback:{}", JSONUtil.toJsonStr(request), JSONUtil.toJsonStr(deviceFeedback)); return false; } - Thread.sleep(5000); return true; }; try { @@ -74,7 +74,7 @@ public class DeviceService { } public boolean test() { - return this.enqueueMoveRailTask("moveRailArmRail", null); + return this.putTask("moveRailArmRail"); } /** @@ -85,7 +85,7 @@ public class DeviceService { public boolean moveRailArmRail(double distance) { Map params = new HashMap<>(); params.put("distance", distance); - return this.enqueueMoveRailTask("moveRailArmRail", params); + return this.putTask(DeviceCommands.MOVE_RAIL_ARM_RAIL, params); } /** @@ -100,7 +100,7 @@ public class DeviceService { params.put("joint1", joint1); params.put("joint2", joint2); params.put("distance", distance); - return this.enqueueMoveRailTask("moveRailArmJoint", params); + return this.putTask(DeviceCommands.MOVE_RAIL_ARM_JOINT, params); } /** @@ -115,7 +115,7 @@ public class DeviceService { params.put("x", x); params.put("y", y); params.put("z", z); - return this.enqueueMoveRailTask("moveRailArmToPoint", params); + return this.putTask(DeviceCommands.MOVE_RAIL_ARM_TO_POINT, params); } /** @@ -126,14 +126,14 @@ public class DeviceService { public boolean setRailArmSpeed(int speed) { Map params = new HashMap<>(); params.put("speed", speed); - return this.enqueueMoveRailTask("setRailArmSpeed", params); + return this.putTask(DeviceCommands.SET_RAIL_ARM_SPEED, params); } /** * 轨道机械臂移动停止移动 */ public boolean stopRailArm() { - return this.enqueueMoveRailTask("stopRailArm"); + return this.putTask(DeviceCommands.STOP_RAIL_ARM); } /** @@ -146,7 +146,7 @@ public class DeviceService { Map params = new HashMap<>(); params.put("joint1", joint1); params.put("joint2", joint2); - return this.enqueueMoveRailTask("moveLiquidArmJoint", params); + return this.putTask(DeviceCommands.MOVE_LIQUID_ARM_JOINT, params); } /** @@ -161,7 +161,7 @@ public class DeviceService { params.put("x", x); params.put("y", y); params.put("z", z); - return this.enqueueMoveRailTask("moveLiquidArmToPoint", params); + return this.putTask(DeviceCommands.MOVE_LIQUID_ARM_TO_POINT, params); } /** @@ -172,7 +172,7 @@ public class DeviceService { public synchronized boolean setLiquidArmSpeed(int speed) { Map params = new HashMap<>(); params.put("speed", speed); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("setLiquidArmSpeed", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand(DeviceCommands.SET_LIQUID_ARM_SPEED, params); if (deviceFeedback == null || deviceFeedback.getError() != null) { log.error("TCP setLiquidArmSpeed 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); return false; @@ -184,7 +184,7 @@ public class DeviceService { * 加液机械臂移动停止 */ public synchronized boolean stopLiquidArm() { - DeviceFeedback deviceFeedback = tcpClient.sendCommand("stopLiquidArm"); + DeviceFeedback deviceFeedback = tcpClient.sendCommand(DeviceCommands.STOP_LIQUID_ARM); if (deviceFeedback == null || deviceFeedback.getError() != null) { log.error("TCP stopLiquidArm 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); return false; @@ -196,7 +196,7 @@ public class DeviceService { * 张开夹爪 */ public synchronized boolean openClaw() { - DeviceFeedback deviceFeedback = tcpClient.sendCommand("openClaw"); + DeviceFeedback deviceFeedback = tcpClient.sendCommand(DeviceCommands.OPEN_CLAW); if (deviceFeedback == null || deviceFeedback.getError() != null) { log.error("TCP openClaw 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); return false; @@ -208,7 +208,7 @@ public class DeviceService { * 收合夹爪 */ public synchronized boolean closeClaw() { - DeviceFeedback deviceFeedback = tcpClient.sendCommand("closeClaw"); + DeviceFeedback deviceFeedback = tcpClient.sendCommand(DeviceCommands.CLOSE_CLAW); if (deviceFeedback == null || deviceFeedback.getError() != null) { log.error("TCP closeClaw 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); return false; @@ -228,7 +228,7 @@ public class DeviceService { params.put("mode", mode); params.put("speed", speed); params.put("targetPos", targetPos); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("setClawParams", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand(DeviceCommands.SET_CLAW_PARAMS, params); if (deviceFeedback == null || deviceFeedback.getError() != null) { log.error("TCP setClawParams 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); return false; @@ -246,7 +246,7 @@ public class DeviceService { Map params = new HashMap<>(); params.put("pumpId", pumpId); params.put("volume", volume); - return this.enqueueMoveRailTask("addLiquid", params); + return this.putTask(DeviceCommands.ADD_LIQUID, params); } /** @@ -259,7 +259,7 @@ public class DeviceService { Map params = new HashMap<>(); params.put("heaterId", heaterId); params.put("on", on); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("setSealLid", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand(DeviceCommands.SET_SEAL_LID, params); if (deviceFeedback == null || deviceFeedback.getError() != null) { log.error("TCP setSealLid 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); return false; @@ -272,14 +272,14 @@ public class DeviceService { * 开门 */ public synchronized boolean openDoor() { - return this.enqueueMoveRailTask("openDoor"); + return this.putTask(DeviceCommands.OPEN_DOOR); } /** * 关门 */ public synchronized boolean closeDoor() { - return this.enqueueMoveRailTask("openDoor"); + return this.putTask(DeviceCommands.CLOSE_DOOR); } /** @@ -292,7 +292,7 @@ public class DeviceService { Map params = new HashMap<>(); params.put("speed", speed); params.put("targetPos", targetPos); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("setDoorParams", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand(DeviceCommands.SET_DOOR_PARAMS, params); if (deviceFeedback == null || deviceFeedback.getError() != null) { log.error("TCP setDoorParams 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); return false; @@ -310,7 +310,7 @@ public class DeviceService { Map params = new HashMap<>(); params.put("pumpId", pumpId); params.put("flowRate", flowRate); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("setFlowRate", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand(DeviceCommands.SET_FLOW_RATE, params); if (deviceFeedback == null || deviceFeedback.getError() != null) { log.error("TCP setFlowRate 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); return false; @@ -322,7 +322,7 @@ public class DeviceService { * 开始摇匀 */ public synchronized boolean startShaking() { - DeviceFeedback deviceFeedback = tcpClient.sendCommand("startShaking"); + DeviceFeedback deviceFeedback = tcpClient.sendCommand(DeviceCommands.START_SHAKING); if (deviceFeedback == null || deviceFeedback.getError() != null) { log.error("TCP startShaking 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); return false; @@ -334,7 +334,7 @@ public class DeviceService { * 停止摇匀 */ public synchronized boolean stopShaking() { - DeviceFeedback deviceFeedback = tcpClient.sendCommand("stopShaking"); + DeviceFeedback deviceFeedback = tcpClient.sendCommand(DeviceCommands.STOP_SHAKING); if (deviceFeedback == null || deviceFeedback.getError() != null) { log.error("TCP stopShaking 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); return false; @@ -350,7 +350,7 @@ public class DeviceService { public synchronized boolean setShakingSpeed(int speed) { Map params = new HashMap<>(); params.put("speed", speed); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("setShakingSpeed", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand(DeviceCommands.SET_SHAKING_SPEED, params); if (deviceFeedback == null || deviceFeedback.getError() != null) { log.error("TCP setShakingSpeed 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); return false; @@ -366,7 +366,7 @@ public class DeviceService { public boolean raiseTray(String heaterId) { Map params = new HashMap<>(); params.put("heaterId", heaterId); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("raiseTray", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand(DeviceCommands.RAISE_TRAY, params); if (deviceFeedback == null || deviceFeedback.getError() != null) { log.error("TCP raiseTray 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); return false; @@ -382,7 +382,7 @@ public class DeviceService { public boolean lowerTray(String heaterId) { Map params = new HashMap<>(); params.put("heaterId", heaterId); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("lowerTray", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand(DeviceCommands.LOWER_TRAY, params); if (deviceFeedback == null || deviceFeedback.getError() != null) { log.error("TCP lowerTray 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); return false; @@ -402,7 +402,7 @@ public class DeviceService { params.put("heaterId", heaterId); params.put("speed", speed); params.put("targetPos", targetPos); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("setTrayParams", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand(DeviceCommands.SET_TRAY_PARAMS, params); if (deviceFeedback == null || deviceFeedback.getError() != null) { log.error("TCP setTrayParams 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); return false; @@ -418,7 +418,7 @@ public class DeviceService { public synchronized boolean setCapHeight(int level) { Map params = new HashMap<>(); params.put("level", level); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("setCapHeight", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand(DeviceCommands.SET_CAP_HEIGHT, params); if (deviceFeedback == null || deviceFeedback.getError() != null) { log.error("TCP setCapHeight 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); return false; @@ -436,7 +436,7 @@ public class DeviceService { Map params = new HashMap<>(); params.put("speed", speed); params.put("targetPos", targetPos); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("setCapParams", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand(DeviceCommands.SET_CAP_PARAMS, params); if (deviceFeedback == null || deviceFeedback.getError() != null) { log.error("TCP setCapParams 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); return false; @@ -454,7 +454,7 @@ public class DeviceService { Map params = new HashMap<>(); params.put("heaterId", heaterId); params.put("temperature", temperature); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("startHeating", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand(DeviceCommands.START_HEATING, params); if (deviceFeedback == null || deviceFeedback.getError() != null) { log.error("TCP startHeating 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); return false; @@ -470,7 +470,7 @@ public class DeviceService { public boolean stopHeating(String heaterId) { Map params = new HashMap<>(); params.put("heaterId", heaterId); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("stopHeating", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand(DeviceCommands.STOP_HEATING, params); if (deviceFeedback == null || deviceFeedback.getError() != null) { log.error("TCP stopHeating 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); return false; @@ -482,7 +482,7 @@ public class DeviceService { * 拍照 */ public synchronized boolean takePhoto() { - DeviceFeedback deviceFeedback = tcpClient.sendCommand("takePhoto"); + DeviceFeedback deviceFeedback = tcpClient.sendCommand(DeviceCommands.TAKE_PHOTO); if (deviceFeedback == null || deviceFeedback.getError() != null) { log.error("TCP takePhoto 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); return false;