DeviceSyncFullCardService.java 5.0 KB

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