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); }