TRX 1 år sedan
förälder
incheckning
640e8f3165

+ 15 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/dataConfig/DeviceConfig.java

@@ -0,0 +1,15 @@
+package com.zhongshu.iot.server.core.dataConfig;
+
+/**
+ * @author TRX
+ * @date 2025/1/16
+ */
+public class DeviceConfig {
+
+    // 设备deviceId或gatewayId的最大长度
+    public static final int maxDeviceIdLength = 50;
+
+    // 设备deviceId或gatewayId的最小长度
+    public static final int minDeviceIdLength = 6;
+
+}

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

@@ -19,6 +19,7 @@ import com.zhongshu.iot.server.core.service.iot.IotServiceImpl;
 import com.zhongshu.iot.server.core.service.sync.DeviceSyncFullCardService;
 import com.zhongshu.iot.server.core.util.CommonUtil;
 import com.zhongshu.iot.server.core.util.DateUtils;
+import com.zhongshu.iot.server.core.util.JMXUtil;
 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.page.PageEntityUtil;
@@ -158,6 +159,16 @@ public class GateWayInfoService extends SuperService {
         if (param.getHbInterval() != null && param.getHbInterval() <= 0) {
             return ResultContent.buildFail("hbInterval不符合规范");
         }
+        ResultContent checkIdContent = JMXUtil.checkDeviceIdLength(param.getGateWayId(), "gateWayId");
+        if (checkIdContent.isFailed()) {
+            return ResultContent.buildFail(checkIdContent.getMsg());
+        }
+        ResultContent checkNameContent = JMXUtil.checkDeviceIdLength(param.getGateWayName(), "gateWayName");
+        if (checkNameContent.isFailed()) {
+            return ResultContent.buildFail(checkNameContent.getMsg());
+        }
+
+
         ProjectInfo projectInfo = null;
         if (StringUtils.isNotEmpty(param.getProjectInfoCode())) {
             projectInfo = projectInfoDao.findTopByCode(param.getProjectInfoCode());
@@ -262,6 +273,15 @@ public class GateWayInfoService extends SuperService {
         if (StringUtils.isEmpty(param.getProjectInfoCode())) {
             return ResultContent.buildFail("projectInfoCode不能为空");
         }
+        ResultContent checkIdContent = JMXUtil.checkDeviceIdLength(param.getDeviceId(), "deviceId");
+        if (checkIdContent.isFailed()) {
+            return ResultContent.buildFail(checkIdContent.getMsg());
+        }
+        ResultContent checkNameContent = JMXUtil.checkDeviceIdLength(param.getDeviceName(), "deviceName");
+        if (checkNameContent.isFailed()) {
+            return ResultContent.buildFail(checkNameContent.getMsg());
+        }
+
         ProjectInfo projectInfo = projectInfoDao.findTopByCode(param.getProjectInfoCode());
         if (ObjectUtils.isEmpty(projectInfo)) {
             return ResultContent.buildFail("projectInfoCode不存在");

+ 20 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/util/JMXUtil.java

@@ -2,6 +2,8 @@ package com.zhongshu.iot.server.core.util;
 
 import cn.hutool.json.JSONArray;
 import cn.hutool.json.JSONObject;
+import com.github.microservice.net.ResultContent;
+import com.zhongshu.iot.server.core.dataConfig.DeviceConfig;
 import com.zhongshu.iot.server.core.dataConfig.MqttConfig;
 import com.zhongshu.iot.server.core.domain.iot.mqtt.DeviceInfo;
 import com.zhongshu.iot.server.core.domain.iot.mqtt.GateWayInfo;
@@ -121,4 +123,22 @@ public class JMXUtil {
         return roleName;
     }
 
+    /**
+     * 检测deviceId或gatewayId的长度是否符合规范
+     *
+     * @param id
+     * @return
+     */
+    public static ResultContent checkDeviceIdLength(String id, String key) {
+        if (StringUtils.isNotEmpty(id)) {
+            if (id.contains(" ")) {
+                return ResultContent.buildFail(String.format("%s含有空格,不符合规范"));
+            }
+            if (id.length() > DeviceConfig.maxDeviceIdLength || id.length() < DeviceConfig.minDeviceIdLength) {
+                return ResultContent.buildFail(String.format("%s长度不能大于:%d,小于:%d", key, DeviceConfig.maxDeviceIdLength, DeviceConfig.minDeviceIdLength));
+            }
+        }
+        return ResultContent.buildSuccess();
+    }
+
 }