TRX hace 1 año
padre
commit
8aff97b966

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

@@ -0,0 +1,44 @@
+package com.zhongshu.iot.client.model.deskStatistics;
+
+import com.zhongshu.iot.client.utils.ITree;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 物联网  拓扑图数据模型
+ * @author TRX
+ * @date 2024/6/3
+ */
+@Data
+public class TopologyTreeModel implements ITree<TopologyTreeModel>, Serializable {
+
+    @Schema(description = "数据ID")
+    private String id;
+
+    @Schema(description = "名称")
+    private String name;
+
+    @Schema(description = "数据类型(大类,区分数据列表),如:DW")
+    private String type;
+
+    @Schema(description = "数据类型名称")
+    private String typeStr;
+
+    @Schema(description = "设备列表")
+    private String typeLevel2 = "";
+
+    private String typeLevel2Str = "";
+
+    @Schema(description = "状态")
+    private String state = "";
+
+    @Schema(description = "上级ID")
+    private String parentId = ITree.ROOT_ID;
+
+    @Schema(description = "子层级")
+    private List<TopologyTreeModel> children = new ArrayList<>();
+}

+ 17 - 0
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/deskStatistics/TopologyTreeSearch.java

@@ -0,0 +1,17 @@
+package com.zhongshu.iot.client.model.deskStatistics;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+/**
+ * 物联网  拓扑图数据模型 搜索模型
+ * @author TRX
+ * @date 2024/6/3
+ */
+@Data
+public class TopologyTreeSearch {
+
+    @Schema(description = "账号数据ID")
+    private String mqttUserDataId;
+
+}

+ 10 - 1
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/controller/devices/DeviceStatisticsController.java

@@ -4,6 +4,7 @@ import com.github.microservice.auth.security.annotations.ResourceAuth;
 import com.github.microservice.auth.security.type.AuthType;
 import com.github.microservice.net.ResultContent;
 import com.zhongshu.iot.client.model.artemis.OperationMessageSearchParam;
+import com.zhongshu.iot.client.model.deskStatistics.TopologyTreeSearch;
 import com.zhongshu.iot.client.model.iot.ping.DeviceOnLineRecordSearch;
 import com.zhongshu.iot.server.core.service.device.DeviceOnLineRecordService;
 import com.zhongshu.iot.server.core.service.device.DeviceStatisticsService;
@@ -40,7 +41,7 @@ public class DeviceStatisticsController {
     public ResultContent deviceStatistics() {
         return deviceStatisticsService.deviceStatistics();
     }
-    
+
     @ResourceAuth(value = "user", type = AuthType.User)
     @Operation(summary = "设备在线折线图状态统计")
     @RequestMapping(value = "statisticsDeviceOnLine", method = {RequestMethod.POST})
@@ -54,4 +55,12 @@ public class DeviceStatisticsController {
     public ResultContent statisticsMessageLine(@RequestBody OperationMessageSearchParam param) {
         return deviceStatisticsService.statisticsMessageLine(param);
     }
+
+    @ResourceAuth(value = "user", type = AuthType.User)
+    @Operation(summary = "设备数据拓扑")
+    @RequestMapping(value = "deviceTopologyData", method = {RequestMethod.POST})
+    public ResultContent deviceTopologyData(@RequestBody TopologyTreeSearch param) {
+        return deviceStatisticsService.deviceTopologyData(param);
+    }
+
 }

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

@@ -31,4 +31,5 @@ public interface DeviceInfoDao extends DeviceInfoDaoExtend, org.springframework.
 
     Long countByDeviceCategory(DeviceCategory deviceCategory);
 
+    List<DeviceInfo> findByIsDelete(Boolean isDelete);
 }

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

@@ -16,4 +16,6 @@ public interface GateWayUserInfoDao extends GateWayUserInfoDaoExtend, org.spring
     GateWayUserInfo findTopByUserName(String userName);
 
     List<GateWayUserInfo> findByIdIn(List<String> ids);
+
+    List<GateWayUserInfo> findByIsDelete(Boolean isDelete);
 }

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

@@ -9,20 +9,30 @@ 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.TopologyTreeModel;
+import com.zhongshu.iot.client.model.deskStatistics.TopologyTreeSearch;
 import com.zhongshu.iot.client.model.iot.statistics.DeviceStatisticsModel;
 import com.zhongshu.iot.client.model.mqtt.DeviceInfoSearchParam;
 import com.zhongshu.iot.client.type.OperationBusType;
 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.GateWayUserInfoDao;
 import com.zhongshu.iot.server.core.dao.mqtt.OperationMessageDao;
+import com.zhongshu.iot.server.core.domain.iot.device.DeviceInfo;
+import com.zhongshu.iot.server.core.domain.iot.device.GateWay2Device;
+import com.zhongshu.iot.server.core.domain.iot.device.GateWayUserInfo;
 import com.zhongshu.iot.server.core.util.CommonUtil;
 import com.zhongshu.iot.server.core.util.DateUtils;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.ObjectUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * 设备统计服务
