| | |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.data.redis.core.StringRedisTemplate; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.time.Duration; |
| | | import java.util.*; |
| | | |
| | | @SuppressWarnings("rawtypes") |
| | |
| | | |
| | | @Autowired |
| | | private RedisTemplate<Object, Object> redisTemplate; |
| | | |
| | | @Autowired |
| | | private StringRedisTemplate stringRedisTemplate; |
| | | |
| | | @Override |
| | | public Long getCSEQ() { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 开始播放时将流存入redis |
| | | */ |
| | | @Override |
| | | public boolean startPlay(StreamInfo stream) { |
| | | |
| | | redisTemplate.opsForValue().set(String.format("%S_%s_%s_%s_%s_%s", VideoManagerConstants.PLAYER_PREFIX, userSetting.getServerId(), |
| | | stream.getMediaServerId(), stream.getStream(), stream.getDeviceID(), stream.getChannelId()), |
| | | stream); |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 停止播放时从redis删除 |
| | | */ |
| | | @Override |
| | | public boolean stopPlay(StreamInfo streamInfo) { |
| | | if (streamInfo == null) { |
| | | return false; |
| | | } |
| | | Boolean result = redisTemplate.delete(String.format("%S_%s_%s_%s_%s_%s", VideoManagerConstants.PLAYER_PREFIX, |
| | | userSetting.getServerId(), |
| | | streamInfo.getMediaServerId(), |
| | | streamInfo.getStream(), |
| | | streamInfo.getDeviceID(), |
| | | streamInfo.getChannelId())); |
| | | return result != null && result; |
| | | } |
| | | |
| | | /** |
| | | * 查询播放列表 |
| | | */ |
| | | @Override |
| | | public StreamInfo queryPlay(StreamInfo streamInfo) { |
| | | return (StreamInfo)redisTemplate.opsForValue().get(String.format("%S_%s_%s_%s_%s_%s", |
| | | VideoManagerConstants.PLAYER_PREFIX, |
| | | userSetting.getServerId(), |
| | | streamInfo.getMediaServerId(), |
| | | streamInfo.getStream(), |
| | | streamInfo.getDeviceID(), |
| | | streamInfo.getChannelId())); |
| | | } |
| | | @Override |
| | | public StreamInfo queryPlayByStreamId(String streamId) { |
| | | List<Object> playLeys = RedisUtil.scan(redisTemplate, String.format("%S_%s_*_%s_*", VideoManagerConstants.PLAYER_PREFIX, userSetting.getServerId(), streamId)); |
| | | if (playLeys.size() == 0) { |
| | | return null; |
| | | } |
| | | return (StreamInfo)redisTemplate.opsForValue().get(playLeys.get(0).toString()); |
| | | } |
| | | |
| | | @Override |
| | | public StreamInfo queryPlayByDevice(String deviceId, String channelId) { |
| | | List<Object> playLeys = RedisUtil.scan(redisTemplate, String.format("%S_%s_*_*_%s_%s", VideoManagerConstants.PLAYER_PREFIX, |
| | | userSetting.getServerId(), |
| | | deviceId, |
| | | channelId)); |
| | | if (playLeys.size() == 0) { |
| | | return null; |
| | | } |
| | | return (StreamInfo)redisTemplate.opsForValue().get(playLeys.get(0).toString()); |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, StreamInfo> queryPlayByDeviceId(String deviceId) { |
| | | Map<String, StreamInfo> streamInfos = new HashMap<>(); |
| | | List<Object> players = RedisUtil.scan(redisTemplate, String.format("%S_%s_*_*_%s_*", VideoManagerConstants.PLAYER_PREFIX, userSetting.getServerId(),deviceId)); |
| | | if (players.size() == 0) { |
| | | return streamInfos; |
| | | } |
| | | for (Object player : players) { |
| | | String key = (String) player; |
| | | StreamInfo streamInfo = JsonUtil.redisJsonToObject(redisTemplate, key, StreamInfo.class); |
| | | if (Objects.isNull(streamInfo)) { |
| | | continue; |
| | | } |
| | | streamInfos.put(streamInfo.getDeviceID() + "_" + streamInfo.getChannelId(), streamInfo); |
| | | } |
| | | return streamInfos; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public boolean startPlayback(StreamInfo stream, String callId) { |
| | |
| | | redisTemplate.opsForValue().set(key, stream); |
| | | }else { |
| | | logger.debug("添加下载缓存==未完成下载=》{}",key); |
| | | redisTemplate.opsForValue().set(key, stream, 60*60); |
| | | Duration duration = Duration.ofSeconds(60*60L); |
| | | redisTemplate.opsForValue().set(key, stream, duration); |
| | | } |
| | | return true; |
| | | } |
| | |
| | | @Override |
| | | public void updatePlatformRegisterInfo(String callId, PlatformRegisterInfo platformRegisterInfo) { |
| | | String key = VideoManagerConstants.PLATFORM_REGISTER_INFO_PREFIX + userSetting.getServerId() + "_" + callId; |
| | | redisTemplate.opsForValue().set(key, platformRegisterInfo, 30); |
| | | Duration duration = Duration.ofSeconds(30L); |
| | | redisTemplate.opsForValue().set(key, platformRegisterInfo, duration); |
| | | } |
| | | |
| | | |
| | |
| | | @Override |
| | | public void updateWVPInfo(JSONObject jsonObject, int time) { |
| | | String key = VideoManagerConstants.WVP_SERVER_PREFIX + userSetting.getServerId(); |
| | | redisTemplate.opsForValue().set(key, jsonObject, time); |
| | | Duration duration = Duration.ofSeconds(time); |
| | | redisTemplate.opsForValue().set(key, jsonObject, duration); |
| | | } |
| | | |
| | | @Override |
| | |
| | | @Override |
| | | public void updateGpsMsgInfo(GPSMsgInfo gpsMsgInfo) { |
| | | String key = VideoManagerConstants.WVP_STREAM_GPS_MSG_PREFIX + userSetting.getServerId() + "_" + gpsMsgInfo.getId(); |
| | | redisTemplate.opsForValue().set(key, gpsMsgInfo, 60); // 默认GPS消息保存1分钟 |
| | | Duration duration = Duration.ofSeconds(60L); |
| | | redisTemplate.opsForValue().set(key, gpsMsgInfo, duration); // 默认GPS消息保存1分钟 |
| | | } |
| | | |
| | | @Override |
| | |
| | | @Override |
| | | public void sendDeviceOrChannelStatus(String deviceId, String channelId, boolean online) { |
| | | String key = VideoManagerConstants.VM_MSG_SUBSCRIBE_DEVICE_STATUS; |
| | | logger.info("[redis通知] 推送设备/通道状态, {}/{}-{}", deviceId, channelId, online); |
| | | if (channelId == null) { |
| | | logger.info("[redis通知] 推送设备状态, {}-{}", deviceId, online); |
| | | }else { |
| | | logger.info("[redis通知] 推送通道状态, {}/{}-{}", deviceId, channelId, online); |
| | | } |
| | | |
| | | StringBuilder msg = new StringBuilder(); |
| | | msg.append(deviceId); |
| | | if (channelId != null) { |
| | | msg.append(":").append(channelId); |
| | | } |
| | | msg.append(" ").append(online? "ON":"OFF"); |
| | | |
| | | redisTemplate.convertAndSend(key, msg.toString()); |
| | | // 使用 RedisTemplate<Object, Object> 发送字符串消息会导致发送的消息多带了双引号 |
| | | stringRedisTemplate.convertAndSend(key, msg.toString()); |
| | | } |
| | | } |