From 3be4440f6800e10efd8db51b957d17a6cc3b39df Mon Sep 17 00:00:00 2001
From: ‘liusuyi’ <1951119284@qq.com>
Date: 星期二, 27 六月 2023 16:17:07 +0800
Subject: [PATCH] 增加雷达报警点坐标计算 增加雷达报警数据推送mqtt

---
 src/main/java/com/ard/utils/tcp/NettyTcpClient.java |   65 +++++++++++++++++++-------------
 1 files changed, 39 insertions(+), 26 deletions(-)

diff --git a/src/main/java/com/ard/utils/tcp/NettyTcpClient.java b/src/main/java/com/ard/utils/tcp/NettyTcpClient.java
index 2d6c324..2eab8e2 100644
--- a/src/main/java/com/ard/utils/tcp/NettyTcpClient.java
+++ b/src/main/java/com/ard/utils/tcp/NettyTcpClient.java
@@ -1,5 +1,6 @@
 package com.ard.utils.tcp;
 
+import com.ard.alarm.radar.domain.ArdEquipRadar;
 import io.netty.bootstrap.Bootstrap;
 import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelFutureListener;
@@ -11,6 +12,8 @@
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.scheduling.annotation.EnableAsync;
 import org.springframework.stereotype.Component;
+
+import java.util.concurrent.TimeUnit;
 
 /**
  * @Description:
@@ -24,33 +27,43 @@
 @Slf4j(topic = "radar")
 public class NettyTcpClient {
     @Async
-    public void init(String host, Integer port) {
-        NioEventLoopGroup group = new NioEventLoopGroup();
-        try {
-            Bootstrap bootstrap = new Bootstrap();
-            bootstrap.group(group)
-                    .channel(NioSocketChannel.class)
-                    .handler(new ChannelInitializer<SocketChannel>() {
-                        @Override
-                        protected void initChannel(SocketChannel ch) throws Exception {
-                            ch.pipeline().addLast(new NettyTcpClientHandler());
-                        }
-                    });
-            ChannelFuture future = bootstrap.connect(host, port).sync();
-            // 娣诲姞杩炴帴鎴愬姛鐨勭洃鍚櫒
-            future.addListener((ChannelFutureListener) future1 -> {
-                if (future1.isSuccess()) {
-                    log.info("tcp杩炴帴鎴愬姛"+host+":"+port);
-                } else {
-                    log.info("tcp杩炴帴澶辫触"+host+":"+port);
-                }
-            });
-            future.channel().closeFuture().sync();
+    public void init(ArdEquipRadar ardEquipRadar) {
 
-        } catch (Exception ex) {
-            log.error("nettyTcp瀹㈡埛绔垵濮嬪寲寮傚父:" + ex.getMessage());
-        } finally {
-            group.shutdownGracefully();
+        while (true) {
+            NioEventLoopGroup group = new NioEventLoopGroup();
+            try {
+                Bootstrap bootstrap = new Bootstrap();
+                bootstrap.group(group)
+                        .channel(NioSocketChannel.class)
+                        .handler(new ChannelInitializer<SocketChannel>() {
+                            @Override
+                            protected void initChannel(SocketChannel ch) throws Exception {
+                                ch.pipeline().addLast(new NettyTcpClientHandler(ardEquipRadar));
+                            }
+                        });
+                String host = ardEquipRadar.getIp();
+                Integer port = ardEquipRadar.getPort();
+                ChannelFuture future = bootstrap.connect(host, port).sync();
+                // 娣诲姞杩炴帴鎴愬姛鐨勭洃鍚櫒
+                future.addListener((ChannelFutureListener) future1 -> {
+                    if (future1.isSuccess()) {
+                        log.info("tcp杩炴帴鎴愬姛" + host + ":" + port);
+                    } else {
+                        log.info("tcp杩炴帴澶辫触" + host + ":" + port);
+                    }
+                });
+                future.channel().closeFuture().sync();
+
+            } catch (Exception e) {
+                log.error("nettyTcp瀹㈡埛绔垵濮嬪寲寮傚父:" + e.getMessage());
+                try {
+                    TimeUnit.SECONDS.sleep(5); // 绛夊緟5绉掑悗閲嶆柊杩炴帴
+                } catch (InterruptedException ex) {
+                    Thread.currentThread().interrupt();
+                }
+            } finally {
+                group.shutdownGracefully();
+            }
         }
     }
 }

--
Gitblit v1.9.3