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 6fe1a2e..d893219 100644 --- a/src/main/java/com/iflytop/gd/infrastructure/drivers/WebSocketCommandBusImpl.java +++ b/src/main/java/com/iflytop/gd/infrastructure/drivers/WebSocketCommandBusImpl.java @@ -9,16 +9,20 @@ import com.iflytop.gd.system.models.DataPacket; import com.iflytop.gd.system.utils.ByteArray; import jakarta.websocket.*; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.socket.client.WebSocketClient; import java.io.IOException; +import java.net.URI; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.locks.ReentrantLock; - +/** + * 使用WebSocket实现命令总线功能 + */ @Slf4j @Component @ClientEndpoint @@ -28,12 +32,18 @@ public class WebSocketCommandBusImpl implements CommandBus { private CountDownLatch countDownLatch; private DataPacket lastDataPacket; - - public static final int PACKET_TYPE_CMD = 0xA0; public static final int PACKET_TYPE_ACK = 0xA1; public static final int PACKET_TYPE_ERROR_ACK = 0xA2; public static final int PACKET_TYPE_EVENT = 0xA3; + + //TODO 配置硬件服务段ws链接 + public WebSocketCommandBusImpl(@Value("${}") String websocketServerUrl) throws DeploymentException, IOException { + WebSocketContainer container = ContainerProvider.getWebSocketContainer(); + URI endpointURI = URI.create(websocketServerUrl); + container.connectToServer(this, endpointURI); + } + @Override public synchronized DataPacket waitForCommandExec(DataPacket commandPacket, Integer timeout, TimeUnit unit) throws CommandExecTimeoutException, HardwareErrorException, IOException, InterruptedException { @@ -43,9 +53,12 @@ public class WebSocketCommandBusImpl implements CommandBus { boolean isTimeout = this.countDownLatch.await(timeout, unit); // 命令返回或者超时了 if (isTimeout) { - // TODO处理超时 + log.error("Command exec timeout, moduleId={}, commandId={}, timeoutInMilSeconds={}", + commandPacket.getModuleId(), commandPacket.getCmdId(), unit.toMillis(timeout)); throw new CommandExecTimeoutException(); } + + // 在指定的时间内得到了响应 if (this.lastDataPacket.getPacketType() == PACKET_TYPE_ERROR_ACK) { log.error("moduleId={}执行command={}发送硬件错误", this.lastDataPacket.getModuleId(), this.lastDataPacket.getCmdId()); throw new HardwareErrorException(); @@ -55,8 +68,7 @@ public class WebSocketCommandBusImpl implements CommandBus { log.error("发送指令发生异常", e); throw e; } catch (InterruptedException e) { - Thread.currentThread().getName(); - log.error(""); + log.error("Thread: {}被中断", Thread.currentThread().getName()); throw e; } } @@ -78,7 +90,10 @@ public class WebSocketCommandBusImpl implements CommandBus { public void onMessage(String message) { byte[] bytes = ByteArray.hexStringToBytes(message); this.lastDataPacket = new DataPacket(bytes); - this.countDownLatch.countDown(); + if (this.lastDataPacket.getPacketType() == DataPacket.PACKET_TYPE_ACK || + this.lastDataPacket.getPacketType() == DataPacket.PACKET_TYPE_ERROR_ACK) { + this.countDownLatch.countDown(); + } } diff --git a/src/main/java/com/iflytop/gd/system/drivers/CommandBus.java b/src/main/java/com/iflytop/gd/system/drivers/CommandBus.java index 98960ee..19944e9 100644 --- a/src/main/java/com/iflytop/gd/system/drivers/CommandBus.java +++ b/src/main/java/com/iflytop/gd/system/drivers/CommandBus.java @@ -18,5 +18,6 @@ public interface CommandBus { * @return 响应数据包 * @throws Exception */ - DataPacket waitForCommandExec(DataPacket commandPacket, Integer timeout, TimeUnit unit) throws CommandExecTimeoutException, HardwareErrorException, IOException, InterruptedException; + DataPacket waitForCommandExec(DataPacket commandPacket, Integer timeout, TimeUnit unit) + throws CommandExecTimeoutException, HardwareErrorException, IOException, InterruptedException; }