| | |
| | | |
| | | import java.util.*; |
| | | |
| | | import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel; |
| | | import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform; |
| | | import com.genersoft.iot.vmp.gb28181.bean.ParentPlatformCatch; |
| | | import com.genersoft.iot.vmp.common.StreamInfo; |
| | | import com.genersoft.iot.vmp.gb28181.bean.*; |
| | | import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager; |
| | | import com.genersoft.iot.vmp.media.zlm.dto.StreamProxyItem; |
| | | import com.genersoft.iot.vmp.media.zlm.dto.StreamPushItem; |
| | | 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.storager.dao.ParentPlatformMapper; |
| | | import com.genersoft.iot.vmp.vmanager.platform.bean.ChannelReduce; |
| | | 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.gb28181.bean.Device; |
| | | import com.genersoft.iot.vmp.storager.IVideoManagerStorager; |
| | | import org.springframework.transaction.TransactionDefinition; |
| | | import org.springframework.transaction.TransactionStatus; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | /** |
| | |
| | | * @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 |
| | | TransactionDefinition transactionDefinition; |
| | | |
| | | @Autowired |
| | | private DeviceMapper deviceMapper; |
| | | |
| | | @Autowired |
| | | private DeviceChannelMapper deviceChannelMapper; |
| | | private DeviceChannelMapper deviceChannelMapper; |
| | | |
| | | @Autowired |
| | | private DeviceMobilePositionMapper deviceMobilePositionMapper; |
| | | |
| | | @Autowired |
| | | private ParentPlatformMapper platformMapper; |
| | |
| | | @Autowired |
| | | private IRedisCatchStorage redisCatchStorage; |
| | | |
| | | @Autowired |
| | | private PlatformChannelMapper platformChannelMapper; |
| | | |
| | | @Autowired |
| | | private StreamProxyMapper streamProxyMapper; |
| | | |
| | | @Autowired |
| | | private StreamPushMapper streamPushMapper; |
| | | |
| | | @Autowired |
| | | private GbStreamMapper gbStreamMapper; |
| | | |
| | | @Autowired |
| | | private VideoStreamSessionManager streamSession; |
| | | |
| | | |
| | | /** |
| | |
| | | public synchronized void updateChannel(String deviceId, DeviceChannel channel) { |
| | | String channelId = channel.getChannelId(); |
| | | channel.setDeviceId(deviceId); |
| | | channel.setStreamId(streamSession.getStreamId(deviceId, channel.getChannelId())); |
| | | DeviceChannel deviceChannel = deviceChannelMapper.queryChannel(deviceId, channelId); |
| | | if (deviceChannel == null) { |
| | | deviceChannelMapper.add(channel); |
| | |
| | | */ |
| | | @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; |
| | | } |
| | | |
| | | /** |
| | |
| | | @Override |
| | | public synchronized boolean online(String deviceId) { |
| | | Device device = deviceMapper.getDeviceByDeviceId(deviceId); |
| | | device.setOnline(1); |
| | | System.out.println("更新设备在线"); |
| | | if (device == null) { |
| | | return false; |
| | | } |
| | | device.setOnline(1); |
| | | logger.info("更新设备在线: " + deviceId); |
| | | return deviceMapper.update(device) > 0; |
| | | } |
| | | |
| | |
| | | */ |
| | | @Override |
| | | public synchronized boolean outline(String deviceId) { |
| | | logger.info("更新设备离线: " + deviceId); |
| | | Device device = deviceMapper.getDeviceByDeviceId(deviceId); |
| | | if (device == null) return false; |
| | | device.setOnline(0); |
| | | System.out.println("更新设备离线"); |
| | | return deviceMapper.update(device) > 0; |
| | | } |
| | | |
| | | /** |
| | | * 更新所有设备离线 |
| | | * |
| | | * @return true:更新成功 false:更新失败 |
| | | */ |
| | | @Override |
| | | public synchronized boolean outlineForAll() { |
| | | logger.info("更新所有设备离线"); |
| | | int result = deviceMapper.outlineForAll(); |
| | | return result > 0; |
| | | } |
| | | |
| | | /** |
| | | * 清空通道 |
| | | * @param deviceId |
| | | */ |
| | | @Override |
| | | public void cleanChannelsForDevice(String deviceId) { |
| | | int result = deviceChannelMapper.cleanChannelsByDeviceId(deviceId); |
| | | deviceChannelMapper.cleanChannelsByDeviceId(deviceId); |
| | | } |
| | | |
| | | /** |
| | | * 添加Mobile Position设备移动位置 |
| | | * @param mobilePosition |
| | | */ |
| | | @Override |
| | | public synchronized boolean insertMobilePosition(MobilePosition mobilePosition) { |
| | | return deviceMobilePositionMapper.insertNewPosition(mobilePosition) > 0; |
| | | } |
| | | |
| | | /** |
| | | * 查询移动位置轨迹 |
| | | * @param deviceId |
| | | * @param startTime |
| | | * @param endTime |
| | | */ |
| | | @Override |
| | | public synchronized List<MobilePosition> queryMobilePositions(String deviceId, String startTime, String endTime) { |
| | | return deviceMobilePositionMapper.queryPositionByDeviceIdAndTime(deviceId, startTime, endTime); |
| | | } |
| | | |
| | | @Override |
| | |
| | | @Override |
| | | public boolean updateParentPlatform(ParentPlatform parentPlatform) { |
| | | int result = 0; |
| | | ParentPlatformCatch parentPlatformCatch = redisCatchStorage.queryPlatformCatchInfo(parentPlatform.getDeviceGBId()); |
| | | if ( platformMapper.getParentPlatById(parentPlatform.getDeviceGBId()) == null) { |
| | | ParentPlatformCatch parentPlatformCatch = redisCatchStorage.queryPlatformCatchInfo(parentPlatform.getServerGBId()); // .getDeviceGBId()); |
| | | if (parentPlatform.getId() == null ) { |
| | | result = platformMapper.addParentPlatform(parentPlatform); |
| | | |
| | | if (parentPlatformCatch == null) { |
| | | parentPlatformCatch = new ParentPlatformCatch(); |
| | | parentPlatformCatch.setParentPlatform(parentPlatform); |
| | | parentPlatformCatch.setId(parentPlatform.getDeviceGBId()); |
| | | 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); |
| | | } |
| | | // 更新缓存 |
| | |
| | | return result > 0; |
| | | } |
| | | |
| | | @Transactional |
| | | @Override |
| | | public boolean deleteParentPlatform(ParentPlatform parentPlatform) { |
| | | int result = platformMapper.delParentPlatform(parentPlatform); |
| | | // 删除关联的通道 |
| | | platformChannelMapper.cleanChannelForGB(parentPlatform.getServerGBId()); |
| | | return result > 0; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | @Override |
| | | public ParentPlatform queryParentPlatById(String platformGbId) { |
| | | return platformMapper.getParentPlatById(platformGbId); |
| | | public ParentPlatform queryParentPlatByServerGBId(String platformGbId) { |
| | | return platformMapper.getParentPlatByServerGBId(platformGbId); |
| | | } |
| | | |
| | | @Override |
| | |
| | | |
| | | |
| | | @Override |
| | | public PageInfo<ChannelReduce> queryChannelListInAll(int page, int count, String query, Boolean online, |
| | | Boolean channelType, String parentChannelId) { |
| | | public PageInfo<ChannelReduce> queryAllChannelList(int page, int count, String query, Boolean online, |
| | | Boolean channelType, String platformId, Boolean inPlatform) { |
| | | PageHelper.startPage(page, count); |
| | | List<ChannelReduce> all = deviceChannelMapper.queryChannelListInAll(query, online, channelType, parentChannelId); |
| | | List<ChannelReduce> all = deviceChannelMapper.queryChannelListInAll(query, online, channelType, platformId, inPlatform); |
| | | return new PageInfo<>(all); |
| | | } |
| | | |
| | | @Override |
| | | public List<ChannelReduce> queryChannelListInParentPlatform(String platformId) { |
| | | |
| | | @Transactional |
| | | return deviceChannelMapper.queryChannelListInAll(null, null, null, platformId, true); |
| | | } |
| | | |
| | | @Override |
| | | public int updateChannelForGB(String platformId, List<ChannelReduce> channelReduces) { |
| | | return 0; |
| | | |
| | | Map<String, ChannelReduce> deviceAndChannels = new HashMap<>(); |
| | | for (ChannelReduce channelReduce : channelReduces) { |
| | | deviceAndChannels.put(channelReduce.getDeviceId() + "_" + channelReduce.getChannelId(), channelReduce); |
| | | } |
| | | List<String> deviceAndChannelList = new ArrayList<>(deviceAndChannels.keySet()); |
| | | // 查询当前已经存在的 |
| | | List<String> relatedPlatformchannels = platformChannelMapper.findChannelRelatedPlatform(platformId, deviceAndChannelList); |
| | | if (relatedPlatformchannels != null) { |
| | | deviceAndChannelList.removeAll(relatedPlatformchannels); |
| | | } |
| | | for (String relatedPlatformchannel : relatedPlatformchannels) { |
| | | deviceAndChannels.remove(relatedPlatformchannel); |
| | | } |
| | | List<ChannelReduce> channelReducesToAdd = new ArrayList<>(deviceAndChannels.values()); |
| | | // 对剩下的数据进行存储 |
| | | int result = 0; |
| | | if (channelReducesToAdd.size() > 0) { |
| | | result = platformChannelMapper.addChannels(platformId, channelReducesToAdd); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public int delChannelForGB(String platformId, List<ChannelReduce> channelReduces) { |
| | | |
| | | int result = platformChannelMapper.delChannelForGB(platformId, channelReduces); |
| | | |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | public DeviceChannel queryChannelInParentPlatform(String platformId, String channelId) { |
| | | DeviceChannel channel = platformChannelMapper.queryChannelInParentPlatform(platformId, channelId); |
| | | return channel; |
| | | } |
| | | |
| | | @Override |
| | | public Device queryVideoDeviceByPlatformIdAndChannelId(String platformId, String channelId) { |
| | | Device device = platformChannelMapper.queryVideoDeviceByPlatformIdAndChannelId(platformId, channelId); |
| | | return device; |
| | | } |
| | | |
| | | /** |
| | | * 查询最新移动位置 |
| | | * @param deviceId |
| | | */ |
| | | @Override |
| | | public MobilePosition queryLatestPosition(String deviceId) { |
| | | return deviceMobilePositionMapper.queryLatestPositionByDevice(deviceId); |
| | | } |
| | | |
| | | /** |
| | | * 删除指定设备的所有移动位置 |
| | | * @param deviceId |
| | | */ |
| | | public int clearMobilePositionsByDeviceId(String deviceId) { |
| | | return deviceMobilePositionMapper.clearMobilePositionsByDeviceId(deviceId); |
| | | } |
| | | |
| | | /** |
| | | * 新增代理流 |
| | | * @param streamProxyItem |
| | | * @return |
| | | */ |
| | | @Override |
| | | public boolean addStreamProxy(StreamProxyItem streamProxyItem) { |
| | | TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition); |
| | | boolean result = false; |
| | | streamProxyItem.setStreamType("proxy"); |
| | | streamProxyItem.setStatus(true); |
| | | try { |
| | | if (gbStreamMapper.add(streamProxyItem)<0 || streamProxyMapper.add(streamProxyItem) < 0) { |
| | | //事务回滚 |
| | | dataSourceTransactionManager.rollback(transactionStatus); |
| | | } |
| | | result = true; |
| | | dataSourceTransactionManager.commit(transactionStatus); //手动提交 |
| | | }catch (Exception e) { |
| | | dataSourceTransactionManager.rollback(transactionStatus); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 更新代理流 |
| | | * @param streamProxyItem |
| | | * @return |
| | | */ |
| | | @Override |
| | | public boolean updateStreamProxy(StreamProxyItem streamProxyItem) { |
| | | TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition); |
| | | boolean result = false; |
| | | streamProxyItem.setStreamType("proxy"); |
| | | try { |
| | | if (gbStreamMapper.update(streamProxyItem)<0 || streamProxyMapper.update(streamProxyItem) < 0) { |
| | | //事务回滚 |
| | | dataSourceTransactionManager.rollback(transactionStatus); |
| | | } |
| | | dataSourceTransactionManager.commit(transactionStatus); //手动提交 |
| | | result = true; |
| | | }catch (Exception e) { |
| | | e.printStackTrace(); |
| | | dataSourceTransactionManager.rollback(transactionStatus); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 移除代理流 |
| | | * @param app |
| | | * @param stream |
| | | * @return |
| | | */ |
| | | @Override |
| | | public int deleteStreamProxy(String app, String stream) { |
| | | return streamProxyMapper.del(app, stream); |
| | | } |
| | | |
| | | /** |
| | | * 根据是否启用获取代理流列表 |
| | | * @param enable |
| | | * @return |
| | | */ |
| | | @Override |
| | | public List<StreamProxyItem> getStreamProxyListForEnable(boolean enable) { |
| | | return streamProxyMapper.selectForEnable(enable); |
| | | } |
| | | |
| | | /** |
| | | * 分页查询代理流列表 |
| | | * @param page |
| | | * @param count |
| | | * @return |
| | | */ |
| | | @Override |
| | | public PageInfo<StreamProxyItem> queryStreamProxyList(Integer page, Integer count) { |
| | | PageHelper.startPage(page, count); |
| | | List<StreamProxyItem> all = streamProxyMapper.selectAll(); |
| | | return new PageInfo<>(all); |
| | | } |
| | | |
| | | /** |
| | | * 根据国标ID获取平台关联的直播流 |
| | | * @param platformId |
| | | * @param gbId |
| | | * @return |
| | | */ |
| | | @Override |
| | | public GbStream queryStreamInParentPlatform(String platformId, String gbId) { |
| | | return gbStreamMapper.queryStreamInPlatform(platformId, gbId); |
| | | } |
| | | |
| | | /** |
| | | * 获取平台关联的直播流 |
| | | * @param platformId |
| | | * @return |
| | | */ |
| | | @Override |
| | | public List<GbStream> queryGbStreamListInPlatform(String platformId) { |
| | | return gbStreamMapper.queryGbStreamListInPlatform(platformId); |
| | | } |
| | | |
| | | /** |
| | | * 按照是app和stream获取代理流 |
| | | * @param app |
| | | * @param stream |
| | | * @return |
| | | */ |
| | | @Override |
| | | public StreamProxyItem queryStreamProxy(String app, String stream){ |
| | | return streamProxyMapper.selectOne(app, stream); |
| | | } |
| | | |
| | | @Override |
| | | public void updateMediaList(List<StreamPushItem> streamPushItems) { |
| | | if (streamPushItems == null || streamPushItems.size() == 0) return; |
| | | logger.info("updateMediaList: " + streamPushItems.size()); |
| | | streamPushMapper.addAll(streamPushItems); |
| | | // TODO 待优化 |
| | | for (int i = 0; i < streamPushItems.size(); i++) { |
| | | gbStreamMapper.setStatus(streamPushItems.get(i).getApp(), streamPushItems.get(i).getStream(), true); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void updateMedia(StreamPushItem streamPushItem) { |
| | | streamPushMapper.del(streamPushItem.getApp(), streamPushItem.getStream()); |
| | | streamPushMapper.add(streamPushItem); |
| | | gbStreamMapper.setStatus(streamPushItem.getApp(), streamPushItem.getStream(), true); |
| | | } |
| | | |
| | | @Override |
| | | public void removeMedia(String app, String stream) { |
| | | streamPushMapper.del(app, stream); |
| | | } |
| | | |
| | | @Override |
| | | public void clearMediaList() { |
| | | streamPushMapper.clear(); |
| | | } |
| | | |
| | | @Override |
| | | public void mediaOutline(String app, String streamId) { |
| | | gbStreamMapper.setStatus(app, streamId, false); |
| | | } |
| | | |
| | | @Override |
| | | public void updateParentPlatformStatus(String platformGbID, boolean online) { |
| | | platformMapper.updateParentPlatformStatus(platformGbID, online); |
| | | } |
| | | } |