GbStreamServiceImpl.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package com.genersoft.iot.vmp.service.impl;
  2. import com.genersoft.iot.vmp.conf.SipConfig;
  3. import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
  4. import com.genersoft.iot.vmp.gb28181.bean.GbStream;
  5. import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform;
  6. import com.genersoft.iot.vmp.gb28181.event.EventPublisher;
  7. import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent;
  8. import com.genersoft.iot.vmp.media.zlm.dto.StreamProxyItem;
  9. import com.genersoft.iot.vmp.storager.dao.GbStreamMapper;
  10. import com.genersoft.iot.vmp.storager.dao.ParentPlatformMapper;
  11. import com.genersoft.iot.vmp.storager.dao.PlatformGbStreamMapper;
  12. import com.genersoft.iot.vmp.service.IGbStreamService;
  13. import com.github.pagehelper.PageHelper;
  14. import com.github.pagehelper.PageInfo;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.transaction.TransactionDefinition;
  21. import org.springframework.transaction.TransactionStatus;
  22. import org.springframework.util.StringUtils;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. @Service
  26. public class GbStreamServiceImpl implements IGbStreamService {
  27. private final static Logger logger = LoggerFactory.getLogger(GbStreamServiceImpl.class);
  28. @Autowired
  29. DataSourceTransactionManager dataSourceTransactionManager;
  30. @Autowired
  31. TransactionDefinition transactionDefinition;
  32. @Autowired
  33. private GbStreamMapper gbStreamMapper;
  34. @Autowired
  35. private PlatformGbStreamMapper platformGbStreamMapper;
  36. @Autowired
  37. private ParentPlatformMapper platformMapper;
  38. @Autowired
  39. private SipConfig sipConfig;
  40. @Autowired
  41. private EventPublisher eventPublisher;
  42. @Override
  43. public PageInfo<GbStream> getAll(Integer page, Integer count, String platFormId, String catalogId, String query, Boolean pushing, String mediaServerId) {
  44. PageHelper.startPage(page, count);
  45. List<GbStream> all = gbStreamMapper.selectAll(platFormId, catalogId, query, pushing, mediaServerId);
  46. return new PageInfo<>(all);
  47. }
  48. @Override
  49. public void del(String app, String stream) {
  50. gbStreamMapper.del(app, stream);
  51. }
  52. @Override
  53. public boolean addPlatformInfo(List<GbStream> gbStreams, String platformId, String catalogId) {
  54. // 放在事务内执行
  55. boolean result = false;
  56. TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
  57. ParentPlatform parentPlatform = platformMapper.getParentPlatByServerGBId(platformId);
  58. if (catalogId == null) catalogId = parentPlatform.getCatalogId();
  59. try {
  60. List<DeviceChannel> deviceChannelList = new ArrayList<>();
  61. for (GbStream gbStream : gbStreams) {
  62. gbStream.setCatalogId(catalogId);
  63. gbStream.setPlatformId(platformId);
  64. // TODO 修改为批量提交
  65. platformGbStreamMapper.add(gbStream);
  66. DeviceChannel deviceChannelListByStream = getDeviceChannelListByStream(gbStream, catalogId, parentPlatform.getDeviceGBId());
  67. deviceChannelList.add(deviceChannelListByStream);
  68. }
  69. dataSourceTransactionManager.commit(transactionStatus); //手动提交
  70. eventPublisher.catalogEventPublish(platformId, deviceChannelList, CatalogEvent.ADD);
  71. result = true;
  72. }catch (Exception e) {
  73. logger.error("批量保存流与平台的关系时错误", e);
  74. dataSourceTransactionManager.rollback(transactionStatus);
  75. }
  76. return result;
  77. }
  78. @Override
  79. public DeviceChannel getDeviceChannelListByStream(GbStream gbStream, String catalogId, String deviceGBId) {
  80. DeviceChannel deviceChannel = new DeviceChannel();
  81. deviceChannel.setChannelId(gbStream.getGbId());
  82. deviceChannel.setName(gbStream.getName());
  83. deviceChannel.setLongitude(gbStream.getLongitude());
  84. deviceChannel.setLatitude(gbStream.getLatitude());
  85. deviceChannel.setDeviceId(deviceGBId);
  86. deviceChannel.setManufacture("wvp-pro");
  87. deviceChannel.setStatus(gbStream.isStatus()?1:0);
  88. deviceChannel.setParentId(catalogId ==null?gbStream.getCatalogId():catalogId);
  89. deviceChannel.setRegisterWay(1);
  90. deviceChannel.setCivilCode(deviceGBId.substring(0, 6));
  91. deviceChannel.setModel("live");
  92. deviceChannel.setOwner("wvp-pro");
  93. deviceChannel.setParental(0);
  94. deviceChannel.setSecrecy("0");
  95. return deviceChannel;
  96. }
  97. @Override
  98. public boolean delPlatformInfo(String platformId, List<GbStream> gbStreams) {
  99. // 放在事务内执行
  100. boolean result = false;
  101. TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
  102. try {
  103. List<DeviceChannel> deviceChannelList = new ArrayList<>();
  104. platformGbStreamMapper.delByAppAndStreamsByPlatformId(gbStreams, platformId);
  105. for (GbStream gbStream : gbStreams) {
  106. DeviceChannel deviceChannel = new DeviceChannel();
  107. deviceChannel.setChannelId(gbStream.getGbId());
  108. deviceChannelList.add(deviceChannel);
  109. }
  110. eventPublisher.catalogEventPublish(platformId, deviceChannelList, CatalogEvent.DEL);
  111. dataSourceTransactionManager.commit(transactionStatus); //手动提交
  112. result = true;
  113. }catch (Exception e) {
  114. logger.error("批量移除流与平台的关系时错误", e);
  115. dataSourceTransactionManager.rollback(transactionStatus);
  116. }
  117. return result;
  118. }
  119. @Override
  120. public void sendCatalogMsg(GbStream gbStream, String type) {
  121. List<GbStream> gbStreams = new ArrayList<>();
  122. if (gbStream.getGbId() != null) {
  123. gbStreams.add(gbStream);
  124. }else {
  125. StreamProxyItem streamProxyItem = gbStreamMapper.selectOne(gbStream.getApp(), gbStream.getStream());
  126. if (streamProxyItem != null && streamProxyItem.getGbId() != null){
  127. gbStreams.add(streamProxyItem);
  128. }
  129. }
  130. sendCatalogMsgs(gbStreams, type);
  131. }
  132. @Override
  133. public void sendCatalogMsgs(List<GbStream> gbStreams, String type) {
  134. if (gbStreams.size() > 0) {
  135. for (GbStream gs : gbStreams) {
  136. if (StringUtils.isEmpty(gs.getGbId())){
  137. continue;
  138. }
  139. List<ParentPlatform> parentPlatforms = platformGbStreamMapper.selectByAppAndStream(gs.getApp(), gs.getStream());
  140. if (parentPlatforms.size() > 0) {
  141. for (ParentPlatform parentPlatform : parentPlatforms) {
  142. if (parentPlatform != null) {
  143. eventPublisher.catalogEventPublishForStream(parentPlatform.getServerGBId(), gs, type);
  144. }
  145. }
  146. }
  147. }
  148. }
  149. }
  150. }