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 4 import com.genersoft.iot.vmp.conf.UserSetting;
5 5 import com.genersoft.iot.vmp.gb28181.bean.SipTransactionInfo;
6 6 import com.genersoft.iot.vmp.gb28181.bean.SsrcTransaction;
  7 +import com.genersoft.iot.vmp.utils.JsonUtil;
7 8 import com.genersoft.iot.vmp.utils.redis.RedisUtil;
8 9 import gov.nist.javax.sip.message.SIPResponse;
9 10 import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -133,7 +134,7 @@ public class VideoStreamSessionManager {
133 134 List<SsrcTransaction> result= new ArrayList<>();
134 135 for (int i = 0; i < ssrcTransactionKeys.size(); i++) {
135 136 String key = (String)ssrcTransactionKeys.get(i);
136   - SsrcTransaction ssrcTransaction = (SsrcTransaction)RedisUtil.get(key);
  137 + SsrcTransaction ssrcTransaction = JsonUtil.redisJsonToObject(key, SsrcTransaction.class);
137 138 result.add(ssrcTransaction);
138 139 }
139 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 22 import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
23 23 import com.genersoft.iot.vmp.storager.dao.MediaServerMapper;
24 24 import com.genersoft.iot.vmp.utils.DateUtil;
  25 +import com.genersoft.iot.vmp.utils.JsonUtil;
25 26 import com.genersoft.iot.vmp.utils.redis.RedisUtil;
26 27 import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
27 28 import okhttp3.OkHttpClient;
... ... @@ -228,11 +229,10 @@ public class MediaServerServiceImpl implements IMediaServerService {
228 229 String onlineKey = VideoManagerConstants.MEDIA_SERVERS_ONLINE_PREFIX + userSetting.getServerId();
229 230 for (Object mediaServerKey : mediaServerKeys) {
230 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 234 continue;
234 235 }
235   - MediaServerItem mediaServerItem = JSON.parseObject(jsonObject.toJSONString(), MediaServerItem.class);
236 236 // 检查状态
237 237 Double aDouble = RedisUtil.zScore(onlineKey, mediaServerItem.getId());
238 238 if (aDouble != null) {
... ... @@ -284,7 +284,7 @@ public class MediaServerServiceImpl implements IMediaServerService {
284 284 return null;
285 285 }
286 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 290 @Override
... ... @@ -400,8 +400,10 @@ public class MediaServerServiceImpl implements IMediaServerService {
400 400 SsrcConfig ssrcConfig = new SsrcConfig(zlmServerConfig.getGeneralMediaServerId(), null, sipConfig.getDomain());
401 401 serverItem.setSsrcConfig(ssrcConfig);
402 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 408 RedisUtil.set(key, serverItem);
407 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 17 import com.genersoft.iot.vmp.storager.dao.DeviceChannelMapper;
18 18 import com.genersoft.iot.vmp.storager.dao.dto.PlatformRegisterInfo;
19 19 import com.genersoft.iot.vmp.utils.DateUtil;
  20 +import com.genersoft.iot.vmp.utils.JsonUtil;
20 21 import com.genersoft.iot.vmp.utils.SystemInfoUtils;
21 22 import com.genersoft.iot.vmp.utils.redis.RedisUtil;
22 23 import org.slf4j.Logger;
... ... @@ -157,7 +158,10 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage {
157 158 }
158 159 for (Object player : players) {
159 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 165 streamInfos.put(streamInfo.getDeviceID() + "_" + streamInfo.getChannelId(), streamInfo);
162 166 }
163 167 return streamInfos;
... ... @@ -624,8 +628,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage {
624 628 @Override
625 629 public ThirdPartyGB queryMemberNoGBId(String queryKey) {
626 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 634 @Override
... ... @@ -664,7 +667,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage {
664 667 @Override
665 668 public Device getDevice(String deviceId) {
666 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 673 @Override
... ... @@ -676,7 +679,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage {
676 679 @Override
677 680 public GPSMsgInfo getGpsMsgInfo(String gbId) {
678 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 685 @Override
... ... @@ -686,9 +689,9 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage {
686 689 List<Object> keys = RedisUtil.scan(scanKey);
687 690 for (Object o : keys) {
688 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 713 @Override
711 714 public StreamAuthorityInfo getStreamAuthorityInfo(String app, String stream) {
712 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 724 List<Object> keys = RedisUtil.scan(scanKey);
722 725 for (Object o : keys) {
723 726 String key = (String) o;
724   - result.add((StreamAuthorityInfo) RedisUtil.get(key));
  727 + result.add(JsonUtil.redisJsonToObject(key, StreamAuthorityInfo.class));
725 728 }
726 729 return result;
727 730 }
... ... @@ -735,7 +738,7 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage {
735 738 List<Object> keys = RedisUtil.scan(scanKey);
736 739 if (keys.size() > 0) {
737 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 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 38 \ No newline at end of file
... ...