TRX il y a 1 an
Parent
commit
4e5689ebdc

+ 14 - 0
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/deskStatistics/TopologyLinks.java

@@ -0,0 +1,14 @@
+package com.zhongshu.iot.client.model.deskStatistics;
+
+import lombok.Data;
+
+/**
+ *
+ * @author TRX
+ * @date 2025/3/21
+ */
+@Data
+public class TopologyLinks {
+    private String source;
+    private String target;
+}

+ 45 - 0
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/deskStatistics/TopologyModel.java

@@ -0,0 +1,45 @@
+package com.zhongshu.iot.client.model.deskStatistics;
+
+import lombok.Data;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 物联网  拓扑图数据模型
+ * @author TRX
+ * @date 2024/6/3
+ */
+@Data
+public class TopologyModel {
+
+    private List<TopologyNodes> nodes = new ArrayList<>();
+
+    public void addNode(TopologyNodes node) {
+        nodes.add(node);
+    }
+
+    public void addNode(String id, String user, String description) {
+        TopologyNodes node = new TopologyNodes();
+        node.setId(id);
+        node.setUser(user);
+        node.setDescription(description);
+        nodes.add(node);
+    }
+
+    private List<TopologyLinks> links = new ArrayList<>();
+
+    public void addLink(TopologyLinks link) {
+        links.add(link);
+    }
+
+    public void addLink(String source, String target) {
+        TopologyLinks link = new TopologyLinks();
+        link.setSource(source);
+        link.setTarget(target);
+        links.add(link);
+    }
+
+    private List<TopologyTreeModel> trees = new ArrayList<>();
+
+}

+ 15 - 0
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/deskStatistics/TopologyNodes.java

@@ -0,0 +1,15 @@
+package com.zhongshu.iot.client.model.deskStatistics;
+
+import lombok.Data;
+
+/**
+ *
+ * @author TRX
+ * @date 2025/3/21
+ */
+@Data
+public class TopologyNodes {
+    private String id;
+    private String user;
+    private String description;
+}

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

@@ -29,7 +29,7 @@ public class TopologyTreeModel implements ITree<TopologyTreeModel>, Serializable
     @Schema(description = "数据类型名称")
     private String typeStr;
 
-    @Schema(description = "设备列表")
+    @Schema(description = "设备类型")
     private String typeLevel2 = "";
 
     private String typeLevel2Str = "";

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

@@ -4,6 +4,8 @@ import com.zhongshu.iot.server.core.dao.mqtt.extend.OperationMessageResultDaoExt
 import com.zhongshu.iot.server.core.domain.iot.message.OperationMessageResult;
 import org.springframework.data.mongodb.repository.MongoRepository;
 
