DeviceServiceImpl.java 4.8 KB

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