DeviceServiceImpl.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package com.genersoft.iot.vmp.service.impl;
  2. import com.genersoft.iot.vmp.conf.DynamicTask;
  3. import com.genersoft.iot.vmp.gb28181.bean.Device;
  4. import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommander;
  5. import com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.response.cmd.CatalogResponseMessageHandler;
  6. import com.genersoft.iot.vmp.service.IDeviceService;
  7. import com.genersoft.iot.vmp.gb28181.task.impl.CatalogSubscribeTask;
  8. import com.genersoft.iot.vmp.gb28181.task.impl.MobilePositionSubscribeTask;
  9. import com.genersoft.iot.vmp.gb28181.bean.SyncStatus;
  10. import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.beans.factory.annotation.Qualifier;
  15. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  16. import org.springframework.stereotype.Service;
  17. import javax.sip.DialogState;
  18. /**
  19. * 设备业务(目录订阅)
  20. */
  21. @Service
  22. public class DeviceServiceImpl implements IDeviceService {
  23. private final static Logger logger = LoggerFactory.getLogger(DeviceServiceImpl.class);
  24. @Autowired
  25. private DynamicTask dynamicTask;
  26. @Autowired
  27. private ISIPCommander sipCommander;
  28. @Autowired
  29. private CatalogResponseMessageHandler catalogResponseMessageHandler;
  30. @Autowired
  31. private IRedisCatchStorage redisCatchStorage;
  32. @Override
  33. public boolean addCatalogSubscribe(Device device) {
  34. if (device == null || device.getSubscribeCycleForCatalog() < 0) {
  35. return false;
  36. }
  37. logger.info("[添加目录订阅] 设备{}", device.getDeviceId());
  38. // 添加目录订阅
  39. CatalogSubscribeTask catalogSubscribeTask = new CatalogSubscribeTask(device, sipCommander, dynamicTask);
  40. // 提前开始刷新订阅
  41. int subscribeCycleForCatalog = Math.max(device.getSubscribeCycleForCatalog(),30);
  42. // 设置最小值为30
  43. dynamicTask.startCron(device.getDeviceId() + "catalog", catalogSubscribeTask, subscribeCycleForCatalog -1);
  44. return true;
  45. }
  46. @Override
  47. public boolean removeCatalogSubscribe(Device device) {
  48. if (device == null || device.getSubscribeCycleForCatalog() < 0) {
  49. return false;
  50. }
  51. logger.info("移除目录订阅: {}", device.getDeviceId());
  52. dynamicTask.stop(device.getDeviceId() + "catalog");
  53. return true;
  54. }
  55. @Override
  56. public boolean addMobilePositionSubscribe(Device device) {
  57. if (device == null || device.getSubscribeCycleForMobilePosition() < 0) {
  58. return false;
  59. }
  60. logger.info("[添加移动位置订阅] 设备{}", device.getDeviceId());
  61. // 添加目录订阅
  62. MobilePositionSubscribeTask mobilePositionSubscribeTask = new MobilePositionSubscribeTask(device, sipCommander, dynamicTask);
  63. // 设置最小值为30
  64. int subscribeCycleForCatalog = Math.max(device.getSubscribeCycleForMobilePosition(),30);
  65. // 提前开始刷新订阅
  66. dynamicTask.startCron(device.getDeviceId() + "mobile_position" , mobilePositionSubscribeTask, subscribeCycleForCatalog -1 );
  67. return true;
  68. }
  69. @Override
  70. public boolean removeMobilePositionSubscribe(Device device) {
  71. if (device == null || device.getSubscribeCycleForCatalog() < 0) {
  72. return false;
  73. }
  74. logger.info("移除移动位置订阅: {}", device.getDeviceId());
  75. dynamicTask.stop(device.getDeviceId() + "mobile_position");
  76. return true;
  77. }
  78. @Override
  79. public SyncStatus getChannelSyncStatus(String deviceId) {
  80. return catalogResponseMessageHandler.getChannelSyncProgress(deviceId);
  81. }
  82. @Override
  83. public Boolean isSyncRunning(String deviceId) {
  84. return catalogResponseMessageHandler.isSyncRunning(deviceId);
  85. }
  86. @Override
  87. public void sync(Device device) {
  88. if (catalogResponseMessageHandler.isSyncRunning(device.getDeviceId())) {
  89. logger.info("开启同步时发现同步已经存在");
  90. return;
  91. }
  92. int sn = (int)((Math.random()*9+1)*100000);
  93. catalogResponseMessageHandler.setChannelSyncReady(device, sn);
  94. sipCommander.catalogQuery(device, sn, event -> {
  95. String errorMsg = String.format("同步通道失败,错误码: %s, %s", event.statusCode, event.msg);
  96. catalogResponseMessageHandler.setChannelSyncEnd(device.getDeviceId(), errorMsg);
  97. });
  98. }
  99. }