TRX před 1 rokem
rodič
revize
5068aafe34

+ 1 - 1
src/main/java/com/zswl/dataservice/domain/iot/IotMain.java

@@ -71,7 +71,7 @@ public class IotMain extends SuperEntity {
     private BigDecimal end;
 
     @Schema(description = "最大的长度,如:类型为字符串时使用")
-    private String maxLength;
+    private Integer maxLength;
 
     @Schema(description = "属性,参数列表,输入参数")
     private List<IotAttribute> childs;

+ 4 - 1
src/main/java/com/zswl/dataservice/model/iot/IotAttribute.java

@@ -30,6 +30,9 @@ public class IotAttribute {
     @Schema(description = "参数名称,如:name")
     private String identifier;
 
+    @Schema(description = "是否必须")
+    private Boolean required = Boolean.FALSE;
+
     @Schema(description = "单位名称,如:千克、流明、立方米等...")
     private String unitName;
 
@@ -43,7 +46,7 @@ public class IotAttribute {
     private BigDecimal end;
 
     @Schema(description = "最大的长度,如:类型为字符串时使用")
-    private String maxLength;
+    private Integer maxLength;
 
     @Schema(description = "相关值的描述")
     private List<IotDict> dicts;

+ 1 - 1
src/main/java/com/zswl/dataservice/model/iot/IotMainModel.java

@@ -91,7 +91,7 @@ public class IotMainModel extends SuperModel {
     private BigDecimal stepNumber;
 
     @Schema(description = "最大的长度,如:类型为字符串时使用")
-    private String maxLength;
+    private Integer maxLength;
 
     @Schema(description = "属性,参数列表,输入参数")
     private List<IotAttribute> childs;

+ 142 - 0
src/main/java/com/zswl/dataservice/service/iot/IotDataVerifyService.java

@@ -0,0 +1,142 @@
+package com.zswl.dataservice.service.iot;
+
+import cn.hutool.json.JSONArray;
+import cn.hutool.json.JSONObject;
+import com.zswl.dataservice.domain.iot.IotMain;
+import com.zswl.dataservice.model.iot.IotAttribute;
+import com.zswl.dataservice.type.DataType;
+import com.zswl.dataservice.type.FunctionType;
+import com.zswl.dataservice.utils.CommonUtil;
+import com.zswl.dataservice.utils.bean.BeanUtils;
+import com.zswl.dataservice.utils.result.ResultContent;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.stereotype.Service;
+
+import java.math.BigDecimal;
+
+/**
+ * @author TRX
+ * @date 2024/7/17
+ */
+@Slf4j
+@Service
+public class IotDataVerifyService {
+
+    /**
+     * 验证
+     *
+     * @param iotMain
+     * @param data
+     * @return
+     */
+    public ResultContent<Boolean> verifyIotParam(IotMain iotMain, JSONObject data) {
+        if (iotMain != null && data != null) {
+            FunctionType functionType = iotMain.getFunctionType();
+            if (functionType == FunctionType.Attribute) {
+                IotAttribute attribute = new IotAttribute();
+                BeanUtils.copyProperties(iotMain, attribute);
+                attribute.setChilds(iotMain.getChilds());
+
+                String identifier = attribute.getIdentifier();
+                String name = attribute.getName();
+                // 属性
+                if (!data.containsKey(identifier)) {
+                    return ResultContent.buildFail(String.format("数据中不包含【%s】", identifier));
+                }
+
+                DataType dataType = attribute.getDataType();
+                if (dataType == DataType.String) {
+                    // 字符串
+                    String value = data.get(identifier, String.class);
+                    Integer maxLength = attribute.getMaxLength();
+                    if (!CommonUtil.longIsEmpty(maxLength) && StringUtils.isNotEmpty(value) && value.length() > maxLength) {
+                        return ResultContent.buildFail(String.format("【%s】值超过最大长度【%d】", identifier, maxLength));
+                    }
+                }
+
+                if (dataType == DataType.Number) {
+                    BigDecimal value = data.get(identifier, BigDecimal.class);
+                    BigDecimal start = attribute.getStart();
+                    BigDecimal end = attribute.getEnd();
+                    if (start != null && start.compareTo(value) > 0) {
+                        return ResultContent.buildFail(String.format("【%s】值小于规定最小值【%s】", identifier, start.toString()));
+                    }
+                    if (end != null && end.compareTo(value) < 0) {
+                        return ResultContent.buildFail(String.format("【%s】值大于规定最大值【%s】", identifier, end.toString()));
+                    }
+                    data.set(identifier, value);
+                }
+
+                if (dataType == DataType.Boolean) {
+                    // 布尔型
+                    Boolean value = data.get(identifier, Boolean.class);
+                    data.set(identifier, value);
+                }
+
+                if (dataType == DataType.Object) {
+                    // 对象
+                    try {
+                        JSONObject object = data.get(identifier, JSONObject.class);
+                    } catch (Exception e) {
+                        String msg = String.format("【%s】无法转换为JSONObject", identifier);
+                        log.error(msg);
+                        return ResultContent.buildFail(msg);
+                    }
+                }
+                if (dataType == DataType.Array) {
+                    // 数组
+                    try {
+                        JSONArray array = data.get(identifier, JSONArray.class);
+                        if (array != null && array.size() > 0) {
+                            DataType elementType = attribute.getElementType();
+                            for (int i = 0; i < array.size(); i++) {
+                                Object object = array.get(i);
+                                if (elementType == DataType.String) {
+                                    if (!(object instanceof String)) {
+                                        return ResultContent.buildFail(
+                                                String.format("【%s】属性第【%d】项内容不属于【%s】类型", identifier, (i + 1), DataType.String.getRemark())
+                                        );
+                                    }
+                                }
+
+                                if (elementType == DataType.Number) {
+                                    if (!(object instanceof Number)) {
+                                        return ResultContent.buildFail(
+                                                String.format("【%s】属性第【%d】项内容不属于【%s】类型",
+                                                        identifier, (i + 1), DataType.Number.getRemark())
+                                        );
+                                    }
+                                }
+
+                                if (elementType == DataType.Boolean) {
+                                    if (!(object instanceof Boolean)) {
+                                        return ResultContent.buildFail(
+                                                String.format("【%s】属性第【%d】项内容不属于【%s】类型",
+                                                        identifier, (i + 1), DataType.Boolean.getRemark())
+                                        );
+                                    }
+                                }
+                            }
+                        }
+                    } catch (Exception e) {
+                        String msg = String.format("【%s】无法转换为JSONArray", identifier);
+                        log.error(msg);
+                        return ResultContent.buildFail(msg);
+                    }
+                }
+            }
+        }
+        return ResultContent.buildSuccess();
+    }
+
+    public <T> T getValue(JSONObject data, String identifier, Class<T> t) {
+        try {
+            return data.get(identifier, t);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+}

+ 2 - 0
src/main/java/com/zswl/dataservice/service/iot/IotServiceImpl.java

@@ -1,6 +1,7 @@
 package com.zswl.dataservice.service.iot;
 
 import ch.qos.logback.core.model.NamedModel;
+import cn.hutool.json.JSONObject;
 import com.zswl.dataservice.dao.UserDao;
 import com.zswl.dataservice.dao.iot.IotDeviceDataDao;
 import com.zswl.dataservice.dao.iot.IotMainDao;
@@ -27,6 +28,7 @@ import com.zswl.dataservice.utils.bean.BeanUtil;
 import com.zswl.dataservice.utils.bean.BeanUtils;
 import com.zswl.dataservice.utils.page.PageEntityUtil;
 import com.zswl.dataservice.utils.result.ResultContent;
+import jdk.security.jarsigner.JarSigner;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;

+ 7 - 0
src/main/java/com/zswl/dataservice/utils/CommonUtil.java

@@ -72,6 +72,13 @@ public class CommonUtil {
         return false;
     }
 
+    public static boolean longIsEmpty(Integer intValue) {
+        if (intValue == null || intValue <= 0) {
+            return true;
+        }
+        return false;
+    }
+
     /**
      * 判断bigDecimal值是否为空:为 null 或 0 时都为空
      *

+ 12 - 0
src/main/java/com/zswl/dataservice/utils/test/TestReplace.java

@@ -1,5 +1,8 @@
 package com.zswl.dataservice.utils.test;
 
+import cn.hutool.json.JSONObject;
+
+import java.math.BigDecimal;
 import java.util.regex.Pattern;
 
 /**
@@ -12,5 +15,14 @@ public class TestReplace {
         String str1 = str.replaceAll(Pattern.quote("${deviceId}"), "aaa");
 
         System.out.println(str1);
+
+        BigDecimal bigDecimal = BigDecimal.valueOf(15.24);
+
+        System.out.println(bigDecimal.toString());
+
+        JSONObject object = new JSONObject();
+        object.put("isRe", 458.21);
+        Object ob = object.get("isRe");
+        System.out.println(ob instanceof String);
     }
 }