TRX пре 1 година
родитељ
комит
b7b90ae1db
18 измењених фајлова са 225 додато и 48 уклоњено
  1. 1 1
      OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/iot/IotMainParam.java
  2. 8 1
      OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/iot/IotMainSearch.java
  3. 10 0
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/controller/TestController.java
  4. 0 1
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/controller/iot/IotSendController.java
  5. 1 0
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/controller/openAPi/IotSendApiController.java
  6. 2 0
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/dao/iot/IotMainDao.java
  7. 2 0
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/dao/iot/extend/IotMainDaoExtend.java
  8. 8 0
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/dao/iot/impl/IotMainDaoImpl.java
  9. 3 0
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/domain/iot/IotMain.java
  10. 3 0
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/domain/iot/mqtt/OperationMessage.java
  11. 17 0
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/domain/iot/mqtt/OperationMessageResult.java
  12. 22 13
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/artemis/OperationMessageService.java
  13. 18 0
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/base/CommonService.java
  14. 76 30
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/iot/IotSendMessageService.java
  15. 35 0
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/iot/IotServiceImpl.java
  16. 6 0
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/mqtt/GateWayInfoService.java
  17. 1 1
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/openApi/OpenAPIRegisterService.java
  18. 12 1
      OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/sync/DeviceSyncFullCardService.java

+ 1 - 1
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/iot/IotMainParam.java

