Commit b89e46871ab8560d23c77a778663e48b50966707

Authored by kunlong-luo
1 parent 192f0128

fix: 修复一些问题

1. 修复空指针异常
2. 修复类型转换异常
3. 封装 JsonUtil 工具类支持类型转换
src/main/java/com/genersoft/iot/vmp/gb28181/session/VideoStreamSessionManager.java
@@ -4,6 +4,7 @@ import com.genersoft.iot.vmp.common.VideoManagerConstants; @@ -4,6 +4,7 @@ import com.genersoft.iot.vmp.common.VideoManagerConstants;
4 import com.genersoft.iot.vmp.conf.UserSetting; 4 import com.genersoft.iot.vmp.conf.UserSetting;
5 import com.genersoft.iot.vmp.gb28181.bean.SipTransactionInfo; 5 import com.genersoft.iot.vmp.gb28181.bean.SipTransactionInfo;
6 import com.genersoft.iot.vmp.gb28181.bean.SsrcTransaction; 6 import com.genersoft.iot.vmp.gb28181.bean.SsrcTransaction;
  7 +import com.genersoft.iot.vmp.utils.JsonUtil;
7 import com.genersoft.iot.vmp.utils.redis.RedisUtil; 8 import com.genersoft.iot.vmp.utils.redis.RedisUtil;
8 import gov.nist.javax.sip.message.SIPResponse; 9 import gov.nist.javax.sip.message.SIPResponse;
9 import org.springframework.beans.factory.annotation.Autowired; 10 import org.springframework.beans.factory.annotation.Autowired;
@@ -133,7 +134,7 @@ public class VideoStreamSessionManager { @@ -133,7 +134,7 @@ public class VideoStreamSessionManager {
133 List<SsrcTransaction> result= new ArrayList<>(); 134 List<SsrcTransaction> result= new ArrayList<>();
134 for (int i = 0; i < ssrcTransactionKeys.size(); i++) { 135 for (int i = 0; i < ssrcTransactionKeys.size(); i++) {
135 String key = (String)ssrcTransactionKeys.get(i); 136 String key = (String)ssrcTransactionKeys.get(i);
136 - SsrcTransaction ssrcTransaction = (SsrcTransaction)RedisUtil.get(key); 137 + SsrcTransaction ssrcTransaction = JsonUtil.redisJsonToObject(key, SsrcTransaction.class);
137 result.add(ssrcTransaction); 138 result.add(ssrcTransaction);
138 } 139 }
139 return result; 140 return result;
src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java
@@ -22,6 +22,7 @@ import com.genersoft.iot.vmp.service.bean.SSRCInfo; @@ -22,6 +22,7 @@ import com.genersoft.iot.vmp.service.bean.SSRCInfo;
22 import com.genersoft.iot.vmp.storager.IRedisCatchStorage; 22 import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
23 import com.genersoft.iot.vmp.storager.dao.MediaServerMapper; 23 import com.genersoft.iot.vmp.storager.dao.MediaServerMapper;
24 import com.genersoft.iot.vmp.utils.DateUtil; 24 import com.genersoft.iot.vmp.utils.DateUtil;
  25 +import com.genersoft.iot.vmp.utils.JsonUtil;
25 import com.genersoft.iot.vmp.utils.redis.RedisUtil; 26 import com.genersoft.iot.vmp.utils.redis.RedisUtil;
26 import com.genersoft.iot.vmp.vmanager.bean.ErrorCode; 27 import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
27 import okhttp3.OkHttpClient; 28 import okhttp3.OkHttpClient;
@@ -228,11 +229,10 @@ public class MediaServerServiceImpl implements IMediaServerService { @@ -228,11 +229,10 @@ public class MediaServerServiceImpl implements IMediaServerService {
228 String onlineKey = VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId(); 229 String onlineKey = VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId();
229 for (Object mediaServerKey : mediaServerKeys) { 230 for (Object mediaServerKey : mediaServerKeys) {
230 String key = (String) mediaServerKey; 231 String key = (String) mediaServerKey;
231 - JSONObject jsonObject = (JSONObject) RedisUtil.get(key);  
232 - if (Objects.isNull(jsonObject)) { 232 + MediaServerItem mediaServerItem = JsonUtil.redisJsonToObject(key, MediaServerItem.class);
  233 + if (Objects.isNull(mediaServerItem)) {
233 continue; 234 continue;
234 } 235 }
235 - MediaServerItem mediaServerItem = JSON.parseObject(jsonObject.toJSONString(), MediaServerItem.class);  
236 // 检查状态 236 // 检查状态
237 Double aDouble = RedisUtil.zScore(onlineKey, mediaServerItem.getId()); 237 Double aDouble = RedisUtil.zScore(onlineKey, mediaServerItem.getId());
238 if (aDouble != null) { 238 if (aDouble != null) {
@@ -284,7 +284,7 @@ public class MediaServerServiceImpl implements IMediaServerService { @@ -284,7 +284,7 @@ public class MediaServerServiceImpl implements IMediaServerService {
284 return null; 284 return null;
285 } 285 }
286 String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerId; 286 String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerId;
287 - return (MediaServerItem)RedisUtil.get(key); 287 + return JsonUtil.redisJsonToObject(key, MediaServerItem.class);
288 } 288 }
289 289
290 @Override 290 @Override
@@ -400,8 +400,10 @@ public class MediaServerServiceImpl implements IMediaServerService { @@ -400,8 +400,10 @@ public class MediaServerServiceImpl implements IMediaServerService {
400 SsrcConfig ssrcConfig = new SsrcConfig(zlmServerConfig.getGeneralMediaServerId(), null, sipConfig.getDomain()); 400 SsrcConfig ssrcConfig = new SsrcConfig(zlmServerConfig.getGeneralMediaServerId(), null, sipConfig.getDomain());
401 serverItem.setSsrcConfig(ssrcConfig); 401 serverItem.setSsrcConfig(ssrcConfig);
402 }else { 402 }else {
403 - MediaServerItem mediaServerItemInRedis = (MediaServerItem)RedisUtil.get(key);  
404 - serverItem.setSsrcConfig(mediaServerItemInRedis.getSsrcConfig()); 403 + MediaServerItem mediaServerItemInRedis = JsonUtil.redisJsonToObject(key, MediaServerItem.class);
  404 + if (Objects.nonNull(mediaServerItemInRedis)) {
  405 + serverItem.setSsrcConfig(mediaServerItemInRedis.getSsrcConfig());
  406 + }
405 } 407 }
406 RedisUtil.set(key, serverItem); 408 RedisUtil.set(key, serverItem);
407 resetOnlineServerItem(serverItem); 409 resetOnlineServerItem(serverItem);
src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java
@@ -17,6 +17,7 @@ import com.genersoft.iot.vmp.storager.IRedisCatchStorage; @@ -17,6 +17,7 @@ import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
17 import com.genersoft.iot.vmp.storager.dao.DeviceChannelMapper; 17 import com.genersoft.iot.vmp.storager.dao.DeviceChannelMapper;
18 import com.genersoft.iot.vmp.storager.dao.dto.PlatformRegisterInfo; 18 import com.genersoft.iot.vmp.storager.dao.dto.PlatformRegisterInfo;
19 import com.genersoft.iot.vmp.utils.DateUtil; 19 import com.genersoft.iot.vmp.utils.DateUtil;
  20 +import com.genersoft.iot.vmp.utils.JsonUtil;
20 import com.genersoft.iot.vmp.utils.SystemInfoUtils; 21 import com.genersoft.iot.vmp.utils.SystemInfoUtils;
21 import com.genersoft.iot.vmp.utils.redis.RedisUtil; 22 import com.genersoft.iot.vmp.utils.redis.RedisUtil;
22 import org.slf4j.Logger; 23 import org.slf4j.Logger;
@@ -157,7 +158,10 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { @@ -157,7 +158,10 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage {
157 } 158 }
158 for (Object player : players) { 159 for (Object player : players) {
159 String key = (String) player; 160 String key = (String) player;
160 - StreamInfo streamInfo = (StreamInfo) RedisUtil.get(key); 161 + StreamInfo streamInfo = JsonUtil.redisJsonToObject(key, StreamInfo.class);
  162 + if (Objects.isNull(streamInfo)) {
  163 + continue;
  164 + }
161 streamInfos.put(streamInfo.getDeviceID() + "_" + streamInfo.getChannelId(), streamInfo); 165 streamInfos.put(streamInfo.getDeviceID() + "_" + streamInfo.getChannelId(), streamInfo);
162 } 166 }
163 return streamInfos; 167 return streamInfos;
@@ -624,8 +628,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { @@ -624,8 +628,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage {
624 @Override 628 @Override
625 public ThirdPartyGB queryMemberNoGBId(String queryKey) { 629 public ThirdPartyGB queryMemberNoGBId(String queryKey) {
626 String key = VideoManagerConstants.WVP_STREAM_GB_ID_PREFIX + queryKey; 630 String key = VideoManagerConstants.WVP_STREAM_GB_ID_PREFIX + queryKey;
627 - JSONObject jsonObject = (JSONObject)RedisUtil.get(key);  
628 - return jsonObject.to(ThirdPartyGB.class); 631 + return JsonUtil.redisJsonToObject(key, ThirdPartyGB.class);
629 } 632 }
630 633
631 @Override 634 @Override
@@ -664,7 +667,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { @@ -664,7 +667,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage {
664 @Override 667 @Override
665 public Device getDevice(String deviceId) { 668 public Device getDevice(String deviceId) {
666 String key = VideoManagerConstants.DEVICE_PREFIX + userSetting.getServerId() + "_" + deviceId; 669 String key = VideoManagerConstants.DEVICE_PREFIX + userSetting.getServerId() + "_" + deviceId;
667 - return (Device)RedisUtil.get(key); 670 + return JsonUtil.redisJsonToObject(key, Device.class);
668 } 671 }
669 672
670 @Override 673 @Override
@@ -676,7 +679,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { @@ -676,7 +679,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage {
676 @Override 679 @Override
677 public GPSMsgInfo getGpsMsgInfo(String gbId) { 680 public GPSMsgInfo getGpsMsgInfo(String gbId) {
678 String key = VideoManagerConstants.WVP_STREAM_GPS_MSG_PREFIX + userSetting.getServerId() + "_" + gbId; 681 String key = VideoManagerConstants.WVP_STREAM_GPS_MSG_PREFIX + userSetting.getServerId() + "_" + gbId;
679 - return (GPSMsgInfo)RedisUtil.get(key); 682 + return JsonUtil.redisJsonToObject(key, GPSMsgInfo.class);
680 } 683 }
681 684
682 @Override 685 @Override
@@ -686,9 +689,9 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { @@ -686,9 +689,9 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage {
686 List<Object> keys = RedisUtil.scan(scanKey); 689 List<Object> keys = RedisUtil.scan(scanKey);
687 for (Object o : keys) { 690 for (Object o : keys) {
688 String key = (String) o; 691 String key = (String) o;
689 - GPSMsgInfo gpsMsgInfo = (GPSMsgInfo) RedisUtil.get(key);  
690 - if (!gpsMsgInfo.isStored()) { // 只取没有存过得  
691 - result.add((GPSMsgInfo) RedisUtil.get(key)); 692 + GPSMsgInfo gpsMsgInfo = JsonUtil.redisJsonToObject(key, GPSMsgInfo.class);
  693 + if (Objects.nonNull(gpsMsgInfo) && !gpsMsgInfo.isStored()) { // 只取没有存过得
  694 + result.add(JsonUtil.redisJsonToObject(key, GPSMsgInfo.class));
692 } 695 }
693 } 696 }
694 697
@@ -710,7 +713,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { @@ -710,7 +713,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage {
710 @Override 713 @Override
711 public StreamAuthorityInfo getStreamAuthorityInfo(String app, String stream) { 714 public StreamAuthorityInfo getStreamAuthorityInfo(String app, String stream) {
712 String key = VideoManagerConstants.MEDIA_STREAM_AUTHORITY + userSetting.getServerId() + "_" + app+ "_" + stream ; 715 String key = VideoManagerConstants.MEDIA_STREAM_AUTHORITY + userSetting.getServerId() + "_" + app+ "_" + stream ;
713 - return (StreamAuthorityInfo) RedisUtil.get(key); 716 + return JsonUtil.redisJsonToObject(key, StreamAuthorityInfo.class);
714 717
715 } 718 }
716 719
@@ -721,7 +724,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { @@ -721,7 +724,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage {
721 List<Object> keys = RedisUtil.scan(scanKey); 724 List<Object> keys = RedisUtil.scan(scanKey);
722 for (Object o : keys) { 725 for (Object o : keys) {
723 String key = (String) o; 726 String key = (String) o;
724 - result.add((StreamAuthorityInfo) RedisUtil.get(key)); 727 + result.add(JsonUtil.redisJsonToObject(key, StreamAuthorityInfo.class));
725 } 728 }
726 return result; 729 return result;
727 } 730 }
@@ -735,7 +738,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { @@ -735,7 +738,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage {
735 List<Object> keys = RedisUtil.scan(scanKey); 738 List<Object> keys = RedisUtil.scan(scanKey);
736 if (keys.size() > 0) { 739 if (keys.size() > 0) {
737 String key = (String) keys.get(0); 740 String key = (String) keys.get(0);
738 - result = (OnStreamChangedHookParam)RedisUtil.get(key); 741 + result = JsonUtil.redisJsonToObject(key, OnStreamChangedHookParam.class);
739 } 742 }
740 743
741 return result; 744 return result;
src/main/java/com/genersoft/iot/vmp/utils/JsonUtil.java 0 → 100644
  1 +package com.genersoft.iot.vmp.utils;
  2 +
  3 +import com.alibaba.fastjson2.JSON;
  4 +import com.alibaba.fastjson2.JSONObject;
  5 +import com.genersoft.iot.vmp.utils.redis.RedisUtil;
  6 +
  7 +import java.util.Objects;
  8 +
  9 +/**
  10 + * JsonUtil
  11 + *
  12 + * @author KunLong-Luo
  13 + * @version 1.0.0
  14 + * @since 2023/2/2 15:24
  15 + */
  16 +public final class JsonUtil {
  17 +
  18 + private JsonUtil() {
  19 + }
  20 +
  21 + /**
  22 + * safe json type conversion
  23 + *
  24 + * @param key redis key
  25 + * @param clazz cast type
  26 + * @param <T>
  27 + * @return result type
  28 + */
  29 + public static <T> T redisJsonToObject(String key, Class<T> clazz) {
  30 + JSONObject jsonObject = (JSONObject) RedisUtil.get(key);
  31 + if (Objects.isNull(jsonObject)) {
  32 + return null;
  33 + }
  34 + return JSON.parseObject(jsonObject.toJSONString(), clazz);
  35 + }
  36 +
  37 +}
0 \ No newline at end of file 38 \ No newline at end of file