From aa0382e44311f9f7e62a688c8fcaa9c69a512e0f Mon Sep 17 00:00:00 2001
From: dengzedong <dengzedong@email>
Date: 星期二, 24 十二月 2024 15:15:10 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/CalculateHandle.java |   40 +++++++++++++++++++++++++++++++---------
 1 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/CalculateHandle.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/CalculateHandle.java
index c45c633..f140509 100644
--- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/CalculateHandle.java
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/CalculateHandle.java
@@ -1,8 +1,11 @@
 package com.iailab.module.data.point.collection.handler;
 
+import com.iailab.framework.common.util.string.StrUtils;
+import com.iailab.module.data.common.enums.CommonConstant;
 import com.iailab.module.data.common.enums.DataTypeEnum;
 import com.iailab.module.data.common.enums.JsErrorCode;
 import com.iailab.module.data.common.utils.JavaScriptHandler;
+import com.iailab.module.data.point.collection.PointCollector;
 import com.iailab.module.data.point.collection.utils.GenInfluxPointValueUtils;
 import com.iailab.module.data.point.dto.DaPointDTO;
 import com.iailab.module.data.point.service.DaPointService;
@@ -10,6 +13,9 @@
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import javax.annotation.Resource;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
@@ -38,6 +44,9 @@
 
     @Resource
     private JavaScriptHandler javaScriptHandler;
+
+    @Autowired
+    private RedisTemplate<String, Object> redisTemplate;
 
     public static final String regex = "[+\\-\\*/()\\&\\|\\>\\<]";
 
@@ -86,7 +95,7 @@
         String result = javaScriptHandler.eval(expression);
         log.info("result=" + result);
         if (result == null) {
-            return null;
+            return CommonConstant.BAD_VALUE;
         } else if (result.contains(JsErrorCode.Infinity.name()) ||
                 result.contains(JsErrorCode.NaN.name())) {
             log.info("计算异常,使用默认值");
@@ -95,7 +104,7 @@
             if (DataTypeEnum.INT.getCode().equals(dto.getDataType())) {
                 return new BigDecimal(result).intValue();
             } else if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType())) {
-                return new BigDecimal(result).setScale(10, BigDecimal.ROUND_UP).doubleValue();
+                return new BigDecimal(result).setScale(4, BigDecimal.ROUND_UP).doubleValue();
             } else if (DataTypeEnum.BOOLEAN.getCode().equals(dto.getDataType())) {
                 return Boolean.parseBoolean(result);
             }
@@ -110,12 +119,19 @@
             return data;
         }
         pointMathList.forEach(item -> {
-            data.put(item.getPointNo(), singleCompute(item));
+            Object value = CommonConstant.BAD_VALUE;
+            if (redisTemplate.hasKey(PointCollector.PV + item.getPointNo())) {
+                value = redisTemplate.opsForValue().get(PointCollector.PV + item.getPointNo());
+            } else {
+                value = singleCompute(item);
+            }
+            data.put(item.getPointNo(), value);
         });
         return data;
     }
 
     private Object singleCompute(DaPointDTO dto) {
+        String result = CommonConstant.BAD_VALUE.toString();
         Map<String, Object> dataMap = new HashMap<>();
         String expression = dto.getExpression();
         String[] arr = expression.split(regex);
@@ -128,26 +144,32 @@
             pointNos.add(s);
             dataMap.putAll(measureHandle.getCurrent(pointNos));
             dataMap.putAll(constantHandle.getCurrent(pointNos));
-            expression = expression.replace(s, dataMap.get(s).toString());
+            String valueStr =  dataMap.get(s).toString();
+            if (StrUtils.isNumeric(valueStr) && new BigDecimal(valueStr).compareTo(CommonConstant.BAD_VALUE) == 0) {
+                log.info("BAD_VALUE:" + s);
+            }
+            if (StrUtils.isNumeric(valueStr) && new BigDecimal(valueStr).compareTo(BigDecimal.ZERO) < 0) {
+                valueStr = "(" + valueStr + ")";
+            }
+            expression = expression.replace(s, valueStr);
         }
         expression = expression.replace("&", "&&");
         expression = expression.replace("|", "||");
         expression = expression.replace("False", "false");
         expression = expression.replace("True", "true");
         log.info("PointNo=" + dto.getPointNo() + ";expression=" + expression);
-        String result = javaScriptHandler.eval(expression);
+        result = javaScriptHandler.eval(expression);
         log.info("result=" + result);
         if (result == null) {
-            return null;
-        } else if (result.contains(JsErrorCode.Infinity.name()) ||
-                result.contains(JsErrorCode.NaN.name())) {
+            return CommonConstant.BAD_VALUE;
+        } else if (result.contains(JsErrorCode.Infinity.name()) || result.contains(JsErrorCode.NaN.name())) {
             log.info("计算异常,使用默认值");
             return dto.getDefaultValue() == null ? BigDecimal.ZERO : dto.getDefaultValue();
         } else {
             if (DataTypeEnum.INT.getCode().equals(dto.getDataType())) {
                 return new BigDecimal(result).intValue();
             } else if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType())) {
-                return new BigDecimal(result).setScale(10, BigDecimal.ROUND_UP).doubleValue();
+                return new BigDecimal(result).setScale(2, BigDecimal.ROUND_UP).doubleValue();
             } else if (DataTypeEnum.BOOLEAN.getCode().equals(dto.getDataType())) {
                 return Boolean.parseBoolean(result);
             }

--
Gitblit v1.9.3