GbStreamServiceImpl.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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) {
  59. catalogId = parentPlatform.getCatalogId();
  60. }
  61. try {
  62. List<DeviceChannel> deviceChannelList = new ArrayList<>();
  63. for (GbStream gbStream : gbStreams) {
  64. gbStream.setCatalogId(catalogId);
  65. gbStream.setPlatformId(platformId);
  66. // TODO 修改为批量提交
  67. platformGbStreamMapper.add(gbStream);
  68. DeviceChannel deviceChannelListByStream = getDeviceChannelListByStream(gbStream, catalogId, parentPlatform);
  69. deviceChannelList.add(deviceChannelListByStream);
  70. }
  71. dataSourceTransactionManager.commit(transactionStatus); //手动提交
  72. eventPublisher.catalogEventPublish(platformId, deviceChannelList, CatalogEvent.ADD);
  73. result = true;
  74. }catch (Exception e) {
  75. logger.error("批量保存流与平台的关系时错误", e);
  76. dataSourceTransactionManager.rollback(transactionStatus);
  77. }
  78. return result;
  79. }
  80. @Override
  81. public DeviceChannel getDeviceChannelListByStream(GbStream gbStream, String catalogId, ParentPlatform platform) {
  82. DeviceChannel deviceChannel = new DeviceChannel();
  83. deviceChannel.setChannelId(gbStream.getGbId());
  84. deviceChannel.setName(gbStream.getName());
  85. deviceChannel.setLongitude(gbStream.getLongitude());
  86. deviceChannel.setLatitude(gbStream.getLatitude());
  87. deviceChannel.setDeviceId(platform.getDeviceGBId());
  88. deviceChannel.setManufacture("wvp-pro");
  89. // deviceChannel.setStatus(gbStream.isStatus()?1:0);
  90. deviceChannel.setStatus(1);
  91. deviceChannel.setParentId(catalogId ==null?gbStream.getCatalogId():catalogId);
  92. deviceChannel.setRegisterWay(1);
  93. if (catalogId.length() > 0 && catalogId.length() <= 10) {
  94. // 父节点是行政区划,则设置CivilCode使用此行政区划
  95. deviceChannel.setCivilCode(catalogId);
  96. }else {
  97. deviceChannel.setCivilCode(platform.getAdministrativeDivision());
  98. }
  99. deviceChannel.setModel("live");
  100. deviceChannel.setOwner("wvp-pro");
  101. deviceChannel.setParental(0);
  102. deviceChannel.setSecrecy("0");
  103. return deviceChannel;
  104. }
  105. @Override
  106. public boolean delPlatformInfo(String platformId, List<GbStream> gbStreams) {
  107. // 放在事务内执行
  108. boolean result = false;
  109. TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
  110. try {
  111. List<DeviceChannel> deviceChannelList = new ArrayList<>();
  112. platformGbStreamMapper.delByAppAndStreamsByPlatformId(gbStreams, platformId);
  113. for (GbStream gbStream : gbStreams) {
  114. DeviceChannel deviceChannel = new DeviceChannel();
  115. deviceChannel.setChannelId(gbStream.getGbId());
  116. deviceChannelList.add(deviceChannel);
  117. }
  118. eventPublisher.catalogEventPublish(platformId, deviceChannelList, CatalogEvent.DEL);
  119. dataSourceTransactionManager.commit(transactionStatus); //手动提交
  120. result = true;
  121. }catch (Exception e) {
  122. logger.error("批量移除流与平台的关系时错误", e);
  123. dataSourceTransactionManager.rollback(transactionStatus);
  124. }
  125. return result;
  126. }
  127. @Override
  128. public void sendCatalogMsg(GbStream gbStream, String type) {
  129. List<GbStream> gbStreams = new ArrayList<>();
  130. if (gbStream.getGbId() != null) {
  131. gbStreams.add(gbStream);
  132. }else {
  133. StreamProxyItem streamProxyItem = gbStreamMapper.selectOne(gbStream.getApp(), gbStream.getStream());
  134. if (streamProxyItem != null && streamProxyItem.getGbId() != null){
  135. gbStreams.add(streamProxyItem);
  136. }
  137. }
  138. sendCatalogMsgs(gbStreams, type);
  139. }
  140. @Override
  141. public void sendCatalogMsgs(List<GbStream> gbStreams, String type) {
  142. if (gbStreams.size() > 0) {
  143. for (GbStream gs : gbStreams) {
  144. if (StringUtils.isEmpty(gs.getGbId())){
  145. continue;
  146. }
  147. List<ParentPlatform> parentPlatforms = platformGbStreamMapper.selectByAppAndStream(gs.getApp(), gs.getStream());
  148. if (parentPlatforms.size() > 0) {
  149. for (ParentPlatform parentPlatform : parentPlatforms) {
  150. if (parentPlatform != null) {
  151. eventPublisher.catalogEventPublishForStream(parentPlatform.getServerGBId(), gs, type);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. }