From 14c61f5cc890101c22aac39ed8afbe0b58d2f150 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:47:37 +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=90=8C=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 | 36 +---- .../gd/device/handler/DeviceMessageHandler.java | 13 +- .../com/qyft/gd/device/service/DeviceService.java | 169 +++++++++++++++++---- 3 files changed, 146 insertions(+), 72 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 7e5a618..faedc4c 100644 --- a/src/main/java/com/qyft/gd/device/client/TcpClient.java +++ b/src/main/java/com/qyft/gd/device/client/TcpClient.java @@ -98,49 +98,20 @@ public class TcpClient { } } - public boolean sendCommand(String method) { + public DeviceFeedback sendCommand(String method) { JsonRpcRequest request = new JsonRpcRequest(); request.setMethod(method); return this.sendCommand(request); } - public boolean sendCommand(String method, Map params) { + public DeviceFeedback sendCommand(String method, Map params) { JsonRpcRequest request = new JsonRpcRequest(); request.setMethod(method); request.setParams(params); return this.sendCommand(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) { + public DeviceFeedback sendCommand(JsonRpcRequest request) { if (request.getId() == null) { request.setId(UUID.randomUUID().toString()); } @@ -161,7 +132,6 @@ public class TcpClient { return null; } - private class TcpConnectionHandler extends ChannelInboundHandlerAdapter { @Override 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 69fcdd7..1b39ec9 100644 --- a/src/main/java/com/qyft/gd/device/handler/DeviceMessageHandler.java +++ b/src/main/java/com/qyft/gd/device/handler/DeviceMessageHandler.java @@ -1,14 +1,12 @@ 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; @@ -47,23 +45,20 @@ public class DeviceMessageHandler extends ChannelInboundHandlerAdapter { } else if (TcpMessageType.FEEDBACK.equals(jsonRpcResponse.getType())) {//设备指令反馈 DeviceFeedback deviceFeedback = JSONUtil.toBean(jsonRpcResponse.getData(), DeviceFeedback.class); - if(!this.handleTcpResponse(deviceFeedback)){ - //TODO 前端任务回调在这里 - } - + this.handleTcpResponse(deviceFeedback); } } catch (Exception e) { log.error("TCP服务消息处理错误: {}, error: {}", serverMsg, e.getMessage(), e); } } - private boolean handleTcpResponse(DeviceFeedback deviceFeedback) { + private void handleTcpResponse(DeviceFeedback deviceFeedback) { String requestId = deviceFeedback.getId(); CompletableFuture future = responseMap.remove(requestId); if (future != null) { future.complete(deviceFeedback); - return true; + } else { + log.error("未找到 requestId: {} 对应的等待请求", requestId); } - 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 083eaee..29eefbd 100644 --- a/src/main/java/com/qyft/gd/device/service/DeviceService.java +++ b/src/main/java/com/qyft/gd/device/service/DeviceService.java @@ -22,10 +22,15 @@ public class DeviceService { * * @param distance 移动距离 */ - public boolean moveRailArmRail(double distance) { + public synchronized boolean moveRailArmRail(double distance) { Map params = new HashMap<>(); params.put("distance", distance); - return tcpClient.sendCommand("moveRailArmRail", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveRailArmRail", params); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP moveRailArmRail 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } /** @@ -35,12 +40,17 @@ public class DeviceService { * @param joint2 关节2角度 * @param distance 移动距离 */ - public boolean moveRailArmJoint(double joint1, double joint2, double distance) { + public synchronized boolean moveRailArmJoint(double joint1, double joint2, double distance) { Map params = new HashMap<>(); params.put("joint1", joint1); params.put("joint2", joint2); params.put("distance", distance); - return tcpClient.sendCommand("moveRailArmJoint", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveRailArmJoint", params); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP moveRailArmJoint 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } /** @@ -50,12 +60,17 @@ public class DeviceService { * @param y 坐标y * @param z 坐标z */ - public boolean moveRailArmToPoint(int x, int y, int z) { + public synchronized boolean moveRailArmToPoint(int x, int y, int z) { Map params = new HashMap<>(); params.put("x", x); params.put("y", y); params.put("z", z); - return tcpClient.sendCommand("moveRailArmToPoint", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveRailArmToPoint", params); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP moveRailArmToPoint 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } /** @@ -66,35 +81,60 @@ public class DeviceService { public boolean setRailArmSpeed(int speed) { Map params = new HashMap<>(); params.put("speed", speed); - return tcpClient.sendCommand("setRailArmSpeed", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("setRailArmSpeed", params); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP setRailArmSpeed 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } /** * 开门 */ public boolean openDoor() { - return tcpClient.sendCommand("openDoor"); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("openDoor"); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP openDoor 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } /** * 关门 */ public boolean closeDoor() { - return tcpClient.sendCommand("closeDoor"); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("closeDoor"); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP closeDoor 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } /** * 张开夹爪 */ public boolean openClaw() { - return tcpClient.sendCommand("openClaw"); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("openClaw"); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP openClaw 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } /** * 收合夹爪 */ public boolean closeClaw() { - return tcpClient.sendCommand("closeClaw"); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("closeClaw"); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP closeClaw 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } /** @@ -103,11 +143,16 @@ public class DeviceService { * @param joint1 关节1角度 * @param joint2 关节2角度 */ - public boolean moveLiquidArmJoint(double joint1, double joint2) { + public synchronized boolean moveLiquidArmJoint(double joint1, double joint2) { Map params = new HashMap<>(); params.put("joint1", joint1); params.put("joint2", joint2); - return tcpClient.sendCommand("moveLiquidArmJoint", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveLiquidArmJoint", params); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP moveLiquidArmJoint 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } /** @@ -117,12 +162,17 @@ public class DeviceService { * @param y 坐标y * @param z 坐标z */ - public boolean moveLiquidArmToPoint(int x, int y, int z) { + public synchronized boolean moveLiquidArmToPoint(int x, int y, int z) { Map params = new HashMap<>(); params.put("x", x); params.put("y", y); params.put("z", z); - return tcpClient.sendCommand("moveLiquidArmToPoint", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveLiquidArmToPoint", params); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP moveLiquidArmToPoint 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } /** @@ -133,7 +183,12 @@ public class DeviceService { public boolean setLiquidArmSpeed(int speed) { Map params = new HashMap<>(); params.put("speed", speed); - return tcpClient.sendCommand("setLiquidArmSpeed", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("setLiquidArmSpeed", params); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP setLiquidArmSpeed 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } /** @@ -142,11 +197,16 @@ public class DeviceService { * @param pumpId 泵id * @param volume 液体体积 */ - public boolean addLiquid(String pumpId, int volume) { + public synchronized boolean addLiquid(String pumpId, int volume) { Map params = new HashMap<>(); params.put("pumpId", pumpId); params.put("volume", volume); - return tcpClient.sendCommand("addLiquid", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("addLiquid", params); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP addLiquid 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } @@ -160,21 +220,36 @@ public class DeviceService { Map params = new HashMap<>(); params.put("pumpId", pumpId); params.put("flowRate", flowRate); - return tcpClient.sendCommand("setFlowRate", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("setFlowRate", params); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP setFlowRate 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } /** * 开始摇匀 */ public boolean startShaking() { - return tcpClient.sendCommand("startShaking"); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("startShaking"); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP startShaking 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } /** * 停止摇匀 */ public boolean stopShaking() { - return tcpClient.sendCommand("stopShaking"); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("stopShaking"); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP stopShaking 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } /** @@ -185,7 +260,12 @@ public class DeviceService { public boolean setShakingSpeed(int speed) { Map params = new HashMap<>(); params.put("speed", speed); - return tcpClient.sendCommand("setShakingSpeed", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("setShakingSpeed", params); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP setShakingSpeed 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } /** @@ -198,18 +278,27 @@ public class DeviceService { Map params = new HashMap<>(); params.put("heaterId", heaterId); params.put("temperature", temperature); - return tcpClient.sendCommand("startHeating", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("startHeating", params); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP startHeating 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } /** * 停止加热 - * - * @param heaterId 加热器id + * @param heaterId 加热器id */ public boolean stopHeating(String heaterId) { Map params = new HashMap<>(); params.put("heaterId", heaterId); - return tcpClient.sendCommand("stopHeating", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("stopHeating", params); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP stopHeating 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } @@ -221,7 +310,12 @@ public class DeviceService { public boolean moveTrayToHeight(double distance) { Map params = new HashMap<>(); params.put("distance", distance); - return tcpClient.sendCommand("moveTrayToHeight", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("moveTrayToHeight", params); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP moveTrayToHeight 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } /** @@ -232,14 +326,24 @@ public class DeviceService { public boolean setTraySpeed(int speed) { Map params = new HashMap<>(); params.put("speed", speed); - return tcpClient.sendCommand("setTraySpeed", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("setTraySpeed", params); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP setTraySpeed 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } /** * 拍照 */ public boolean takePhoto() { - return tcpClient.sendCommand("takePhoto"); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("takePhoto"); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP takePhoto 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } /** @@ -250,6 +354,11 @@ public class DeviceService { params.put("code", code); params.put("msg", msg); params.put("module", module); - return tcpClient.sendCommand("alarmTest", params); + DeviceFeedback deviceFeedback = tcpClient.sendCommand("alarmTest", params); + if (deviceFeedback == null || deviceFeedback.getError() != null) { + log.error("TCP alarmTest 指令执行错误 {}", JSONUtil.toJsonStr(deviceFeedback)); + return false; + } + return true; } }