From f8ef14bfea43abd8716aab15f5e302e7c1f23f79 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期二, 11 六月 2024 10:08:13 +0800
Subject: [PATCH] 修复移动位置位置订阅上报间隔为0的bug

---
 src/main/java/com/genersoft/iot/vmp/gb28181/task/impl/MobilePositionSubscribeTask.java |   81 +++++++++++++++++++++++++++-------------
 1 files changed, 54 insertions(+), 27 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/task/impl/MobilePositionSubscribeTask.java b/src/main/java/com/genersoft/iot/vmp/gb28181/task/impl/MobilePositionSubscribeTask.java
old mode 100644
new mode 100755
index f67f6d2..9fed079
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/task/impl/MobilePositionSubscribeTask.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/task/impl/MobilePositionSubscribeTask.java
@@ -1,54 +1,74 @@
 package com.genersoft.iot.vmp.gb28181.task.impl;
 
+import com.genersoft.iot.vmp.common.CommonCallback;
+import com.genersoft.iot.vmp.conf.DynamicTask;
 import com.genersoft.iot.vmp.gb28181.bean.Device;
 import com.genersoft.iot.vmp.gb28181.task.ISubscribeTask;
 import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommander;
-import org.dom4j.Element;
+import gov.nist.javax.sip.message.SIPRequest;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import javax.sip.Dialog;
-import javax.sip.DialogState;
+import javax.sip.InvalidArgumentException;
 import javax.sip.ResponseEvent;
+import javax.sip.SipException;
+import javax.sip.header.ToHeader;
+import java.text.ParseException;
 
 /**
  * 绉诲姩浣嶇疆璁㈤槄鐨勫畾鏃舵洿鏂�
+ * @author lin
  */
 public class MobilePositionSubscribeTask implements ISubscribeTask {
     private final Logger logger = LoggerFactory.getLogger(MobilePositionSubscribeTask.class);
-    private  Device device;
-    private  ISIPCommander sipCommander;
-    private Dialog dialog;
+    private Device device;
+    private ISIPCommander sipCommander;
 
-    public MobilePositionSubscribeTask(Device device, ISIPCommander sipCommander) {
+    private SIPRequest request;
+    private DynamicTask dynamicTask;
+    private String taskKey = "mobile-position-subscribe-timeout";
+
+    public MobilePositionSubscribeTask(Device device, ISIPCommander sipCommander, DynamicTask dynamicTask) {
         this.device = device;
         this.sipCommander = sipCommander;
+        this.dynamicTask = dynamicTask;
     }
 
     @Override
     public void run() {
-        sipCommander.mobilePositionSubscribe(device, dialog, eventResult -> {
-            if (eventResult.dialog != null || eventResult.dialog.getState().equals(DialogState.CONFIRMED)) {
-                dialog = eventResult.dialog;
-            }
-            ResponseEvent event = (ResponseEvent) eventResult.event;
-            if (event.getResponse().getRawContent() != null) {
+        if (dynamicTask.get(taskKey) != null) {
+            dynamicTask.stop(taskKey);
+        }
+        SIPRequest sipRequest = null;
+        try {
+            sipRequest = sipCommander.mobilePositionSubscribe(device, request, eventResult -> {
                 // 鎴愬姛
                 logger.info("[绉诲姩浣嶇疆璁㈤槄]鎴愬姛锛� {}", device.getDeviceId());
-            }else {
-                // 鎴愬姛
-                logger.info("[绉诲姩浣嶇疆璁㈤槄]鎴愬姛锛� {}", device.getDeviceId());
-            }
-        },eventResult -> {
-            dialog = null;
-            // 澶辫触
-            logger.warn("[绉诲姩浣嶇疆璁㈤槄]澶辫触锛屼俊浠ゅ彂閫佸け璐ワ細 {}-{} ", device.getDeviceId(), eventResult.msg);
-        });
+                ResponseEvent event = (ResponseEvent) eventResult.event;
+                ToHeader toHeader = (ToHeader)event.getResponse().getHeader(ToHeader.NAME);
+                try {
+                    this.request.getToHeader().setTag(toHeader.getTag());
+                } catch (ParseException e) {
+                    logger.info("[绉诲姩浣嶇疆璁㈤槄]鎴愬姛锛� 涓簉equest璁剧疆ToTag澶辫触");
+                    this.request = null;
+                }
+            },eventResult -> {
+                this.request = null;
+                // 澶辫触
+                logger.warn("[绉诲姩浣嶇疆璁㈤槄]澶辫触锛屼俊浠ゅ彂閫佸け璐ワ細 {}-{} ", device.getDeviceId(), eventResult.msg);
+                dynamicTask.startDelay(taskKey, MobilePositionSubscribeTask.this, 2000);
+            });
+        } catch (InvalidArgumentException | SipException | ParseException e) {
+            logger.error("[鍛戒护鍙戦�佸け璐 绉诲姩浣嶇疆璁㈤槄: {}", e.getMessage());
+        }
+        if (sipRequest != null) {
+            this.request = sipRequest;
+        }
 
     }
 
     @Override
-    public void stop() {
+    public void stop(CommonCallback<Boolean> callback) {
         /**
          * dialog 鐨勫悇涓姸鎬�
          * EARLY-> Early state鐘舵��-鍒濆璇锋眰鍙戦�佷互鍚庯紝鏀跺埌浜嗕竴涓复鏃跺搷搴旀秷鎭�
@@ -56,10 +76,12 @@
          * COMPLETED-> Completed Dialog鐘舵��-宸插畬鎴�
          * TERMINATED-> Terminated Dialog鐘舵��-缁堟
          */
-        logger.info("鍙栨秷绉诲姩璁㈤槄鏃禿ialog鐘舵�佷负{}", dialog.getState());
-        if (dialog != null && dialog.getState().equals(DialogState.CONFIRMED)) {
-            device.setSubscribeCycleForMobilePosition(0);
-            sipCommander.mobilePositionSubscribe(device, dialog, eventResult -> {
+        if (dynamicTask.get(taskKey) != null) {
+            dynamicTask.stop(taskKey);
+        }
+        device.setSubscribeCycleForMobilePosition(0);
+        try {
+            sipCommander.mobilePositionSubscribe(device, request, eventResult -> {
                 ResponseEvent event = (ResponseEvent) eventResult.event;
                 if (event.getResponse().getRawContent() != null) {
                     // 鎴愬姛
@@ -68,10 +90,15 @@
                     // 鎴愬姛
                     logger.info("[鍙栨秷绉诲姩浣嶇疆璁㈤槄]鎴愬姛锛� {}", device.getDeviceId());
                 }
+                if (callback != null) {
+                    callback.run(event.getResponse().getRawContent() != null);
+                }
             },eventResult -> {
                 // 澶辫触
                 logger.warn("[鍙栨秷绉诲姩浣嶇疆璁㈤槄]澶辫触锛屼俊浠ゅ彂閫佸け璐ワ細 {}-{} ", device.getDeviceId(), eventResult.msg);
             });
+        } catch (InvalidArgumentException | SipException | ParseException e) {
+            logger.error("[鍛戒护鍙戦�佸け璐 鍙栨秷绉诲姩浣嶇疆璁㈤槄: {}", e.getMessage());
         }
     }
 }

--
Gitblit v1.8.0