From 2b1f7a47394363e95deb4dfa0f1c67d41e747f7f Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期三, 01 二月 2023 10:56:40 +0800
Subject: [PATCH] Merge branch 'wvp-28181-2.0' into fix-269

---
 src/main/java/com/genersoft/iot/vmp/media/zlm/ZlmHttpHookSubscribe.java |  155 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 155 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZlmHttpHookSubscribe.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZlmHttpHookSubscribe.java
new file mode 100644
index 0000000..cf33bb2
--- /dev/null
+++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZlmHttpHookSubscribe.java
@@ -0,0 +1,155 @@
+package com.genersoft.iot.vmp.media.zlm;
+
+import com.alibaba.fastjson2.JSONObject;
+import com.genersoft.iot.vmp.media.zlm.dto.HookType;
+import com.genersoft.iot.vmp.media.zlm.dto.IHookSubscribe;
+import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+import org.springframework.util.CollectionUtils;
+
+import java.time.Instant;
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * ZLMediaServer鐨刪ook浜嬩欢璁㈤槄
+ * @author lin
+ */
+@Component
+public class ZlmHttpHookSubscribe {
+
+    private final static Logger logger = LoggerFactory.getLogger(ZlmHttpHookSubscribe.class);
+
+    @FunctionalInterface
+    public interface Event{
+        void response(MediaServerItem mediaServerItem, JSONObject response);
+    }
+
+    private Map<HookType, Map<IHookSubscribe, ZlmHttpHookSubscribe.Event>> allSubscribes = new ConcurrentHashMap<>();
+
+    public void addSubscribe(IHookSubscribe hookSubscribe, ZlmHttpHookSubscribe.Event event) {
+        if (hookSubscribe.getExpires() == null) {
+            // 榛樿5鍒嗛挓杩囨湡
+            Instant expiresInstant = Instant.now().plusSeconds(TimeUnit.MINUTES.toSeconds(5));
+            hookSubscribe.setExpires(expiresInstant);
+        }
+        allSubscribes.computeIfAbsent(hookSubscribe.getHookType(), k -> new ConcurrentHashMap<>()).put(hookSubscribe, event);
+    }
+
+    public ZlmHttpHookSubscribe.Event sendNotify(HookType type, JSONObject hookResponse) {
+        ZlmHttpHookSubscribe.Event event= null;
+        Map<IHookSubscribe, Event> eventMap = allSubscribes.get(type);
+        if (eventMap == null) {
+            return null;
+        }
+        for (IHookSubscribe key : eventMap.keySet()) {
+            Boolean result = null;
+            for (String s : key.getContent().keySet()) {
+                if (result == null) {
+                    result = key.getContent().getString(s).equals(hookResponse.getString(s));
+                }else {
+                    if (key.getContent().getString(s) == null) {
+                        continue;
+                    }
+                    result = result && key.getContent().getString(s).equals(hookResponse.getString(s));
+                }
+            }
+            if (null != result && result) {
+                event = eventMap.get(key);
+            }
+        }
+        return event;
+    }
+
+    public void removeSubscribe(IHookSubscribe hookSubscribe) {
+        Map<IHookSubscribe, Event> eventMap = allSubscribes.get(hookSubscribe.getHookType());
+        if (eventMap == null) {
+            return;
+        }
+
+        Set<Map.Entry<IHookSubscribe, Event>> entries = eventMap.entrySet();
+        if (entries.size() > 0) {
+            List<Map.Entry<IHookSubscribe, ZlmHttpHookSubscribe.Event>> entriesToRemove = new ArrayList<>();
+            for (Map.Entry<IHookSubscribe, ZlmHttpHookSubscribe.Event> entry : entries) {
+                JSONObject content = entry.getKey().getContent();
+                if (content == null || content.size() == 0) {
+                    entriesToRemove.add(entry);
+                    continue;
+                }
+                Boolean result = null;
+                for (String s : content.keySet()) {
+                    if (result == null) {
+                        result = content.getString(s).equals(hookSubscribe.getContent().getString(s));
+                    }else {
+                        if (content.getString(s) == null) {
+                            continue;
+                        }
+                        result = result && content.getString(s).equals(hookSubscribe.getContent().getString(s));
+                    }
+                }
+                if (result){
+                    entriesToRemove.add(entry);
+                }
+            }
+
+            if (!CollectionUtils.isEmpty(entriesToRemove)) {
+                for (Map.Entry<IHookSubscribe, ZlmHttpHookSubscribe.Event> entry : entriesToRemove) {
+                    entries.remove(entry);
+                }
+            }
+
+        }
+    }
+
+    /**
+     * 鑾峰彇鏌愪釜绫诲瀷鐨勬墍鏈夌殑璁㈤槄
+     * @param type
+     * @return
+     */
+    public List<ZlmHttpHookSubscribe.Event> getSubscribes(HookType type) {
+        Map<IHookSubscribe, Event> eventMap = allSubscribes.get(type);
+        if (eventMap == null) {
+            return null;
+        }
+        List<ZlmHttpHookSubscribe.Event> result = new ArrayList<>();
+        for (IHookSubscribe key : eventMap.keySet()) {
+            result.add(eventMap.get(key));
+        }
+        return result;
+    }
+
+    public List<IHookSubscribe> getAll(){
+        ArrayList<IHookSubscribe> result = new ArrayList<>();
+        Collection<Map<IHookSubscribe, Event>> values = allSubscribes.values();
+        for (Map<IHookSubscribe, Event> value : values) {
+            result.addAll(value.keySet());
+        }
+        return result;
+    }
+
+    /**
+     * 瀵硅闃呮暟鎹繘琛岃繃鏈熸竻鐞�
+     */
+    @Scheduled(cron="0 0/5 * * * ?")   //姣�5鍒嗛挓鎵ц涓�娆�
+    public void execute(){
+
+        Instant instant = Instant.now().minusMillis(TimeUnit.MINUTES.toMillis(5));
+        int total = 0;
+        for (HookType hookType : allSubscribes.keySet()) {
+            Map<IHookSubscribe, Event> hookSubscribeEventMap = allSubscribes.get(hookType);
+            if (hookSubscribeEventMap.size() > 0) {
+                for (IHookSubscribe hookSubscribe : hookSubscribeEventMap.keySet()) {
+                    if (hookSubscribe.getExpires().isBefore(instant)) {
+                        // 杩囨湡鐨�
+                        hookSubscribeEventMap.remove(hookSubscribe);
+                        total ++;
+                    }
+                }
+            }
+        }
+    }
+}

--
Gitblit v1.8.0