DeviceSyncFullCardService.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package com.zswl.dataservice.service.mqtt;
  2. import com.zswl.dataservice.dao.mqtt.DeviceInfoDao;
  3. import com.zswl.dataservice.dao.mqtt.GateWayInfoDao;
  4. import com.zswl.dataservice.domain.mqtt.DeviceInfo;
  5. import com.zswl.dataservice.domain.mqtt.GateWayInfo;
  6. import com.zswl.dataservice.event.DeviceSyncEvent;
  7. import com.zswl.dataservice.event.GateWaySyncEvent;
  8. import com.zswl.dataservice.httpRequest.ApiRequestService;
  9. import com.zswl.dataservice.service.base.SuperService;
  10. import com.zswl.dataservice.utils.result.ResultContent;
  11. import lombok.SneakyThrows;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.apache.commons.lang3.ObjectUtils;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.context.ApplicationContext;
  16. import org.springframework.context.event.EventListener;
  17. import org.springframework.scheduling.annotation.Async;
  18. import org.springframework.stereotype.Service;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.stream.Collectors;
  22. /**
  23. * 物联网平台 设备 网关同步到全卡项目
  24. *
  25. * @author TRX
  26. * @date 2024/6/26
  27. */
  28. @Slf4j
  29. @Service
  30. public class DeviceSyncFullCardService extends SuperService {
  31. @Autowired
  32. DeviceInfoDao deviceInfoDao;
  33. @Autowired
  34. GateWayInfoDao gateWayInfoDao;
  35. @Autowired
  36. OperationLogsService operationLogsService;
  37. @Autowired
  38. ApiRequestService apiRequestService;
  39. @Autowired
  40. ApplicationContext applicationContext;
  41. /**
  42. * 通知同步设备
  43. *
  44. * @param deviceInfo
  45. * @return
  46. */
  47. public ResultContent noticeSyncDevice(DeviceInfo deviceInfo) {
  48. if (ObjectUtils.isNotEmpty(deviceInfo)) {
  49. List<String> deviceIds = new ArrayList<>();
  50. deviceIds.add(deviceInfo.getDeviceId());
  51. DeviceSyncEvent event = new DeviceSyncEvent(this, deviceIds);
  52. applicationContext.publishEvent(event);
  53. }
  54. return ResultContent.buildSuccess();
  55. }
  56. /**
  57. * 通知同步设备
  58. *
  59. * @param deviceInfos
  60. * @return
  61. */
  62. public ResultContent noticeSyncDevice(List<DeviceInfo> deviceInfos) {
  63. if (ObjectUtils.isNotEmpty(deviceInfos)) {
  64. List<String> deviceIds = deviceInfos.stream().map(it -> it.getDeviceId()).collect(Collectors.toList());
  65. DeviceSyncEvent event = new DeviceSyncEvent(this, deviceIds);
  66. applicationContext.publishEvent(event);
  67. }
  68. return ResultContent.buildSuccess();
  69. }
  70. /**
  71. * 通知同步设备
  72. *
  73. * @param deviceIds
  74. * @return
  75. */
  76. public ResultContent noticeSyncDeviceIds(List<String> deviceIds) {
  77. if (ObjectUtils.isNotEmpty(deviceIds)) {
  78. DeviceSyncEvent event = new DeviceSyncEvent(this, deviceIds);
  79. applicationContext.publishEvent(event);
  80. }
  81. return ResultContent.buildSuccess();
  82. }
  83. /**
  84. * 接收到同步设备的事件
  85. *
  86. * @param event
  87. */
  88. @EventListener(classes = DeviceSyncEvent.class)
  89. @Async
  90. @SneakyThrows
  91. public void syncDeviceInfo(DeviceSyncEvent event) {
  92. List<String> deviceIds = event.getDeviceIds();
  93. log.info("event syncDeviceInfo: {}", deviceIds);
  94. }
  95. /**
  96. * 通知同步网关信息
  97. *
  98. * @param gateWayInfo
  99. * @return
  100. */
  101. public ResultContent noticeSyncGateWay(GateWayInfo gateWayInfo) {
  102. if (ObjectUtils.isNotEmpty(gateWayInfo)) {
  103. GateWaySyncEvent event = new GateWaySyncEvent(this, gateWayInfo.getGateWayId());
  104. applicationContext.publishEvent(event);
  105. }
  106. return ResultContent.buildSuccess();
  107. }
  108. /**
  109. * 同步网关信息
  110. *
  111. * @param event
  112. */
  113. @EventListener(classes = GateWaySyncEvent.class)
  114. @Async
  115. @SneakyThrows
  116. public void syncGateWayInfo(GateWaySyncEvent event) {
  117. String gatewayId = event.getGateWayId();
  118. log.info("event syncGateWayInfo: {}", gatewayId);
  119. }
  120. }