GbStreamServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package com.genersoft.iot.vmp.service.impl;
  2. import com.genersoft.iot.vmp.gb28181.bean.*;
  3. import com.genersoft.iot.vmp.gb28181.event.EventPublisher;
  4. import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent;
  5. import com.genersoft.iot.vmp.media.zlm.dto.StreamPushItem;
  6. import com.genersoft.iot.vmp.service.IGbStreamService;
  7. import com.genersoft.iot.vmp.storager.dao.GbStreamMapper;
  8. import com.genersoft.iot.vmp.storager.dao.ParentPlatformMapper;
  9. import com.genersoft.iot.vmp.storager.dao.PlatformCatalogMapper;
  10. import com.genersoft.iot.vmp.storager.dao.PlatformGbStreamMapper;
  11. import com.github.pagehelper.PageHelper;
  12. import com.github.pagehelper.PageInfo;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.transaction.TransactionDefinition;
  19. import org.springframework.transaction.TransactionStatus;
  20. import org.springframework.util.ObjectUtils;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. @Service
  24. public class GbStreamServiceImpl implements IGbStreamService {
  25. private final static Logger logger = LoggerFactory.getLogger(GbStreamServiceImpl.class);
  26. @Autowired
  27. DataSourceTransactionManager dataSourceTransactionManager;
  28. @Autowired
  29. TransactionDefinition transactionDefinition;
  30. @Autowired
  31. private GbStreamMapper gbStreamMapper;
  32. @Autowired
  33. private PlatformGbStreamMapper platformGbStreamMapper;
  34. @Autowired
  35. private SubscribeHolder subscribeHolder;
  36. @Autowired
  37. private ParentPlatformMapper platformMapper;
  38. @Autowired
  39. private PlatformCatalogMapper catalogMapper;
  40. @Autowired
  41. private EventPublisher eventPublisher;
  42. @Override
  43. public PageInfo<GbStream> getAll(Integer page, Integer count, String platFormId, String catalogId, String query, String mediaServerId) {
  44. PageHelper.startPage(page, count);
  45. List<GbStream> all = gbStreamMapper.selectAll(platFormId, catalogId, query, 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 (int i = 0; i < gbStreams.size(); i++) {
  64. GbStream gbStream = gbStreams.get(i);
  65. gbStream.setCatalogId(catalogId);
  66. gbStream.setPlatformId(platformId);
  67. // TODO 修改为批量提交
  68. platformGbStreamMapper.add(gbStream);
  69. logger.info("[关联通道]直播流通道 平台:{}, 共需关联通道数:{}, 已关联:{}", platformId, gbStreams.size(), i + 1);
  70. DeviceChannel deviceChannelListByStream = getDeviceChannelListByStreamWithStatus(gbStream, catalogId, parentPlatform);
  71. deviceChannelList.add(deviceChannelListByStream);
  72. }
  73. dataSourceTransactionManager.commit(transactionStatus); //手动提交
  74. if (subscribeHolder.getCatalogSubscribe(platformId) != null) {
  75. eventPublisher.catalogEventPublish(platformId, deviceChannelList, CatalogEvent.ADD);
  76. }
  77. result = true;
  78. }catch (Exception e) {
  79. logger.error("批量保存流与平台的关系时错误", e);
  80. dataSourceTransactionManager.rollback(transactionStatus);
  81. }
  82. return result;
  83. }
  84. @Override
  85. public DeviceChannel getDeviceChannelListByStream(GbStream gbStream, String catalogId, ParentPlatform platform) {
  86. DeviceChannel deviceChannel = new DeviceChannel();
  87. deviceChannel.setChannelId(gbStream.getGbId());
  88. deviceChannel.setName(gbStream.getName());
  89. deviceChannel.setLongitude(gbStream.getLongitude());
  90. deviceChannel.setLatitude(gbStream.getLatitude());
  91. deviceChannel.setDeviceId(platform.getDeviceGBId());
  92. deviceChannel.setManufacture("wvp-pro");
  93. deviceChannel.setStatus(gbStream.isStatus());
  94. deviceChannel.setRegisterWay(1);
  95. deviceChannel.setCivilCode(platform.getAdministrativeDivision());
  96. if (platform.getTreeType().equals(TreeType.CIVIL_CODE)){
  97. deviceChannel.setCivilCode(catalogId);
  98. }else if (platform.getTreeType().equals(TreeType.BUSINESS_GROUP)){
  99. PlatformCatalog catalog = catalogMapper.select(catalogId);
  100. if (catalog == null) {
  101. deviceChannel.setParentId(platform.getDeviceGBId());
  102. deviceChannel.setBusinessGroupId(null);
  103. }else {
  104. deviceChannel.setParentId(catalog.getId());
  105. deviceChannel.setBusinessGroupId(catalog.getBusinessGroupId());
  106. }
  107. }
  108. deviceChannel.setModel("live");
  109. deviceChannel.setOwner("wvp-pro");
  110. deviceChannel.setParental(0);
  111. deviceChannel.setSecrecy("0");
  112. return deviceChannel;
  113. }
  114. @Override
  115. public boolean delPlatformInfo(String platformId, List<GbStream> gbStreams) {
  116. // 放在事务内执行
  117. boolean result = false;
  118. TransactionStatus transactionStatus = dataSourceTransactionManager.getTransaction(transactionDefinition);
  119. try {
  120. List<DeviceChannel> deviceChannelList = new ArrayList<>();
  121. platformGbStreamMapper.delByAppAndStreamsByPlatformId(gbStreams, platformId);
  122. for (GbStream gbStream : gbStreams) {
  123. DeviceChannel deviceChannel = new DeviceChannel();
  124. deviceChannel.setChannelId(gbStream.getGbId());
  125. deviceChannelList.add(deviceChannel);
  126. }
  127. eventPublisher.catalogEventPublish(platformId, deviceChannelList, CatalogEvent.DEL);
  128. dataSourceTransactionManager.commit(transactionStatus); //手动提交
  129. result = true;
  130. }catch (Exception e) {
  131. logger.error("批量移除流与平台的关系时错误", e);
  132. dataSourceTransactionManager.rollback(transactionStatus);
  133. }
  134. return result;
  135. }
  136. @Override
  137. public void sendCatalogMsg(GbStream gbStream, String type) {
  138. if (gbStream == null || type == null) {
  139. logger.warn("[发送目录订阅]类型:流信息或类型为NULL");
  140. return;
  141. }
  142. List<GbStream> gbStreams = new ArrayList<>();
  143. if (gbStream.getGbId() != null) {
  144. gbStreams.add(gbStream);
  145. }else {
  146. GbStream gbStreamIndb = gbStreamMapper.selectOne(gbStream.getApp(), gbStream.getStream());
  147. if (gbStreamIndb != null && gbStreamIndb.getGbId() != null){
  148. gbStreams.add(gbStreamIndb);
  149. }
  150. }
  151. sendCatalogMsgs(gbStreams, type);
  152. }
  153. @Override
  154. public void sendCatalogMsgs(List<GbStream> gbStreams, String type) {
  155. if (gbStreams.size() > 0) {
  156. for (GbStream gs : gbStreams) {
  157. if (ObjectUtils.isEmpty(gs.getGbId())){
  158. continue;
  159. }
  160. List<ParentPlatform> parentPlatforms = platformGbStreamMapper.selectByAppAndStream(gs.getApp(), gs.getStream());
  161. if (parentPlatforms.size() > 0) {
  162. for (ParentPlatform parentPlatform : parentPlatforms) {
  163. if (parentPlatform != null) {
  164. eventPublisher.catalogEventPublishForStream(parentPlatform.getServerGBId(), gs, type);
  165. }
  166. }
  167. }
  168. }
  169. }
  170. }
  171. @Override
  172. public int updateGbIdOrName(List<StreamPushItem> streamPushItemForUpdate) {
  173. return gbStreamMapper.updateGbIdOrName(streamPushItemForUpdate);
  174. }
  175. @Override
  176. public DeviceChannel getDeviceChannelListByStreamWithStatus(GbStream gbStream, String catalogId, ParentPlatform platform) {
  177. DeviceChannel deviceChannel = new DeviceChannel();
  178. deviceChannel.setChannelId(gbStream.getGbId());
  179. deviceChannel.setName(gbStream.getName());
  180. deviceChannel.setLongitude(gbStream.getLongitude());
  181. deviceChannel.setLatitude(gbStream.getLatitude());
  182. deviceChannel.setDeviceId(platform.getDeviceGBId());
  183. deviceChannel.setManufacture("wvp-pro");
  184. // todo 目前是每一条查询一次,需要优化
  185. Boolean status = null;
  186. if ("proxy".equals(gbStream.getStreamType())) {
  187. status = gbStreamMapper.selectStatusForProxy(gbStream.getApp(), gbStream.getStream());
  188. }else {
  189. status = gbStreamMapper.selectStatusForPush(gbStream.getApp(), gbStream.getStream());
  190. }
  191. deviceChannel.setStatus(status != null && status);
  192. deviceChannel.setRegisterWay(1);
  193. deviceChannel.setCivilCode(platform.getAdministrativeDivision());
  194. if (platform.getTreeType().equals(TreeType.CIVIL_CODE)){
  195. deviceChannel.setCivilCode(catalogId);
  196. }else if (platform.getTreeType().equals(TreeType.BUSINESS_GROUP)){
  197. PlatformCatalog catalog = catalogMapper.select(catalogId);
  198. if (catalog == null) {
  199. deviceChannel.setParentId(platform.getDeviceGBId());
  200. deviceChannel.setBusinessGroupId(null);
  201. }else {
  202. deviceChannel.setParentId(catalog.getId());
  203. deviceChannel.setBusinessGroupId(catalog.getBusinessGroupId());
  204. }
  205. }
  206. deviceChannel.setModel("live");
  207. deviceChannel.setOwner("wvp-pro");
  208. deviceChannel.setParental(0);
  209. deviceChannel.setSecrecy("0");
  210. return deviceChannel;
  211. }
  212. @Override
  213. public List<GbStream> getAllGBChannels(String platformId) {
  214. return gbStreamMapper.selectAll(platformId, null, null, null);
  215. }
  216. @Override
  217. public void delAllPlatformInfo(String platformId, String catalogId) {
  218. if (platformId == null) {
  219. return ;
  220. }
  221. ParentPlatform platform = platformMapper.getParentPlatByServerGBId(platformId);
  222. if (platform == null) {
  223. return ;
  224. }
  225. if (ObjectUtils.isEmpty(catalogId)) {
  226. catalogId = platform.getDeviceGBId();
  227. }
  228. if (platformGbStreamMapper.delByPlatformAndCatalogId(platformId, catalogId) > 0) {
  229. List<GbStream> gbStreams = platformGbStreamMapper.queryChannelInParentPlatformAndCatalog(platformId, catalogId);
  230. List<DeviceChannel> deviceChannelList = new ArrayList<>();
  231. for (GbStream gbStream : gbStreams) {
  232. DeviceChannel deviceChannel = new DeviceChannel();
  233. deviceChannel.setChannelId(gbStream.getGbId());
  234. deviceChannelList.add(deviceChannel);
  235. }
  236. eventPublisher.catalogEventPublish(platformId, deviceChannelList, CatalogEvent.DEL);
  237. }
  238. }
  239. }