| | |
| | | package com.genersoft.iot.vmp.storager.impl; |
| | | |
| | | import java.util.*; |
| | | |
| | | import com.genersoft.iot.vmp.gb28181.bean.*; |
| | | import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager; |
| | | import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem; |
| | | import com.genersoft.iot.vmp.media.zlm.dto.StreamProxyItem; |
| | | import com.genersoft.iot.vmp.media.zlm.dto.StreamPushItem; |
| | | import com.genersoft.iot.vmp.service.IGbStreamService; |
| | | import com.genersoft.iot.vmp.storager.IRedisCatchStorage; |
| | | import com.genersoft.iot.vmp.storager.IVideoManagerStorager; |
| | | import com.genersoft.iot.vmp.storager.dao.*; |
| | | import com.genersoft.iot.vmp.vmanager.gb28181.platform.bean.ChannelReduce; |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.github.pagehelper.PageInfo; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import com.genersoft.iot.vmp.storager.IVideoManagerStorager; |
| | | import org.springframework.transaction.TransactionDefinition; |
| | | import org.springframework.transaction.TransactionStatus; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * @Description:视频设备数据存储-jdbc实现 |
| | | * @description:视频设备数据存储-jdbc实现 |
| | | * @author: swwheihei |
| | | * @date: 2020年5月6日 下午2:31:42 |
| | | */ |
| | | @SuppressWarnings("rawtypes") |
| | | @Component |
| | | public class VideoManagerStoragerImpl implements IVideoManagerStorager { |
| | | |
| | | private Logger logger = LoggerFactory.getLogger(VideoManagerStoragerImpl.class); |
| | | |
| | | @Autowired |
| | | DataSourceTransactionManager dataSourceTransactionManager; |
| | | |
| | |
| | | |
| | | @Autowired |
| | | private GbStreamMapper gbStreamMapper; |
| | | ; |
| | | |
| | | @Autowired |
| | | private PlatformGbStreamMapper platformGbStreamMapper; |
| | | |
| | | @Autowired |
| | | private IGbStreamService gbStreamService; |
| | | |
| | | @Autowired |
| | | private ParentPlatformMapper parentPlatformMapper; |
| | | |
| | | @Autowired |
| | | private VideoStreamSessionManager streamSession; |
| | | |
| | | @Autowired |
| | | private MediaServerMapper mediaServerMapper; |
| | | |
| | | private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | public synchronized boolean create(Device device) { |
| | | redisCatchStorage.updateDevice(device); |
| | | return deviceMapper.add(device) > 0; |
| | | } |
| | | |
| | |
| | | */ |
| | | @Override |
| | | public synchronized boolean updateDevice(Device device) { |
| | | String now = this.format.format(System.currentTimeMillis()); |
| | | device.setUpdateTime(now); |
| | | Device deviceByDeviceId = deviceMapper.getDeviceByDeviceId(device.getDeviceId()); |
| | | if (deviceByDeviceId == null) { |
| | | device.setCreateTime(now); |
| | | redisCatchStorage.updateDevice(device); |
| | | return deviceMapper.add(device) > 0; |
| | | }else { |
| | | redisCatchStorage.updateDevice(device); |
| | | return deviceMapper.update(device) > 0; |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | |
| | | public synchronized void updateChannel(String deviceId, DeviceChannel channel) { |
| | | String channelId = channel.getChannelId(); |
| | | channel.setDeviceId(deviceId); |
| | | channel.setStreamId(streamSession.getStreamId(deviceId, channel.getChannelId())); |
| | | String now = this.format.format(System.currentTimeMillis()); |
| | | channel.setUpdateTime(now); |
| | | DeviceChannel deviceChannel = deviceChannelMapper.queryChannel(deviceId, channelId); |
| | | if (deviceChannel == null) { |
| | | channel.setCreateTime(now); |
| | | deviceChannelMapper.add(channel); |
| | | }else { |
| | | deviceChannelMapper.update(channel); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void updateChannels(String deviceId, List<DeviceChannel> channels) { |
| | | List<DeviceChannel> addChannels = new ArrayList<>(); |
| | | List<DeviceChannel> updateChannels = new ArrayList<>(); |
| | | HashMap<String, DeviceChannel> channelsInStore = new HashMap<>(); |
| | | if (channels != null && channels.size() > 0) { |
| | | List<DeviceChannel> channelList = deviceChannelMapper.queryChannelsByDeviceId(deviceId); |
| | | if (channelList.size() == 0) { |
| | | for (DeviceChannel channel : channels) { |
| | | channel.setDeviceId(deviceId); |
| | | channel.setStreamId(streamSession.getStreamId(deviceId, channel.getChannelId())); |
| | | String now = this.format.format(System.currentTimeMillis()); |
| | | channel.setUpdateTime(now); |
| | | channel.setCreateTime(now); |
| | | addChannels.add(channel); |
| | | } |
| | | }else { |
| | | for (DeviceChannel deviceChannel : channelList) { |
| | | channelsInStore.put(deviceChannel.getChannelId(), deviceChannel); |
| | | } |
| | | for (DeviceChannel channel : channels) { |
| | | String channelId = channel.getChannelId(); |
| | | channel.setDeviceId(deviceId); |
| | | channel.setStreamId(streamSession.getStreamId(deviceId, channel.getChannelId())); |
| | | String now = this.format.format(System.currentTimeMillis()); |
| | | channel.setUpdateTime(now); |
| | | if (channelsInStore.get(channel.getChannelId()) != null) { |
| | | updateChannels.add(channel); |
| | | }else { |
| | | addChannels.add(channel); |
| | | channel.setCreateTime(now); |
| | | } |
| | | } |
| | | } |
| | | int limitCount = 300; |
| | | if (addChannels.size() > 0) { |
| | | if (addChannels.size() > limitCount) { |
| | | for (int i = 0; i < addChannels.size(); i += limitCount) { |
| | | int toIndex = i + limitCount; |
| | | if (i + limitCount > addChannels.size()) { |
| | | toIndex = addChannels.size(); |
| | | } |
| | | deviceChannelMapper.batchAdd(addChannels.subList(i, toIndex)); |
| | | } |
| | | }else { |
| | | deviceChannelMapper.batchAdd(addChannels); |
| | | } |
| | | } |
| | | if (updateChannels.size() > 0) { |
| | | if (updateChannels.size() > limitCount) { |
| | | for (int i = 0; i < updateChannels.size(); i += limitCount) { |
| | | int toIndex = i + limitCount; |
| | | if (i + limitCount > updateChannels.size()) { |
| | | toIndex = updateChannels.size(); |
| | | } |
| | | deviceChannelMapper.batchAdd(updateChannels.subList(i, toIndex)); |
| | | } |
| | | }else { |
| | | deviceChannelMapper.batchUpdate(updateChannels); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void deviceChannelOnline(String deviceId, String channelId) { |
| | | deviceChannelMapper.online(deviceId, channelId); |
| | | } |
| | | |
| | | @Override |
| | | public void deviceChannelOffline(String deviceId, String channelId) { |
| | | deviceChannelMapper.offline(deviceId, channelId); |
| | | } |
| | | |
| | | @Override |
| | |
| | | public PageInfo queryChannelsByDeviceId(String deviceId, String query, Boolean hasSubChannel, Boolean online, int page, int count) { |
| | | // 获取到所有正在播放的流 |
| | | PageHelper.startPage(page, count); |
| | | List<DeviceChannel> all = deviceChannelMapper.queryChannelsByDeviceId(deviceId, null, query, hasSubChannel, online); |
| | | List<DeviceChannel> all = deviceChannelMapper.queryChannels(deviceId, null, query, hasSubChannel, online); |
| | | return new PageInfo<>(all); |
| | | } |
| | | |
| | | @Override |
| | | public List<DeviceChannel> queryChannelsByDeviceId(String deviceId) { |
| | | return deviceChannelMapper.queryChannelsByDeviceId(deviceId, null,null, null, null); |
| | | return deviceChannelMapper.queryChannels(deviceId, null,null, null, null); |
| | | } |
| | | |
| | | @Override |
| | | public PageInfo<DeviceChannel> querySubChannels(String deviceId, String parentChannelId, String query, Boolean hasSubChannel, String online, int page, int count) { |
| | | PageHelper.startPage(page, count); |
| | | List<DeviceChannel> all = deviceChannelMapper.queryChannelsByDeviceId(deviceId, parentChannelId, null, null, null); |
| | | List<DeviceChannel> all = deviceChannelMapper.queryChannels(deviceId, parentChannelId, null, null, null); |
| | | return new PageInfo<>(all); |
| | | } |
| | | |
| | |
| | | return deviceChannelMapper.queryChannel(deviceId, channelId); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public int delChannel(String deviceId, String channelId) { |
| | | return deviceChannelMapper.del(deviceId, channelId); |
| | | } |
| | | |
| | | /** |
| | | * 获取多个设备 |
| | |
| | | */ |
| | | @Override |
| | | public boolean delete(String deviceId) { |
| | | int result = deviceMapper.del(deviceId); |
| | | |
| | | return result > 0; |
| | | TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition); |
| | | boolean result = false; |
| | | try { |
| | | if (platformChannelMapper.delChannelForDeviceId(deviceId) <0 // 删除与国标平台的关联 |
| | | || deviceChannelMapper.cleanChannelsByDeviceId(deviceId) < 0 // 删除他的通道 |
| | | || deviceMapper.del(deviceId) < 0 // 移除设备信息 |
| | | ) { |
| | | //事务回滚 |
| | | dataSourceTransactionManager.rollback(transactionStatus); |
| | | } |
| | | result = true; |
| | | dataSourceTransactionManager.commit(transactionStatus); //手动提交 |
| | | }catch (Exception e) { |
| | | dataSourceTransactionManager.rollback(transactionStatus); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | |
| | | return false; |
| | | } |
| | | device.setOnline(1); |
| | | System.out.println("更新设备在线"); |
| | | logger.info("更新设备在线: " + deviceId); |
| | | redisCatchStorage.updateDevice(device); |
| | | return deviceMapper.update(device) > 0; |
| | | } |
| | | |
| | |
| | | */ |
| | | @Override |
| | | public synchronized boolean outline(String deviceId) { |
| | | System.out.println("更新设备离线: " + deviceId); |
| | | logger.info("更新设备离线: " + deviceId); |
| | | Device device = deviceMapper.getDeviceByDeviceId(deviceId); |
| | | if (device == null) return false; |
| | | device.setOnline(0); |
| | | redisCatchStorage.updateDevice(device); |
| | | return deviceMapper.update(device) > 0; |
| | | } |
| | | |
| | | /** |
| | | * 更新所有设备离线 |
| | | * |
| | | * @return true:更新成功 false:更新失败 |
| | | */ |
| | | @Override |
| | | public synchronized boolean outlineForAll() { |
| | | logger.info("更新所有设备离线"); |
| | | int result = deviceMapper.outlineForAll(); |
| | | return result > 0; |
| | | } |
| | | |
| | | /** |
| | |
| | | public boolean updateParentPlatform(ParentPlatform parentPlatform) { |
| | | int result = 0; |
| | | ParentPlatformCatch parentPlatformCatch = redisCatchStorage.queryPlatformCatchInfo(parentPlatform.getServerGBId()); // .getDeviceGBId()); |
| | | if ( platformMapper.getParentPlatById(parentPlatform.getServerGBId()) == null) { |
| | | if (parentPlatform.getId() == null ) { |
| | | result = platformMapper.addParentPlatform(parentPlatform); |
| | | |
| | | if (parentPlatformCatch == null) { |
| | | parentPlatformCatch = new ParentPlatformCatch(); |
| | | parentPlatformCatch.setParentPlatform(parentPlatform); |
| | | parentPlatformCatch.setId(parentPlatform.getServerGBId()); |
| | | } |
| | | }else { |
| | | if (parentPlatformCatch == null) { // serverGBId 已变化 |
| | | ParentPlatform parentPlatById = platformMapper.getParentPlatById(parentPlatform.getId()); |
| | | // 使用旧的查出缓存ID |
| | | parentPlatformCatch = redisCatchStorage.queryPlatformCatchInfo(parentPlatById.getServerGBId()); |
| | | parentPlatformCatch.setId(parentPlatform.getServerGBId()); |
| | | redisCatchStorage.delPlatformCatchInfo(parentPlatById.getServerGBId()); |
| | | } |
| | | result = platformMapper.updateParentPlatform(parentPlatform); |
| | | } |
| | | // 更新缓存 |
| | | parentPlatformCatch.setParentPlatform(parentPlatform); |
| | | redisCatchStorage.updatePlatformCatchInfo(parentPlatformCatch); |
| | | // 共享所有视频流,需要将现有视频流添加到此平台 |
| | | List<GbStream> gbStreams = gbStreamMapper.selectAll(); |
| | | if (gbStreams.size() > 0) { |
| | | if (parentPlatform.isShareAllLiveStream()) { |
| | | gbStreamService.addPlatformInfo(gbStreams, parentPlatform.getServerGBId()); |
| | | }else { |
| | | gbStreamService.delPlatformInfo(gbStreams); |
| | | } |
| | | } |
| | | return result > 0; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | @Override |
| | | public ParentPlatform queryParentPlatById(String platformGbId) { |
| | | return platformMapper.getParentPlatById(platformGbId); |
| | | public ParentPlatform queryParentPlatByServerGBId(String platformGbId) { |
| | | return platformMapper.getParentPlatByServerGBId(platformGbId); |
| | | } |
| | | |
| | | @Override |
| | |
| | | boolean result = false; |
| | | streamProxyItem.setStreamType("proxy"); |
| | | streamProxyItem.setStatus(true); |
| | | String now = this.format.format(System.currentTimeMillis()); |
| | | streamProxyItem.setCreateTime(now); |
| | | try { |
| | | if (gbStreamMapper.add(streamProxyItem)<0 || streamProxyMapper.add(streamProxyItem) < 0) { |
| | | //事务回滚 |
| | |
| | | result = true; |
| | | dataSourceTransactionManager.commit(transactionStatus); //手动提交 |
| | | }catch (Exception e) { |
| | | logger.error("向数据库添加流代理失败:", e); |
| | | dataSourceTransactionManager.rollback(transactionStatus); |
| | | } |
| | | |
| | | |
| | | return result; |
| | | } |
| | | |
| | |
| | | dataSourceTransactionManager.commit(transactionStatus); //手动提交 |
| | | result = true; |
| | | }catch (Exception e) { |
| | | e.printStackTrace(); |
| | | dataSourceTransactionManager.rollback(transactionStatus); |
| | | } |
| | | return result; |
| | |
| | | * @return |
| | | */ |
| | | @Override |
| | | public GbStream queryStreamInParentPlatform(String platformId, String gbId) { |
| | | public List<GbStream> queryStreamInParentPlatform(String platformId, String gbId) { |
| | | return gbStreamMapper.queryStreamInPlatform(platformId, gbId); |
| | | } |
| | | |
| | |
| | | @Override |
| | | public void updateMediaList(List<StreamPushItem> streamPushItems) { |
| | | if (streamPushItems == null || streamPushItems.size() == 0) return; |
| | | System.out.printf("updateMediaList: " + streamPushItems.size()); |
| | | logger.info("updateMediaList: " + streamPushItems.size()); |
| | | streamPushMapper.addAll(streamPushItems); |
| | | // TODO 待优化 |
| | | for (int i = 0; i < streamPushItems.size(); i++) { |
| | |
| | | streamPushMapper.del(streamPushItem.getApp(), streamPushItem.getStream()); |
| | | streamPushMapper.add(streamPushItem); |
| | | gbStreamMapper.setStatus(streamPushItem.getApp(), streamPushItem.getStream(), true); |
| | | if(!StringUtils.isEmpty(streamPushItem.getGbId() )){ |
| | | // 查找开启了全部直播流共享的上级平台 |
| | | List<ParentPlatform> parentPlatforms = parentPlatformMapper.selectAllAhareAllLiveStream(); |
| | | if (parentPlatforms.size() > 0) { |
| | | for (ParentPlatform parentPlatform : parentPlatforms) { |
| | | streamPushItem.setPlatformId(parentPlatform.getServerGBId()); |
| | | String stream = streamPushItem.getStream(); |
| | | StreamProxyItem streamProxyItems = platformGbStreamMapper.selectOne(streamPushItem.getApp(), stream, parentPlatform.getServerGBId()); |
| | | if (streamProxyItems == null) { |
| | | platformGbStreamMapper.add(streamPushItem); |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void removeMedia(String app, String stream) { |
| | | streamPushMapper.del(app, stream); |
| | | public int removeMedia(String app, String stream) { |
| | | return streamPushMapper.del(app, stream); |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | |
| | | @Override |
| | | public void mediaOutline(String app, String streamId) { |
| | | gbStreamMapper.setStatus(app, streamId, false); |
| | | public int mediaOutline(String app, String streamId) { |
| | | return gbStreamMapper.setStatus(app, streamId, false); |
| | | } |
| | | |
| | | @Override |
| | | public void updateParentPlatformStatus(String platformGbID, boolean online) { |
| | | platformMapper.updateParentPlatformStatus(platformGbID, online); |
| | | } |
| | | |
| | | @Override |
| | | public void updateMediaServer(MediaServerItem mediaServerItem) { |
| | | String now = this.format.format(System.currentTimeMillis()); |
| | | mediaServerItem.setUpdateTime(now); |
| | | if (mediaServerMapper.queryOne(mediaServerItem.getId()) != null) { |
| | | mediaServerMapper.update(mediaServerItem); |
| | | }else { |
| | | mediaServerItem.setCreateTime(now); |
| | | mediaServerMapper.add(mediaServerItem); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public List<StreamProxyItem> getStreamProxyListForEnableInMediaServer(String id, boolean enable) { |
| | | return streamProxyMapper.selectForEnableInMediaServer(id, enable); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public Device queryVideoDeviceByChannelId( String channelId) { |
| | | Device result = null; |
| | | List<DeviceChannel> channelList = deviceChannelMapper.queryChannelByChannelId(channelId); |
| | | if (channelList.size() == 1) { |
| | | result = deviceMapper.getDeviceByDeviceId(channelList.get(0).getDeviceId()); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | public StreamProxyItem getStreamProxyByAppAndStream(String app, String streamId) { |
| | | return streamProxyMapper.selectOne(app, streamId); |
| | | } |
| | | |
| | | } |