|
@ -12,16 +12,15 @@ import io.netty.channel.*; |
|
|
import io.netty.channel.nio.NioEventLoopGroup; |
|
|
import io.netty.channel.nio.NioEventLoopGroup; |
|
|
import io.netty.channel.socket.nio.NioSocketChannel; |
|
|
import io.netty.channel.socket.nio.NioSocketChannel; |
|
|
import io.netty.util.CharsetUtil; |
|
|
import io.netty.util.CharsetUtil; |
|
|
|
|
|
import jakarta.annotation.PostConstruct; |
|
|
import lombok.RequiredArgsConstructor; |
|
|
import lombok.RequiredArgsConstructor; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.springframework.scheduling.annotation.Scheduled; |
|
|
import org.springframework.scheduling.annotation.Scheduled; |
|
|
import org.springframework.stereotype.Component; |
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
|
import java.net.InetSocketAddress; |
|
|
import java.net.InetSocketAddress; |
|
|
import java.util.Map; |
|
|
|
|
|
import java.util.UUID; |
|
|
import java.util.UUID; |
|
|
import java.util.concurrent.CompletableFuture; |
|
|
import java.util.concurrent.CompletableFuture; |
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
|
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
@Slf4j |
|
|
@Slf4j |
|
@ -30,16 +29,16 @@ import java.util.concurrent.TimeUnit; |
|
|
public class TcpClient { |
|
|
public class TcpClient { |
|
|
|
|
|
|
|
|
private final TcpConfig tcpConfig; |
|
|
private final TcpConfig tcpConfig; |
|
|
private DeviceMessageHandler deviceMessageHandler; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final DeviceMessageHandler deviceMessageHandler; |
|
|
|
|
|
|
|
|
private final EventLoopGroup group = new NioEventLoopGroup(); |
|
|
private final EventLoopGroup group = new NioEventLoopGroup(); |
|
|
private Channel channel; |
|
|
private Channel channel; |
|
|
private Bootstrap bootstrap; |
|
|
private Bootstrap bootstrap; |
|
|
|
|
|
|
|
|
private final Map<String, CompletableFuture<DeviceFeedback>> responseMap = new ConcurrentHashMap<>(); |
|
|
|
|
|
|
|
|
|
|
|
public void setDeviceMessageHandler(DeviceMessageHandler handler) { |
|
|
|
|
|
this.deviceMessageHandler = handler; |
|
|
|
|
|
|
|
|
@PostConstruct |
|
|
|
|
|
public void init() { |
|
|
if (tcpConfig.isEnable()) { |
|
|
if (tcpConfig.isEnable()) { |
|
|
connect(); |
|
|
connect(); |
|
|
} |
|
|
} |
|
@ -104,30 +103,22 @@ public class TcpClient { |
|
|
request.setId(UUID.randomUUID().toString()); |
|
|
request.setId(UUID.randomUUID().toString()); |
|
|
} |
|
|
} |
|
|
CompletableFuture<DeviceFeedback> future = new CompletableFuture<>(); |
|
|
CompletableFuture<DeviceFeedback> future = new CompletableFuture<>(); |
|
|
responseMap.put(request.getId(), future); |
|
|
|
|
|
|
|
|
deviceMessageHandler.responseMap.put(request.getId(), future); |
|
|
try { |
|
|
try { |
|
|
if(this.send(JSONUtil.toJsonStr(request))){ |
|
|
|
|
|
|
|
|
log.info("发送TCP指令 {}", JSONUtil.toJsonStr(request)); |
|
|
|
|
|
if (this.send(JSONUtil.toJsonStr(request))) { |
|
|
return future.get(tcpConfig.getFeedbackTimeout(), TimeUnit.MILLISECONDS); // 等待 FEEDBACK 响应 |
|
|
return future.get(tcpConfig.getFeedbackTimeout(), TimeUnit.MILLISECONDS); // 等待 FEEDBACK 响应 |
|
|
}else{ |
|
|
|
|
|
|
|
|
} else { |
|
|
return null; |
|
|
return null; |
|
|
} |
|
|
} |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
log.error("发送TCP指令错误 {}", JSONUtil.toJsonStr(request), e); |
|
|
log.error("发送TCP指令错误 {}", JSONUtil.toJsonStr(request), e); |
|
|
} finally { |
|
|
} finally { |
|
|
responseMap.remove(request.getId()); //确保完成后移除 |
|
|
|
|
|
|
|
|
deviceMessageHandler.responseMap.remove(request.getId()); //确保完成后移除 |
|
|
} |
|
|
} |
|
|
return null; |
|
|
return null; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void handleTcpResponse(DeviceFeedback deviceFeedback) { |
|
|
|
|
|
String requestId = deviceFeedback.getId(); |
|
|
|
|
|
CompletableFuture<DeviceFeedback> future = responseMap.remove(requestId); |
|
|
|
|
|
if (future != null) { |
|
|
|
|
|
future.complete(deviceFeedback); |
|
|
|
|
|
} else { |
|
|
|
|
|
log.error("未找到 requestId: {} 对应的等待请求", requestId); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private class TcpConnectionHandler extends ChannelInboundHandlerAdapter { |
|
|
private class TcpConnectionHandler extends ChannelInboundHandlerAdapter { |
|
|
|
|
|
|
|
|