Browse Source

feat:测试命令总线发送功能

tags/freeze
黄翔 3 months ago
parent
commit
63ef762de2
  1. 12
      src/main/java/com/iflytop/gd/infrastructure/drivers/WebSocketCommandBusImpl.java
  2. 10
      src/main/java/com/iflytop/gd/system/models/DataPacket.java
  3. 28
      src/test/java/com/iflytop/gd/infrastructure/drivers/WebSocketCommandBusImplTest.java

12
src/main/java/com/iflytop/gd/infrastructure/drivers/WebSocketCommandBusImpl.java

@ -64,6 +64,7 @@ public class WebSocketCommandBusImpl implements CommandBus {
throws CommandExecTimeoutException, HardwareErrorException, IOException, InterruptedException {
try {
if (!this.session.isOpen()) {
log.error("Session state={}", this.session.isOpen());
throw new IOException("Session is not open");
}
packetIndex = packetIndex + 1;
@ -73,7 +74,7 @@ public class WebSocketCommandBusImpl implements CommandBus {
commandPacket.setPacketIndex(packetIndex);
this.countDownLatch = new CountDownLatch(1);
this.session.getBasicRemote().sendText(commandPacket.toByteString());
boolean isTimeout = this.countDownLatch.await(timeout, unit);
boolean isTimeout = !this.countDownLatch.await(timeout, unit);
// 命令返回或者超时了
if (isTimeout) {
log.error("Command exec timeout, moduleId={}, commandId={}, timeoutInMilSeconds={}",
@ -99,19 +100,14 @@ public class WebSocketCommandBusImpl implements CommandBus {
@OnOpen
public void onOpen(Session session) {
if (this.session != null) {
try {
this.session.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
this.session = session;
log.info("WebSocket connection established");
}
@OnMessage
public void onMessage(String message) {
byte[] bytes = ByteArray.hexStringToBytes(message);
log.info("New packet arrived: {}", message);
this.lastDataPacket = new DataPacket(bytes);
if (this.lastDataPacket.getPacketType() == DataPacket.PACKET_TYPE_ACK ||
this.lastDataPacket.getPacketType() == DataPacket.PACKET_TYPE_ERROR_ACK) {

10
src/main/java/com/iflytop/gd/system/models/DataPacket.java

@ -34,7 +34,7 @@ public class DataPacket {
return ByteArray.toByteString(raw);
}
static public DataPacket createPacket(Integer moduleId, int packetType, Integer cmdId, Integer[] params) {
static private DataPacket createPacket(Integer moduleId, int packetType, Integer cmdId, Integer[] params) {
int bufferSize = DataPacket.PACKET_MIN_LEN + 4 * params.length;
ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
buffer.order(ByteOrder.LITTLE_ENDIAN);
@ -61,6 +61,14 @@ public class DataPacket {
return createPacket(moduleId, PACKET_TYPE_CMD, cmdId, params);
}
public static DataPacket createAckPacket(Integer moduleId, Integer cmdId, Integer... params) {
return createPacket(moduleId, PACKET_TYPE_ACK, cmdId, params);
}
public static DataPacket createErrorAckPacket(Integer moduleId, Integer cmdId, Integer... params) {
return createPacket(moduleId, PACKET_TYPE_ERROR_ACK, cmdId, params);
}
public int getPacketIndex() {
return ByteArray.readU16bit(raw, INDEX_OFFSET);
}

28
src/test/java/com/iflytop/gd/infrastructure/drivers/WebSocketCommandBusImplTest.java

@ -0,0 +1,28 @@
package com.iflytop.gd.infrastructure.drivers;
import com.iflytop.gd.system.drivers.CommandBus;
import com.iflytop.gd.system.exceptions.CommandExecTimeoutException;
import com.iflytop.gd.system.exceptions.HardwareErrorException;
import com.iflytop.gd.system.models.DataPacket;
import jakarta.websocket.DeploymentException;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.*;
class WebSocketCommandBusImplTest {
@Test
void waitForCommandExec() throws DeploymentException, IOException, HardwareErrorException, CommandExecTimeoutException, InterruptedException {
String webSocketUrl = "ws://localhost:8888";
WebSocketCommandBusImpl webSocketCommandBus = new WebSocketCommandBusImpl(webSocketUrl);
webSocketCommandBus.connectToCommandBusWebSocketServer();
DataPacket dataPacket = DataPacket.createCommandDataPacket(ModuleId.HBotXM.index, CmdId.step_motor_easy_move_to.index, 100);
webSocketCommandBus.waitForCommandExec(dataPacket, 30, TimeUnit.SECONDS);
}
}
Loading…
Cancel
Save