+import java.util.List;
+
 /**
  * @author TRX
  * @date 2024/3/21
@@ -12,4 +14,8 @@ public interface OperationMessageResultDao extends OperationMessageResultDaoExte
 
     OperationMessageResult findTopById(String id);
 
+    OperationMessageResult findTopByResponseId(String responseId);
+
+    List<OperationMessageResult> findByForResponseIdOrderByCreateTimeDesc(String responseId);
+
 }

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

@@ -157,6 +157,9 @@ public class OperationMessage extends SuperEntity {
     @Schema(description = "是否响应成功")
     private Boolean reIsSuccess;
 
+    @Schema(description = "")
+    private String forResponseId;
+
     //------------------------------时间信息 start--------------------
     @Schema(description = "年份,如:2024")
     private Integer year;

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

@@ -178,6 +178,9 @@ public class OperationMessageResult extends SuperEntity {
     @Schema(description = "如果下发消息有响应信息")
     private Object responseData;
 
+    @Schema(description = "对应的响应id")
+    private String forResponseId;
+
     //------------------------------时间信息 start--------------------
     @Schema(description = "年份,如:2024")
     private Integer year;

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

@@ -7,7 +7,6 @@ import com.github.microservice.http.APIResponseModel;
 import com.github.microservice.models.hxz.base.IotBaseResult;
 import com.github.microservice.net.ResultContent;
 import com.github.microservice.types.FunctionType;
-import com.google.gson.JsonObject;
 import com.zhongshu.iot.client.model.artemis.OperationMessageModel;
 import com.zhongshu.iot.client.model.artemis.OperationMessageResultModel;
 import com.zhongshu.iot.client.model.artemis.OperationMessageResultSearch;
@@ -36,7 +35,6 @@ import com.zhongshu.iot.server.core.util.CommonUtil;
 import com.zhongshu.iot.server.core.util.DateUtils;
 import com.zhongshu.iot.server.core.util.TokenUtil;
 import com.zhongshu.iot.server.core.util.bean.BeanUtils;
-import com.zhongshu.iot.server.core.util.mqtt.MqttTopicUtils;
 import com.zhongshu.iot.server.core.util.mqtt.mqttConfig.client.MQClient;
 import com.zhongshu.iot.server.core.util.page.PageEntityUtil;
 import jakarta.jms.Message;
@@ -131,57 +129,6 @@ public class OperationMessageService {
         return ResultContent.buildSuccess(msg);
     }
 
-    /**
-     * 给设备下发指令
-     *
-     * @param deviceId 设备ID
-     * @param command  指令,如:on,off
-     * @param data     指令数据
-     * @return
-     */
-    public ResultContent sendMessage(String deviceId, String command, JSONObject data) {
-        DeviceInfo deviceInfo = deviceInfoDao.findTopById(deviceId);
-        if (ObjectUtils.isEmpty(deviceInfo)) {
-            return ResultContent.buildFail(String.format("设备不存在:%s", deviceId));
-        }
-        try {
-            // 消息的TTL时间
-            Long ttl = 10 * 1000L;
-            // 消息的ID
-            String messageId = "";
-            OperationMessage message = new OperationMessage();
-            if (data.containsKey("messageId")) {
-                messageId = data.getStr("messageId");
-            } else {
-                messageId = UUID.randomUUID().toString();
-                data.append("messageId", messageId);
-            }
-            String topic = MqttTopicUtils.buildDeviceOperationInstructionsTopic(deviceId, command);
-
-            message.setMessageId(messageId);
-            message.setData(data);
-            message.setTopic(topic);
-            message.setDeviceId(deviceId);
-            message.setDeviceInfo(deviceInfo);
-            message.setIsReceive(Boolean.FALSE);
-            message.setTtlTime(ttl);
-            message.setTime(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
-
-            JsonObject jsonObject = new JsonObject();
-            jsonObject.addProperty("id", messageId);
-            jsonObject.addProperty("data", data.toString());
-            jsonObject.addProperty("time", System.currentTimeMillis());
-            jsonObject.addProperty("ttl", ttl);
-            mqClient.sendObject(topic, jsonObject.toString());
-            log.info("mqtt msg 发送成功");
-
-            operationMessageDao.save(message);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        return ResultContent.buildSuccess();
-    }
-
     /**
      * /v1/gateway/#
      * 处理网关MQTT信息
@@ -227,6 +174,7 @@ public class OperationMessageService {
             }
 
             String id = jsonObject.getStr("id");
+            String responseId = jsonObject.getStr("responseId");
             Long time = jsonObject.getLong("time");
             Long ttl = jsonObject.getLong("ttl");
             String event = jsonObject.getStr("event");
@@ -275,6 +223,7 @@ public class OperationMessageService {
             if (ObjectUtils.isNotEmpty(deviceId)) {
                 operationMessage.setDeviceId(deviceId);
             }
+            operationMessage.setForResponseId(responseId);
             initAddOperationMessage(operationMessage);
         } catch (Exception e) {
             log.error("消息出错了: {}", msg);
@@ -482,6 +431,15 @@ public class OperationMessageService {
             OperationMessageResult messageResult = new OperationMessageResult();
             messageResult.setOperationMessage(entity);
             messageResult.setData(data);
+            String forResponseId = entity.getForResponseId();
+            messageResult.setForResponseId(forResponseId);
+            if (StringUtils.isNotEmpty(forResponseId)) {
+                OperationMessageResult responseMessage = operationMessageResultDao.findTopByResponseId(forResponseId);
+                if (ObjectUtils.isNotEmpty(responseMessage)) {
+                    responseMessage.setResponseData(data);
+                    operationMessageResultDao.save(responseMessage);
+                }
+            }
 
             // OperationMessageResult 组装 IotMain 的信息
             buildOperationMessageResultIotInfo(messageResult, iotMain);

+ 22 - 6
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/device/DeviceStatisticsService.java

@@ -9,6 +9,7 @@ import com.github.microservice.types.deviceUse.DeviceCategory;
 import com.github.microservice.types.deviceUse.DeviceType;
 import com.github.microservice.types.deviceUse.OnLineState;
 import com.zhongshu.iot.client.model.artemis.OperationMessageSearchParam;
+import com.zhongshu.iot.client.model.deskStatistics.TopologyModel;
 import com.zhongshu.iot.client.model.deskStatistics.TopologyTreeModel;
 import com.zhongshu.iot.client.model.deskStatistics.TopologyTreeSearch;
 import com.zhongshu.iot.client.model.iot.statistics.DeviceStatisticsModel;
@@ -169,7 +170,9 @@ public class DeviceStatisticsService {
      * 物联网设备数据拓扑图
      * @return
      */
-    public ResultContent<List<TopologyTreeModel>> deviceTopologyData(TopologyTreeSearch param) {
+    public ResultContent deviceTopologyData(TopologyTreeSearch param) {
+        TopologyModel topologyModel = new TopologyModel();
+
         List<TopologyTreeModel> models = new ArrayList<>();
         List<GateWayUserInfo> gateWayUserInfos = gateWayUserInfoDao.findByIsDelete(Boolean.FALSE);
         if (ObjectUtils.isNotEmpty(gateWayUserInfos)) {
@@ -177,8 +180,7 @@ public class DeviceStatisticsService {
                 return !it.getUserName().equals("admin");
             }).collect(Collectors.toList());
 
-            List<DeviceInfo> deviceInfos = deviceInfoDao.findByDeviceCategoryInAndIsDelete(
-                    List.of(DeviceCategory.GW, DeviceCategory.DC), Boolean.FALSE);
+            List<DeviceInfo> deviceInfos = deviceInfoDao.findByDeviceCategoryInAndIsDelete(List.of(DeviceCategory.GW, DeviceCategory.DC), Boolean.FALSE);
             Map<String, List<DeviceInfo>> mqttUser2GateWayInfo = new HashMap<>();
             Map<String, List<DeviceInfo>> dcMap = new HashMap<>();
 
@@ -203,9 +205,9 @@ public class DeviceStatisticsService {
                 });
             }
 
-
             gateWayUserInfos.parallelStream().forEach(it -> {
                 TopologyTreeModel mqttModel = mqttUserTopologyTreeModel(it);
+                topologyModel.addNode(mqttModel.getId(), mqttModel.getName(), mqttModel.getTypeLevel2Str());
 
                 // 查询网关信息
                 if (mqttUser2GateWayInfo.containsKey(it.getUserName())) {
@@ -213,11 +215,19 @@ public class DeviceStatisticsService {
                     List<TopologyTreeModel> children = new ArrayList<>();
                     for (DeviceInfo deviceInfo : gateWays) {
                         TopologyTreeModel gateWayModel = deviceTopologyTreeModel(deviceInfo);
+                        topologyModel.addNode(gateWayModel.getId(), gateWayModel.getName(), gateWayModel.getTypeLevel2Str());
+                        topologyModel.addLink(mqttModel.getId(), gateWayModel.getId());
+
                         List<GateWay2Device> gateWay2Devices = gateWay2DeviceDao.findByGateWayInfo(deviceInfo);
                         if (ObjectUtils.isNotEmpty(gateWay2Devices)) {
                             List<TopologyTreeModel> deviceModels = gateWay2Devices.stream().map(it2 -> {
                                 return deviceTopologyTreeModel(it2.getDeviceInfo());
                             }).collect(Collectors.toList());
+
+                            deviceModels.stream().forEach(deviceModel -> {
+                                topologyModel.addNode(deviceModel.getId(), deviceModel.getName(), deviceModel.getTypeLevel2Str());
+                                topologyModel.addLink(gateWayModel.getId(), deviceModel.getId());
+                            });
                             gateWayModel.setChildren(deviceModels);
                         }
                         children.add(gateWayModel);
@@ -228,12 +238,18 @@ public class DeviceStatisticsService {
                 // 添加直连设备
                 if (dcMap.containsKey(it.getUserName())) {
                     List<DeviceInfo> gateWays = dcMap.get(it.getUserName());
-                    mqttModel.addAll(gateWays.stream().map(this::deviceTopologyTreeModel).collect(Collectors.toList()));
+                    List<TopologyTreeModel> list = gateWays.stream().map(this::deviceTopologyTreeModel).collect(Collectors.toList());
+                    list.stream().forEach(deviceInfo -> {
+                        topologyModel.addLink(mqttModel.getId(), deviceInfo.getId());
+                        topologyModel.addNode(deviceInfo.getId(), deviceInfo.getName(), deviceInfo.getTypeLevel2Str());
+                    });
+                    mqttModel.addAll(list);
                 }
                 models.add(mqttModel);
             });
         }
-        return ResultContent.buildSuccess(models);
+        topologyModel.setTrees(models);
+        return ResultContent.buildSuccess(topologyModel);
     }
 
     private TopologyTreeModel deviceTopologyTreeModel(DeviceInfo entity) {