From bde0e13682ed75d2e8c0cb8a1fd6a96bb92f1dd8 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期一, 14 三月 2022 18:34:25 +0800
Subject: [PATCH] #394

---
 src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java |  277 +++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 218 insertions(+), 59 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java b/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java
index 6ea768a..6ad654e 100644
--- a/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java
+++ b/src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java
@@ -2,11 +2,15 @@
 
 import com.alibaba.fastjson.JSONObject;
 import com.genersoft.iot.vmp.common.StreamInfo;
+import com.genersoft.iot.vmp.common.SystemInfoDto;
 import com.genersoft.iot.vmp.common.VideoManagerConstants;
 import com.genersoft.iot.vmp.conf.UserSetup;
 import com.genersoft.iot.vmp.gb28181.bean.*;
+import com.genersoft.iot.vmp.media.zlm.dto.MediaItem;
 import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
+import com.genersoft.iot.vmp.media.zlm.dto.StreamPushItem;
 import com.genersoft.iot.vmp.service.bean.GPSMsgInfo;
+import com.genersoft.iot.vmp.service.bean.SSRCInfo;
 import com.genersoft.iot.vmp.service.bean.ThirdPartyGB;
 import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
 import com.genersoft.iot.vmp.storager.dao.DeviceChannelMapper;
@@ -14,6 +18,7 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.core.parameters.P;
 import org.springframework.stereotype.Component;
 import org.springframework.util.StringUtils;
 
@@ -50,8 +55,30 @@
     }
 
     @Override
+    public Long getSN(String method) {
+        String key = VideoManagerConstants.SIP_SN_PREFIX  + userSetup.getServerId() + "_" +  method;
+
+        long result =  redis.incr(key, 1L);
+        if (result > Integer.MAX_VALUE) {
+            redis.set(key, 1);
+            result = 1;
+        }
+        return result;
+    }
+
+    @Override
     public void resetAllCSEQ() {
         String scanKey = VideoManagerConstants.SIP_CSEQ_PREFIX  + userSetup.getServerId() + "_*";
+        List<Object> keys = redis.scan(scanKey);
+        for (int i = 0; i < keys.size(); i++) {
+            String key = (String) keys.get(i);
+            redis.set(key, 1);
+        }
+    }
+
+    @Override
+    public void resetAllSN() {
+        String scanKey = VideoManagerConstants.SIP_SN_PREFIX  + userSetup.getServerId() + "_*";
         List<Object> keys = redis.scan(scanKey);
         for (int i = 0; i < keys.size(); i++) {
             String key = (String) keys.get(i);
@@ -66,7 +93,8 @@
      */
     @Override
     public boolean startPlay(StreamInfo stream) {
-        return redis.set(String.format("%S_%S_%s_%s_%s", VideoManagerConstants.PLAYER_PREFIX, userSetup.getServerId(), stream.getStreamId(),stream.getDeviceID(), stream.getChannelId()),
+        return redis.set(String.format("%S_%S_%s_%s_%s", VideoManagerConstants.PLAYER_PREFIX, userSetup.getServerId(),
+                        stream.getStream(), stream.getDeviceID(), stream.getChannelId()),
                 stream);
     }
 
@@ -80,7 +108,7 @@
         if (streamInfo == null) return false;
         return redis.del(String.format("%S_%s_%s_%s_%s", VideoManagerConstants.PLAYER_PREFIX,
                 userSetup.getServerId(),
-                streamInfo.getStreamId(),
+                streamInfo.getStream(),
                 streamInfo.getDeviceID(),
                 streamInfo.getChannelId()));
     }
@@ -94,7 +122,7 @@
         return (StreamInfo)redis.get(String.format("%S_%s_%s_%s_%s",
                 VideoManagerConstants.PLAYER_PREFIX,
                 userSetup.getServerId(),
-                streamInfo.getStreamId(),
+                streamInfo.getStream(),
                 streamInfo.getDeviceID(),
                 streamInfo.getChannelId()));
     }
@@ -106,15 +134,7 @@
     }
 
     @Override
