From 03ee15ece2ff749be8f4c211e1ee6fd3a3a6066d Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期四, 09 二月 2023 14:13:44 +0800
Subject: [PATCH] Merge pull request #741 from gaofuwang/wvp-28181-2.0

---
 src/main/java/com/genersoft/iot/vmp/service/impl/PlatformServiceImpl.java |   60 +++++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 37 insertions(+), 23 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/PlatformServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/PlatformServiceImpl.java
index fe67ede..136689c 100644
--- a/src/main/java/com/genersoft/iot/vmp/service/impl/PlatformServiceImpl.java
+++ b/src/main/java/com/genersoft/iot/vmp/service/impl/PlatformServiceImpl.java
@@ -22,7 +22,6 @@
 
 import javax.sip.InvalidArgumentException;
 import javax.sip.SipException;
-import javax.sip.TimeoutEvent;
 import java.text.ParseException;
 import java.util.HashMap;
 import java.util.List;
@@ -131,20 +130,16 @@
         }
 
         final String registerTaskKey = REGISTER_KEY_PREFIX + parentPlatform.getServerGBId();
-        if (dynamicTask.contains(registerTaskKey)) {
-            dynamicTask.stop(registerTaskKey);
-        }
-        // 娣诲姞娉ㄥ唽浠诲姟
-        dynamicTask.startDelay(registerTaskKey,
+        if (!dynamicTask.isAlive(registerTaskKey)) {
+            // 娣诲姞娉ㄥ唽浠诲姟
+            dynamicTask.startCron(registerTaskKey,
                 // 娉ㄥ唽澶辫触锛堟敞鍐屾垚鍔熸椂鐢辩▼搴忕洿鎺ヨ皟鐢ㄤ簡online鏂规硶锛�
                 ()-> {
-                    try {
-                        commanderForPlatform.register(parentPlatform, eventResult -> offline(parentPlatform),null);
-                    } catch (InvalidArgumentException | ParseException | SipException e) {
-                        logger.error("[鍛戒护鍙戦�佸け璐 鍥芥爣绾ц仈瀹氭椂娉ㄥ唽: {}", e.getMessage());
-                    }
+                    registerTask(parentPlatform);
                 },
                 (parentPlatform.getExpires() - 10) *1000);
+        }
+
 
         final String keepaliveTaskKey = KEEPALIVE_KEY_PREFIX + parentPlatform.getServerGBId();
         if (!dynamicTask.contains(keepaliveTaskKey)) {
@@ -160,16 +155,11 @@
                                     // 姝ゆ椂鏄涓夋蹇冭烦瓒呮椂锛� 骞冲彴绂荤嚎
                                     if (platformCatch.getKeepAliveReply()  == 2) {
                                         // 璁剧疆骞冲彴绂荤嚎锛屽苟閲嶆柊娉ㄥ唽
-                                        offline(parentPlatform);
                                         logger.info("[鍥芥爣绾ц仈] {}锛屼笁娆″績璺宠秴鏃跺悗鍐嶆鍙戣捣娉ㄥ唽", parentPlatform.getServerGBId());
                                         try {
                                             commanderForPlatform.register(parentPlatform, eventResult1 -> {
                                                 logger.info("[鍥芥爣绾ц仈] {}锛屼笁娆″績璺宠秴鏃跺悗鍐嶆鍙戣捣娉ㄥ唽浠嶇劧澶辫触锛屽紑濮嬪畾鏃跺彂璧锋敞鍐岋紝闂撮殧涓�1鍒嗛挓", parentPlatform.getServerGBId());
-                                                // 娣诲姞娉ㄥ唽浠诲姟
-                                                dynamicTask.startCron(registerTaskKey,
-                                                        // 娉ㄥ唽澶辫触锛堟敞鍐屾垚鍔熸椂鐢辩▼搴忕洿鎺ヨ皟鐢ㄤ簡online鏂规硶锛�
-                                                        ()->logger.info("[鍥芥爣绾ц仈] {},骞冲彴绂荤嚎鍚庢寔缁彂璧锋敞鍐岋紝澶辫触", parentPlatform.getServerGBId()),
-                                                        60*1000);
+                                                offline(parentPlatform, false);
                                             }, null);
                                         } catch (InvalidArgumentException | ParseException | SipException e) {
                                             logger.error("[鍛戒护鍙戦�佸け璐 鍥芥爣绾ц仈 娉ㄥ唽: {}", e.getMessage());
@@ -197,8 +187,30 @@
         }
     }
 
+    private void registerTask(ParentPlatform parentPlatform){
+        try {
+            // 璁剧疆瓒呮椂閲嶅彂锛� 鍚庣画浠庡簳灞傛敮鎸佹秷鎭噸鍙�
+            String key = KEEPALIVE_KEY_PREFIX + parentPlatform.getServerGBId() + "_timeout";
+            if (dynamicTask.isAlive(key)) {
+                return;
+            }
+            dynamicTask.startDelay(key, ()->{
+                registerTask(parentPlatform);
+            }, 1000);
+            logger.info("[鍥芥爣绾ц仈] 骞冲彴锛歿}娉ㄥ唽鍗冲皢鍒版湡锛岄噸鏂版敞鍐�", parentPlatform.getServerGBId());
+            commanderForPlatform.register(parentPlatform, eventResult -> {
+                dynamicTask.stop(key);
+                offline(parentPlatform, false);
+            },eventResult -> {
+                dynamicTask.stop(key);
+            });
+        } catch (InvalidArgumentException | ParseException | SipException e) {
+            logger.error("[鍛戒护鍙戦�佸け璐 鍥芥爣绾ц仈瀹氭椂娉ㄥ唽: {}", e.getMessage());
+        }
+    }
+
     @Override
-    public void offline(ParentPlatform parentPlatform) {
+    public void offline(ParentPlatform parentPlatform, boolean stopRegister) {
         logger.info("[骞冲彴绂荤嚎]锛歿}", parentPlatform.getServerGBId());
         ParentPlatformCatch parentPlatformCatch = redisCatchStorage.queryPlatformCatchInfo(parentPlatform.getServerGBId());
         parentPlatformCatch.setKeepAliveReply(0);
@@ -212,11 +224,13 @@
         // 鍋滄鎵�鏈夋帹娴�
         logger.info("[骞冲彴绂荤嚎] {}, 鍋滄鎵�鏈夋帹娴�", parentPlatform.getServerGBId());
         stopAllPush(parentPlatform.getServerGBId());
-        // 娓呴櫎娉ㄥ唽瀹氭椂
-        logger.info("[骞冲彴绂荤嚎] {}, 鍋滄瀹氭椂娉ㄥ唽浠诲姟", parentPlatform.getServerGBId());
-        final String registerTaskKey = REGISTER_KEY_PREFIX + parentPlatform.getServerGBId();
-        if (dynamicTask.contains(registerTaskKey)) {
-            dynamicTask.stop(registerTaskKey);
+        if (stopRegister) {
+            // 娓呴櫎娉ㄥ唽瀹氭椂
+            logger.info("[骞冲彴绂荤嚎] {}, 鍋滄瀹氭椂娉ㄥ唽浠诲姟", parentPlatform.getServerGBId());
+            final String registerTaskKey = REGISTER_KEY_PREFIX + parentPlatform.getServerGBId();
+            if (dynamicTask.contains(registerTaskKey)) {
+                dynamicTask.stop(registerTaskKey);
+            }
         }
         // 娓呴櫎蹇冭烦瀹氭椂
         logger.info("[骞冲彴绂荤嚎] {}, 鍋滄瀹氭椂鍙戦�佸績璺充换鍔�", parentPlatform.getServerGBId());

--
Gitblit v1.8.0