From 6968839f21fb05bf6e3204c2040ee47130006cd0 Mon Sep 17 00:00:00 2001
From: panlinlin <648540858@qq.com>
Date: 星期日, 27 十二月 2020 08:19:34 +0800
Subject: [PATCH] 修复空指针异常

---
 src/main/java/com/genersoft/iot/vmp/storager/redis/VideoManagerRedisStoragerImpl.java |   93 ++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 76 insertions(+), 17 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/storager/redis/VideoManagerRedisStoragerImpl.java b/src/main/java/com/genersoft/iot/vmp/storager/redis/VideoManagerRedisStoragerImpl.java
index 10f3815..00d41fc 100644
--- a/src/main/java/com/genersoft/iot/vmp/storager/redis/VideoManagerRedisStoragerImpl.java
+++ b/src/main/java/com/genersoft/iot/vmp/storager/redis/VideoManagerRedisStoragerImpl.java
@@ -101,7 +101,7 @@
 
 		// 濡傛灉鏈夌埗璁惧,鏇存柊鐖惰澶囧唴瀛愯妭鐐规暟
 		String parentId = channel.getParentId();
-		if (!StringUtils.isEmpty(parentId)) {
+		if (!StringUtils.isEmpty(parentId) && !parentId.equals(deviceId)) {
 
 			if (channelMap.get(parentId) == null) {
 				channelMap.put(parentId, new HashSet<String>());
@@ -111,8 +111,6 @@
 			DeviceChannel deviceChannel = queryChannel(deviceId, parentId);
 			if (deviceChannel != null) {
 				deviceChannel.setSubCount(channelMap.get(parentId).size());
-//				redis.set(VideoManagerConstants.CACHEKEY_PREFIX+deviceId + "_" + deviceChannel.getChannelId(),
-//						deviceChannel);
 				redis.set(VideoManagerConstants.CACHEKEY_PREFIX + deviceId +
 								"_" + deviceChannel.getChannelId() +
 								"_" + (deviceChannel.getStatus() == 1 ? "on":"off") +
@@ -153,7 +151,6 @@
 				"_" + queryOnline + // 鎼滅储鏄惁鍦ㄧ嚎
 				"_" + queryHasSubChannel + // 鎼滅储鏄惁鍚湁瀛愯妭鐐�
 				"_" + "*";
-//		List<Object> deviceChannelList = redis.keys(queryStr);
 		List<Object> deviceChannelList = redis.scan(queryStr);
 		//瀵规煡璇㈢粨鏋滄帓搴忥紝閬垮厤鍑虹幇閫氶亾鎺掑垪椤哄簭涔卞簭鐨勬儏鍐�
 		Collections.sort(deviceChannelList,new Comparator<Object>(){
@@ -171,7 +168,7 @@
 				DeviceChannel deviceChannel = (DeviceChannel)redis.get((String)deviceChannelList.get(i));
 				StreamInfo streamInfo = stringStreamInfoMap.get(deviceId + "_" + deviceChannel.getChannelId());
 				deviceChannel.setPlay(streamInfo != null);
-				if (streamInfo != null) deviceChannel.setSsrc(streamInfo.getSsrc());
+				if (streamInfo != null) deviceChannel.setStreamId(streamInfo.getStreamId());
 				result.add(deviceChannel);
 			}
 			pageResult.setData(result);
@@ -278,6 +275,7 @@
 		PageResult pageResult = new PageResult<Device>();
 		pageResult.setPage(page);
 		pageResult.setCount(count);
+		Device device = null;
 
 		if (deviceIds == null || deviceIds.length == 0) {
 
@@ -286,11 +284,21 @@
 			pageResult.setTotal(deviceIdList.size());
 			int maxCount = (page + 1)* count;
 			for (int i = page * count; i < (pageResult.getTotal() > maxCount ? maxCount : pageResult.getTotal() ); i++) {
-				devices.add((Device)redis.get((String)deviceIdList.get(i)));
+				// devices.add((Device)redis.get((String)deviceIdList.get(i)));
+				device =(Device)redis.get((String)deviceIdList.get(i));
+				if (redis.scan(VideoManagerConstants.KEEPLIVEKEY_PREFIX+device.getDeviceId()).size() == 0){
+					// outline(device.getDeviceId());
+				}
+				devices.add(device);
 			}
 		} else {
 			for (int i = 0; i < deviceIds.length; i++) {
-				devices.add((Device)redis.get(VideoManagerConstants.DEVICE_PREFIX+deviceIds[i]));
+				// devices.add((Device)redis.get(VideoManagerConstants.DEVICE_PREFIX+deviceIds[i]));
+				device = (Device)redis.get(VideoManagerConstants.DEVICE_PREFIX+deviceIds[i]);
+				if (redis.scan(VideoManagerConstants.KEEPLIVEKEY_PREFIX+device.getDeviceId()).size() == 0){
+					// outline(device.getDeviceId());
+				}
+				devices.add(device);
 			}
 		}
 		pageResult.setData(devices);
@@ -306,16 +314,25 @@
 	@Override
 	public List<Device> queryVideoDeviceList(String[] deviceIds) {
 		List<Device> devices = new ArrayList<>();
+		Device device = null;
 
 		if (deviceIds == null || deviceIds.length == 0) {
 //			List<Object> deviceIdList = redis.keys(VideoManagerConstants.DEVICE_PREFIX+"*");
 			List<Object> deviceIdList = redis.scan(VideoManagerConstants.DEVICE_PREFIX+"*");
 			for (int i = 0; i < deviceIdList.size(); i++) {
-				devices.add((Device)redis.get((String)deviceIdList.get(i)));
+				device =(Device)redis.get((String)deviceIdList.get(i));
+				if (redis.scan(VideoManagerConstants.KEEPLIVEKEY_PREFIX+device.getDeviceId()).size() == 0){
+					outline(device.getDeviceId());
+				}
+				devices.add(device);
 			}
 		} else {
 			for (int i = 0; i < deviceIds.length; i++) {
-				devices.add((Device)redis.get(VideoManagerConstants.DEVICE_PREFIX+deviceIds[i]));
+				device = (Device)redis.get(VideoManagerConstants.DEVICE_PREFIX+deviceIds[i]);
+				if (redis.scan(VideoManagerConstants.KEEPLIVEKEY_PREFIX+device.getDeviceId()).size() == 0){
+					outline(device.getDeviceId());
+				}
+				devices.add(device);
 			}
 		}
 		return devices;
@@ -366,7 +383,7 @@
 	 */
 	@Override
 	public boolean startPlay(StreamInfo stream) {
-		return redis.set(String.format("%S_%s_%s_%s", VideoManagerConstants.PLAYER_PREFIX, stream.getSsrc(),stream.getDeviceID(), stream.getCahnnelId()),
+		return redis.set(String.format("%S_%s_%s_%s", VideoManagerConstants.PLAYER_PREFIX, stream.getStreamId(),stream.getDeviceID(), stream.getCahnnelId()),
 				stream);
 	}
 
@@ -380,12 +397,12 @@
 		if (streamInfo == null) return false;
 		DeviceChannel deviceChannel = queryChannel(streamInfo.getDeviceID(), streamInfo.getCahnnelId());
 		if (deviceChannel != null) {
-			deviceChannel.setSsrc(null);
+			deviceChannel.setStreamId(null);
 			deviceChannel.setPlay(false);
 			updateChannel(streamInfo.getDeviceID(), deviceChannel);
 		}
 		return redis.del(String.format("%S_%s_%s_%s", VideoManagerConstants.PLAYER_PREFIX,
-				streamInfo.getSsrc(),
+				streamInfo.getStreamId(),
 				streamInfo.getDeviceID(),
 				streamInfo.getCahnnelId()));
 	}
@@ -398,14 +415,20 @@
 	public StreamInfo queryPlay(StreamInfo streamInfo) {
 		return (StreamInfo)redis.get(String.format("%S_%s_%s_%s",
 				VideoManagerConstants.PLAYER_PREFIX,
-				streamInfo.getSsrc(),
+				streamInfo.getStreamId(),
 				streamInfo.getDeviceID(),
 				streamInfo.getCahnnelId()));
 	}
 	@Override
-	public StreamInfo queryPlayBySSRC(String ssrc) {
-//		List<Object> playLeys = redis.keys(String.format("%S_%s_*", VideoManagerConstants.PLAYER_PREFIX, ssrc));
-		List<Object> playLeys = redis.scan(String.format("%S_%s_*", VideoManagerConstants.PLAYER_PREFIX, ssrc));
+	public StreamInfo queryPlayByStreamId(String steamId) {
+		List<Object> playLeys = redis.scan(String.format("%S_%s_*", VideoManagerConstants.PLAYER_PREFIX, steamId));
+		if (playLeys == null || playLeys.size() == 0) return null;
+		return (StreamInfo)redis.get(playLeys.get(0).toString());
+	}
+
+	@Override
+	public StreamInfo queryPlaybackByStreamId(String steamId) {
+		List<Object> playLeys = redis.scan(String.format("%S_%s_*", VideoManagerConstants.PLAY_BLACK_PREFIX, steamId));
 		if (playLeys == null || playLeys.size() == 0) return null;
 		return (StreamInfo)redis.get(playLeys.get(0).toString());
 	}
@@ -448,7 +471,6 @@
 		for (Device device : devices) {
 			// 鏇存柊璁惧涓嬬殑閫氶亾
 			HashMap<String, HashSet<String>> channelMap = new HashMap<String, HashSet<String>>();
-//			List<Object> deviceChannelList = redis.keys(VideoManagerConstants.CACHEKEY_PREFIX +
 			List<Object> deviceChannelList = redis.scan(VideoManagerConstants.CACHEKEY_PREFIX +
 					device.getDeviceId() + "_" + "*");
 			if (deviceChannelList != null && deviceChannelList.size() > 0 ) {
@@ -469,6 +491,7 @@
 			}
 			deviceMap.put(device.getDeviceId(),channelMap);
 		}
+		System.out.println();
 	}
 
 	@Override
@@ -498,5 +521,41 @@
 	}
 
 
+	@Override
+	public boolean startPlayback(StreamInfo stream) {
+		return redis.set(String.format("%S_%s_%s_%s", VideoManagerConstants.PLAY_BLACK_PREFIX, stream.getStreamId(),stream.getDeviceID(), stream.getCahnnelId()),
+				stream);
+	}
 
+
+	@Override
+	public boolean stopPlayback(StreamInfo streamInfo) {
+		if (streamInfo == null) return false;
+		DeviceChannel deviceChannel = queryChannel(streamInfo.getDeviceID(), streamInfo.getCahnnelId());
+		if (deviceChannel != null) {
+			deviceChannel.setStreamId(null);
+			deviceChannel.setPlay(false);
+			updateChannel(streamInfo.getDeviceID(), deviceChannel);
+		}
+		return redis.del(String.format("%S_%s_%s_%s", VideoManagerConstants.PLAY_BLACK_PREFIX,
+				streamInfo.getStreamId(),
+				streamInfo.getDeviceID(),
+				streamInfo.getCahnnelId()));
+	}
+
+	@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", VideoManagerConstants.PLAY_BLACK_PREFIX,
+				deviceId,
+				code));
+		if (playLeys == null || playLeys.size() == 0) {
+			playLeys = redis.scan(String.format("%S_*_*_%s", VideoManagerConstants.PLAY_BLACK_PREFIX,
+				deviceId));
+		}
+		if (playLeys == null || playLeys.size() == 0) return null;
+		return (StreamInfo)redis.get(playLeys.get(0).toString());
+	}
 }

--
Gitblit v1.8.0