-    public StreamInfo queryPlaybackByStreamId(String streamId) {
-        List<Object> playLeys = redis.scan(String.format("%S_%s_%s_*", VideoManagerConstants.PLAY_BLACK_PREFIX, userSetup.getServerId(),  streamId));
-        if (playLeys == null || playLeys.size() == 0) return null;
-        return (StreamInfo)redis.get(playLeys.get(0).toString());
-    }
-
-    @Override
     public StreamInfo queryPlayByDevice(String deviceId, String channelId) {
-//		List<Object> playLeys = redis.keys(String.format("%S_*_%s_%s", VideoManagerConstants.PLAYER_PREFIX,
         List<Object> playLeys = redis.scan(String.format("%S_%s_*_%s_%s", VideoManagerConstants.PLAYER_PREFIX,
                 userSetup.getServerId(),
                 deviceId,
@@ -139,49 +159,67 @@
 
 
     @Override
-    public boolean startPlayback(StreamInfo stream) {
-        return redis.set(String.format("%S_%s_%s_%s_%s", VideoManagerConstants.PLAY_BLACK_PREFIX, userSetup.getServerId(),stream.getStreamId(),
-                        stream.getDeviceID(), stream.getChannelId()), stream);
+    public boolean startPlayback(StreamInfo stream, String callId) {
+        return redis.set(String.format("%S_%s_%s_%s_%s_%s", VideoManagerConstants.PLAY_BLACK_PREFIX,
+                userSetup.getServerId(), stream.getDeviceID(), stream.getChannelId(), stream.getStream(), callId), stream);
     }
 
     @Override
-    public boolean startDownload(StreamInfo streamInfo) {
-        return redis.set(String.format("%S_%s_%s_%s_%s", VideoManagerConstants.DOWNLOAD_PREFIX, userSetup.getServerId(),streamInfo.getStreamId(),
-                        streamInfo.getDeviceID(), streamInfo.getChannelId()), streamInfo);
+    public boolean startDownload(StreamInfo stream, String callId) {
+        return redis.set(String.format("%S_%s_%s_%s_%s_%s", VideoManagerConstants.DOWNLOAD_PREFIX,
+                userSetup.getServerId(), stream.getDeviceID(), stream.getChannelId(), stream.getStream(), callId), stream);
     }
 
     @Override
-    public boolean stopPlayback(StreamInfo streamInfo) {
-        if (streamInfo == null) return false;
-        DeviceChannel deviceChannel = deviceChannelMapper.queryChannel(streamInfo.getDeviceID(), streamInfo.getChannelId());
+    public boolean stopPlayback(String deviceId, String channelId, String stream, String callId) {
+        DeviceChannel deviceChannel = deviceChannelMapper.queryChannel(deviceId, channelId);
         if (deviceChannel != null) {
             deviceChannel.setStreamId(null);
-            deviceChannel.setDeviceId(streamInfo.getDeviceID());
+            deviceChannel.setDeviceId(deviceId);
             deviceChannelMapper.update(deviceChannel);
         }
-        return redis.del(String.format("%S_%s_%s_%s_%s", VideoManagerConstants.PLAY_BLACK_PREFIX,
+        if (deviceId == null) deviceId = "*";
+        if (channelId == null) channelId = "*";
+        if (stream == null) stream = "*";
+        if (callId == null) callId = "*";
+        String key = String.format("%S_%s_%s_%s_%s_%s", VideoManagerConstants.PLAY_BLACK_PREFIX,
                 userSetup.getServerId(),
-                streamInfo.getStreamId(),
-                streamInfo.getDeviceID(),
-                streamInfo.getChannelId()));
+                deviceId,
+                channelId,
+                stream,
+                callId
+        );
+        List<Object> scan = redis.scan(key);
+        if (scan.size() > 0) {
+            for (Object keyObj : scan) {
+                redis.del((String) keyObj);
+            }
+        }
+        return true;
     }
 
     @Override
-    public StreamInfo queryPlaybackByDevice(String deviceId, String code) {
-        // String format = String.format("%S_*_%s_%s", VideoManagerConstants.PLAY_BLACK_PREFIX,
-        //         deviceId,
-        //         code);
-        List<Object> playLeys = redis.scan(String.format("%S_%s_*_%s_%s", VideoManagerConstants.PLAY_BLACK_PREFIX,
+    public StreamInfo queryPlayback(String deviceId, String channelId, String stream, String callId) {
+        if (stream == null && callId == null) {
+            return null;
+        }
+        if (deviceId == null) deviceId = "*";
+        if (channelId == null) channelId = "*";
+        if (stream == null) stream = "*";
+        if (callId == null) callId = "*";
+        String key = String.format("%S_%s_%s_%s_%s_%s", VideoManagerConstants.PLAY_BLACK_PREFIX,
                 userSetup.getServerId(),
                 deviceId,
-                code));
-        if (playLeys == null || playLeys.size() == 0) {
-            playLeys = redis.scan(String.format("%S_%s_*_*_%s", VideoManagerConstants.PLAY_BLACK_PREFIX,
-                    userSetup.getServerId(),
-                    deviceId));
+                channelId,
+                stream,
+                callId
+        );
+        List<Object> streamInfoScan = redis.scan(key);
+        if (streamInfoScan.size() > 0) {
+            return (StreamInfo) redis.get((String) streamInfoScan.get(0));
+        }else {
+            return null;
         }
-        if (playLeys == null || playLeys.size() == 0) return null;
-        return (StreamInfo)redis.get(playLeys.get(0).toString());
     }
 
     @Override
@@ -226,7 +264,7 @@
     @Override
     public void updatePlatformRegisterInfo(String callId, String platformGbId) {
         String key = VideoManagerConstants.PLATFORM_REGISTER_INFO_PREFIX + userSetup.getServerId() + "_" + callId;
-        redis.set(key, platformGbId);
+        redis.set(key, platformGbId, 30);
     }
 
 
@@ -250,19 +288,32 @@
 
     @Override
     public void updateSendRTPSever(SendRtpItem sendRtpItem) {
-        String key = VideoManagerConstants.PLATFORM_SEND_RTP_INFO_PREFIX + userSetup.getServerId() + "_" + sendRtpItem.getPlatformId() + "_" + sendRtpItem.getChannelId();
+        String key = VideoManagerConstants.PLATFORM_SEND_RTP_INFO_PREFIX + userSetup.getServerId() + "_"
+                + sendRtpItem.getPlatformId() + "_" + sendRtpItem.getChannelId() + "_"
+                + sendRtpItem.getStreamId() + "_" + sendRtpItem.getCallId();
         redis.set(key, sendRtpItem);
     }
 
     @Override
-    public SendRtpItem querySendRTPServer(String platformGbId, String channelId) {
-        String key = VideoManagerConstants.PLATFORM_SEND_RTP_INFO_PREFIX + userSetup.getServerId() + "_" + platformGbId + "_" + channelId;
-        return (SendRtpItem)redis.get(key);
+    public SendRtpItem querySendRTPServer(String platformGbId, String channelId, String streamId, String callId) {
+        if (platformGbId == null) platformGbId = "*";
+        if (channelId == null) channelId = "*";
+        if (streamId == null) streamId = "*";
+        if (callId == null) callId = "*";
+        String key = VideoManagerConstants.PLATFORM_SEND_RTP_INFO_PREFIX + userSetup.getServerId() + "_" + platformGbId
+                + "_" + channelId + "_" + streamId + "_" + callId;
+        List<Object> scan = redis.scan(key);
+        if (scan.size() > 0) {
+            return (SendRtpItem)redis.get((String)scan.get(0));
+        }else {
+            return null;
+        }
     }
 
     @Override
     public List<SendRtpItem> querySendRTPServer(String platformGbId) {
-        String key = VideoManagerConstants.PLATFORM_SEND_RTP_INFO_PREFIX + userSetup.getServerId() + "_" + platformGbId + "_*";
+        if (platformGbId == null) platformGbId = "*";
+        String key = VideoManagerConstants.PLATFORM_SEND_RTP_INFO_PREFIX + userSetup.getServerId() + "_" + platformGbId + "_*" + "_*" + "_*";
         List<Object> queryResult = redis.scan(key);
         List<SendRtpItem> result= new ArrayList<>();
 
@@ -280,10 +331,20 @@
      * @param channelId
      */
     @Override
-    public void deleteSendRTPServer(String platformGbId, String channelId) {
-        String key = VideoManagerConstants.PLATFORM_SEND_RTP_INFO_PREFIX + userSetup.getServerId() + "_" + platformGbId + "_" + channelId;
-        redis.del(key);
+    public void deleteSendRTPServer(String platformGbId, String channelId, String callId, String streamId) {
+        if (streamId == null) streamId = "*";
+        if (callId == null) callId = "*";
+        String key = VideoManagerConstants.PLATFORM_SEND_RTP_INFO_PREFIX + userSetup.getServerId() + "_" + platformGbId
+                + "_" + channelId + "_" + streamId + "_" + callId;
+        List<Object> scan = redis.scan(key);
+        if (scan.size() > 0) {
+            for (Object keyStr : scan) {
+                redis.del((String)keyStr);
+            }
+        }
     }
+
+
 
     /**
      * 鏌ヨ鏌愪釜閫氶亾鏄惁瀛樺湪涓婄骇鐐规挱锛圧TP鎺ㄩ�侊級
@@ -291,7 +352,7 @@
      */
     @Override
     public boolean isChannelSendingRTP(String channelId) {
-        String key = VideoManagerConstants.PLATFORM_SEND_RTP_INFO_PREFIX + userSetup.getServerId() + "_" + "*_" + channelId;
+        String key = VideoManagerConstants.PLATFORM_SEND_RTP_INFO_PREFIX + userSetup.getServerId() + "_" + "*_" + channelId + "*_" + "*_";
         List<Object> RtpStreams = redis.scan(key);
         if (RtpStreams.size() > 0) {
             return true;
@@ -311,7 +372,7 @@
             }
         }
 
-        List<Object> playBackers = redis.scan(String.format("%S_%s_*_%s_*", VideoManagerConstants.PLAY_BLACK_PREFIX,
+        List<Object> playBackers = redis.scan(String.format("%S_%s_%s_*_*_*", VideoManagerConstants.PLAY_BLACK_PREFIX,
                 userSetup.getServerId(),
                 deviceId));
         if (playBackers.size() > 0) {
@@ -364,9 +425,9 @@
     }
 
     @Override
-    public void addStream(MediaServerItem mediaServerItem, String type, String app, String streamId, StreamInfo streamInfo) {
+    public void addStream(MediaServerItem mediaServerItem, String type, String app, String streamId, MediaItem mediaItem) {
         String key = VideoManagerConstants.WVP_SERVER_STREAM_PREFIX  + userSetup.getServerId() + "_" + type + "_" + app + "_" + streamId + "_" + mediaServerItem.getId();
-        redis.set(key, streamInfo);
+        redis.set(key, mediaItem);
     }
 
     @Override
@@ -376,10 +437,27 @@
     }
 
     @Override
-    public StreamInfo queryDownloadByStreamId(String streamId) {
-        List<Object> playLeys = redis.scan(String.format("%S_%s_%s_*", VideoManagerConstants.DOWNLOAD_PREFIX, userSetup.getServerId(), streamId));
-        if (playLeys == null || playLeys.size() == 0) return null;
-        return (StreamInfo)redis.get(playLeys.get(0).toString());
+    public StreamInfo queryDownload(String deviceId, String channelId, String stream, String callId) {
+        if (stream == null && callId == null) {
+            return null;
+        }
+        if (deviceId == null) deviceId = "*";
+        if (channelId == null) channelId = "*";
+        if (stream == null) stream = "*";
+        if (callId == null) callId = "*";
+        String key = String.format("%S_%s_%s_%s_%s_%s", VideoManagerConstants.DOWNLOAD_PREFIX,
+                userSetup.getServerId(),
+                deviceId,
+                channelId,
+                stream,
+                callId
+        );
+        List<Object> streamInfoScan = redis.scan(key);
+        if (streamInfoScan.size() > 0) {
+            return (StreamInfo) redis.get((String) streamInfoScan.get(0));
+        }else {
+            return null;
+        }
     }
 
     @Override
@@ -399,13 +477,13 @@
     }
 
     @Override
-    public List<StreamInfo> getStreams(String mediaServerId, String type) {
-        List<StreamInfo> result = new ArrayList<>();
+    public List<MediaItem> getStreams(String mediaServerId, String type) {
+        List<MediaItem> result = new ArrayList<>();
         String key = VideoManagerConstants.WVP_SERVER_STREAM_PREFIX + userSetup.getServerId() + "_" + type + "_*_*_" + mediaServerId;
         List<Object> streams = redis.scan(key);
         for (Object stream : streams) {
-            StreamInfo streamInfo = (StreamInfo)redis.get((String) stream);
-            result.add(streamInfo);
+            MediaItem mediaItem = (MediaItem)redis.get((String) stream);
+            result.add(mediaItem);
         }
         return result;
     }
@@ -431,6 +509,87 @@
     @Override
     public void updateGpsMsgInfo(GPSMsgInfo gpsMsgInfo) {
         String key = VideoManagerConstants.WVP_STREAM_GPS_MSG_PREFIX + userSetup.getServerId() + "_" + gpsMsgInfo.getId();
-        redis.set(key, gpsMsgInfo);
+        redis.set(key, gpsMsgInfo, 60); // 榛樿GPS娑堟伅淇濆瓨1鍒嗛挓
+    }
+
+    @Override
+    public GPSMsgInfo getGpsMsgInfo(String gbId) {
+        String key = VideoManagerConstants.WVP_STREAM_GPS_MSG_PREFIX + userSetup.getServerId() + "_" + gbId;
+        return (GPSMsgInfo)redis.get(key);
+    }
+
+    @Override
+    public List<GPSMsgInfo> getAllGpsMsgInfo() {
+        String scanKey = VideoManagerConstants.WVP_STREAM_GPS_MSG_PREFIX + userSetup.getServerId() + "_*";
+        List<GPSMsgInfo> result = new ArrayList<>();
+        List<Object> keys = redis.scan(scanKey);
+        for (int i = 0; i < keys.size(); i++) {
+            String key = (String) keys.get(i);
+            GPSMsgInfo gpsMsgInfo = (GPSMsgInfo) redis.get(key);
+            if (!gpsMsgInfo.isStored()) { // 鍙彇娌℃湁瀛樿繃寰�
+                result.add((GPSMsgInfo)redis.get(key));
+            }
+        }
+
+        return result;
+    }
+
+    @Override
+    public MediaItem getStreamInfo(String app, String streamId, String mediaServerId) {
+        String scanKey = VideoManagerConstants.WVP_SERVER_STREAM_PREFIX  + userSetup.getServerId() + "_*_" + app + "_" + streamId + "_" + mediaServerId;
+
+        MediaItem result = null;
+        List<Object> keys = redis.scan(scanKey);
+        if (keys.size() > 0) {
+            String key = (String) keys.get(0);
+            result = (MediaItem)redis.get(key);
+        }
+
+        return result;
+    }
+
+    @Override
+    public void addCpuInfo(double cpuInfo) {
+        String key = VideoManagerConstants.SYSTEM_INFO_CPU_PREFIX + userSetup.getServerId();
+        SystemInfoDto<Double> systemInfoDto = new SystemInfoDto<>();
+        systemInfoDto.setTime(format.format(System.currentTimeMillis()));
+        systemInfoDto.setData(cpuInfo);
+        redis.lSet(key, systemInfoDto);
+        // 姣忕涓�涓紝鏈�澶氬彧瀛�30涓�
+        if (redis.lGetListSize(key) > 30) {
+            for (int i = 0; i < redis.lGetListSize(key) - 30; i++) {
+                redis.lLeftPop(key);
+            }
+        }
+    }
+
+    @Override
+    public void addMemInfo(double memInfo) {
+        String key = VideoManagerConstants.SYSTEM_INFO_MEM_PREFIX + userSetup.getServerId();
+        SystemInfoDto<Double> systemInfoDto = new SystemInfoDto<>();
+        systemInfoDto.setTime(format.format(System.currentTimeMillis()));
+        systemInfoDto.setData(memInfo);
+        redis.lSet(key, systemInfoDto);
+        // 姣忕涓�涓紝鏈�澶氬彧瀛�30涓�
+        if (redis.lGetListSize(key) > 30) {
+            for (int i = 0; i < redis.lGetListSize(key) - 30; i++) {
+                redis.lLeftPop(key);
+            }
+        }
+    }
+
+    @Override
+    public void addNetInfo(Map<String, String> networkInterfaces) {
+        String key = VideoManagerConstants.SYSTEM_INFO_NET_PREFIX + userSetup.getServerId();
+        SystemInfoDto<Map<String, String>> systemInfoDto = new SystemInfoDto<>();
+        systemInfoDto.setTime(format.format(System.currentTimeMillis()));
+        systemInfoDto.setData(networkInterfaces);
+        redis.lSet(key, systemInfoDto);
+        // 姣忕涓�涓紝鏈�澶氬彧瀛�30涓�
+        if (redis.lGetListSize(key) > 30) {
+            for (int i = 0; i < redis.lGetListSize(key) - 30; i++) {
+                redis.lLeftPop(key);
+            }
+        }
     }
 }

--
Gitblit v1.8.0