From 03d6ad289baddf8feed64ffca5f1b13828bea710 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期四, 01 九月 2022 14:50:28 +0800
Subject: [PATCH] Merge branch 'wvp-28181-2.0'

---
 src/main/java/com/genersoft/iot/vmp/service/impl/PlatformServiceImpl.java |  232 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 232 insertions(+), 0 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
new file mode 100644
index 0000000..708d693
--- /dev/null
+++ b/src/main/java/com/genersoft/iot/vmp/service/impl/PlatformServiceImpl.java
@@ -0,0 +1,232 @@
+package com.genersoft.iot.vmp.service.impl;
+
+import com.genersoft.iot.vmp.conf.DynamicTask;
+import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform;
+import com.genersoft.iot.vmp.gb28181.bean.ParentPlatformCatch;
+import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem;
+import com.genersoft.iot.vmp.gb28181.bean.SubscribeHolder;
+import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
+import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommanderFroPlatform;
+import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory;
+import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
+import com.genersoft.iot.vmp.service.IMediaServerService;
+import com.genersoft.iot.vmp.service.IPlatformService;
+import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
+import com.genersoft.iot.vmp.storager.dao.ParentPlatformMapper;
+import com.github.pagehelper.PageHelper;
+import com.github.pagehelper.PageInfo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import javax.sip.TimeoutEvent;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author lin
+ */
+@Service
+public class PlatformServiceImpl implements IPlatformService {
+
+    private final static String REGISTER_KEY_PREFIX = "platform_register_";
+    private final static String KEEPALIVE_KEY_PREFIX = "platform_keepalive_";
+
+    private final static Logger logger = LoggerFactory.getLogger(PlatformServiceImpl.class);
+
+    @Autowired
+    private ParentPlatformMapper platformMapper;
+
+    @Autowired
+    private IRedisCatchStorage redisCatchStorage;
+
+    @Autowired
+    private IMediaServerService mediaServerService;
+
+    @Autowired
+    private SIPCommanderFroPlatform commanderForPlatform;
+
+    @Autowired
+    private DynamicTask dynamicTask;
+
+    @Autowired
+    private ZLMRTPServerFactory zlmrtpServerFactory;
+
+    @Autowired
+    private SubscribeHolder subscribeHolder;
+
+
+
+    @Override
+    public ParentPlatform queryPlatformByServerGBId(String platformGbId) {
+        return platformMapper.getParentPlatByServerGBId(platformGbId);
+    }
+
+    @Override
+    public PageInfo<ParentPlatform> queryParentPlatformList(int page, int count) {
+        PageHelper.startPage(page, count);
+        List<ParentPlatform> all = platformMapper.getParentPlatformList();
+        return new PageInfo<>(all);
+    }
+
+    @Override
+    public boolean add(ParentPlatform parentPlatform) {
+
+        if (parentPlatform.getCatalogGroup() == 0) {
+            // 姣忔鍙戦�佺洰褰曠殑鏁伴噺榛樿涓�1
+            parentPlatform.setCatalogGroup(1);
+        }
+        if (parentPlatform.getAdministrativeDivision() == null) {
+            // 琛屾斂鍖哄垝榛樿鍘荤紪鍙风殑鍓�6浣�
+            parentPlatform.setAdministrativeDivision(parentPlatform.getServerGBId().substring(0,6));
+        }
+        parentPlatform.setCatalogId(parentPlatform.getDeviceGBId());
+        int result = platformMapper.addParentPlatform(parentPlatform);
+        // 娣诲姞缂撳瓨
+        ParentPlatformCatch parentPlatformCatch = new ParentPlatformCatch();
+        parentPlatformCatch.setParentPlatform(parentPlatform);
+        parentPlatformCatch.setId(parentPlatform.getServerGBId());
+        parentPlatformCatch.setParentPlatform(parentPlatform);
+        redisCatchStorage.updatePlatformCatchInfo(parentPlatformCatch);
+        if (parentPlatform.isEnable()) {
+            // 淇濆瓨鏃跺惎鐢ㄥ氨鍙戦�佹敞鍐�
+            // 娉ㄥ唽鎴愬姛鏃剁敱绋嬪簭鐩存帴璋冪敤浜唎nline鏂规硶
+            commanderForPlatform.register(parentPlatform, eventResult -> {
+                logger.info("[鍥芥爣绾ц仈] {},娣诲姞鍚戜笂绾ф敞鍐屽け璐ワ紝璇风‘瀹氫笂绾у钩鍙板彲鐢ㄦ椂閲嶆柊淇濆瓨", parentPlatform.getServerGBId());
+            }, null);
+        }
+        return result > 0;
+    }
+
+    @Override
+    public void online(ParentPlatform parentPlatform) {
+        logger.info("[鍥芥爣绾ц仈]锛歿}, 骞冲彴涓婄嚎/鏇存柊娉ㄥ唽", parentPlatform.getServerGBId());
+        platformMapper.updateParentPlatformStatus(parentPlatform.getServerGBId(), true);
+        ParentPlatformCatch parentPlatformCatch = redisCatchStorage.queryPlatformCatchInfo(parentPlatform.getServerGBId());
+        if (parentPlatformCatch != null) {
+            parentPlatformCatch.getParentPlatform().setStatus(true);
+            redisCatchStorage.updatePlatformCatchInfo(parentPlatformCatch);
+        }else {
+            parentPlatformCatch = new ParentPlatformCatch();
+            parentPlatformCatch.setParentPlatform(parentPlatform);
+            parentPlatformCatch.setId(parentPlatform.getServerGBId());
+            parentPlatform.setStatus(true);
+            parentPlatformCatch.setParentPlatform(parentPlatform);
+            redisCatchStorage.updatePlatformCatchInfo(parentPlatformCatch);
+        }
+
+        final String registerTaskKey = REGISTER_KEY_PREFIX + parentPlatform.getServerGBId();
+        if (dynamicTask.contains(registerTaskKey)) {
+            dynamicTask.stop(registerTaskKey);
+        }
+        // 娣诲姞娉ㄥ唽浠诲姟
+        dynamicTask.startDelay(registerTaskKey,
+                // 娉ㄥ唽澶辫触锛堟敞鍐屾垚鍔熸椂鐢辩▼搴忕洿鎺ヨ皟鐢ㄤ簡online鏂规硶锛�
+                ()->commanderForPlatform.register(parentPlatform, eventResult -> offline(parentPlatform),null),
+                parentPlatform.getExpires()*1000);
+
+        final String keepaliveTaskKey = KEEPALIVE_KEY_PREFIX + parentPlatform.getServerGBId();
+        if (!dynamicTask.contains(keepaliveTaskKey)) {
+            // 娣诲姞蹇冭烦浠诲姟
+            dynamicTask.startCron(keepaliveTaskKey,
+                    ()-> commanderForPlatform.keepalive(parentPlatform, eventResult -> {
+                        // 蹇冭烦澶辫触
+                        if (eventResult.type == SipSubscribe.EventResultType.timeout) {
+                            // 蹇冭烦瓒呮椂
+                            ParentPlatformCatch platformCatch = redisCatchStorage.queryPlatformCatchInfo(parentPlatform.getServerGBId());
+                            // 姝ゆ椂鏄涓夋蹇冭烦瓒呮椂锛� 骞冲彴绂荤嚎
+                            if (platformCatch.getKeepAliveReply()  == 2) {
+                                // 璁剧疆骞冲彴绂荤嚎锛屽苟閲嶆柊娉ㄥ唽
+                                offline(parentPlatform);
+                                logger.info("[鍥芥爣绾ц仈] {}锛屼笁娆″績璺宠秴鏃跺悗鍐嶆鍙戣捣娉ㄥ唽", parentPlatform.getServerGBId());
+                                commanderForPlatform.register(parentPlatform, eventResult1 -> {
+                                    logger.info("[鍥芥爣绾ц仈] {}锛屼笁娆″績璺宠秴鏃跺悗鍐嶆鍙戣捣娉ㄥ唽浠嶇劧澶辫触锛屽紑濮嬪畾鏃跺彂璧锋敞鍐岋紝闂撮殧涓�1鍒嗛挓", parentPlatform.getServerGBId());
+                                    // 娣诲姞娉ㄥ唽浠诲姟
+                                    dynamicTask.startCron(registerTaskKey,
+                                            // 娉ㄥ唽澶辫触锛堟敞鍐屾垚鍔熸椂鐢辩▼搴忕洿鎺ヨ皟鐢ㄤ簡online鏂规硶锛�
+                                            ()->logger.info("[鍥芥爣绾ц仈] {},骞冲彴绂荤嚎鍚庢寔缁彂璧锋敞鍐岋紝澶辫触", parentPlatform.getServerGBId()),
+                                            60*1000);
+                                }, null);
+                            }
+
+                        }else {
+                            logger.warn("[鍥芥爣绾ц仈]鍙戦�佸績璺虫敹鍒伴敊璇紝code锛� {}, msg: {}", eventResult.statusCode, eventResult.msg);
+                        }
+
+                    }, eventResult -> {
+                        // 蹇冭烦鎴愬姛
+                        // 娓呯┖涔嬪墠鐨勫績璺宠秴鏃惰鏁�
+                        ParentPlatformCatch platformCatch = redisCatchStorage.queryPlatformCatchInfo(parentPlatform.getServerGBId());
+                        if (platformCatch.getKeepAliveReply() > 0) {
+                            platformCatch.setKeepAliveReply(0);
+                            redisCatchStorage.updatePlatformCatchInfo(platformCatch);
+                        }
+                    }),
+                    parentPlatform.getExpires()*1000);
+        }
+    }
+
+    @Override
+    public void offline(ParentPlatform parentPlatform) {
+        logger.info("[骞冲彴绂荤嚎]锛歿}", parentPlatform.getServerGBId());
+        ParentPlatformCatch parentPlatformCatch = redisCatchStorage.queryPlatformCatchInfo(parentPlatform.getServerGBId());
+        parentPlatformCatch.setKeepAliveReply(0);
+        parentPlatformCatch.setRegisterAliveReply(0);
+        ParentPlatform parentPlatformInCatch = parentPlatformCatch.getParentPlatform();
+        parentPlatformInCatch.setStatus(false);
+        parentPlatformCatch.setParentPlatform(parentPlatformInCatch);
+        redisCatchStorage.updatePlatformCatchInfo(parentPlatformCatch);
+        platformMapper.updateParentPlatformStatus(parentPlatform.getServerGBId(), false);
+
+        // 鍋滄鎵�鏈夋帹娴�
+        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);
+        }
+        // 娓呴櫎蹇冭烦瀹氭椂
+        logger.info("[骞冲彴绂荤嚎] {}, 鍋滄瀹氭椂鍙戦�佸績璺充换鍔�", parentPlatform.getServerGBId());
+        final String keepaliveTaskKey = KEEPALIVE_KEY_PREFIX + parentPlatform.getServerGBId();
+        if (dynamicTask.contains(keepaliveTaskKey)) {
+            // 娣诲姞蹇冭烦浠诲姟
+            dynamicTask.stop(keepaliveTaskKey);
+        }
+        // 鍋滄鐩綍璁㈤槄鍥炲
+        logger.info("[骞冲彴绂荤嚎] {}, 鍋滄璁㈤槄鍥炲", parentPlatform.getServerGBId());
+        subscribeHolder.removeAllSubscribe(parentPlatform.getServerGBId());
+    }
+
+    private void stopAllPush(String platformId) {
+        List<SendRtpItem> sendRtpItems = redisCatchStorage.querySendRTPServer(platformId);
+        if (sendRtpItems != null && sendRtpItems.size() > 0) {
+            for (SendRtpItem sendRtpItem : sendRtpItems) {
+                redisCatchStorage.deleteSendRTPServer(platformId, sendRtpItem.getChannelId(), null, null);
+                MediaServerItem mediaInfo = mediaServerService.getOne(sendRtpItem.getMediaServerId());
+                Map<String, Object> param = new HashMap<>(3);
+                param.put("vhost", "__defaultVhost__");
+                param.put("app", sendRtpItem.getApp());
+                param.put("stream", sendRtpItem.getStreamId());
+                zlmrtpServerFactory.stopSendRtpStream(mediaInfo, param);
+            }
+
+        }
+    }
+
+    @Override
+    public void login(ParentPlatform parentPlatform) {
+        final String registerTaskKey = REGISTER_KEY_PREFIX + parentPlatform.getServerGBId();
+        commanderForPlatform.register(parentPlatform, eventResult1 -> {
+            logger.info("[鍥芥爣绾ц仈] {}锛屽紑濮嬪畾鏃跺彂璧锋敞鍐岋紝闂撮殧涓�1鍒嗛挓", parentPlatform.getServerGBId());
+            // 娣诲姞娉ㄥ唽浠诲姟
+            dynamicTask.startCron(registerTaskKey,
+                    // 娉ㄥ唽澶辫触锛堟敞鍐屾垚鍔熸椂鐢辩▼搴忕洿鎺ヨ皟鐢ㄤ簡online鏂规硶锛�
+                    ()->logger.info("[鍥芥爣绾ц仈] {},骞冲彴绂荤嚎鍚庢寔缁彂璧锋敞鍐岋紝澶辫触", parentPlatform.getServerGBId()),
+                    60*1000);
+        }, null);
+    }
+}

--
Gitblit v1.8.0