|  |  | 
 |  |  | package com.genersoft.iot.vmp.media.zlm; | 
 |  |  |  | 
 |  |  | import com.alibaba.fastjson.JSONObject; | 
 |  |  | import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem; | 
 |  |  | import org.springframework.stereotype.Component; | 
 |  |  | import org.springframework.util.CollectionUtils; | 
 |  |  |  | 
 |  |  | import java.util.ArrayList; | 
 |  |  | import java.util.HashMap; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.Map; | 
 |  |  | import java.util.*; | 
 |  |  | import java.util.concurrent.ConcurrentHashMap; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * @Description:针对 ZLMediaServer的hook事件订阅 | 
 |  |  |  * @description:针对 ZLMediaServer的hook事件订阅 | 
 |  |  |  * @author: pan | 
 |  |  |  * @date:   2020年12月2日 21:17:32 | 
 |  |  |  */ | 
 |  |  | 
 |  |  |         on_stream_changed, | 
 |  |  |         on_stream_none_reader, | 
 |  |  |         on_stream_not_found, | 
 |  |  |         on_server_started | 
 |  |  |         on_server_started, | 
 |  |  |         on_server_keepalive | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @FunctionalInterface | 
 |  |  |     public interface Event{ | 
 |  |  |         void response(JSONObject response); | 
 |  |  |         void response(MediaServerItem mediaServerItem, JSONObject response); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private Map<HookType, Map<JSONObject, 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); | 
 |  |  |         } | 
 |  |  |         eventMap.put(hookResponse, event); | 
 |  |  |         allSubscribes.computeIfAbsent(type, k -> new ConcurrentHashMap<>()).put(hookResponse, event); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public ZLMHttpHookSubscribe.Event getSubscribe(HookType type, JSONObject hookResponse) { | 
 |  |  | 
 |  |  |                 if (result == null) { | 
 |  |  |                     result = key.getString(s).equals(hookResponse.getString(s)); | 
 |  |  |                 }else { | 
 |  |  |                     if (key.getString(s) == null) { | 
 |  |  |                         continue; | 
 |  |  |                     } | 
 |  |  |                     result = result && key.getString(s).equals(hookResponse.getString(s)); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |             } | 
 |  |  |             if (result) { | 
 |  |  |             if (null != result && result) { | 
 |  |  |                 event = eventMap.get(key); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return event; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public void removeSubscribe(HookType type, JSONObject hookResponse) { | 
 |  |  |         Map<JSONObject, Event> eventMap = allSubscribes.get(type); | 
 |  |  |         if (eventMap == null) { | 
 |  |  |             return; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         Set<Map.Entry<JSONObject, Event>> entries = eventMap.entrySet(); | 
 |  |  |         if (entries.size() > 0) { | 
 |  |  |             List<Map.Entry<JSONObject, ZLMHttpHookSubscribe.Event>> entriesToRemove = new ArrayList<>(); | 
 |  |  |             for (Map.Entry<JSONObject, ZLMHttpHookSubscribe.Event> entry : entries) { | 
 |  |  |                 JSONObject key = entry.getKey(); | 
 |  |  |                 Boolean result = null; | 
 |  |  |                 for (String s : key.keySet()) { | 
 |  |  |                     if (result == null) { | 
 |  |  |                         result = key.getString(s).equals(hookResponse.getString(s)); | 
 |  |  |                     }else { | 
 |  |  |                         if (key.getString(s) == null) continue; | 
 |  |  |                         result = result && key.getString(s).equals(hookResponse.getString(s)); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |                 if (null != result && result){ | 
 |  |  |                     entriesToRemove.add(entry); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             if (!CollectionUtils.isEmpty(entriesToRemove)) { | 
 |  |  |                 for (Map.Entry<JSONObject, ZLMHttpHookSubscribe.Event> entry : entriesToRemove) { | 
 |  |  |                     entries.remove(entry); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public List<ZLMHttpHookSubscribe.Event> getSubscribes(HookType type) { | 
 |  |  |         ZLMHttpHookSubscribe.Event event= null; | 
 |  |  |         // ZLMHttpHookSubscribe.Event event= null; | 
 |  |  |         Map<JSONObject, Event> eventMap = allSubscribes.get(type); | 
 |  |  |         if (eventMap == null) { | 
 |  |  |             return null; |