From fb17da42a8d4a2c0ae4a40be683b2c25784e2130 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E5=87=A4=E5=90=89?= Date: Tue, 18 Feb 2025 11:13:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E8=B0=83=E7=94=A8=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E4=BF=AE=E6=94=B9=E4=B8=BA=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/qyft/gd/device/client/TcpClient.java | 39 ++++- .../gd/device/handler/DeviceMessageHandler.java | 13 +- .../com/qyft/gd/device/service/DeviceService.java | 169 ++++----------------- 3 files changed, 72 insertions(+), 149 deletions(-) diff --git a/src/main/java/com/qyft/gd/device/client/TcpClient.java b/src/main/java/com/qyft/gd/device/client/TcpClient.java index 44bc4ed..7e5a618 100644 --- a/src/main/java/com/qyft/gd/device/client/TcpClient.java +++ b/src/main/java/com/qyft/gd/device/client/TcpClient.java @@ -98,14 +98,13 @@ public class TcpClient { } } - - public DeviceFeedback sendCommand(String method) { + public boolean sendCommand(String method) { JsonRpcRequest request = new JsonRpcRequest(); request.setMethod(method); return this.sendCommand(request); } - public DeviceFeedback sendCommand(String method, Map params) { + public boolean sendCommand(String method, Map params) { JsonRpcRequest request = new JsonRpcRequest(); request.setMethod(method); request.setParams(params); @@ -113,21 +112,49 @@ public class TcpClient { } - public DeviceFeedback sendCommand(JsonRpcRequest request) { + public boolean sendCommand(JsonRpcRequest request) { + if (request.getId() == null) { + request.setId(UUID.randomUUID().toString()); + } + try { + log.info("发送TCP指令 {}", JSONUtil.toJsonStr(request)); + this.send(JSONUtil.toJsonStr(request)); + return true; + } catch (Exception e) { + log.error("发送TCP指令错误 {}", JSONUtil.toJsonStr(request), e); + } + return false; + } + + public DeviceFeedback sendCommandSync(String method) { + JsonRpcRequest request = new JsonRpcRequest(); + request.setMethod(method); + return this.sendCommandSync(request); + } + + public DeviceFeedback sendCommandSync(String method, Map params) { + JsonRpcRequest request = new JsonRpcRequest(); + request.setMethod(method); + request.setParams(params); + return this.sendCommandSync(request); + } + + + public DeviceFeedback sendCommandSync(JsonRpcRequest request) { if (request.getId() == null) { request.setId(UUID.randomUUID().toString()); } CompletableFuture future = new CompletableFuture<>(); deviceMessageHandler.responseMap.put(request.getId(), future); try { - log.info("发送TCP指令 {}", JSONUtil.toJsonStr(request)); + log.info("发送TCP指令(同步) {}", JSONUtil.toJsonStr(request)); if (this.send(JSONUtil.toJsonStr(request))) { return future.get(tcpConfig.getFeedbackTimeout(), TimeUnit.MILLISECONDS); // 等待 FEEDBACK 响应 } else { return null; } } catch (Exception e) { - log.error("发送TCP指令错误 {}", JSONUtil.toJsonStr(request), e); + log.error("发送TCP指令错误(同步) {}", JSONUtil.toJsonStr(request), e); } finally { deviceMessageHandler.responseMap.remove(request.getId()); //确保完成后移除 } diff --git a/src/main/java/com/qyft/gd/device/handler/DeviceMessageHandler.java b/src/main/java/com/qyft/gd/device/handler/DeviceMessageHandler.java index 1b39ec9..69fcdd7 100644 --- a/src/main/java/com/qyft/gd/device/handler/DeviceMessageHandler.java +++ b/src/main/java/com/qyft/gd/device/handler/DeviceMessageHandler.java @@ -1,12 +1,14 @@ package com.qyft.gd.device.handler; import cn.hutool.json.JSONUtil; +import com.qyft.gd.config.WebSocketServer; import com.qyft.gd.device.common.constant.TcpMessageType; import com.qyft.gd.device.common.jsonrpc.JsonRpcResponse; import com.qyft.gd.device.model.bo.DeviceAlarm; import com.qyft.gd.device.model.bo.DeviceFeedback; import com.qyft.gd.device.model.bo.DeviceStatus; import com.qyft.gd.device.service.DeviceStateService; +import com.qyft.gd.model.vo.WebsocketResult; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; @@ -45,20 +47,23 @@ public class DeviceMessageHandler extends ChannelInboundHandlerAdapter { } else if (TcpMessageType.FEEDBACK.equals(jsonRpcResponse.getType())) {//设备指令反馈 DeviceFeedback deviceFeedback = JSONUtil.toBean(jsonRpcResponse.getData(), DeviceFeedback.class); - this.handleTcpResponse(deviceFeedback); + if(!this.handleTcpResponse(deviceFeedback)){ + //TODO 前端任务回调在这里 + } + } } catch (Exception e) { log.error("TCP服务消息处理错误: {}, error: {}", serverMsg, e.getMessage(), e); } } - private void handleTcpResponse(DeviceFeedback deviceFeedback) { + private boolean handleTcpResponse(DeviceFeedback deviceFeedback) { String requestId = deviceFeedback.getId(); CompletableFuture future = responseMap.remove(requestId); if (future != null) { future.complete(deviceFeedback); - } else { - log.error("未找到 requestId: {} 对应的等待请求", requestId); + return true; } + return false; } } \ No newline at end of file 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 29eefbd..083eaee 100644 --- a/src/main/java/com/qyft/gd/device/service/DeviceService.java +++ b/src/main/java/com/qyft/gd/device/service/DeviceService.java @@ -22,15 +22,10 @@ public class DeviceService { * * @param distance 移动距离 */ - public synchronized boolean moveRailArmRail(double distance) { + public boolean moveRailArmRail(double distance) { Map params = new HashMap<>(); params.put("distance", distance); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveRailArmRail", params); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP moveRailArmRail 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("moveRailArmRail", params); } /** @@ -40,17 +35,12 @@ public class DeviceService { * @param joint2 关节2角度 * @param distance 移动距离 */ - public synchronized boolean moveRailArmJoint(double joint1, double joint2, double distance) { + public boolean moveRailArmJoint(double joint1, double joint2, double distance) { Map params = new HashMap<>(); params.put("joint1", joint1); params.put("joint2", joint2); params.put("distance", distance); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveRailArmJoint", params); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP moveRailArmJoint 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("moveRailArmJoint", params); } /** @@ -60,17 +50,12 @@ public class DeviceService { * @param y 坐标y * @param z 坐标z */ - public synchronized boolean moveRailArmToPoint(int x, int y, int z) { + public boolean moveRailArmToPoint(int x, int y, int z) { Map params = new HashMap<>(); params.put("x", x); params.put("y", y); params.put("z", z); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveRailArmToPoint", params); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP moveRailArmToPoint 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("moveRailArmToPoint", params); } /** @@ -81,60 +66,35 @@ public class DeviceService { public boolean setRailArmSpeed(int speed) { Map params = new HashMap<>(); params.put("speed", speed); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("setRailArmSpeed", params); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP setRailArmSpeed 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("setRailArmSpeed", params); } /** * 开门 */ public boolean openDoor() { - DeviceFeedback deviceFeedback = tcpClient.sendCommand("openDoor"); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP openDoor 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("openDoor"); } /** * 关门 */ public boolean closeDoor() { - DeviceFeedback deviceFeedback = tcpClient.sendCommand("closeDoor"); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP closeDoor 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("closeDoor"); } /** * 张开夹爪 */ public boolean openClaw() { - DeviceFeedback deviceFeedback = tcpClient.sendCommand("openClaw"); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP openClaw 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("openClaw"); } /** * 收合夹爪 */ public boolean closeClaw() { - DeviceFeedback deviceFeedback = tcpClient.sendCommand("closeClaw"); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP closeClaw 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("closeClaw"); } /** @@ -143,16 +103,11 @@ public class DeviceService { * @param joint1 关节1角度 * @param joint2 关节2角度 */ - public synchronized boolean moveLiquidArmJoint(double joint1, double joint2) { + public boolean moveLiquidArmJoint(double joint1, double joint2) { Map params = new HashMap<>(); params.put("joint1", joint1); params.put("joint2", joint2); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveLiquidArmJoint", params); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP moveLiquidArmJoint 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("moveLiquidArmJoint", params); } /** @@ -162,17 +117,12 @@ public class DeviceService { * @param y 坐标y * @param z 坐标z */ - public synchronized boolean moveLiquidArmToPoint(int x, int y, int z) { + public boolean moveLiquidArmToPoint(int x, int y, int z) { Map params = new HashMap<>(); params.put("x", x); params.put("y", y); params.put("z", z); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveLiquidArmToPoint", params); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP moveLiquidArmToPoint 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("moveLiquidArmToPoint", params); } /** @@ -183,12 +133,7 @@ public class DeviceService { public boolean setLiquidArmSpeed(int speed) { Map params = new HashMap<>(); params.put("speed", speed); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("setLiquidArmSpeed", params); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP setLiquidArmSpeed 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("setLiquidArmSpeed", params); } /** @@ -197,16 +142,11 @@ public class DeviceService { * @param pumpId 泵id * @param volume 液体体积 */ - public synchronized boolean addLiquid(String pumpId, int volume) { + public boolean addLiquid(String pumpId, int volume) { Map params = new HashMap<>(); params.put("pumpId", pumpId); params.put("volume", volume); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("addLiquid", params); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP addLiquid 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("addLiquid", params); } @@ -220,36 +160,21 @@ public class DeviceService { Map params = new HashMap<>(); params.put("pumpId", pumpId); params.put("flowRate", flowRate); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("setFlowRate", params); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP setFlowRate 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("setFlowRate", params); } /** * 开始摇匀 */ public boolean startShaking() { - DeviceFeedback deviceFeedback = tcpClient.sendCommand("startShaking"); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP startShaking 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("startShaking"); } /** * 停止摇匀 */ public boolean stopShaking() { - DeviceFeedback deviceFeedback = tcpClient.sendCommand("stopShaking"); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP stopShaking 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("stopShaking"); } /** @@ -260,12 +185,7 @@ public class DeviceService { public boolean setShakingSpeed(int speed) { Map params = new HashMap<>(); params.put("speed", speed); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("setShakingSpeed", params); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP setShakingSpeed 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("setShakingSpeed", params); } /** @@ -278,27 +198,18 @@ public class DeviceService { Map params = new HashMap<>(); params.put("heaterId", heaterId); params.put("temperature", temperature); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("startHeating", params); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP startHeating 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("startHeating", params); } /** * 停止加热 - * @param heaterId 加热器id + * + * @param heaterId 加热器id */ public boolean stopHeating(String heaterId) { Map params = new HashMap<>(); params.put("heaterId", heaterId); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("stopHeating", params); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP stopHeating 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("stopHeating", params); } @@ -310,12 +221,7 @@ public class DeviceService { public boolean moveTrayToHeight(double distance) { Map params = new HashMap<>(); params.put("distance", distance); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveTrayToHeight", params); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP moveTrayToHeight 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("moveTrayToHeight", params); } /** @@ -326,24 +232,14 @@ public class DeviceService { public boolean setTraySpeed(int speed) { Map params = new HashMap<>(); params.put("speed", speed); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("setTraySpeed", params); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP setTraySpeed 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("setTraySpeed", params); } /** * 拍照 */ public boolean takePhoto() { - DeviceFeedback deviceFeedback = tcpClient.sendCommand("takePhoto"); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP takePhoto 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("takePhoto"); } /** @@ -354,11 +250,6 @@ public class DeviceService { params.put("code", code); params.put("msg", msg); params.put("module", module); - DeviceFeedback deviceFeedback = tcpClient.sendCommand("alarmTest", params); - if (deviceFeedback == null || deviceFeedback.getError() != null) { - log.error("TCP alarmTest 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); - return false; - } - return true; + return tcpClient.sendCommand("alarmTest", params); } }