From d7a1b94f905c5f28c9c8f2d48c3f9e28ebcf9cc4 Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: 星期六, 24 九月 2022 21:04:58 +0800 Subject: [PATCH] Merge branch 'wvp-28181-2.0' --- src/main/java/com/genersoft/iot/vmp/gb28181/task/impl/MobilePositionSubscribeTask.java | 96 ++++++++++++++++++++++------------------------- 1 files changed, 45 insertions(+), 51 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 index 884f040..5dbdbe6 100644 --- 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,8 +1,11 @@ package com.genersoft.iot.vmp.gb28181.task.impl; +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 gov.nist.javax.sip.message.SIPRequest; +import gov.nist.javax.sip.message.SIPResponse; import org.dom4j.Element; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -11,55 +14,55 @@ import javax.sip.Dialog; import javax.sip.DialogState; import javax.sip.ResponseEvent; +import javax.sip.header.ToHeader; +import java.text.ParseException; import java.util.Timer; import java.util.TimerTask; /** * 绉诲姩浣嶇疆璁㈤槄鐨勫畾鏃舵洿鏂� + * @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; - private Timer timer ; + private SIPRequest request; + private DynamicTask dynamicTask; + private String taskKey = "mobile-position-subscribe-timeout"; - public MobilePositionSubscribeTask(Device device, ISIPCommander sipCommander) { + public MobilePositionSubscribeTask(Device device, ISIPCommander sipCommander, DynamicTask dynamicTask) { this.device = device; this.sipCommander = sipCommander; + this.dynamicTask = dynamicTask; } @Override public void run() { - if (timer != null ) { - timer.cancel(); - timer = null; + if (dynamicTask.get(taskKey) != null) { + dynamicTask.stop(taskKey); } - sipCommander.mobilePositionSubscribe(device, dialog, eventResult -> { -// if (eventResult.dialog != null || eventResult.dialog.getState().equals(DialogState.CONFIRMED)) { -// dialog = eventResult.dialog; -// } + SIPRequest sipRequest = sipCommander.mobilePositionSubscribe(device, request, eventResult -> { + // 鎴愬姛 + logger.info("[绉诲姩浣嶇疆璁㈤槄]鎴愬姛锛� {}", device.getDeviceId()); ResponseEvent event = (ResponseEvent) eventResult.event; - if (event.getResponse().getRawContent() != null) { - // 鎴愬姛 - logger.info("[绉诲姩浣嶇疆璁㈤槄]鎴愬姛锛� {}", device.getDeviceId()); - }else { - // 鎴愬姛 - logger.info("[绉诲姩浣嶇疆璁㈤槄]鎴愬姛锛� {}", device.getDeviceId()); + 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 -> { - dialog = null; + this.request = null; // 澶辫触 logger.warn("[绉诲姩浣嶇疆璁㈤槄]澶辫触锛屼俊浠ゅ彂閫佸け璐ワ細 {}-{} ", device.getDeviceId(), eventResult.msg); - timer = new Timer(); - timer.schedule(new TimerTask() { - @Override - public void run() { - MobilePositionSubscribeTask.this.run(); - } - }, 2000); + dynamicTask.startDelay(taskKey, MobilePositionSubscribeTask.this, 2000); }); + if (sipRequest != null) { + this.request = sipRequest; + } } @@ -72,31 +75,22 @@ * COMPLETED-> Completed Dialog鐘舵��-宸插畬鎴� * TERMINATED-> Terminated Dialog鐘舵��-缁堟 */ - if (timer != null ) { - timer.cancel(); - timer = null; + if (dynamicTask.get(taskKey) != null) { + dynamicTask.stop(taskKey); } - if (dialog != null && dialog.getState().equals(DialogState.CONFIRMED)) { - logger.info("鍙栨秷绉诲姩璁㈤槄鏃禿ialog鐘舵�佷负{}", dialog.getState()); - device.setSubscribeCycleForMobilePosition(0); - sipCommander.mobilePositionSubscribe(device, dialog, eventResult -> { - ResponseEvent event = (ResponseEvent) eventResult.event; - if (event.getResponse().getRawContent() != null) { - // 鎴愬姛 - logger.info("[鍙栨秷绉诲姩浣嶇疆璁㈤槄]鎴愬姛锛� {}", device.getDeviceId()); - }else { - // 鎴愬姛 - logger.info("[鍙栨秷绉诲姩浣嶇疆璁㈤槄]鎴愬姛锛� {}", device.getDeviceId()); - } - },eventResult -> { - // 澶辫触 - logger.warn("[鍙栨秷绉诲姩浣嶇疆璁㈤槄]澶辫触锛屼俊浠ゅ彂閫佸け璐ワ細 {}-{} ", device.getDeviceId(), eventResult.msg); - }); - } - } - @Override - public DialogState getDialogState() { - if (dialog == null) return null; - return dialog.getState(); + device.setSubscribeCycleForMobilePosition(0); + sipCommander.mobilePositionSubscribe(device, request, eventResult -> { + ResponseEvent event = (ResponseEvent) eventResult.event; + if (event.getResponse().getRawContent() != null) { + // 鎴愬姛 + logger.info("[鍙栨秷绉诲姩浣嶇疆璁㈤槄]鎴愬姛锛� {}", device.getDeviceId()); + }else { + // 鎴愬姛 + logger.info("[鍙栨秷绉诲姩浣嶇疆璁㈤槄]鎴愬姛锛� {}", device.getDeviceId()); + } + },eventResult -> { + // 澶辫触 + logger.warn("[鍙栨秷绉诲姩浣嶇疆璁㈤槄]澶辫触锛屼俊浠ゅ彂閫佸け璐ワ細 {}-{} ", device.getDeviceId(), eventResult.msg); + }); } } -- Gitblit v1.8.0