Pu Zhibing
2025-03-14 3c66b754ee314ae87d0f2eda2fa86a30ea2304e7
ruoyi-service/ruoyi-dataInterchange/src/main/java/com/ruoyi/dataInterchange/netty/client/NettyClient.java
@@ -1,11 +1,16 @@
package com.ruoyi.dataInterchange.netty.client;
import com.alibaba.fastjson.JSON;
import com.ruoyi.dataInterchange.model.DOWNConnectReq;
import com.ruoyi.dataInterchange.model.enu.DataType;
import com.ruoyi.dataInterchange.util.jtt809.common.ByteArrayUtil;
import com.ruoyi.dataInterchange.util.jtt809.common.Jtt809Constant;
import com.ruoyi.dataInterchange.util.jtt809.common.Jtt809Util;
import com.ruoyi.dataInterchange.util.jtt809.decoder.Jtt809Decoder;
import com.ruoyi.dataInterchange.util.jtt809.encoder.Jtt809Encoder;
import com.ruoyi.dataInterchange.util.jtt809.gnsscenter.GnssCenterService;
import com.ruoyi.dataInterchange.util.jtt809.packet.common.OuterPacket;
import com.ruoyi.dataInterchange.wapper.UPConnect;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
@@ -14,6 +19,7 @@
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;
import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;
@@ -22,6 +28,7 @@
 * @author zhibing.pu
 * @Date 2025/3/3 20:11
 */
@Slf4j
public class NettyClient {
   /**
    * 连接IP
@@ -44,7 +51,7 @@
    *
    * @throws Exception
    */
   public void start(int code) throws Exception {
   public void start(int inferiorPlatformId, int verifyCode) throws Exception {
      EventLoopGroup nioEventLoopGroup = null;
      try {
         //创建Bootstrap对象用来引导启动客户端
@@ -66,14 +73,32 @@
                     //  空闲检测处理器 触发空闲状态事件            读空闲:5秒      写空闲:7秒  读写空闲:10秒
                     pipeline.addLast(new IdleStateHandler(5, 7, 3, TimeUnit.SECONDS));
                     // 处理器
//                     pipeline.addLast("handler", new NettyHandle());
                     pipeline.addLast("handler", new NettyClientHandler());
                  }
               });
         // • 调用Bootstrap.connect()来连接服务器
         ChannelFuture f = bootstrap.connect().sync();
         //将通道添加到缓存中,便于后期直接使用
         Channel channel = f.channel();
         ChannelMap.addClientChannel(code, channel);
         ChannelMap.addClientChannel(inferiorPlatformId, channel);
         log.info("从链路连接成功");
         //构建从链路请求
         DOWNConnectReq downConnectReq = new DOWNConnectReq();
         downConnectReq.setVerifyCode(verifyCode);
         log.info("从链路连接请求({}):{}", DataType.DOWN_CONNECT_REQ.getCode(), JSON.toJSONString(downConnectReq));
         byte[] body = downConnectReq.encode();
         OuterPacket out = new OuterPacket(DataType.DOWN_CONNECT_REQ.getCode(), body);
         channel.writeAndFlush(out);
         channel.flush();
         //缓存从链路地址
         UPConnect upConnect = new UPConnect();
         upConnect.setDownLinkIp(host);
         upConnect.setDownLinkPort(port);
         upConnect.setVerifyCode(verifyCode);
         ChannelMap.addIpAndPort(inferiorPlatformId, upConnect);
         // • 最后关闭EventLoopGroup来释放资源
         f.channel().closeFuture().sync();
      } finally {