648540858
2020-11-13 33b51c40ddd9750a19b2c6a6618fb22386f87cd7
src/main/java/com/genersoft/iot/vmp/storager/redis/VideoManagerRedisStoragerImpl.java
@@ -66,7 +66,6 @@
      if (deviceMap.get(device.getDeviceId()) == null) {
         deviceMap.put(device.getDeviceId(), new HashMap<String, HashSet<String>>());
      }
//      List<Object> deviceChannelList = redis.keys(VideoManagerConstants.CACHEKEY_PREFIX + device.getDeviceId() + "_" + "*");
      // 更新device中的通道数量
      device.setChannelCount(deviceMap.get(device.getDeviceId()).size());
      // 存储device
@@ -90,10 +89,9 @@
      // 存储通道
      redis.set(VideoManagerConstants.CACHEKEY_PREFIX + deviceId +
                  "_" + channel.getChannelId() +
                  ":" + channel.getName() +
                  "_" + (channel.getStatus() == 1 ? "on":"off") +
                  "_" + (channelMap.get(channelId).size() > 0)+
                  "_" + channel.getParentId(),
                  "_" + (StringUtils.isEmpty(channel.getParentId())?null:channel.getParentId()),
            channel);
      // 更新device中的通道数量
      Device device = (Device)redis.get(VideoManagerConstants.DEVICE_PREFIX+deviceId);
