|  |  |  | 
|---|
|  |  |  | import com.genersoft.iot.vmp.service.IGbStreamService; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.service.IMediaServerService; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.service.IStreamPushService; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.service.bean.StreamPushItemFromRedis; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.storager.IRedisCatchStorage; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.storager.dao.*; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.utils.DateUtil; | 
|---|
|  |  |  | 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.Service; | 
|---|
|  |  |  | import org.springframework.transaction.TransactionDefinition; | 
|---|
|  |  |  | import org.springframework.transaction.TransactionStatus; | 
|---|
|  |  |  | import org.springframework.util.StringUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private StreamPushMapper streamPushMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private StreamProxyMapper streamProxyMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private ParentPlatformMapper parentPlatformMapper; | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private IMediaServerService mediaServerService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | DataSourceTransactionManager dataSourceTransactionManager; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | TransactionDefinition transactionDefinition; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<StreamPushItem> handleJSON(String jsonData, MediaServerItem mediaServerItem) { | 
|---|
|  |  |  | 
|---|
|  |  |  | streamPushItem.setOriginType(item.getOriginType()); | 
|---|
|  |  |  | streamPushItem.setOriginTypeStr(item.getOriginTypeStr()); | 
|---|
|  |  |  | streamPushItem.setOriginUrl(item.getOriginUrl()); | 
|---|
|  |  |  | streamPushItem.setCreateStamp(item.getCreateStamp() * 1000); | 
|---|
|  |  |  | streamPushItem.setCreateTime(DateUtil.getNow()); | 
|---|
|  |  |  | streamPushItem.setAliveSecond(item.getAliveSecond()); | 
|---|
|  |  |  | streamPushItem.setStatus(true); | 
|---|
|  |  |  | streamPushItem.setStreamType("push"); | 
|---|
|  |  |  | 
|---|
|  |  |  | public boolean saveToGB(GbStream stream) { | 
|---|
|  |  |  | stream.setStreamType("push"); | 
|---|
|  |  |  | stream.setStatus(true); | 
|---|
|  |  |  | stream.setCreateStamp(System.currentTimeMillis()); | 
|---|
|  |  |  | stream.setCreateTime(DateUtil.getNow()); | 
|---|
|  |  |  | int add = gbStreamMapper.add(stream); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 查找开启了全部直播流共享的上级平台 | 
|---|
|  |  |  | List<ParentPlatform> parentPlatforms = parentPlatformMapper.selectAllAhareAllLiveStream(); | 
|---|
|  |  |  | if (parentPlatforms.size() > 0) { | 
|---|
|  |  |  | for (ParentPlatform parentPlatform : parentPlatforms) { | 
|---|
|  |  |  | stream.setCatalogId(parentPlatform.getCatalogId()); | 
|---|
|  |  |  | stream.setPlatformId(parentPlatform.getServerGBId()); | 
|---|
|  |  |  | String streamId = stream.getStream(); | 
|---|
|  |  |  | StreamProxyItem streamProxyItem = platformGbStreamMapper.selectOne(stream.getApp(), streamId, parentPlatform.getServerGBId()); | 
|---|
|  |  |  | if (streamProxyItem == null) { | 
|---|
|  |  |  | platformGbStreamMapper.add(stream); | 
|---|
|  |  |  | eventPublisher.catalogEventPublishForStream(parentPlatform.getServerGBId(), stream, CatalogEvent.ADD); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | if (!streamProxyItem.getGbId().equals(stream.getGbId())) { | 
|---|
|  |  |  | // 此流使用另一个国标Id已经与该平台关联,移除此记录 | 
|---|
|  |  |  | platformGbStreamMapper.delByAppAndStreamAndPlatform(stream.getApp(), streamId, parentPlatform.getServerGBId()); | 
|---|
|  |  |  | platformGbStreamMapper.add(stream); | 
|---|
|  |  |  | eventPublisher.catalogEventPublishForStream(parentPlatform.getServerGBId(), stream, CatalogEvent.ADD); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return add > 0; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public StreamPushItem getPush(String app, String streamId) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return streamPushMapper.selectOne(app, streamId); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | streamPushMapper.deleteWithoutGBId(mediaServerId); | 
|---|
|  |  |  | gbStreamMapper.deleteWithoutGBId("push", mediaServerId); | 
|---|
|  |  |  | // 其他的流设置未启用 | 
|---|
|  |  |  | gbStreamMapper.updateStatusByMediaServerId(mediaServerId, false); | 
|---|
|  |  |  | streamPushMapper.updateStatusByMediaServerId(mediaServerId, false); | 
|---|
|  |  |  | streamProxyMapper.updateStatusByMediaServerId(mediaServerId, false); | 
|---|
|  |  |  | // 发送流停止消息 | 
|---|
|  |  |  | String type = "PUSH"; | 
|---|
|  |  |  | // 发送redis消息 | 
|---|
|  |  |  | 
|---|
|  |  |  | streamPushItem.setStreamType("push"); | 
|---|
|  |  |  | streamPushItem.setStatus(true); | 
|---|
|  |  |  | streamPushItem.setGbId("34020000004111" + gbId); | 
|---|
|  |  |  | streamPushItem.setCreateStamp(System.currentTimeMillis()); | 
|---|
|  |  |  | streamPushItem.setCreateTime(DateUtil.getNow()); | 
|---|
|  |  |  | gbId ++; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | int  limitCount = 30; | 
|---|
|  |  |  | 
|---|
|  |  |  | public void batchAdd(List<StreamPushItem> streamPushItems) { | 
|---|
|  |  |  | streamPushMapper.addAll(streamPushItems); | 
|---|
|  |  |  | gbStreamMapper.batchAdd(streamPushItems); | 
|---|
|  |  |  | // 查找开启了全部直播流共享的上级平台 | 
|---|
|  |  |  | List<ParentPlatform> parentPlatforms = parentPlatformMapper.selectAllAhareAllLiveStream(); | 
|---|
|  |  |  | if (parentPlatforms.size() > 0) { | 
|---|
|  |  |  | for (StreamPushItem stream : streamPushItems) { | 
|---|
|  |  |  | for (ParentPlatform parentPlatform : parentPlatforms) { | 
|---|
|  |  |  | stream.setCatalogId(parentPlatform.getCatalogId()); | 
|---|
|  |  |  | stream.setPlatformId(parentPlatform.getServerGBId()); | 
|---|
|  |  |  | String streamId = stream.getStream(); | 
|---|
|  |  |  | StreamProxyItem streamProxyItem = platformGbStreamMapper.selectOne(stream.getApp(), streamId, parentPlatform.getServerGBId()); | 
|---|
|  |  |  | if (streamProxyItem == null) { | 
|---|
|  |  |  | platformGbStreamMapper.add(stream); | 
|---|
|  |  |  | eventPublisher.catalogEventPublishForStream(parentPlatform.getServerGBId(), stream, CatalogEvent.ADD); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | if (!streamProxyItem.getGbId().equals(stream.getGbId())) { | 
|---|
|  |  |  | // 此流使用另一个国标Id已经与该平台关联,移除此记录 | 
|---|
|  |  |  | platformGbStreamMapper.delByAppAndStreamAndPlatform(stream.getApp(), streamId, parentPlatform.getServerGBId()); | 
|---|
|  |  |  | platformGbStreamMapper.add(stream); | 
|---|
|  |  |  | eventPublisher.catalogEventPublishForStream(parentPlatform.getServerGBId(), stream, CatalogEvent.ADD); | 
|---|
|  |  |  | stream.setGbId(streamProxyItem.getGbId()); | 
|---|
|  |  |  | eventPublisher.catalogEventPublishForStream(parentPlatform.getServerGBId(), stream, CatalogEvent.DEL); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void allStreamOffline() { | 
|---|
|  |  |  | List<GbStream> onlinePushers = streamPushMapper.getOnlinePusherForGb(); | 
|---|
|  |  |  | if (onlinePushers.size() == 0) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | streamPushMapper.setAllStreamOffline(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 发送通知 | 
|---|
|  |  |  | eventPublisher.catalogEventPublishForStream(null, onlinePushers, CatalogEvent.OFF); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void offline(List<StreamPushItemFromRedis> offlineStreams) { | 
|---|
|  |  |  | // 更新部分设备离线 | 
|---|
|  |  |  | List<GbStream> onlinePushers = streamPushMapper.getOnlinePusherForGbInList(offlineStreams); | 
|---|
|  |  |  | streamPushMapper.offline(offlineStreams); | 
|---|
|  |  |  | // 发送通知 | 
|---|
|  |  |  | eventPublisher.catalogEventPublishForStream(null, onlinePushers, CatalogEvent.OFF); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public void online(List<StreamPushItemFromRedis> onlineStreams) { | 
|---|
|  |  |  | // 更新部分设备上线streamPushService | 
|---|
|  |  |  | List<GbStream> onlinePushers = streamPushMapper.getOfflinePusherForGbInList(onlineStreams); | 
|---|
|  |  |  | streamPushMapper.online(onlineStreams); | 
|---|
|  |  |  | // 发送通知 | 
|---|
|  |  |  | eventPublisher.catalogEventPublishForStream(null, onlinePushers, CatalogEvent.ON); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public boolean add(StreamPushItem stream) { | 
|---|
|  |  |  | stream.setUpdateTime(DateUtil.getNow()); | 
|---|
|  |  |  | stream.setCreateTime(DateUtil.getNow()); | 
|---|
|  |  |  | stream.setServerId(userSetting.getServerId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 放在事务内执行 | 
|---|
|  |  |  | boolean result = false; | 
|---|
|  |  |  | TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | int addStreamResult = streamPushMapper.add(stream); | 
|---|
|  |  |  | if (!StringUtils.isEmpty(stream.getGbId())) { | 
|---|
|  |  |  | gbStreamMapper.add(stream); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | dataSourceTransactionManager.commit(transactionStatus); | 
|---|
|  |  |  | result = true; | 
|---|
|  |  |  | }catch (Exception e) { | 
|---|
|  |  |  | logger.error("批量移除流与平台的关系时错误", e); | 
|---|
|  |  |  | dataSourceTransactionManager.rollback(transactionStatus); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return result; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|