@@ -40,6 +50,12 @@ public class DeviceStatisticsService {
     @Autowired
     private OperationMessageDao operationMessageDao;
 
+    @Autowired
+    private GateWayUserInfoDao gateWayUserInfoDao;
+
+    @Autowired
+    private GateWay2DeviceDao gateWay2DeviceDao;
+
     /**
      * 设备等的统计
      *
@@ -98,8 +114,7 @@ public class DeviceStatisticsService {
         Long endTime = search.getEndTime();
         // 已分钟一条数据
         while (startTime <= endTime) {
-            String minuteKey = com.zhongshu.card.client.utils.DateUtils.paresTime(startTime,
-                    com.zhongshu.card.client.utils.DateUtils.patternyyyyMMddHHmmKey);
+            String minuteKey = com.zhongshu.card.client.utils.DateUtils.paresTime(startTime, com.zhongshu.card.client.utils.DateUtils.patternyyyyMMddHHmmKey);
             String xAxi = com.zhongshu.card.client.utils.DateUtils.paresTime(startTime, com.zhongshu.card.client.utils.DateUtils.patternHHmm);
 
             xAxis.add(xAxi);
@@ -150,4 +165,84 @@ public class DeviceStatisticsService {
         return pieModel;
     }
 
+    /**
+     * 物联网设备数据拓扑图
+     * @return
+     */
+    public ResultContent<List<TopologyTreeModel>> deviceTopologyData(TopologyTreeSearch param) {
+        List<TopologyTreeModel> models = new ArrayList<>();
+        List<GateWayUserInfo> gateWayUserInfos = gateWayUserInfoDao.findByIsDelete(Boolean.FALSE);
+        if (ObjectUtils.isNotEmpty(gateWayUserInfos)) {
+            gateWayUserInfos = gateWayUserInfos.stream().filter(it -> {
+                return !it.getUserName().equals("admin");
+            }).collect(Collectors.toList());
+            List<DeviceInfo> deviceInfos = deviceInfoDao.findByIsDelete(Boolean.FALSE);
+            Map<String, List<DeviceInfo>> mqttUser2GateWayInfo = new HashMap<>();
+            for (DeviceInfo deviceInfo : deviceInfos) {
+                if (deviceInfo.getDeviceCategory() == DeviceCategory.GW) {
+                    // 网关
+                    List<DeviceInfo> list = new ArrayList<>();
+                    if (mqttUser2GateWayInfo.containsKey(deviceInfo.getMqttUserName())) {
+                        list = mqttUser2GateWayInfo.get(deviceInfo.getMqttUserName());
+                    }
+                    list.add(deviceInfo);
+                    mqttUser2GateWayInfo.put(deviceInfo.getMqttUserName(), list);
+                } else {
+
+                }
+            }
+
+            gateWayUserInfos.stream().forEach(it -> {
+                TopologyTreeModel mqttModel = mqttUserTopologyTreeModel(it);
+                // 查询网关信息
+                if (mqttUser2GateWayInfo.containsKey(it.getUserName())) {
+                    List<DeviceInfo> gateWays = mqttUser2GateWayInfo.get(it.getUserName());
+                    List<TopologyTreeModel> children = new ArrayList<>();
+                    for (DeviceInfo deviceInfo : gateWays) {
+                        TopologyTreeModel gateWayModel = deviceTopologyTreeModel(deviceInfo);
+                        List<GateWay2Device> gateWay2Devices = gateWay2DeviceDao.findByGateWayInfo(deviceInfo);
+                        if (ObjectUtils.isNotEmpty(gateWay2Devices)) {
+                            List<TopologyTreeModel> deviceModels = gateWay2Devices.stream().map(it2 -> {
+                                return deviceTopologyTreeModel(it2.getDeviceInfo());
+                            }).collect(Collectors.toList());
+                            gateWayModel.setChildren(deviceModels);
+                        }
+                        children.add(gateWayModel);
+                    }
+                    mqttModel.setChildren(children);
+                }
+                models.add(mqttModel);
+            });
+        }
+        return ResultContent.buildSuccess(models);
+    }
+
+    private TopologyTreeModel deviceTopologyTreeModel(DeviceInfo entity) {
+        TopologyTreeModel model = new TopologyTreeModel();
+        if (ObjectUtils.isNotEmpty(entity)) {
+            model.setId(entity.getDeviceId());
+            model.setName(entity.getDeviceName());
+            model.setType(entity.getDeviceCategory().name());
+            model.setTypeStr(entity.getDeviceCategory().getRemark());
+            model.setTypeLevel2(entity.getDeviceType().name());
+            model.setTypeLevel2Str(entity.getDeviceType().getRemark());
+            model.setState(entity.getState().name());
+        }
+        return model;
+    }
+
+    private TopologyTreeModel mqttUserTopologyTreeModel(GateWayUserInfo entity) {
+        TopologyTreeModel model = new TopologyTreeModel();
+        if (ObjectUtils.isNotEmpty(entity)) {
+            model.setId(entity.getUserName());
+            model.setName(entity.getUserName());
+            model.setType("gatewayUser");
+            model.setTypeStr("连接账号");
+            model.setTypeLevel2("gatewayUser");
+            model.setTypeLevel2Str("连接账号");
+            model.setState(entity.getState().name());
+        }
+        return model;
+    }
+
 }