From d09d8f11f06ee6eeeb4c7f9bd6d35e6fbd49e05f Mon Sep 17 00:00:00 2001
From: jiang <893224616@qq.com>
Date: 星期四, 04 八月 2022 17:01:31 +0800
Subject: [PATCH] 修改‘修改pushkey’窗口的标题提示文案

---
 src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookSubscribe.java |  149 +++++++++++++++++++++++++++++++------------------
 1 files changed, 95 insertions(+), 54 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
index 0c00b82..57b6d81 100644
--- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookSubscribe.java
+++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookSubscribe.java
@@ -1,90 +1,131 @@
 package com.genersoft.iot.vmp.media.zlm;
 
-import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
-import com.genersoft.iot.vmp.common.StreamInfo;
-import com.genersoft.iot.vmp.conf.MediaServerConfig;
-import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
-import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
+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.springframework.stereotype.Component;
-import org.springframework.util.ConcurrentReferenceHashMap;
-import org.springframework.web.bind.annotation.*;
+import org.springframework.util.CollectionUtils;
 
-import javax.servlet.http.HttpServletRequest;
-import java.math.BigInteger;
-import java.text.DecimalFormat;
-import java.util.HashMap;
-import java.util.Map;
+import java.time.Instant;
+import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
 
 /**
- * @Description:閽堝 ZLMediaServer鐨刪ook浜嬩欢璁㈤槄
+ * @description:閽堝 ZLMediaServer鐨刪ook浜嬩欢璁㈤槄
  * @author: pan
  * @date:   2020骞�12鏈�2鏃� 21:17:32
  */
 @Component
 public class ZLMHttpHookSubscribe {
 
-    private final static Logger logger = LoggerFactory.getLogger(ZLMHttpHookSubscribe.class);
-
-    public enum HookType{
-        on_flow_report,
-        on_http_access,
-        on_play,
-        on_publish,
-        on_record_mp4,
-        on_rtsp_auth,
-        on_rtsp_realm,
-        on_shell_login,
-        on_stream_changed,
-        on_stream_none_reader,
-        on_stream_not_found,
-        on_server_started
-    }
-
+    @FunctionalInterface
     public interface Event{
-        void response(JSONObject response);
+        void response(MediaServerItem mediaServerItem, JSONObject response);
     }
 
-    private Map<HookType, Map<JSONObject, ZLMHttpHookSubscribe.Event>> allSubscribes = new ConcurrentHashMap<>();
+    private Map<HookType, Map<IHookSubscribe, ZLMHttpHookSubscribe.Event>> allSubscribes = new ConcurrentHashMap<>();
 
-    public void addSubscribe(HookType type, JSONObject hookResponse, ZLMHttpHookSubscribe.Event event) {
-        Map<JSONObject, Event> eventMap = allSubscribes.get(type);
-        if (eventMap == null) {
-            eventMap = new HashMap<JSONObject, Event>();
-            allSubscribes.put(type,eventMap);
+    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);
         }
-        eventMap.put(hookResponse, event);
+        allSubscribes.computeIfAbsent(hookSubscribe.getHookType(), k -> new ConcurrentHashMap<>()).put(hookSubscribe, event);
     }
 
-    public ZLMHttpHookSubscribe.Event getSubscribe(HookType type, JSONObject hookResponse) {
+    public ZLMHttpHookSubscribe.Event sendNotify(HookType type, JSONObject hookResponse) {
         ZLMHttpHookSubscribe.Event event= null;
-        Map<JSONObject, Event> eventMap = allSubscribes.get(type);
+        Map<IHookSubscribe, Event> eventMap = allSubscribes.get(type);
         if (eventMap == null) {
             return null;
         }
-        for (JSONObject key : eventMap.keySet()) {
+        for (IHookSubscribe key : eventMap.keySet()) {
             Boolean result = null;
-            for (String s : key.keySet()) {
-                String string = hookResponse.getString(s);
-                String string1 = key.getString(s);
+            for (String s : key.getContent().keySet()) {
                 if (result == null) {
-                    result = key.getString(s).equals(hookResponse.getString(s));
+                    result = key.getContent().getString(s).equals(hookResponse.getString(s));
                 }else {
-                    result = result && key.getString(s).equals(hookResponse.getString(s));
+                    if (key.getContent().getString(s) == null) {
+                        continue;
+                    }
+                    result = result && key.getContent().getString(s).equals(hookResponse.getString(s));
                 }
-
             }
-            if (result) {
+            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 (null != result && 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;
+    }
+
+
 }

--
Gitblit v1.8.0