From b5aae24c0e58e37a9564139a307b61f3a6b628e5 Mon Sep 17 00:00:00 2001 From: huangxiang <155373492@qq.com> Date: Mon, 28 Apr 2025 19:02:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=8C=85=E7=B4=A2=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iflytop/gd/infrastructure/drivers/PacketFactory.java | 6 ------ .../gd/infrastructure/drivers/WebSocketCommandBusImpl.java | 8 +++++++- src/main/java/com/iflytop/gd/system/models/DataPacket.java | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 7 deletions(-) delete mode 100644 src/main/java/com/iflytop/gd/infrastructure/drivers/PacketFactory.java diff --git a/src/main/java/com/iflytop/gd/infrastructure/drivers/PacketFactory.java b/src/main/java/com/iflytop/gd/infrastructure/drivers/PacketFactory.java deleted file mode 100644 index a7374ed..0000000 --- a/src/main/java/com/iflytop/gd/infrastructure/drivers/PacketFactory.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.iflytop.gd.infrastructure.drivers; - -public class PacketFactory { - - -} diff --git a/src/main/java/com/iflytop/gd/infrastructure/drivers/WebSocketCommandBusImpl.java b/src/main/java/com/iflytop/gd/infrastructure/drivers/WebSocketCommandBusImpl.java index d893219..000101e 100644 --- a/src/main/java/com/iflytop/gd/infrastructure/drivers/WebSocketCommandBusImpl.java +++ b/src/main/java/com/iflytop/gd/infrastructure/drivers/WebSocketCommandBusImpl.java @@ -31,6 +31,7 @@ public class WebSocketCommandBusImpl implements CommandBus { private WebSocketClient webSocketClient; private CountDownLatch countDownLatch; private DataPacket lastDataPacket; + private Integer packetIndex = 0; public static final int PACKET_TYPE_CMD = 0xA0; public static final int PACKET_TYPE_ACK = 0xA1; @@ -48,6 +49,11 @@ public class WebSocketCommandBusImpl implements CommandBus { public synchronized DataPacket waitForCommandExec(DataPacket commandPacket, Integer timeout, TimeUnit unit) throws CommandExecTimeoutException, HardwareErrorException, IOException, InterruptedException { try { + packetIndex = packetIndex + 1; + if (packetIndex > 30000) { + packetIndex = 1; + } + commandPacket.setPacketIndex(packetIndex); this.countDownLatch = new CountDownLatch(1); this.session.getBasicRemote().sendText(commandPacket.toByteString()); boolean isTimeout = this.countDownLatch.await(timeout, unit); @@ -57,7 +63,7 @@ public class WebSocketCommandBusImpl implements CommandBus { commandPacket.getModuleId(), commandPacket.getCmdId(), unit.toMillis(timeout)); throw new CommandExecTimeoutException(); } - + log.debug("收到数据包{}", this.lastDataPacket); // 在指定的时间内得到了响应 if (this.lastDataPacket.getPacketType() == PACKET_TYPE_ERROR_ACK) { log.error("moduleId={}执行command={}发送硬件错误", this.lastDataPacket.getModuleId(), this.lastDataPacket.getCmdId()); diff --git a/src/main/java/com/iflytop/gd/system/models/DataPacket.java b/src/main/java/com/iflytop/gd/system/models/DataPacket.java index e72ac3d..5997a59 100644 --- a/src/main/java/com/iflytop/gd/system/models/DataPacket.java +++ b/src/main/java/com/iflytop/gd/system/models/DataPacket.java @@ -65,6 +65,20 @@ public class DataPacket { return ByteArray.readU16bit(raw, INDEX_OFFSET); } + public void setPacketIndex(int packetIndex) { + ByteArray.setU16bit(raw, INDEX_OFFSET, packetIndex); + int checkSum = computeCheckSum(); + ByteArray.setU8(raw, raw.length - 1, checkSum); + } + + public int computeCheckSum() { + int checkcode = 0; + for (int i = 0; i < raw.length - 1; i++) { + checkcode += raw[i]; + } + return checkcode & 0xFF; + } + public int getCmdId() { return ByteArray.readU16bit(raw, CMDID_OFFSET); }