648540858
2020-10-15 b15115565ceaeada7c700099e06b37f1ba5ebadd
src/main/java/com/genersoft/iot/vmp/storager/redis/VideoManagerRedisStoragerImpl.java
@@ -93,7 +93,7 @@
                  ":" + 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);
@@ -113,7 +113,14 @@
         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);
            redis.set(VideoManagerConstants.CACHEKEY_PREFIX + deviceId +
                        "_" + deviceChannel.getChannelId() +
                        ":" + deviceChannel.getName() +
                        "_" + (deviceChannel.getStatus() == 1 ? "on":"off") +
                        "_" + (channelMap.get(deviceChannel.getChannelId()).size() > 0)+
                        "_" + (StringUtils.isEmpty(deviceChannel.getParentId())?null:deviceChannel.getParentId()),
                  deviceChannel);
         }
@@ -134,6 +141,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 = "*";
@@ -154,13 +163,19 @@
      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);
      }
      return pageResult;
   }
   @Override
   public List<DeviceChannel> queryChannelsByDeviceId(String deviceId) {
@@ -231,7 +246,13 @@
   @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 +
            "_" + channelId  + "*");
      if (deviceChannelList != null && deviceChannelList.size() > 0 ) {
         deviceChannel = (DeviceChannel)redis.get((String)deviceChannelList.get(0));
      }
      return deviceChannel;
   }
@@ -329,40 +350,61 @@
   /**
    * 开始播放时将流存入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));
      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,
            deviceId,
            code));
      if (playLeys == null || playLeys.size() == 0) return null;
      return (StreamInfo)redis.get(playLeys.get(0).toString());
   }
   /**
    * 更新流媒体信息
@@ -403,7 +445,8 @@
               if (subChannel == null) {
                  subChannel = new HashSet<>();
               }
               if (!"null".equals(s[6])) {
               System.out.println(key);
               if (s.length == 7 && !"null".equals(s[6])) {
                  subChannel.add(s[6]);
               }
               channelMap.put(channelId, subChannel);
@@ -423,4 +466,20 @@
         }
      }
   }
   @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));
      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;
   }
}