GbStreamServiceImpl.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.setStatus(1);
  89. deviceChannel.setParentId(catalogId ==null?gbStream.getCatalogId():catalogId);
  90. deviceChannel.setRegisterWay(1);
  91. deviceChannel.setCivilCode(deviceGBId.substring(0, 6));
  92. deviceChannel.setModel("live");
  93. deviceChannel.setOwner("wvp-pro");
  94. deviceChannel.setParental(0);
  95. deviceChannel.setSecrecy("0");
  96. return deviceChannel;
  97. }
  98. @Override
  99. public boolean delPlatformInfo(String platformId, List<GbStream> gbStreams) {
  100. // 放在事务内执行
  101. boolean result = false;
  102. TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
  103. try {
  104. List<DeviceChannel> deviceChannelList = new ArrayList<>();
  105. platformGbStreamMapper.delByAppAndStreamsByPlatformId(gbStreams, platformId);
  106. for (GbStream gbStream : gbStreams) {
  107. DeviceChannel deviceChannel = new DeviceChannel();
  108. deviceChannel.setChannelId(gbStream.getGbId());
  109. deviceChannelList.add(deviceChannel);
  110. }
  111. eventPublisher.catalogEventPublish(platformId, deviceChannelList, CatalogEvent.DEL);
  112. dataSourceTransactionManager.commit(transactionStatus); //手动提交
  113. result = true;
  114. }catch (Exception e) {
  115. logger.error("批量移除流与平台的关系时错误", e);
  116. dataSourceTransactionManager.rollback(transactionStatus);
  117. }
  118. return result;
  119. }
  120. @Override
  121. public void sendCatalogMsg(GbStream gbStream, String type) {
  122. List<GbStream> gbStreams = new ArrayList<>();
  123. if (gbStream.getGbId() != null) {
  124. gbStreams.add(gbStream);
  125. }else {
  126. StreamProxyItem streamProxyItem = gbStreamMapper.selectOne(gbStream.getApp(), gbStream.getStream());
  127. if (streamProxyItem != null && streamProxyItem.getGbId() != null){
  128. gbStreams.add(streamProxyItem);
  129. }
  130. }
  131. sendCatalogMsgs(gbStreams, type);
  132. }
  133. @Override
  134. public void sendCatalogMsgs(List<GbStream> gbStreams, String type) {
  135. if (gbStreams.size() > 0) {
  136. for (GbStream gs : gbStreams) {
  137. if (StringUtils.isEmpty(gs.getGbId())){
  138. continue;
  139. }
  140. List<ParentPlatform> parentPlatforms = platformGbStreamMapper.selectByAppAndStream(gs.getApp(), gs.getStream());
  141. if (parentPlatforms.size() > 0) {
  142. for (ParentPlatform parentPlatform : parentPlatforms) {
  143. if (parentPlatform != null) {
  144. eventPublisher.catalogEventPublishForStream(parentPlatform.getServerGBId(), gs, type);
  145. }
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }