浏览代码

机构类型

TRX 1 年之前
父节点
当前提交
5c90913bd2

+ 8 - 0
src/main/java/com/zswl/dataservice/controller/free/GateWayFreeController.java

@@ -1,6 +1,7 @@
 package com.zswl.dataservice.controller.free;
 
 import com.zswl.dataservice.model.mqtt.GateWayBindDeviceParam;
+import com.zswl.dataservice.model.mqtt.GateWayInfoAddParam;
 import com.zswl.dataservice.service.mqtt.GateWayInfoService;
 import com.zswl.dataservice.utils.result.ResultContent;
 import io.swagger.v3.oas.annotations.Operation;
@@ -34,4 +35,11 @@ public class GateWayFreeController {
         Assert.hasText(param.getGateWayId(), "网关ID不能为空");
         return gateWayInfoService.gateWayBindDevice(param);
     }
+
+    @Operation(summary = "注册网关")
+    @RequestMapping(value = "registerGateWay", method = {RequestMethod.POST})
+    public ResultContent registerGateWay(@RequestBody GateWayInfoAddParam param) {
+        Assert.hasText(param.getGateWayId(), "网关ID不能为空");
+        return gateWayInfoService.registerGateWay(param);
+    }
 }

+ 19 - 0
src/main/java/com/zswl/dataservice/event/GateWaySyncEvent.java

@@ -0,0 +1,19 @@
+package com.zswl.dataservice.event;
+
+import lombok.Getter;
+import org.springframework.context.ApplicationEvent;
+
+/**
+ * @author TRX
+ * @date 2024/6/26
+ */
+public class GateWaySyncEvent extends ApplicationEvent {
+
+    @Getter
+    private String gateWayId;
+
+    public GateWaySyncEvent(Object source, String gateWayId) {
+        super(source);
+        this.gateWayId = gateWayId;
+    }
+}

+ 3 - 0
src/main/java/com/zswl/dataservice/model/mqtt/GateWayInfoAddParam.java

@@ -17,6 +17,9 @@ public class GateWayInfoAddParam extends SuperParam {
     @Schema(description = "网关名称")
     private String gateWayName;
 
+    @Schema(description = "ip")
+    private String ip;
+
     @Schema(description = "设备在线状态")
     OnLineState state = OnLineState.OffLine;
 }

+ 4 - 0
src/main/java/com/zswl/dataservice/model/sync/package-info.java

@@ -0,0 +1,4 @@
+package com.zswl.dataservice.model.sync;
+/**
+ * 同步信息 到全卡项目的数据模型
+ */

+ 78 - 0
src/main/java/com/zswl/dataservice/service/mqtt/DeviceSyncFullCardService.java

@@ -0,0 +1,78 @@
+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.GateWayInfo;
+import com.zswl.dataservice.event.GateWaySyncEvent;
+import com.zswl.dataservice.httpRequest.ApiRequestService;
+import com.zswl.dataservice.service.base.SuperService;
+import com.zswl.dataservice.utils.result.ResultContent;
+import lombok.Data;
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.ObjectUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.event.EventListener;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Service;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 物联网平台 设备 网关同步到全卡项目
+ *
+ * @author TRX
+ * @date 2024/6/26
+ */
+@Slf4j
+@Service
+public class DeviceSyncFullCardService extends SuperService {
+
+    @Autowired
+    DeviceInfoDao deviceInfoDao;
+
+    @Autowired
+    GateWayInfoDao gateWayInfoDao;
+
+    @Autowired
+    OperationLogsService operationLogsService;
+
+    @Autowired
+    ApiRequestService apiRequestService;
+
+    @Autowired
+    ApplicationContext applicationContext;
+
+    public ResultContent syncDevices() {
+
+        return ResultContent.buildSuccess();
+    }
+
+    /**
+     * 通知同步网关信息
+     * @param gateWayInfo
+     * @return
+     */
+    public ResultContent noticeSyncGateWay(GateWayInfo gateWayInfo){
+        if (ObjectUtils.isNotEmpty(gateWayInfo)) {
+            GateWaySyncEvent event = new GateWaySyncEvent(this, gateWayInfo.getGateWayId());
+            applicationContext.publishEvent(event);
+        }
+        return ResultContent.buildSuccess();
+    }
+
+    /**
+     * 同步网关信息
+     *
+     * @param testEvent
+     */
+    @EventListener(classes = GateWaySyncEvent.class)
+    @Async
+    @SneakyThrows
+    public void syncGateWayInfo(GateWaySyncEvent testEvent) {
+        String gatewayId = testEvent.getGateWayId();
+        log.info("syncGateWayInfo: {}", gatewayId);
+
+    }
+}

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

@@ -2,6 +2,7 @@ package com.zswl.dataservice.service.mqtt;
 
 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.utils.DateUtils;
 import com.zswl.dataservice.utils.bean.BeanUtils;
@@ -16,6 +17,7 @@ import org.apache.activemq.artemis.api.core.management.ObjectNameBuilder;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationContext;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 import org.springframework.stereotype.Service;
@@ -61,6 +63,12 @@ public class GateWayInfoService {
     @Autowired
     OperationLogsService operationLogsService;
 
+    @Autowired
+    ApplicationContext applicationContext;
+
+    @Autowired
+    DeviceSyncFullCardService deviceSyncFullCardService;
+
     /**
      * 添加网关
      *
@@ -69,15 +77,25 @@ public class GateWayInfoService {
      */
     public ResultContent addGateWayInfo(GateWayInfoAddParam param) {
         GateWayInfo gateWayInfo = gateWayInfoDao.findTopByGateWayId(param.getGateWayId());
-        if (ObjectUtils.isNotEmpty(gateWayInfo)) {
-            return ResultContent.buildFail(String.format("网关ID已存在:%s", param.getGateWayId()));
+        if(ObjectUtils.isEmpty(gateWayInfo)){
+            gateWayInfo = new GateWayInfo();
         }
-        gateWayInfo = new GateWayInfo();
         BeanUtils.copyProperties(param, gateWayInfo);
         if (param.getState() == null) {
             gateWayInfo.setState(OnLineState.OffLine);
         }
         gateWayInfoDao.save(gateWayInfo);
+        deviceSyncFullCardService.noticeSyncGateWay(gateWayInfo);
+        return ResultContent.buildSuccess();
+    }
+
+    /**
+     * 注册网关
+     * @param param
+     * @return
+     */
+    public ResultContent registerGateWay(GateWayInfoAddParam param){
+        addGateWayInfo(param);
         return ResultContent.buildSuccess();
     }