panlinlin
4 years ago
30 changed files with 932 additions and 955 deletions
@ -0,0 +1,58 @@ |
|||
package com.genersoft.iot.vmp.storager; |
|||
|
|||
import com.genersoft.iot.vmp.common.StreamInfo; |
|||
import com.genersoft.iot.vmp.conf.MediaServerConfig; |
|||
|
|||
import java.util.Map; |
|||
|
|||
public interface IRedisCatchStorage { |
|||
|
|||
/** |
|||
* 开始播放时将流存入 |
|||
* |
|||
* @param stream 流信息 |
|||
* @return |
|||
*/ |
|||
boolean startPlay(StreamInfo stream); |
|||
|
|||
|
|||
/** |
|||
* 停止播放时删除 |
|||
* |
|||
* @return |
|||
*/ |
|||
boolean stopPlay(StreamInfo streamInfo); |
|||
|
|||
/** |
|||
* 查询播放列表 |
|||
* @return |
|||
*/ |
|||
StreamInfo queryPlay(StreamInfo streamInfo); |
|||
|
|||
StreamInfo queryPlayByStreamId(String steamId); |
|||
|
|||
StreamInfo queryPlaybackByStreamId(String steamId); |
|||
|
|||
StreamInfo queryPlayByDevice(String deviceId, String code); |
|||
|
|||
/** |
|||
* 更新流媒体信息 |
|||
* @param mediaServerConfig |
|||
* @return |
|||
*/ |
|||
boolean updateMediaInfo(MediaServerConfig mediaServerConfig); |
|||
|
|||
/** |
|||
* 获取流媒体信息 |
|||
* @return |
|||
*/ |
|||
MediaServerConfig getMediaInfo(); |
|||
|
|||
Map<String, StreamInfo> queryPlayByDeviceId(String deviceId); |
|||
|
|||
boolean startPlayback(StreamInfo stream); |
|||
|
|||
boolean stopPlayback(StreamInfo streamInfo); |
|||
|
|||
StreamInfo queryPlaybackByDevice(String deviceId, String code); |
|||
} |
@ -1,36 +0,0 @@ |
|||
package com.genersoft.iot.vmp.storager; |
|||
|
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.context.annotation.Bean; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import com.genersoft.iot.vmp.conf.VManagerConfig; |
|||
|
|||
/** |
|||
* @Description:视频设备数据存储工厂,根据存储策略,返回对应的存储器 |
|||
* @author: swwheihei |
|||
* @date: 2020年5月6日 下午2:15:16 |
|||
*/ |
|||
@Component |
|||
public class VideoManagerStoragerFactory { |
|||
|
|||
@Autowired |
|||
private VManagerConfig vmConfig; |
|||
|
|||
@Autowired |
|||
private IVideoManagerStorager jdbcStorager; |
|||
|
|||
@Autowired |
|||
private IVideoManagerStorager redisStorager; |
|||
|
|||
@Bean("storager") |
|||
public IVideoManagerStorager getStorager() { |
|||
if ("redis".equals(vmConfig.getDatabase().toLowerCase())) { |
|||
return redisStorager; |
|||
} else if ("jdbc".equals(vmConfig.getDatabase().toLowerCase())) { |
|||
return jdbcStorager; |
|||
} |
|||
return redisStorager; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,20 @@ |
|||
package com.genersoft.iot.vmp.storager.dao; |
|||
|
|||
import com.genersoft.iot.vmp.common.PageResult; |
|||
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel; |
|||
import org.apache.ibatis.annotations.Mapper; |
|||
|
|||
import java.util.List; |
|||
|
|||
@Mapper |
|||
public interface DeviceChannelMapper { |
|||
int update(DeviceChannel channel); |
|||
|
|||
List<DeviceChannel> queryChannelsByDeviceId(String deviceId); |
|||
|
|||
List<DeviceChannel> queryChannelsByDeviceId(String deviceId, String parentChannelId); |
|||
|
|||
DeviceChannel queryChannel(String deviceId, String channelId); |
|||
|
|||
int cleanChannelsByDeviceId(String deviceId); |
|||
} |
@ -0,0 +1,24 @@ |
|||
package com.genersoft.iot.vmp.storager.dao; |
|||
|
|||
import com.genersoft.iot.vmp.gb28181.bean.Device; |
|||
import org.apache.ibatis.annotations.Insert; |
|||
import org.apache.ibatis.annotations.Mapper; |
|||
import org.apache.ibatis.annotations.Select; |
|||
|
|||
import java.util.List; |
|||
|
|||
@Mapper |
|||
public interface DeviceMapper { |
|||
|
|||
@Select("SELECT * FROM device WHERE deviceId = #{deviceId}") |
|||
Device getDeviceByDeviceId(String deviceId); |
|||
|
|||
@Insert("SELECT * FROM device WHERE deviceId = #{deviceId}") |
|||
int add(Device device); |
|||
|
|||
int update(Device device); |
|||
|
|||
List<Device> getDevices(); |
|||
|
|||
int del(String deviceId); |
|||
} |
@ -0,0 +1,172 @@ |
|||
package com.genersoft.iot.vmp.storager.impl; |
|||
|
|||
import com.genersoft.iot.vmp.common.StreamInfo; |
|||
import com.genersoft.iot.vmp.common.VideoManagerConstants; |
|||
import com.genersoft.iot.vmp.conf.MediaServerConfig; |
|||
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel; |
|||
import com.genersoft.iot.vmp.storager.IRedisCatchStorage; |
|||
import com.genersoft.iot.vmp.storager.dao.DeviceChannelMapper; |
|||
import com.genersoft.iot.vmp.storager.dao.DeviceMapper; |
|||
import com.genersoft.iot.vmp.utils.redis.RedisUtil; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.HashSet; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
@Component |
|||
public class RedisCatchStorageImpl implements IRedisCatchStorage { |
|||
|
|||
@Autowired |
|||
private RedisUtil redis; |
|||
|
|||
@Autowired |
|||
private DeviceMapper deviceMapper; |
|||
|
|||
@Autowired |
|||
private DeviceChannelMapper deviceChannelMapper; |
|||
|
|||
|
|||
/** |
|||
* 开始播放时将流存入redis |
|||
* |
|||
* @return |
|||
*/ |
|||
@Override |
|||
public boolean startPlay(StreamInfo stream) { |
|||
return redis.set(String.format("%S_%s_%s_%s", VideoManagerConstants.PLAYER_PREFIX, stream.getStreamId(),stream.getDeviceID(), stream.getCahnnelId()), |
|||
stream); |
|||
} |
|||
|
|||
/** |
|||
* 停止播放时从redis删除 |
|||
* |
|||
* @return |
|||
*/ |
|||
@Override |
|||
public boolean stopPlay(StreamInfo streamInfo) { |
|||
if (streamInfo == null) return false; |
|||
DeviceChannel deviceChannel = deviceChannelMapper.queryChannel(streamInfo.getDeviceID(), streamInfo.getCahnnelId()); |
|||
if (deviceChannel != null) { |
|||
deviceChannel.setStreamId(null); |
|||
deviceChannel.setPlay(false); |
|||
deviceChannel.setDeviceId(streamInfo.getDeviceID()); |
|||
deviceChannelMapper.update(deviceChannel); |
|||
} |
|||
return redis.del(String.format("%S_%s_%s_%s", VideoManagerConstants.PLAYER_PREFIX, |
|||
streamInfo.getStreamId(), |
|||
streamInfo.getDeviceID(), |
|||
streamInfo.getCahnnelId())); |
|||
} |
|||
|
|||
/** |
|||
* 查询播放列表 |
|||
* @return |
|||
*/ |
|||
@Override |
|||
public StreamInfo queryPlay(StreamInfo streamInfo) { |
|||
return (StreamInfo)redis.get(String.format("%S_%s_%s_%s", |
|||
VideoManagerConstants.PLAYER_PREFIX, |
|||
streamInfo.getStreamId(), |
|||
streamInfo.getDeviceID(), |
|||
streamInfo.getCahnnelId())); |
|||
} |
|||
@Override |
|||
public StreamInfo queryPlayByStreamId(String steamId) { |
|||
List<Object> playLeys = redis.scan(String.format("%S_%s_*", VideoManagerConstants.PLAYER_PREFIX, steamId)); |
|||
if (playLeys == null || playLeys.size() == 0) return null; |
|||
return (StreamInfo)redis.get(playLeys.get(0).toString()); |
|||
} |
|||
|
|||
@Override |
|||
public StreamInfo queryPlaybackByStreamId(String steamId) { |
|||
List<Object> playLeys = redis.scan(String.format("%S_%s_*", VideoManagerConstants.PLAY_BLACK_PREFIX, steamId)); |
|||
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()); |
|||
} |
|||
|
|||
/** |
|||
* 更新流媒体信息 |
|||
* @param mediaServerConfig |
|||
* @return |
|||
*/ |
|||
@Override |
|||
public boolean updateMediaInfo(MediaServerConfig mediaServerConfig) { |
|||
return redis.set(VideoManagerConstants.MEDIA_SERVER_PREFIX,mediaServerConfig); |
|||
} |
|||
|
|||
/** |
|||
* 获取流媒体信息 |
|||
* @return |
|||
*/ |
|||
@Override |
|||
public MediaServerConfig getMediaInfo() { |
|||
return (MediaServerConfig)redis.get(VideoManagerConstants.MEDIA_SERVER_PREFIX); |
|||
} |
|||
|
|||
@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> players = redis.scan(String.format("%S_*_%S_*", VideoManagerConstants.PLAYER_PREFIX, deviceId)); |
|||
if (players.size() == 0) return streamInfos; |
|||
for (int i = 0; i < players.size(); i++) { |
|||
String key = (String) players.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.getStreamId(),stream.getDeviceID(), stream.getCahnnelId()), |
|||
stream); |
|||
} |
|||
|
|||
|
|||
@Override |
|||
public boolean stopPlayback(StreamInfo streamInfo) { |
|||
if (streamInfo == null) return false; |
|||
DeviceChannel deviceChannel = deviceChannelMapper.queryChannel(streamInfo.getDeviceID(), streamInfo.getCahnnelId()); |
|||
if (deviceChannel != null) { |
|||
deviceChannel.setStreamId(null); |
|||
deviceChannel.setPlay(false); |
|||
deviceChannel.setDeviceId(streamInfo.getDeviceID()); |
|||
deviceChannelMapper.update(deviceChannel); |
|||
} |
|||
return redis.del(String.format("%S_%s_%s_%s", VideoManagerConstants.PLAY_BLACK_PREFIX, |
|||
streamInfo.getStreamId(), |
|||
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) { |
|||
playLeys = redis.scan(String.format("%S_*_*_%s", VideoManagerConstants.PLAY_BLACK_PREFIX, |
|||
deviceId)); |
|||
} |
|||
if (playLeys == null || playLeys.size() == 0) return null; |
|||
return (StreamInfo)redis.get(playLeys.get(0).toString()); |
|||
} |
|||
} |
@ -0,0 +1,401 @@ |
|||
package com.genersoft.iot.vmp.storager.impl; |
|||
|
|||
import java.util.*; |
|||
|
|||
import com.genersoft.iot.vmp.common.PageResult; |
|||
import com.genersoft.iot.vmp.common.StreamInfo; |
|||
import com.genersoft.iot.vmp.conf.MediaServerConfig; |
|||
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel; |
|||
import com.genersoft.iot.vmp.storager.dao.DeviceChannelMapper; |
|||
import com.genersoft.iot.vmp.storager.dao.DeviceMapper; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import com.genersoft.iot.vmp.common.VideoManagerConstants; |
|||
import com.genersoft.iot.vmp.gb28181.bean.Device; |
|||
import com.genersoft.iot.vmp.storager.IVideoManagerStorager; |
|||
import com.genersoft.iot.vmp.utils.redis.RedisUtil; |
|||
import org.springframework.util.StringUtils; |
|||
|
|||
/** |
|||
* @Description:视频设备数据存储-jdbc实现 |
|||
* @author: swwheihei |
|||
* @date: 2020年5月6日 下午2:31:42 |
|||
*/ |
|||
@Component |
|||
public class VideoManagerStoragerImpl implements IVideoManagerStorager { |
|||
|
|||
@Autowired |
|||
private DeviceMapper deviceMapper; |
|||
|
|||
@Autowired |
|||
private DeviceChannelMapper deviceChannelMapper; |
|||
|
|||
|
|||
/** |
|||
* 根据设备ID判断设备是否存在 |
|||
* |
|||
* @param deviceId 设备ID |
|||
* @return true:存在 false:不存在 |
|||
*/ |
|||
@Override |
|||
public boolean exists(String deviceId) { |
|||
return deviceMapper.getDeviceByDeviceId(deviceId) != null; |
|||
} |
|||
|
|||
/** |
|||
* 视频设备创建 |
|||
* |
|||
* @param device 设备对象 |
|||
* @return true:创建成功 false:创建失败 |
|||
*/ |
|||
@Override |
|||
public boolean create(Device device) { |
|||
return deviceMapper.add(device) > 0; |
|||
} |
|||
|
|||
|
|||
|
|||
/** |
|||
* 视频设备更新 |
|||
* |
|||
* @param device 设备对象 |
|||
* @return true:更新成功 false:更新失败 |
|||
*/ |
|||
@Override |
|||
public boolean updateDevice(Device device) { |
|||
// if (deviceMap.get(device.getDeviceId()) == null) {
|
|||
// deviceMap.put(device.getDeviceId(), new HashMap<String, HashSet<String>>());
|
|||
// }
|
|||
// 更新device中的通道数量
|
|||
// device.setChannelCount(deviceMap.get(device.getDeviceId()).size());
|
|||
int result = deviceMapper.update(device); |
|||
// 存储device
|
|||
return result > 0; |
|||
|
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void updateChannel(String deviceId, DeviceChannel channel) { |
|||
String channelId = channel.getChannelId(); |
|||
channel.setDeviceId(deviceId); |
|||
deviceChannelMapper.update(channel); |
|||
|
|||
// HashMap<String, HashSet<String>> channelMap = deviceMap.get(deviceId);
|
|||
// if (channelMap == null) return;
|
|||
// // 作为父设备, 确定自己的子节点数
|
|||
// if (channelMap.get(channelId) == null) {
|
|||
// channelMap.put(channelId, new HashSet<String>());
|
|||
// }else if (channelMap.get(channelId).size() > 0) {
|
|||
// channel.setSubCount(channelMap.get(channelId).size());
|
|||
// }
|
|||
//
|
|||
// // 存储通道
|
|||
// redis.set(VideoManagerConstants.CACHEKEY_PREFIX + deviceId +
|
|||
// "_" + channel.getChannelId() +
|
|||
// "_" + (channel.getStatus() == 1 ? "on":"off") +
|
|||
// "_" + (channelMap.get(channelId).size() > 0)+
|
|||
// "_" + (StringUtils.isEmpty(channel.getParentId())?null:channel.getParentId()),
|
|||
// channel);
|
|||
// // 更新device中的通道数量
|
|||
// Device device = (Device)redis.get(VideoManagerConstants.DEVICE_PREFIX+deviceId);
|
|||
// device.setChannelCount(deviceMap.get(deviceId).size());
|
|||
// redis.set(VideoManagerConstants.DEVICE_PREFIX+device.getDeviceId(), device);
|
|||
//
|
|||
//
|
|||
// // 如果有父设备,更新父设备内子节点数
|
|||
// String parentId = channel.getParentId();
|
|||
// if (!StringUtils.isEmpty(parentId) && !parentId.equals(deviceId)) {
|
|||
//
|
|||
// if (channelMap.get(parentId) == null) {
|
|||
// channelMap.put(parentId, new HashSet<String>());
|
|||
// }
|
|||
// channelMap.get(parentId).add(channelId);
|
|||
//
|
|||
// DeviceChannel deviceChannel = queryChannel(deviceId, parentId);
|
|||
// if (deviceChannel != null) {
|
|||
// deviceChannel.setSubCount(channelMap.get(parentId).size());
|
|||
// 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);
|
|||
//
|
|||
// }
|
|||
// }
|
|||
|
|||
} |
|||
|
|||
/** |
|||
* 获取设备 |
|||
* |
|||
* @param deviceId 设备ID |
|||
* @return Device 设备对象 |
|||
*/ |
|||
@Override |
|||
public Device queryVideoDevice(String deviceId) { |
|||
return deviceMapper.getDeviceByDeviceId(deviceId); |
|||
} |
|||
|
|||
@Override |
|||
public PageResult queryChannelsByDeviceId(String deviceId, String query, Boolean hasSubChannel, String online, int page, int count) { |
|||
// 获取到所有正在播放的流
|
|||
List<DeviceChannel> result = new ArrayList<>(); |
|||
PageResult pageResult = new PageResult<DeviceChannel>(); |
|||
|
|||
deviceChannelMapper.queryChannelsByDeviceId(deviceId); |
|||
// String queryContent = "*";
|
|||
// if (!StringUtils.isEmpty(query)) queryContent = String.format("*%S*",query);
|
|||
// String queryHasSubChannel = "*";
|
|||
// if (hasSubChannel != null) queryHasSubChannel = hasSubChannel?"true":"false";
|
|||
// String queryOnline = "*";
|
|||
// if (!StringUtils.isEmpty(online)) queryOnline = online;
|
|||
// String queryStr = VideoManagerConstants.CACHEKEY_PREFIX + deviceId +
|
|||
// "_" + queryContent + // 搜索编号和名称
|
|||
// "_" + queryOnline + // 搜索是否在线
|
|||
// "_" + queryHasSubChannel + // 搜索是否含有子节点
|
|||
// "_" + "*";
|
|||
// 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++) {
|
|||
// DeviceChannel deviceChannel = (DeviceChannel)redis.get((String)deviceChannelList.get(i));
|
|||
// StreamInfo streamInfo = stringStreamInfoMap.get(deviceId + "_" + deviceChannel.getChannelId());
|
|||
// deviceChannel.setPlay(streamInfo != null);
|
|||
// if (streamInfo != null) deviceChannel.setStreamId(streamInfo.getStreamId());
|
|||
// result.add(deviceChannel);
|
|||
// }
|
|||
// pageResult.setData(result);
|
|||
// }
|
|||
|
|||
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.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)));
|
|||
// }
|
|||
// }
|
|||
return deviceChannelMapper.queryChannelsByDeviceId(deviceId); |
|||
} |
|||
|
|||
@Override |
|||
public PageResult querySubChannels(String deviceId, String parentChannelId, String query, Boolean hasSubChannel, String online, int page, int count) { |
|||
|
|||
deviceChannelMapper.queryChannelsByDeviceId(deviceId, parentChannelId); |
|||
|
|||
// List<DeviceChannel> allDeviceChannels = new ArrayList<>();
|
|||
// String queryContent = "*";
|
|||
// if (!StringUtils.isEmpty(query)) queryContent = String.format("*%S*",query);
|
|||
// String queryHasSubChannel = "*";
|
|||
// if (hasSubChannel != null) queryHasSubChannel = hasSubChannel?"true":"false";
|
|||
// String queryOnline = "*";
|
|||
// if (!StringUtils.isEmpty(online)) queryOnline = online;
|
|||
// String queryStr = VideoManagerConstants.CACHEKEY_PREFIX + deviceId +
|
|||
// "_" + queryContent + // 搜索编号和名称
|
|||
// "_" + queryOnline + // 搜索是否在线
|
|||
// "_" + queryHasSubChannel + // 搜索是否含有子节点
|
|||
// "_" + parentChannelId;
|
|||
//
|
|||
//// 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++) {
|
|||
// DeviceChannel deviceChannel = (DeviceChannel)redis.get((String)deviceChannelList.get(i));
|
|||
// if (deviceChannel.getParentId() != null && deviceChannel.getParentId().equals(parentChannelId)) {
|
|||
// allDeviceChannels.add(deviceChannel);
|
|||
// }
|
|||
// }
|
|||
// }
|
|||
// int maxCount = (page + 1 ) * count;
|
|||
PageResult pageResult = new PageResult<DeviceChannel>(); |
|||
// pageResult.setPage(page);
|
|||
// pageResult.setCount(count);
|
|||
// pageResult.setTotal(allDeviceChannels.size());
|
|||
//
|
|||
// if (allDeviceChannels.size() > 0) {
|
|||
// pageResult.setData(allDeviceChannels.subList(
|
|||
// page * count, pageResult.getTotal() > maxCount ? maxCount : pageResult.getTotal()
|
|||
// ));
|
|||
// }
|
|||
return pageResult; |
|||
} |
|||
|
|||
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.scan(VideoManagerConstants.CACHEKEY_PREFIX + deviceId + "_" + "*");
|
|||
//
|
|||
// if (deviceChannelList != null && deviceChannelList.size() > 0 ) {
|
|||
// for (int i = 0; i < deviceChannelList.size(); i++) {
|
|||
// DeviceChannel deviceChannel = (DeviceChannel)redis.get((String)deviceChannelList.get(i));
|
|||
// if (deviceChannel.getParentId() != null && deviceChannel.getParentId().equals(parentChannelId)) {
|
|||
// allDeviceChannels.add(deviceChannel);
|
|||
// }
|
|||
// }
|
|||
// }
|
|||
|
|||
return allDeviceChannels; |
|||
} |
|||
|
|||
@Override |
|||
public DeviceChannel queryChannel(String deviceId, String channelId) { |
|||
DeviceChannel deviceChannel = null; |
|||
return deviceChannelMapper.queryChannel(deviceId, channelId); |
|||
//// 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;
|
|||
} |
|||
|
|||
|
|||
/** |
|||
* 获取多个设备 |
|||
* |
|||
* @param deviceIds 设备ID数组 |
|||
* @return List<Device> 设备对象数组 |
|||
*/ |
|||
@Override |
|||
public PageResult<Device> queryVideoDeviceList(String[] deviceIds, int page, int count) { |
|||
List<Device> devices = new ArrayList<>(); |
|||
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.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)));
|
|||
// 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);
|
|||
// }
|
|||
// }
|
|||
// pageResult.setData(devices);
|
|||
return pageResult; |
|||
} |
|||
|
|||
/** |
|||
* 获取多个设备 |
|||
* |
|||
* @return List<Device> 设备对象数组 |
|||
*/ |
|||
@Override |
|||
public List<Device> queryVideoDeviceList() { |
|||
|
|||
// if (deviceIds == null || deviceIds.length == 0) {
|
|||
//// List<Object> deviceIdList = redis.keys(VideoManagerConstants.DEVICE_PREFIX+"*");
|
|||
// List<Object> deviceIdList = redis.scan(VideoManagerConstants.DEVICE_PREFIX+"*");
|
|||
// for (int i = 0; i < deviceIdList.size(); 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++) {
|
|||
// 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);
|
|||
// }
|
|||
// }
|
|||
|
|||
List<Device> deviceList = deviceMapper.getDevices(); |
|||
return deviceList; |
|||
} |
|||
|
|||
/** |
|||
* 删除设备 |
|||
* |
|||
* @param deviceId 设备ID |
|||
* @return true:删除成功 false:删除失败 |
|||
*/ |
|||
@Override |
|||
public boolean delete(String deviceId) { |
|||
int result = deviceMapper.del(deviceId); |
|||
|
|||
return result > 0; |
|||
} |
|||
|
|||
/** |
|||
* 更新设备在线 |
|||
* |
|||
* @param deviceId 设备ID |
|||
* @return true:更新成功 false:更新失败 |
|||
*/ |
|||
@Override |
|||
public boolean online(String deviceId) { |
|||
Device device = deviceMapper.getDeviceByDeviceId(deviceId); |
|||
device.setOnline(1); |
|||
return deviceMapper.update(device) > 0; |
|||
} |
|||
|
|||
/** |
|||
* 更新设备离线 |
|||
* |
|||
* @param deviceId 设备ID |
|||
* @return true:更新成功 false:更新失败 |
|||
*/ |
|||
@Override |
|||
public boolean outline(String deviceId) { |
|||
// Device device = (Device)redis.get(VideoManagerConstants.DEVICE_PREFIX+deviceId);
|
|||
// if (device == null) return false;
|
|||
// device.setOnline(0);
|
|||
// return redis.set(VideoManagerConstants.DEVICE_PREFIX+device.getDeviceId(), device);
|
|||
|
|||
Device device = deviceMapper.getDeviceByDeviceId(deviceId); |
|||
device.setOnline(0); |
|||
return deviceMapper.update(device) > 0; |
|||
} |
|||
|
|||
|
|||
@Override |
|||
public void cleanChannelsForDevice(String deviceId) { |
|||
int result = deviceChannelMapper.cleanChannelsByDeviceId(deviceId); |
|||
} |
|||
|
|||
|
|||
} |
@ -1,217 +0,0 @@ |
|||
package com.genersoft.iot.vmp.storager.jdbc; |
|||
|
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
import com.genersoft.iot.vmp.common.PageResult; |
|||
import com.genersoft.iot.vmp.common.StreamInfo; |
|||
import com.genersoft.iot.vmp.conf.MediaServerConfig; |
|||
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel; |
|||
import org.springframework.stereotype.Component; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import com.genersoft.iot.vmp.common.VideoManagerConstants; |
|||
import com.genersoft.iot.vmp.gb28181.bean.Device; |
|||
import com.genersoft.iot.vmp.storager.IVideoManagerStorager; |
|||
|
|||
/** |
|||
* @Description:视频设备数据存储-jdbc实现 |
|||
* @author: swwheihei |
|||
* @date: 2020年5月6日 下午2:28:12 |
|||
*/ |
|||
@Component("jdbcStorager") |
|||
public class VideoManagerJdbcStoragerImpl implements IVideoManagerStorager { |
|||
|
|||
@Override |
|||
public boolean updateMediaInfo(MediaServerConfig mediaServerConfig) { |
|||
return false; |
|||
} |
|||
|
|||
@Override |
|||
public MediaServerConfig getMediaInfo() { |
|||
return null; |
|||
} |
|||
|
|||
/** |
|||
* 根据设备ID判断设备是否存在 |
|||
* |
|||
* @param deviceId 设备ID |
|||
* @return true:存在 false:不存在 |
|||
*/ |
|||
@Override |
|||
public boolean exists(String deviceId) { |
|||
// TODO Auto-generated method stub
|
|||
return false; |
|||
} |
|||
|
|||
/** |
|||
* 视频设备创建 |
|||
* |
|||
* @param device 设备对象 |
|||
* @return true:创建成功 false:创建失败 |
|||
*/ |
|||
@Override |
|||
public boolean create(Device device) { |
|||
// TODO Auto-generated method stub
|
|||
return false; |
|||
} |
|||
|
|||
@Override |
|||
public boolean updateDevice(Device device) { |
|||
return false; |
|||
} |
|||
|
|||
@Override |
|||
public void updateChannel(String deviceId, DeviceChannel channel) { |
|||
|
|||
} |
|||
|
|||
|
|||
/** |
|||
* 获取设备 |
|||
* |
|||
* @param deviceId 设备ID |
|||
* @return Device 设备对象 |
|||
*/ |
|||
@Override |
|||
public Device queryVideoDevice(String deviceId) { |
|||
// TODO Auto-generated method stub
|
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public PageResult queryChannelsByDeviceId(String deviceId, String query, Boolean hasSubChannel, String online, int page, int count) { |
|||
return null; |
|||
} |
|||
|
|||
|
|||
@Override |
|||
public List<DeviceChannel> queryChannelsByDeviceId(String deviceId) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public DeviceChannel queryChannel(String deviceId, String channelId) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public PageResult<Device> queryVideoDeviceList(String[] deviceIds, int page, int count) { |
|||
return null; |
|||
} |
|||
|
|||
/** |
|||
* 获取多个设备 |
|||
* |
|||
* @param deviceIds 设备ID数组 |
|||
* @return List<Device> 设备对象数组 |
|||
*/ |
|||
@Override |
|||
public List<Device> queryVideoDeviceList(String[] deviceIds) { |
|||
// TODO Auto-generated method stub
|
|||
return null; |
|||
} |
|||
|
|||
/** |
|||
* 删除设备 |
|||
* |
|||
* @param deviceId 设备ID |
|||
* @return true:删除成功 false:删除失败 |
|||
*/ |
|||
@Override |
|||
public boolean delete(String deviceId) { |
|||
// TODO Auto-generated method stub
|
|||
return false; |
|||
} |
|||
|
|||
/** |
|||
* 更新设备在线 |
|||
* |
|||
* @param deviceId 设备ID |
|||
* @return true:更新成功 false:更新失败 |
|||
*/ |
|||
@Override |
|||
public boolean online(String deviceId) { |
|||
// TODO Auto-generated method stub
|
|||
return false; |
|||
} |
|||
|
|||
/** |
|||
* 更新设备离线 |
|||
* |
|||
* @param deviceId 设备ID |
|||
* @return true:更新成功 false:更新失败 |
|||
*/ |
|||
@Override |
|||
public boolean outline(String deviceId) { |
|||
// TODO Auto-generated method stub
|
|||
return false; |
|||
} |
|||
|
|||
@Override |
|||
public boolean stopPlay(StreamInfo streamInfo) { |
|||
return false; |
|||
} |
|||
|
|||
@Override |
|||
public StreamInfo queryPlay(StreamInfo streamInfo) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public PageResult querySubChannels(String deviceId, String channelId, String query, Boolean hasSubChannel, String online, int page, int count) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public void updateCatch() { |
|||
System.out.println("##################"); |
|||
} |
|||
|
|||
@Override |
|||
public void cleanChannelsForDevice(String deviceId) { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public boolean startPlay(StreamInfo stream) { |
|||
return false; |
|||
} |
|||
|
|||
|
|||
@Override |
|||
public StreamInfo queryPlayByDevice(String deviceId, String code) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public Map<String, StreamInfo> queryPlayByDeviceId(String deviceId) { |
|||
|
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public boolean startPlayback(StreamInfo streamInfo) { |
|||
return false; |
|||
} |
|||
|
|||
@Override |
|||
public boolean stopPlayback(StreamInfo streamInfo) { |
|||
return false; |
|||
} |
|||
|
|||
@Override |
|||
public StreamInfo queryPlaybackByDevice(String deviceId, String channelId) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public StreamInfo queryPlayByStreamId(String streamId) { |
|||
return null; |
|||
} |
|||
|
|||
@Override |
|||
public StreamInfo queryPlaybackByStreamId(String streamId) { |
|||
return null; |
|||
} |
|||
} |
@ -1,561 +0,0 @@ |
|||
package com.genersoft.iot.vmp.storager.redis; |
|||
|
|||
import java.util.*; |
|||
|
|||
import com.alibaba.fastjson.JSON; |
|||
import com.alibaba.fastjson.JSONObject; |
|||
import com.genersoft.iot.vmp.common.PageResult; |
|||
import com.genersoft.iot.vmp.common.StreamInfo; |
|||
import com.genersoft.iot.vmp.conf.MediaServerConfig; |
|||
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
import com.genersoft.iot.vmp.common.VideoManagerConstants; |
|||
import com.genersoft.iot.vmp.gb28181.bean.Device; |
|||
import com.genersoft.iot.vmp.storager.IVideoManagerStorager; |
|||
import com.genersoft.iot.vmp.utils.redis.RedisUtil; |
|||
import org.springframework.util.StringUtils; |
|||
|
|||
/** |
|||
* @Description:视频设备数据存储-redis实现 |
|||
* @author: swwheihei |
|||
* @date: 2020年5月6日 下午2:31:42 |
|||
*/ |
|||
@Component("redisStorager") |
|||
public class VideoManagerRedisStoragerImpl implements IVideoManagerStorager { |
|||
|
|||
@Autowired |
|||
private RedisUtil redis; |
|||
|
|||
private HashMap<String, HashMap<String, HashSet<String>>> deviceMap = new HashMap<>(); |
|||
|
|||
|
|||
/** |
|||
* 根据设备ID判断设备是否存在 |
|||
* |
|||
* @param deviceId 设备ID |
|||
* @return true:存在 false:不存在 |
|||
*/ |
|||
@Override |
|||
public boolean exists(String deviceId) { |
|||
return redis.hasKey(VideoManagerConstants.DEVICE_PREFIX+deviceId); |
|||
} |
|||
|
|||
/** |
|||
* 视频设备创建 |
|||
* |
|||
* @param device 设备对象 |
|||
* @return true:创建成功 false:创建失败 |
|||
*/ |
|||
@Override |
|||
public boolean create(Device device) { |
|||
return redis.set(VideoManagerConstants.DEVICE_PREFIX+device.getDeviceId(), device); |
|||
} |
|||
|
|||
|
|||
|
|||
/** |
|||
* 视频设备更新 |
|||
* |
|||
* @param device 设备对象 |
|||
* @return true:更新成功 false:更新失败 |
|||
*/ |
|||
@Override |
|||
public boolean updateDevice(Device device) { |
|||
if (deviceMap.get(device.getDeviceId()) == null) { |
|||
deviceMap.put(device.getDeviceId(), new HashMap<String, HashSet<String>>()); |
|||
} |
|||
// 更新device中的通道数量
|
|||
device.setChannelCount(deviceMap.get(device.getDeviceId()).size()); |
|||
// 存储device
|
|||
return redis.set(VideoManagerConstants.DEVICE_PREFIX+device.getDeviceId(), device); |
|||
|
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void updateChannel(String deviceId, DeviceChannel channel) { |
|||
String channelId = channel.getChannelId(); |
|||
HashMap<String, HashSet<String>> channelMap = deviceMap.get(deviceId); |
|||
if (channelMap == null) return; |
|||
// 作为父设备, 确定自己的子节点数
|
|||
if (channelMap.get(channelId) == null) { |
|||
channelMap.put(channelId, new HashSet<String>()); |
|||
}else if (channelMap.get(channelId).size() > 0) { |
|||
channel.setSubCount(channelMap.get(channelId).size()); |
|||
} |
|||
|
|||
// 存储通道
|
|||
redis.set(VideoManagerConstants.CACHEKEY_PREFIX + deviceId + |
|||
"_" + channel.getChannelId() + |
|||
"_" + (channel.getStatus() == 1 ? "on":"off") + |
|||
"_" + (channelMap.get(channelId).size() > 0)+ |
|||
"_" + (StringUtils.isEmpty(channel.getParentId())?null:channel.getParentId()), |
|||
channel); |
|||
// 更新device中的通道数量
|
|||
Device device = (Device)redis.get(VideoManagerConstants.DEVICE_PREFIX+deviceId); |
|||
device.setChannelCount(deviceMap.get(deviceId).size()); |
|||
redis.set(VideoManagerConstants.DEVICE_PREFIX+device.getDeviceId(), device); |
|||
|
|||
|
|||
// 如果有父设备,更新父设备内子节点数
|
|||
String parentId = channel.getParentId(); |
|||
if (!StringUtils.isEmpty(parentId) && !parentId.equals(deviceId)) { |
|||
|
|||
if (channelMap.get(parentId) == null) { |
|||
channelMap.put(parentId, new HashSet<String>()); |
|||
} |
|||
channelMap.get(parentId).add(channelId); |
|||
|
|||
DeviceChannel deviceChannel = queryChannel(deviceId, parentId); |
|||
if (deviceChannel != null) { |
|||
deviceChannel.setSubCount(channelMap.get(parentId).size()); |
|||
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); |
|||
|
|||
} |
|||
} |
|||
|
|||
} |
|||
|
|||
/** |
|||
* 获取设备 |
|||
* |
|||
* @param deviceId 设备ID |
|||
* @return Device 设备对象 |
|||
*/ |
|||
@Override |
|||
public Device queryVideoDevice(String deviceId) { |
|||
return (Device)redis.get(VideoManagerConstants.DEVICE_PREFIX+deviceId); |
|||
} |
|||
|
|||
@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 = "*"; |
|||
if (!StringUtils.isEmpty(query)) queryContent = String.format("*%S*",query); |
|||
String queryHasSubChannel = "*"; |
|||
if (hasSubChannel != null) queryHasSubChannel = hasSubChannel?"true":"false"; |
|||
String queryOnline = "*"; |
|||
if (!StringUtils.isEmpty(online)) queryOnline = online; |
|||
String queryStr = VideoManagerConstants.CACHEKEY_PREFIX + deviceId + |
|||
"_" + queryContent + // 搜索编号和名称
|
|||
"_" + queryOnline + // 搜索是否在线
|
|||
"_" + queryHasSubChannel + // 搜索是否含有子节点
|
|||
"_" + "*"; |
|||
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++) { |
|||
DeviceChannel deviceChannel = (DeviceChannel)redis.get((String)deviceChannelList.get(i)); |
|||
StreamInfo streamInfo = stringStreamInfoMap.get(deviceId + "_" + deviceChannel.getChannelId()); |
|||
deviceChannel.setPlay(streamInfo != null); |
|||
if (streamInfo != null) deviceChannel.setStreamId(streamInfo.getStreamId()); |
|||
result.add(deviceChannel); |
|||
} |
|||
pageResult.setData(result); |
|||
} |
|||
|
|||
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.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))); |
|||
} |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
@Override |
|||
public PageResult querySubChannels(String deviceId, String parentChannelId, String query, Boolean hasSubChannel, String online, int page, int count) { |
|||
List<DeviceChannel> allDeviceChannels = new ArrayList<>(); |
|||
String queryContent = "*"; |
|||
if (!StringUtils.isEmpty(query)) queryContent = String.format("*%S*",query); |
|||
String queryHasSubChannel = "*"; |
|||
if (hasSubChannel != null) queryHasSubChannel = hasSubChannel?"true":"false"; |
|||
String queryOnline = "*"; |
|||
if (!StringUtils.isEmpty(online)) queryOnline = online; |
|||
String queryStr = VideoManagerConstants.CACHEKEY_PREFIX + deviceId + |
|||
"_" + queryContent + // 搜索编号和名称
|
|||
"_" + queryOnline + // 搜索是否在线
|
|||
"_" + queryHasSubChannel + // 搜索是否含有子节点
|
|||
"_" + parentChannelId; |
|||
|
|||
// 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++) { |
|||
DeviceChannel deviceChannel = (DeviceChannel)redis.get((String)deviceChannelList.get(i)); |
|||
if (deviceChannel.getParentId() != null && deviceChannel.getParentId().equals(parentChannelId)) { |
|||
allDeviceChannels.add(deviceChannel); |
|||
} |
|||
} |
|||
} |
|||
int maxCount = (page + 1 ) * count; |
|||
PageResult pageResult = new PageResult<DeviceChannel>(); |
|||
pageResult.setPage(page); |
|||
pageResult.setCount(count); |
|||
pageResult.setTotal(allDeviceChannels.size()); |
|||
|
|||
if (allDeviceChannels.size() > 0) { |
|||
pageResult.setData(allDeviceChannels.subList( |
|||
page * count, pageResult.getTotal() > maxCount ? maxCount : pageResult.getTotal() |
|||
)); |
|||
} |
|||
return pageResult; |
|||
} |
|||
|
|||
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.scan(VideoManagerConstants.CACHEKEY_PREFIX + deviceId + "_" + "*"); |
|||
|
|||
if (deviceChannelList != null && deviceChannelList.size() > 0 ) { |
|||
for (int i = 0; i < deviceChannelList.size(); i++) { |
|||
DeviceChannel deviceChannel = (DeviceChannel)redis.get((String)deviceChannelList.get(i)); |
|||
if (deviceChannel.getParentId() != null && deviceChannel.getParentId().equals(parentChannelId)) { |
|||
allDeviceChannels.add(deviceChannel); |
|||
} |
|||
} |
|||
} |
|||
|
|||
return allDeviceChannels; |
|||
} |
|||
|
|||
@Override |
|||
public DeviceChannel queryChannel(String deviceId, String 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; |
|||
} |
|||
|
|||
|
|||
/** |
|||
* 获取多个设备 |
|||
* |
|||
* @param deviceIds 设备ID数组 |
|||
* @return List<Device> 设备对象数组 |
|||
*/ |
|||
@Override |
|||
public PageResult<Device> queryVideoDeviceList(String[] deviceIds, int page, int count) { |
|||
List<Device> devices = new ArrayList<>(); |
|||
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.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)));
|
|||
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); |
|||
} |
|||
} |
|||
pageResult.setData(devices); |
|||
return pageResult; |
|||
} |
|||
|
|||
/** |
|||
* 获取多个设备 |
|||
* |
|||
* @param deviceIds 设备ID数组 |
|||
* @return List<Device> 设备对象数组 |
|||
*/ |
|||
@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.scan(VideoManagerConstants.DEVICE_PREFIX+"*"); |
|||
for (int i = 0; i < deviceIdList.size(); 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++) { |
|||
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; |
|||
} |
|||
|
|||
/** |
|||
* 删除设备 |
|||
* |
|||
* @param deviceId 设备ID |
|||
* @return true:删除成功 false:删除失败 |
|||
*/ |
|||
@Override |
|||
public boolean delete(String deviceId) { |
|||
return redis.del(VideoManagerConstants.DEVICE_PREFIX+deviceId); |
|||
} |
|||
|
|||
/** |
|||
* 更新设备在线 |
|||
* |
|||
* @param deviceId 设备ID |
|||
* @return true:更新成功 false:更新失败 |
|||
*/ |
|||
@Override |
|||
public boolean online(String deviceId) { |
|||
Device device = (Device)redis.get(VideoManagerConstants.DEVICE_PREFIX+deviceId); |
|||
device.setOnline(1); |
|||
return redis.set(VideoManagerConstants.DEVICE_PREFIX+device.getDeviceId(), device); |
|||
} |
|||
|
|||
/** |
|||
* 更新设备离线 |
|||
* |
|||
* @param deviceId 设备ID |
|||
* @return true:更新成功 false:更新失败 |
|||
*/ |
|||
@Override |
|||
public boolean outline(String deviceId) { |
|||
Device device = (Device)redis.get(VideoManagerConstants.DEVICE_PREFIX+deviceId); |
|||
if (device == null) return false; |
|||
device.setOnline(0); |
|||
return redis.set(VideoManagerConstants.DEVICE_PREFIX+device.getDeviceId(), device); |
|||
} |
|||
|
|||
/** |
|||
* 开始播放时将流存入redis |
|||
* |
|||
* @return |
|||
*/ |
|||
@Override |
|||
public boolean startPlay(StreamInfo stream) { |
|||
return redis.set(String.format("%S_%s_%s_%s", VideoManagerConstants.PLAYER_PREFIX, stream.getStreamId(),stream.getDeviceID(), stream.getCahnnelId()), |
|||
stream); |
|||
} |
|||
|
|||
/** |
|||
* 停止播放时从redis删除 |
|||
* |
|||
* @return |
|||
*/ |
|||
@Override |
|||
public boolean stopPlay(StreamInfo streamInfo) { |
|||
if (streamInfo == null) return false; |
|||
DeviceChannel deviceChannel = queryChannel(streamInfo.getDeviceID(), streamInfo.getCahnnelId()); |
|||
if (deviceChannel != null) { |
|||
deviceChannel.setStreamId(null); |
|||
deviceChannel.setPlay(false); |
|||
updateChannel(streamInfo.getDeviceID(), deviceChannel); |
|||
} |
|||
return redis.del(String.format("%S_%s_%s_%s", VideoManagerConstants.PLAYER_PREFIX, |
|||
streamInfo.getStreamId(), |
|||
streamInfo.getDeviceID(), |
|||
streamInfo.getCahnnelId())); |
|||
} |
|||
|
|||
/** |
|||
* 查询播放列表 |
|||
* @return |
|||
*/ |
|||
@Override |
|||
public StreamInfo queryPlay(StreamInfo streamInfo) { |
|||
return (StreamInfo)redis.get(String.format("%S_%s_%s_%s", |
|||
VideoManagerConstants.PLAYER_PREFIX, |
|||
streamInfo.getStreamId(), |
|||
streamInfo.getDeviceID(), |
|||
streamInfo.getCahnnelId())); |
|||
} |
|||
@Override |
|||
public StreamInfo queryPlayByStreamId(String steamId) { |
|||
List<Object> playLeys = redis.scan(String.format("%S_%s_*", VideoManagerConstants.PLAYER_PREFIX, steamId)); |
|||
if (playLeys == null || playLeys.size() == 0) return null; |
|||
return (StreamInfo)redis.get(playLeys.get(0).toString()); |
|||
} |
|||
|
|||
@Override |
|||
public StreamInfo queryPlaybackByStreamId(String steamId) { |
|||
List<Object> playLeys = redis.scan(String.format("%S_%s_*", VideoManagerConstants.PLAY_BLACK_PREFIX, steamId)); |
|||
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()); |
|||
} |
|||
|
|||
/** |
|||
* 更新流媒体信息 |
|||
* @param mediaServerConfig |
|||
* @return |
|||
*/ |
|||
@Override |
|||
public boolean updateMediaInfo(MediaServerConfig mediaServerConfig) { |
|||
return redis.set(VideoManagerConstants.MEDIA_SERVER_PREFIX,mediaServerConfig); |
|||
} |
|||
|
|||
/** |
|||
* 获取流媒体信息 |
|||
* @return |
|||
*/ |
|||
@Override |
|||
public MediaServerConfig getMediaInfo() { |
|||
return (MediaServerConfig)redis.get(VideoManagerConstants.MEDIA_SERVER_PREFIX); |
|||
} |
|||
|
|||
@Override |
|||
public void updateCatch() { |
|||
deviceMap = new HashMap<>(); |
|||
// 更新设备
|
|||
List<Device> devices = queryVideoDeviceList(null); |
|||
if (devices == null && devices.size() == 0) return; |
|||
for (Device device : devices) { |
|||
// 更新设备下的通道
|
|||
HashMap<String, HashSet<String>> channelMap = new HashMap<String, HashSet<String>>(); |
|||
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]; |
|||
HashSet<String> subChannel = channelMap.get(channelId); |
|||
if (subChannel == null) { |
|||
subChannel = new HashSet<>(); |
|||
} |
|||
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.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.getStreamId(),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.setStreamId(null); |
|||
deviceChannel.setPlay(false); |
|||
updateChannel(streamInfo.getDeviceID(), deviceChannel); |
|||
} |
|||
return redis.del(String.format("%S_%s_%s_%s", VideoManagerConstants.PLAY_BLACK_PREFIX, |
|||
streamInfo.getStreamId(), |
|||
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) { |
|||
playLeys = redis.scan(String.format("%S_*_*_%s", VideoManagerConstants.PLAY_BLACK_PREFIX, |
|||
deviceId)); |
|||
} |
|||
if (playLeys == null || playLeys.size() == 0) return null; |
|||
return (StreamInfo)redis.get(playLeys.get(0).toString()); |
|||
} |
|||
} |
Binary file not shown.
Loading…
Reference in new issue