@@ -103,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>());
@@ -113,7 +111,11 @@
         DeviceChannel deviceChannel = queryChannel(deviceId, parentId);
         if (deviceChannel != null) {
            deviceChannel.setSubCount(channelMap.get(parentId).size());
            redis.set(VideoManagerConstants.CACHEKEY_PREFIX+deviceId + "_" + deviceChannel.getChannelId(),
            redis.set(VideoManagerConstants.CACHEKEY_PREFIX + deviceId +
                        "_" + deviceChannel.getChannelId() +
                        "_" + (deviceChannel.getStatus() == 1 ? "on":"off") +
                        "_" + (channelMap.get(deviceChannel.getChannelId()).size() > 0)+
                        "_" + (StringUtils.isEmpty(deviceChannel.getParentId())?null:deviceChannel.getParentId()),
                  deviceChannel);
         }
@@ -134,6 +136,8 @@
   @Override
   public PageResult queryChannelsByDeviceId(String deviceId, String query, Boolean hasSubChannel, String online, int page, int count) {
      // 获取到所有正在播放的流
      Map<String, StreamInfo> stringStreamInfoMap = queryPlayByDeviceId(deviceId);
      List<DeviceChannel> result = new ArrayList<>();
      PageResult pageResult = new PageResult<DeviceChannel>();
      String queryContent = "*";
@@ -147,14 +151,26 @@
            "_" + queryOnline + // 搜索是否在线
            "_" + queryHasSubChannel + // 搜索是否含有子节点
            "_" + "*";
      List<Object> deviceChannelList = redis.keys(queryStr);
//      List<Object> deviceChannelList = redis.keys(queryStr);
      List<Object> deviceChannelList = redis.scan(queryStr);
      //对查询结果排序,避免出现通道排列顺序乱序的情况
      Collections.sort(deviceChannelList,new Comparator<Object>(){
         @Override
         public int compare(Object o1, Object o2) {
            return o1.toString().compareToIgnoreCase(o2.toString());
         }
      });
      pageResult.setPage(page);
      pageResult.setCount(count);
      pageResult.setTotal(deviceChannelList.size());
      int maxCount = (page + 1 ) * count;
      if (deviceChannelList != null && deviceChannelList.size() > 0 ) {
         for (int i = page * count; i < (pageResult.getTotal() > maxCount ? maxCount : pageResult.getTotal() ); i++) {
            result.add((DeviceChannel)redis.get((String)deviceChannelList.get(i)));
            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());
            result.add(deviceChannel);
         }
         pageResult.setData(result);
      }
@@ -162,13 +178,17 @@
      return pageResult;
   }
   @Override
   public List<DeviceChannel> queryChannelsByDeviceId(String deviceId) {
      List<DeviceChannel> result = new ArrayList<>();
      List<Object> deviceChannelList = redis.keys(VideoManagerConstants.CACHEKEY_PREFIX + deviceId + "_" + "*");
//      List<Object> deviceChannelList = redis.keys(VideoManagerConstants.CACHEKEY_PREFIX + deviceId + "_" + "*");
      List<Object> deviceChannelList = redis.scan(VideoManagerConstants.CACHEKEY_PREFIX + deviceId + "_" + "*");
      if (deviceChannelList != null && deviceChannelList.size() > 0 ) {
         for (int i = 0; i < deviceChannelList.size(); i++) {
            result.add((DeviceChannel)redis.get((String)deviceChannelList.get(i)));
            result.add((DeviceChannel)redis.get((String) deviceChannelList.get(i)));
         }
      }
      return result;
@@ -189,7 +209,8 @@
            "_" + queryHasSubChannel + // 搜索是否含有子节点
            "_" + parentChannelId;
      List<Object> deviceChannelList = redis.keys(queryStr);
//      List<Object> deviceChannelList = redis.keys(queryStr);
      List<Object> deviceChannelList = redis.scan(queryStr);
      if (deviceChannelList != null && deviceChannelList.size() > 0 ) {
         for (int i = 0; i < deviceChannelList.size(); i++) {
@@ -215,7 +236,8 @@
   public List<DeviceChannel> querySubChannels(String deviceId, String parentChannelId) {
      List<DeviceChannel> allDeviceChannels = new ArrayList<>();
      List<Object> deviceChannelList = redis.keys(VideoManagerConstants.CACHEKEY_PREFIX + deviceId + "_" + "*");
//      List<Object> deviceChannelList = redis.keys(VideoManagerConstants.CACHEKEY_PREFIX + deviceId + "_" + "*");
      List<Object> deviceChannelList = redis.scan(VideoManagerConstants.CACHEKEY_PREFIX + deviceId + "_" + "*");
      if (deviceChannelList != null && deviceChannelList.size() > 0 ) {
         for (int i = 0; i < deviceChannelList.size(); i++) {
@@ -231,7 +253,14 @@
   @Override
   public DeviceChannel queryChannel(String deviceId, String channelId) {
      return (DeviceChannel)redis.get(VideoManagerConstants.CACHEKEY_PREFIX + deviceId + "_" + channelId + "_");
      DeviceChannel deviceChannel = null;
//      List<Object> deviceChannelList = redis.keys(VideoManagerConstants.CACHEKEY_PREFIX + deviceId +
      List<Object> deviceChannelList = redis.scan(VideoManagerConstants.CACHEKEY_PREFIX + deviceId +
            "_" + channelId  + "*");
      if (deviceChannelList != null && deviceChannelList.size() > 0 ) {
         deviceChannel = (DeviceChannel)redis.get((String)deviceChannelList.get(0));
      }
      return deviceChannel;
   }
@@ -247,18 +276,30 @@
      PageResult pageResult = new PageResult<Device>();
      pageResult.setPage(page);
      pageResult.setCount(count);
      Device device = null;
      if (deviceIds == null || deviceIds.length == 0) {
         List<Object> deviceIdList = redis.keys(VideoManagerConstants.DEVICE_PREFIX+"*");
//         List<Object> deviceIdList = redis.keys(VideoManagerConstants.DEVICE_PREFIX+"*");
         List<Object> deviceIdList = redis.scan(VideoManagerConstants.DEVICE_PREFIX+"*");
         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);
@@ -274,15 +315,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.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;
@@ -329,40 +380,71 @@
   /**
    * 开始播放时将流存入redis
    *
    * @param deviceId 设备ID
    * @param channelId 通道ID
    * @return
    */
   @Override
   public boolean startPlay(String deviceId, String channelId, StreamInfo stream) {
      return redis.set(String.format("%S_%s_%s", VideoManagerConstants.PLAYER_PREFIX, deviceId, channelId),
   public boolean startPlay(StreamInfo stream) {
      return redis.set(String.format("%S_%s_%s_%s", VideoManagerConstants.PLAYER_PREFIX, stream.getSsrc(),stream.getDeviceID(), stream.getCahnnelId()),
            stream);
   }
   /**
    * 停止播放时从redis删除
    *
    * @param deviceId 设备ID
    * @param channelId 通道ID
    * @return
    */
   @Override
   public boolean stopPlay(String deviceId, String channelId) {
      return redis.del(String.format("%S_%s_%s", VideoManagerConstants.PLAYER_PREFIX, deviceId, channelId));
   public boolean stopPlay(StreamInfo streamInfo) {
      if (streamInfo == null) return false;
      DeviceChannel deviceChannel = queryChannel(streamInfo.getDeviceID(), streamInfo.getCahnnelId());
      if (deviceChannel != null) {
         deviceChannel.setSsrc(null);
         deviceChannel.setPlay(false);
         updateChannel(streamInfo.getDeviceID(), deviceChannel);
      }
      return redis.del(String.format("%S_%s_%s_%s", VideoManagerConstants.PLAYER_PREFIX,
            streamInfo.getSsrc(),
            streamInfo.getDeviceID(),
            streamInfo.getCahnnelId()));
   }
   /**
    * 查询播放列表
    * @param deviceId 设备ID
    * @param channelId 通道ID
    * @return
    */
   @Override
   public StreamInfo queryPlay(String deviceId, String channelId) {
      return (StreamInfo)redis.get(String.format("%S_%s_%s", VideoManagerConstants.PLAYER_PREFIX, deviceId, channelId));
   public StreamInfo queryPlay(StreamInfo streamInfo) {
      return (StreamInfo)redis.get(String.format("%S_%s_%s_%s",
            VideoManagerConstants.PLAYER_PREFIX,
            streamInfo.getSsrc(),
            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));
      if (playLeys == null || playLeys.size() == 0) return null;
      return (StreamInfo)redis.get(playLeys.get(0).toString());
   }
   @Override
   public StreamInfo queryPlaybackBySSRC(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.PLAY_BLACK_PREFIX, ssrc));
      if (playLeys == null || playLeys.size() == 0) return null;
      return (StreamInfo)redis.get(playLeys.get(0).toString());
   }
   @Override
   public StreamInfo queryPlayByDevice(String deviceId, String code) {
//      List<Object> playLeys = redis.keys(String.format("%S_*_%s_%s", VideoManagerConstants.PLAYER_PREFIX,
      List<Object> playLeys = redis.scan(String.format("%S_*_%s_%s", VideoManagerConstants.PLAYER_PREFIX,
            deviceId,
            code));
      if (playLeys == null || playLeys.size() == 0) return null;
      return (StreamInfo)redis.get(playLeys.get(0).toString());
   }
   /**
    * 更新流媒体信息
@@ -392,35 +474,87 @@
      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 ) {
            for (int i = 0; i < deviceChannelList.size(); i++) {
               String key = (String)deviceChannelList.get(i);
               String[] s = key.split("_");
               String channelId = s[3].split(":")[0];
               String channelId = s[3];
               HashSet<String> subChannel = channelMap.get(channelId);
               if (subChannel == null) {
                  subChannel = new HashSet<>();
               }
               if (!"null".equals(s[6])) {
                  subChannel.add(s[6]);
               System.out.println(key);
               if (s.length == 6 && !"null".equals(s[5])) {
                  subChannel.add(s[5]);
               }
               channelMap.put(channelId, subChannel);
            }
         }
         deviceMap.put(device.getDeviceId(),channelMap);
      }
      System.out.println();
   }
   @Override
   public void cleanChannelsForDevice(String deviceId) {
      List<DeviceChannel> result = new ArrayList<>();
      List<Object> deviceChannelList = redis.keys(VideoManagerConstants.CACHEKEY_PREFIX + deviceId + "_" + "*");
//      List<Object> deviceChannelList = redis.keys(VideoManagerConstants.CACHEKEY_PREFIX + deviceId + "_" + "*");
      List<Object> deviceChannelList = redis.scan(VideoManagerConstants.CACHEKEY_PREFIX + deviceId + "_" + "*");
      if (deviceChannelList != null && deviceChannelList.size() > 0 ) {
         for (int i = 0; i < deviceChannelList.size(); i++) {
            redis.del((String)deviceChannelList.get(i));
         }
      }
   }
   @Override
   public Map<String, StreamInfo> queryPlayByDeviceId(String deviceId) {
      Map<String, StreamInfo> streamInfos = new HashMap<>();
//      List<Object> playLeys = redis.keys(String.format("%S_*_%S_*", VideoManagerConstants.PLAYER_PREFIX, deviceId));
      List<Object> playLeys = redis.scan(String.format("%S_*_%S_*", VideoManagerConstants.PLAYER_PREFIX, deviceId));
      if (playLeys.size() == 0) return streamInfos;
      for (int i = 0; i < playLeys.size(); i++) {
         String key = (String) playLeys.get(i);
         StreamInfo streamInfo = (StreamInfo)redis.get(key);
         streamInfos.put(streamInfo.getDeviceID() + "_" + streamInfo.getCahnnelId(), streamInfo);
      }
      return streamInfos;
   }
   @Override
   public boolean startPlayback(StreamInfo stream) {
      return redis.set(String.format("%S_%s_%s_%s", VideoManagerConstants.PLAY_BLACK_PREFIX, stream.getSsrc(),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.setSsrc(null);
         deviceChannel.setPlay(false);
         updateChannel(streamInfo.getDeviceID(), deviceChannel);
      }
      return redis.del(String.format("%S_%s_%s_%s", VideoManagerConstants.PLAY_BLACK_PREFIX,
            streamInfo.getSsrc(),
            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) return null;
      return (StreamInfo)redis.get(playLeys.get(0).toString());
   }
}