@@ -31,7 +31,7 @@ public class IotMainParam extends SuperParam {
     private String identifier;
 
     @Schema(description = "功能类型,属性 事件 服务", required = true)
-    FunctionType functionType;
+    private FunctionType functionType;
 
     @Schema(description = "物模型Topic")
     private String iotTopic;

+ 8 - 1
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/iot/IotMainSearch.java

@@ -16,11 +16,18 @@ import lombok.NoArgsConstructor;
 @AllArgsConstructor
 @NoArgsConstructor
 public class IotMainSearch extends SuperSearchParam {
+
     @Schema(description = "功能名称")
     private String name;
 
+    @Schema(description = "关联的设备")
+    private String deviceId;
+
+    @Schema(description = "网关信息")
+    private String gateWayId;
+
     @Schema(description = "功能类型")
-    FunctionType functionType;
+    private FunctionType functionType;
 
     @Schema(description = "标识符")
     private String identifier;

+ 10 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/controller/TestController.java

@@ -2,6 +2,7 @@ package com.zhongshu.iot.server.core.controller;
 
 import com.zhongshu.iot.client.model.mqtt.SendMessageModel;
 import com.zhongshu.iot.server.core.service.artemis.OperationMessageService;
+import com.zhongshu.iot.server.core.service.iot.IotServiceImpl;
 import com.zhongshu.iot.server.core.service.openApi.OpenAPIRegisterService;
 import com.zhongshu.iot.server.core.service.other.ScanExecuteService;
 import com.zhongshu.iot.server.core.util.result.ResultContent;
@@ -32,6 +33,9 @@ public class TestController {
     @Autowired
     OpenAPIRegisterService openAPIRegisterService;
 
+    @Autowired
+    IotServiceImpl iotService;
+
     @Operation(summary = "发送指令")
     @RequestMapping(value = "free/sendMessage", method = {RequestMethod.POST})
     public ResultContent sendMessage(@RequestBody SendMessageModel param) {
@@ -58,5 +62,11 @@ public class TestController {
         return ResultContent.buildSuccess();
     }
 
+    @Operation(summary = "修复设备网关信息")
+    @RequestMapping(value = "fixDeviceIot", method = {RequestMethod.GET})
+    public ResultContent fixDeviceIot() {
+        return iotService.fixDeviceIot();
+    }
+
 }
 

+ 0 - 1
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/controller/iot/IotSendController.java

@@ -41,7 +41,6 @@ public class IotSendController {
     @Operation(summary = "下发消息")
     @RequestMapping(value = "sendIotMessage", method = {RequestMethod.POST})
     public ResultContent sendIotMessage(@RequestBody @Valid IotSendParam param) {
-        param.setUserId(authHelper.getCurrentUser().getUserId());
         return iotSendMessageService.sendIotMessage(param);
     }
 

+ 1 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/controller/openAPi/IotSendApiController.java

@@ -3,6 +3,7 @@ package com.zhongshu.iot.server.core.controller.openAPi;
 import com.github.microservice.auth.security.annotations.ResourceAuth;
 import com.github.microservice.auth.security.helper.AuthHelper;
 import com.github.microservice.auth.security.type.AuthType;
+import com.github.microservice.models.iot.IotSendGateWayParam;
 import com.github.microservice.models.iot.IotSendParam;
 import com.zhongshu.iot.server.core.service.iot.IotSendMessageService;
 import com.zhongshu.iot.server.core.util.result.ResultContent;

+ 2 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/dao/iot/IotMainDao.java

@@ -34,4 +34,6 @@ public interface IotMainDao extends IotMainDaoExtend, org.springframework.data.m
 
     void deleteByIotTemplate(IotTemplate iotTemplate);
 
+    List<IotMain> findByDeviceIdAndIotDataType(String deviceId, IotDataType iotDataType);
+
 }

+ 2 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/dao/iot/extend/IotMainDaoExtend.java

@@ -11,5 +11,7 @@ import org.springframework.data.domain.Pageable;
  * @Version: 1.0
  */
 public interface IotMainDaoExtend {
+
     Page<IotMain> page(Pageable pageable, IotMainSearch param);
+
 }

+ 8 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/dao/iot/impl/IotMainDaoImpl.java

@@ -49,6 +49,14 @@ public class IotMainDaoImpl extends BaseImpl implements IotMainDaoExtend {
             criteria.and("dataType").is(param.getDataType());
         }
 
+        if (StringUtils.isNotEmpty(param.getDeviceId())) {
+            criteria.and("deviceId").is(param.getDeviceId());
+        }
+
+        if (StringUtils.isNotEmpty(param.getGateWayId())) {
+            criteria.and("gateWayId").is(param.getGateWayId());
+        }
+
         // 模糊搜索
         List<Criteria> criterias = new ArrayList<>();
         if (StringUtils.isNotEmpty(param.getName())) {

+ 3 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/domain/iot/IotMain.java

@@ -105,4 +105,7 @@ public class IotMain extends SuperEntity {
 
     @Schema(description = "业务处理地址")
     private String remoteUrl;
+
+    @Schema(description = "网关信息")
+    private String gateWayId;
 }

+ 3 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/domain/iot/mqtt/OperationMessage.java

@@ -23,6 +23,7 @@ import java.util.Date;
 @NoArgsConstructor
 @AllArgsConstructor
 public class OperationMessage extends SuperEntity {
+
     @Schema(description = "mqtt服务器消息ID")
     private String messageId;
 
@@ -36,8 +37,10 @@ public class OperationMessage extends SuperEntity {
     @Indexed(unique = true, sparse = true)
     private String dataId;
 
+    @Schema(description = "处理token")
     private String token;
 
+    @Schema(description = "消息过期时间")
     private Long expireAt;
 
     @Schema(description = "消息内容")

+ 17 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/domain/iot/mqtt/OperationMessageResult.java

@@ -26,6 +26,12 @@ public class OperationMessageResult extends SuperEntity {
     @DBRef(lazy = true)
     private OperationMessage operationMessage;
 
+    @Schema(description = "主数据dataId")
+    private String dataId;
+
+    @Schema(description = "本数据的业务ID")
+    private String messageId;
+
     @Schema(description = "mqtt消息类型: 发送 接收")
     private OperationType operationType;
 
@@ -95,4 +101,15 @@ public class OperationMessageResult extends SuperEntity {
     @Schema(description = "是否响应成功")
     private Boolean reIsSuccess;
 
+    //-------------------------消息是否送达 start-------------------------
+
+    @Schema(description = "是否需要确定是否送达")
+    private Boolean isNeedReceived = Boolean.FALSE;
+
+    @Schema(description = "是否已送达")
+    private Boolean isReceived;
+
+    @Schema(description = "确定送达时间")
+    private Long receivedTime;
+
 }

+ 22 - 13
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/artemis/OperationMessageService.java

@@ -51,10 +51,9 @@ import org.springframework.data.domain.Pageable;
 import org.springframework.stereotype.Service;
 
 import java.lang.reflect.Method;
-import java.util.Date;
-import java.util.List;
-import java.util.UUID;
+import java.util.*;
 import java.util.concurrent.CompletableFuture;
+import java.util.stream.Collectors;
 
 /**
  * 指令数据管理
@@ -224,6 +223,11 @@ public class OperationMessageService {
                 event = arr[arr.length - 1];
                 jsonObject.put("event", event);
             }
+            Boolean issued = jsonObject.getBool("issued");
+            if (issued != null && issued) {
+                return;
+            }
+
             // 是否是测试
             boolean isTest = true;
             // ping不执行
@@ -342,18 +346,21 @@ public class OperationMessageService {
             String handleMsg = "处理成功";
             // 业务处理的消息内容
             JSONObject requestData = (JSONObject) json.get("data");
-            requestData.put("mqttDataId", entity.getDataId());
-            requestData.put("GateWayId", entity.getGateWayId());
+            requestData.set("mqttDataId", entity.getDataId());
+            requestData.set("GateWayId", entity.getGateWayId());
             String DeviceId = requestData.getStr("DeviceId");
             if (StringUtils.isEmpty(DeviceId)) {
                 DeviceId = entity.getDeviceId();
-                requestData.put("DeviceId", DeviceId);
+                requestData.set("DeviceId", DeviceId);
             }
             entity.setDeviceId(DeviceId);
 
             // 查询有对应事件的设备
             List<IotMain> events = iotMainDao.findByRealIotTopicAndFunctionTypeOrderByCreateTimeAsc(entity.getTopic(), FunctionType.Event);
             if (ObjectUtils.isNotEmpty(events)) {
+                // 去重
+                events = events.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
+                        () -> new TreeSet<>(Comparator.comparing(IotMain::getRealIotTopic))), ArrayList::new));
                 for (IotMain iotMain : events) {
                     executeOperationMessage(entity, requestData, iotMain);
                 }
@@ -527,13 +534,15 @@ public class OperationMessageService {
             if (isNeedReplay) {
                 // 返回
                 JSONObject jsonObject = new JSONObject();
-                jsonObject.put("id", entity.getDataId());
-                jsonObject.put("data", messageResult.getReplayData());
-                jsonObject.put("time", System.currentTimeMillis());
-                jsonObject.put("ttl", entity.getTtlTime());
-                jsonObject.put("event", entity.getEvent());
-                jsonObject.put("deviceId", entity.getDeviceId());
-                jsonObject.put("gateWayId", entity.getGateWayId());
+                jsonObject.set("id", entity.getDataId());
+                jsonObject.set("data", messageResult.getReplayData());
+                jsonObject.set("time", System.currentTimeMillis());
+                jsonObject.set("ttl", entity.getTtlTime());
+                jsonObject.set("event", entity.getEvent());
+                jsonObject.set("deviceId", entity.getDeviceId());
+                jsonObject.set("gateWayId", entity.getGateWayId());
+                jsonObject.set("issued", Boolean.TRUE);
+
                 String reTopic = String.format("%s/reply", entity.getTopic());
                 String reMsg = "响应成功";
                 Boolean reIsSuccess = Boolean.TRUE;

+ 18 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/base/CommonService.java

@@ -3,6 +3,7 @@ package com.zhongshu.iot.server.core.service.base;
 import com.mongodb.client.result.UpdateResult;
 import com.zhongshu.iot.server.core.util.CommonUtil;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.ObjectUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.mongodb.core.MongoTemplate;
 import org.springframework.data.mongodb.core.query.Criteria;
@@ -10,6 +11,7 @@ import org.springframework.data.mongodb.core.query.Query;
 import org.springframework.data.mongodb.core.query.Update;
 import org.springframework.stereotype.Service;
 
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -66,4 +68,20 @@ public class CommonService {
         return updateResult.getUpsertedId();
     }
 
+    public Object updateData(List<String> ids, Map<String, Object> standardData, String collectionName) {
+        collectionName = CommonUtil.getCollectionName(collectionName);
+        if (ObjectUtils.isNotEmpty(ids)) {
+            Query query = new Query(Criteria.where("_id").in(ids));
+            Update update = new Update();
+            standardData.forEach((key, value) -> {
+                if (!"id".equals(key)) {
+                    update.set(key, value);
+                }
+            });
+            UpdateResult updateResult = mongoTemplate.upsert(query, update, collectionName);
+            return updateResult.getUpsertedId();
+        }
+        return null;
+    }
+
 }

+ 76 - 30
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/iot/IotSendMessageService.java

@@ -1,15 +1,15 @@
 package com.zhongshu.iot.server.core.service.iot;
 
 import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
+import com.github.microservice.models.iot.IotSendGateWayParam;
 import com.github.microservice.models.iot.IotSendParam;
 import com.github.microservice.types.FunctionType;
+import com.zhongshu.iot.client.model.iot.IotMainSearch;
 import com.zhongshu.iot.client.type.IotDataType;
 import com.zhongshu.iot.client.type.OperationType;
 import com.zhongshu.iot.server.core.dao.iot.IotMainDao;
-import com.zhongshu.iot.server.core.dao.mqtt.DeviceInfoDao;
-import com.zhongshu.iot.server.core.dao.mqtt.GateWay2DeviceDao;
-import com.zhongshu.iot.server.core.dao.mqtt.OperationMessageDao;
-import com.zhongshu.iot.server.core.dao.mqtt.OperationMessageResultDao;
+import com.zhongshu.iot.server.core.dao.mqtt.*;
 import com.zhongshu.iot.server.core.domain.iot.IotMain;
 import com.zhongshu.iot.server.core.domain.iot.mqtt.*;
 import com.zhongshu.iot.server.core.service.base.SuperService;
@@ -22,13 +22,16 @@ import com.zhongshu.iot.server.core.util.result.ResultContent;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Pageable;
 import org.springframework.stereotype.Service;
 
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
+import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * @author TRX
@@ -60,6 +63,9 @@ public class IotSendMessageService extends SuperService {
     @Value("${artemisstore.time}")
     public Long ttlMill = 30 * 24L * 60 * 60 * 1000L;
 
+    @Autowired
+    private GateWayInfoDao gateWayInfoDao;
+
     /**
      * 发送信息
      *
@@ -67,34 +73,34 @@ public class IotSendMessageService extends SuperService {
      * @return
      */
     public ResultContent sendIotMessage(IotSendParam param) {
-        // 验证数据
-        DeviceInfo deviceInfo = deviceInfoDao.findTopByDeviceId(param.getDeviceId());
-        if (ObjectUtils.isEmpty(deviceInfo)) {
-            return ResultContent.buildFail(String.format("设备不存在:%S", param.getDeviceId()));
-        }
-        FunctionType functionType = param.getFunctionType();
-        List<IotMain> list = new ArrayList<>();
-        if (functionType != null) {
-            list = iotMainDao.findByDeviceIdAndIdentifierAndIotDataTypeAndFunctionType(
-                    param.getDeviceId(), param.getIdentifier(), IotDataType.Device, functionType);
-        } else {
-            list = iotMainDao.findByDeviceIdAndIdentifierAndIotDataType(
-                    param.getDeviceId(), param.getIdentifier(), IotDataType.Device);
-        }
+        IotMainSearch search = new IotMainSearch();
+        BeanUtils.copyProperties(param, search);
+        Pageable pageable = PageRequest.of(0, Integer.MAX_VALUE);
+        Page<IotMain> page = iotMainDao.page(pageable, search);
 
+        List<IotMain> list = page.getContent();
         if (ObjectUtils.isEmpty(list)) {
-            log.error("设备没有对应的物模型 {} {}", param.getDeviceId(), param.getIdentifier());
+            log.error("没有对应的物模型  {}", param.getIdentifier());
             return ResultContent.buildFail("没有对应的设备物模型");
         }
+        // list 去重
+        list= list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
+                        () -> new TreeSet<>(Comparator.comparing(IotMain::getRealIotTopic))), ArrayList::new));
+
+        DeviceInfo deviceInfo = null;
         GateWayInfo gateWayInfo = null;
-        GateWay2Device gateWay2Device = gateWay2DeviceDao.findTopByDeviceInfoOrderByUpdateTimeDesc(deviceInfo);
-        if (ObjectUtils.isEmpty(gateWay2Device)) {
-            gateWayInfo = gateWay2Device.getGateWayInfo();
+        if (StringUtils.isNotEmpty(param.getGateWayId())) {
+            gateWayInfo = gateWayInfoDao.findTopByGateWayId(param.getGateWayId());
+        }
+        if (StringUtils.isNotEmpty(param.getDeviceId())) {
+            deviceInfo = deviceInfoDao.findTopByDeviceId(param.getDeviceId());
         }
+
         String dataId = param.getDataId();
         if (StringUtils.isEmpty(dataId)) {
             dataId = CommonUtil.UUID();
         }
+
         // 组装要发送的数据
         JSONObject jsonObject = new JSONObject();
         jsonObject.set("id", dataId);
@@ -103,6 +109,8 @@ public class IotSendMessageService extends SuperService {
         jsonObject.set("time", System.currentTimeMillis());
         jsonObject.set("ttl", param.getTtl());
         jsonObject.set("imitate", param.getIsImitate());
+        jsonObject.set("event", param.getEvent());
+        jsonObject.set("issued", Boolean.TRUE);
 
         String token = TokenUtil.create();
         OperationMessage entity = operationMessageDao.init(dataId, token);
@@ -118,12 +126,15 @@ public class IotSendMessageService extends SuperService {
             entity.setDataId(dataId);
             entity.setIsTimeOut(false);
             entity.setEvent("");
-            entity.setGateWayInfo(gateWayInfo);
             if (ObjectUtils.isNotEmpty(gateWayInfo)) {
+                entity.setGateWayInfo(gateWayInfo);
                 entity.setGateWayId(gateWayInfo.getGateWayId());
             }
-            entity.setDeviceId(deviceInfo.getDeviceId());
-            entity.setDeviceInfo(deviceInfo);
+            if (ObjectUtils.isNotEmpty(deviceInfo)) {
+                entity.setDeviceId(deviceInfo.getDeviceId());
+                entity.setDeviceInfo(deviceInfo);
+            }
+
             entity.setTtl(new Date(System.currentTimeMillis() + ttlMill));
             entity.setReceiveTime(System.currentTimeMillis());
             entity.setReceiveTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
@@ -131,9 +142,12 @@ public class IotSendMessageService extends SuperService {
 
             for (IotMain iotMain : list) {
                 // %s/issued 下发数据 Topic
-                String topic = MqttTopicUtils.buildIssuedTopic(iotMain.getRealIotTopic());
+                String topic = iotMain.getRealIotTopic();
 
+                String messageId = CommonUtil.UUID();
                 OperationMessageResult messageResult = new OperationMessageResult();
+                messageResult.setMessageId(messageId);
+                messageResult.setDataId(entity.getDataId());
                 messageResult.setOperationMessage(entity);
                 // 物模型信息
                 messageResult.setIotMain(iotMain);
@@ -145,9 +159,14 @@ public class IotSendMessageService extends SuperService {
                 messageResult.setGateWayId(entity.getGateWayId());
                 messageResult.setRealIotTopic(topic);
                 messageResult.setData(jsonObject);
-                messageResult.setGateWayId(entity.getGateWayId());
                 messageResult.setOperationType(entity.getOperationType());
 
+                jsonObject.set("id", messageId);
+                try {
+                    mqClient.sendObject(topic, JSONUtil.toJsonStr(jsonObject), messageId);
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
                 operationMessageResultDao.save(messageResult);
             }
         } else {
@@ -156,4 +175,31 @@ public class IotSendMessageService extends SuperService {
         return ResultContent.buildSuccess();
     }
 
+    /**
+     * 给网关发生信息
+     *
+     * @param param
+     * @return
+     */
+    public ResultContent sendIotGateWayMessage(IotSendGateWayParam param) {
+
+        String dataId = CommonUtil.UUID();
+        JSONObject jsonObject = new JSONObject();
+        jsonObject.set("id", dataId);
+        jsonObject.set("data", param.getData());
+        jsonObject.set("timeStr", DateUtils.paresTime(System.currentTimeMillis(), DateUtils.patternyyyySSS));
+        jsonObject.set("time", System.currentTimeMillis());
+        jsonObject.set("ttl", param.getTtl());
+        jsonObject.set("imitate", param.getIsImitate());
+        jsonObject.set("gateWayId", param.getGateWayId());
+        jsonObject.set("event", "permissionNotice");
+
+        String topIc = "/v1/gateway/" + param.getGateWayId() + "/reply";
+        try {
+            mqClient.sendObject(topIc, JSONUtil.toJsonStr(jsonObject), dataId);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return ResultContent.buildSuccess();
+    }
 }

+ 35 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/iot/IotServiceImpl.java

@@ -18,6 +18,7 @@ import com.zhongshu.iot.server.core.domain.iot.IotTemplate;
 import com.zhongshu.iot.server.core.domain.iot.IotTopic;
 import com.zhongshu.iot.server.core.domain.iot.mqtt.DeviceInfo;
 import com.zhongshu.iot.server.core.domain.iot.mqtt.GateWay2Device;
+import com.zhongshu.iot.server.core.service.base.CommonService;
 import com.zhongshu.iot.server.core.service.base.SuperService;
 import com.zhongshu.iot.server.core.util.bean.BeanUtils;
 import com.zhongshu.iot.server.core.util.page.PageEntityUtil;
@@ -33,7 +34,9 @@ import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.Assert;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
@@ -66,6 +69,9 @@ public class IotServiceImpl extends SuperService {
     @Autowired
     IotDeviceDataDao iotDeviceDataDao;
 
+    @Autowired
+    CommonService commonService;
+
     //----------------------------- 模版 start----------------------------
 
     /**
@@ -401,12 +407,41 @@ public class IotServiceImpl extends SuperService {
                 if (ObjectUtils.isNotEmpty(gateWay2Device)) {
                     String gateWayId = gateWay2Device.getGateWayInfo().getGateWayId();
                     realIotTopic = realIotTopic.replaceAll(Pattern.quote("${gateWayId}"), gateWayId);
+                    iotMain.setGateWayId(gateWayId);
                 }
             }
             iotMain.setRealIotTopic(realIotTopic);
         }
     }
 
+    public ResultContent updateAllDeviceIotMainGateWayInfo(DeviceInfo deviceInfo) {
+        if (ObjectUtils.isNotEmpty(deviceInfo)) {
+            List<IotMain> list = iotMainDao.findByDeviceIdAndIotDataType(deviceInfo.getDeviceId(), IotDataType.Device);
+            if (ObjectUtils.isNotEmpty(list)) {
+                for (IotMain iotMain : list) {
+                    initRealTopic(iotMain);
+                }
+                iotMainDao.saveAll(list);
+            }
+        }
+        return ResultContent.buildSuccess();
+    }
+
+    /**
+     * 修复设备的物模型 网关信息
+     *
+     * @return
+     */
+    public ResultContent fixDeviceIot() {
+        List<DeviceInfo> list = deviceInfoDao.findAll();
+        if (ObjectUtils.isNotEmpty(list)) {
+            list.stream().forEach(it -> {
+                updateAllDeviceIotMainGateWayInfo(it);
+            });
+        }
+        return ResultContent.buildSuccess();
+    }
+
     public ResultContent deleteIotMain(String id) {
         IotMain entity = iotMainDao.findTopById(id);
         if (ObjectUtils.isEmpty(entity)) {

+ 6 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/mqtt/GateWayInfoService.java

@@ -5,6 +5,7 @@ import com.zhongshu.iot.client.model.mqtt.*;
 import com.zhongshu.iot.server.core.dao.mqtt.*;
 import com.zhongshu.iot.server.core.domain.iot.mqtt.*;
 import com.zhongshu.iot.server.core.service.base.SuperService;
+import com.zhongshu.iot.server.core.service.iot.IotServiceImpl;
 import com.zhongshu.iot.server.core.service.sync.DeviceSyncFullCardService;
 import com.zhongshu.iot.server.core.service.user.OperationLogsService;
 import com.zhongshu.iot.server.core.util.DateUtils;
@@ -80,6 +81,9 @@ public class GateWayInfoService extends SuperService {
     @Autowired
     DevicePingInfoDao devicePingInfoDao;
 
+    @Autowired
+    IotServiceImpl iotService;
+
     /**
      * 添加网关
      *
@@ -204,6 +208,8 @@ public class GateWayInfoService extends SuperService {
                 gateWay2Device.setBindTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
                 gateWay2DeviceDao.save(gateWay2Device);
                 deviceInfos.add(deviceInfo);
+
+                iotService.updateAllDeviceIotMainGateWayInfo(deviceInfo);
             }
         }
 

+ 1 - 1
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/openApi/OpenAPIRegisterService.java

@@ -38,7 +38,7 @@ public class OpenAPIRegisterService {
         param.setPredicateArgs(String.format("/%s/**", sign));
         param.setOpenApiInfo(openAPIS);
         param.setApiType(ApiType.IOT.name());
-        ResultContent content = restTemplate.postForObject("http://openapiserver-wjf/manager/apiManager/refresh", param, ResultContent.class);
+        ResultContent content = restTemplate.postForObject("http://openapiserver-wjf/manager/gateway/api/refresh", param, ResultContent.class);
         if (content == null) {
             content = ResultContent.buildSuccess();
         }

+ 12 - 1
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/sync/DeviceSyncFullCardService.java

@@ -126,11 +126,22 @@ public class DeviceSyncFullCardService extends SuperService {
                 DeviceInfoSyncParam syncParam = new DeviceInfoSyncParam();
 
                 BeanUtils.copyProperties(it, syncParam);
-                List<GateWay2Device> gateWay2Devices = gateWay2DeviceDao.findByDeviceInfo(it);
+
+                // 设备绑定的网关
+                List<GateWay2Device> gateWay2Devices = new ArrayList<>();
+
+                // 多个
+//                gateWay2Devices = gateWay2DeviceDao.findByDeviceInfo(it);
+
+                // 最新的
+                GateWay2Device gateWay2Device = gateWay2DeviceDao.findTopByDeviceInfoOrderByUpdateTimeDesc(it);
+                gateWay2Devices.add(gateWay2Device);
+
                 List<String> gateWayIds = gateWay2Devices.stream().map(it2 -> {
                     return it2.getGateWayInfo().getGateWayId();
                 }).collect(Collectors.toList());
                 syncParam.setGateWayId(String.join(",", gateWayIds));
+
                 // 项目code
                 syncParam.setProjectInfoCode(it.getProjectInfoCode());
                 return syncParam;