TRX пре 1 година
родитељ
комит
58f3b1e41a

+ 22 - 0
src/main/java/com/zswl/dataservice/event/DeviceSyncEvent.java

@@ -0,0 +1,22 @@
+package com.zswl.dataservice.event;
+
+import lombok.Getter;
+import org.springframework.context.ApplicationEvent;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author TRX
+ * @date 2024/6/26
+ */
+public class DeviceSyncEvent extends ApplicationEvent {
+
+    @Getter
+    private List<String> deviceIds = new ArrayList<String>();
+
+    public DeviceSyncEvent(Object source, List<String> deviceIds) {
+        super(source);
+        this.deviceIds = deviceIds;
+    }
+}

+ 5 - 1
src/main/java/com/zswl/dataservice/model/mqtt/GateWayInfoAddParam.java

@@ -3,13 +3,17 @@ package com.zswl.dataservice.model.mqtt;
 import com.zswl.dataservice.model.baseParam.SuperParam;
 import com.zswl.dataservice.utils.mqtt.type.OnLineState;
 import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.AllArgsConstructor;
 import lombok.Data;
+import lombok.NoArgsConstructor;
 
 /**
  * @author TRX
  * @date 2024/5/17
  */
 @Data
+@AllArgsConstructor
+@NoArgsConstructor
 public class GateWayInfoAddParam extends SuperParam {
     @Schema(description = "网关ID")
     private String gateWayId;
@@ -20,6 +24,6 @@ public class GateWayInfoAddParam extends SuperParam {
     @Schema(description = "ip")
     private String ip;
 
-    @Schema(description = "设备在线状态")
+    @Schema(description = "设备在线状态", hidden = true)
     OnLineState state = OnLineState.OffLine;
 }

+ 65 - 6
src/main/java/com/zswl/dataservice/service/mqtt/DeviceSyncFullCardService.java

@@ -2,7 +2,9 @@ package com.zswl.dataservice.service.mqtt;
 
 import com.zswl.dataservice.dao.mqtt.DeviceInfoDao;
 import com.zswl.dataservice.dao.mqtt.GateWayInfoDao;
+import com.zswl.dataservice.domain.mqtt.DeviceInfo;
 import com.zswl.dataservice.domain.mqtt.GateWayInfo;
+import com.zswl.dataservice.event.DeviceSyncEvent;
 import com.zswl.dataservice.event.GateWaySyncEvent;
 import com.zswl.dataservice.httpRequest.ApiRequestService;
 import com.zswl.dataservice.service.base.SuperService;
@@ -17,7 +19,10 @@ import org.springframework.context.event.EventListener;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
 
 /**
  * 物联网平台 设备 网关同步到全卡项目
@@ -44,17 +49,71 @@ public class DeviceSyncFullCardService extends SuperService {
     @Autowired
     ApplicationContext applicationContext;
 
-    public ResultContent syncDevices() {
+    /**
+     * 通知同步设备
+     *
+     * @param deviceInfo
+     * @return
+     */
+    public ResultContent noticeSyncDevice(DeviceInfo deviceInfo) {
+        if (ObjectUtils.isNotEmpty(deviceInfo)) {
+            List<String> deviceIds = new ArrayList<>();
+            deviceIds.add(deviceInfo.getDeviceId());
+            DeviceSyncEvent event = new DeviceSyncEvent(this, deviceIds);
+            applicationContext.publishEvent(event);
+        }
+        return ResultContent.buildSuccess();
+    }
 
+    /**
+     * 通知同步设备
+     *
+     * @param deviceInfos
+     * @return
+     */
+    public ResultContent noticeSyncDevice(List<DeviceInfo> deviceInfos) {
+        if (ObjectUtils.isNotEmpty(deviceInfos)) {
+            List<String> deviceIds = deviceInfos.stream().map(it -> it.getDeviceId()).collect(Collectors.toList());
+            DeviceSyncEvent event = new DeviceSyncEvent(this, deviceIds);
+            applicationContext.publishEvent(event);
+        }
         return ResultContent.buildSuccess();
     }
 
+    /**
+     * 通知同步设备
+     * @param deviceIds
+     * @return
+     */
+    public ResultContent noticeSyncDeviceIds(List<String> deviceIds) {
+        if (ObjectUtils.isNotEmpty(deviceIds)) {
+            DeviceSyncEvent event = new DeviceSyncEvent(this, deviceIds);
+            applicationContext.publishEvent(event);
+        }
+        return ResultContent.buildSuccess();
+    }
+
+    /**
+     * 接收到同步设备的事件
+     *
+     * @param event
+     */
+    @EventListener(classes = DeviceSyncEvent.class)
+    @Async
+    @SneakyThrows
+    public void syncDeviceInfo(DeviceSyncEvent event) {
+        List<String> deviceIds = event.getDeviceIds();
+        log.info("event syncDeviceInfo: {}", deviceIds);
+
+    }
+
     /**
      * 通知同步网关信息
+     *
      * @param gateWayInfo
      * @return
      */
-    public ResultContent noticeSyncGateWay(GateWayInfo gateWayInfo){
+    public ResultContent noticeSyncGateWay(GateWayInfo gateWayInfo) {
         if (ObjectUtils.isNotEmpty(gateWayInfo)) {
             GateWaySyncEvent event = new GateWaySyncEvent(this, gateWayInfo.getGateWayId());
             applicationContext.publishEvent(event);
@@ -65,14 +124,14 @@ public class DeviceSyncFullCardService extends SuperService {
     /**
      * 同步网关信息
      *
-     * @param testEvent
+     * @param event
      */
     @EventListener(classes = GateWaySyncEvent.class)
     @Async
     @SneakyThrows
-    public void syncGateWayInfo(GateWaySyncEvent testEvent) {
-        String gatewayId = testEvent.getGateWayId();
-        log.info("syncGateWayInfo: {}", gatewayId);
+    public void syncGateWayInfo(GateWaySyncEvent event) {
+        String gatewayId = event.getGateWayId();
+        log.info("event syncGateWayInfo: {}", gatewayId);
 
     }
 }

+ 3 - 1
src/main/java/com/zswl/dataservice/service/mqtt/GateWayInfoService.java

@@ -4,6 +4,7 @@ import com.zswl.dataservice.dao.mqtt.*;
 import com.zswl.dataservice.domain.mqtt.*;
 import com.zswl.dataservice.event.GateWaySyncEvent;
 import com.zswl.dataservice.model.mqtt.*;
+import com.zswl.dataservice.service.base.SuperService;
 import com.zswl.dataservice.utils.DateUtils;
 import com.zswl.dataservice.utils.bean.BeanUtils;
 import com.zswl.dataservice.utils.mqtt.MqttTopicUtils;
@@ -37,7 +38,7 @@ import java.util.List;
  */
 @Slf4j
 @Service
-public class GateWayInfoService {
+public class GateWayInfoService extends SuperService {
 
     @Autowired
     GateWayInfoDao gateWayInfoDao;
@@ -80,6 +81,7 @@ public class GateWayInfoService {
         if(ObjectUtils.isEmpty(gateWayInfo)){
             gateWayInfo = new GateWayInfo();
         }
+        initDefaultUser(param);
         BeanUtils.copyProperties(param, gateWayInfo);
         if (param.getState() == null) {
             gateWayInfo.setState(OnLineState.OffLine);