IotDataVerifyService.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package com.zswl.dataservice.service.iot;
  2. import cn.hutool.json.JSONArray;
  3. import cn.hutool.json.JSONObject;
  4. import com.zswl.dataservice.domain.iot.IotMain;
  5. import com.zswl.dataservice.model.iot.IotAttribute;
  6. import com.zswl.dataservice.type.DataType;
  7. import com.zswl.dataservice.type.FunctionType;
  8. import com.zswl.dataservice.utils.CommonUtil;
  9. import com.zswl.dataservice.utils.bean.BeanUtils;
  10. import com.zswl.dataservice.utils.result.ResultContent;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.apache.commons.lang3.StringUtils;
  13. import org.springframework.stereotype.Service;
  14. import java.math.BigDecimal;
  15. /**
  16. * @author TRX
  17. * @date 2024/7/17
  18. */
  19. @Slf4j
  20. @Service
  21. public class IotDataVerifyService {
  22. /**
  23. * 验证
  24. *
  25. * @param iotMain
  26. * @param data
  27. * @return
  28. */
  29. public ResultContent<Boolean> verifyIotParam(IotMain iotMain, JSONObject data) {
  30. if (iotMain != null && data != null) {
  31. FunctionType functionType = iotMain.getFunctionType();
  32. if (functionType == FunctionType.Attribute) {
  33. IotAttribute attribute = new IotAttribute();
  34. BeanUtils.copyProperties(iotMain, attribute);
  35. attribute.setChilds(iotMain.getChilds());
  36. String identifier = attribute.getIdentifier();
  37. String name = attribute.getName();
  38. // 属性
  39. if (!data.containsKey(identifier)) {
  40. return ResultContent.buildFail(String.format("数据中不包含【%s】", identifier));
  41. }
  42. DataType dataType = attribute.getDataType();
  43. if (dataType == DataType.String) {
  44. // 字符串
  45. String value = data.get(identifier, String.class);
  46. Integer maxLength = attribute.getMaxLength();
  47. if (!CommonUtil.longIsEmpty(maxLength) && StringUtils.isNotEmpty(value) && value.length() > maxLength) {
  48. return ResultContent.buildFail(String.format("【%s】值超过最大长度【%d】", identifier, maxLength));
  49. }
  50. }
  51. if (dataType == DataType.Number) {
  52. BigDecimal value = data.get(identifier, BigDecimal.class);
  53. BigDecimal start = attribute.getStart();
  54. BigDecimal end = attribute.getEnd();
  55. if (start != null && start.compareTo(value) > 0) {
  56. return ResultContent.buildFail(String.format("【%s】值小于规定最小值【%s】", identifier, start.toString()));
  57. }
  58. if (end != null && end.compareTo(value) < 0) {
  59. return ResultContent.buildFail(String.format("【%s】值大于规定最大值【%s】", identifier, end.toString()));
  60. }
  61. data.set(identifier, value);
  62. }
  63. if (dataType == DataType.Boolean) {
  64. // 布尔型
  65. Boolean value = data.get(identifier, Boolean.class);
  66. data.set(identifier, value);
  67. }
  68. if (dataType == DataType.Object) {
  69. // 对象
  70. try {
  71. JSONObject object = data.get(identifier, JSONObject.class);
  72. } catch (Exception e) {
  73. String msg = String.format("【%s】无法转换为JSONObject", identifier);
  74. log.error(msg);
  75. return ResultContent.buildFail(msg);
  76. }
  77. }
  78. if (dataType == DataType.Array) {
  79. // 数组
  80. try {
  81. JSONArray array = data.get(identifier, JSONArray.class);
  82. if (array != null && array.size() > 0) {
  83. DataType elementType = attribute.getElementType();
  84. for (int i = 0; i < array.size(); i++) {
  85. Object object = array.get(i);
  86. if (elementType == DataType.String) {
  87. if (!(object instanceof String)) {
  88. return ResultContent.buildFail(
  89. String.format("【%s】属性第【%d】项内容不属于【%s】类型", identifier, (i + 1), DataType.String.getRemark())
  90. );
  91. }
  92. }
  93. if (elementType == DataType.Number) {
  94. if (!(object instanceof Number)) {
  95. return ResultContent.buildFail(
  96. String.format("【%s】属性第【%d】项内容不属于【%s】类型",
  97. identifier, (i + 1), DataType.Number.getRemark())
  98. );
  99. }
  100. }
  101. if (elementType == DataType.Boolean) {
  102. if (!(object instanceof Boolean)) {
  103. return ResultContent.buildFail(
  104. String.format("【%s】属性第【%d】项内容不属于【%s】类型",
  105. identifier, (i + 1), DataType.Boolean.getRemark())
  106. );
  107. }
  108. }
  109. }
  110. }
  111. } catch (Exception e) {
  112. String msg = String.format("【%s】无法转换为JSONArray", identifier);
  113. log.error(msg);
  114. return ResultContent.buildFail(msg);
  115. }
  116. }
  117. }
  118. }
  119. return ResultContent.buildSuccess();
  120. }
  121. public <T> T getValue(JSONObject data, String identifier, Class<T> t) {
  122. try {
  123. return data.get(identifier, t);
  124. } catch (Exception e) {
  125. e.printStackTrace();
  126. }
  127. return null;
  128. }
  129. }