From cd069965f32189683598ac1096dab66c992c517b Mon Sep 17 00:00:00 2001
From: 潘志宝 <979469083@qq.com>
Date: 星期五, 14 三月 2025 16:36:10 +0800
Subject: [PATCH] BAD点值修复

---
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/CalculateHandle.java |   59 ++++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 46 insertions(+), 13 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 699ea60..8b1376a 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,5 +1,7 @@
 package com.iailab.module.data.point.collection.handler;
 
+import com.alibaba.fastjson.JSON;
+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;
@@ -11,6 +13,7 @@
 import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
+
 import javax.annotation.Resource;
 
 import org.springframework.beans.factory.annotation.Autowired;
@@ -20,6 +23,7 @@
 
 import java.math.BigDecimal;
 import java.util.*;
+import java.util.stream.Stream;
 
 /**
  * 计算点处理
@@ -49,17 +53,20 @@
 
     public static final String regex = "[+\\-\\*/()\\&\\|\\>\\<]";
 
-    public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> dataMap) {
+    public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> dataMap, List<String> listGood, List<String> listBad) {
         List<InfluxPointValuePOJO> result = new ArrayList<>();
         try {
             log.info("计算点处理开始");
             if (CollectionUtils.isEmpty(dtos)) {
                 return result;
             }
+            log.info(JSON.toJSONString(listBad));
             dtos.forEach(dto -> {
                 try {
-                    Object value = singleCompute(dto, dataMap);
-                    InfluxPointValuePOJO pojo = GenInfluxPointValueUtils.getByPoint(dto, value);
+                    Object rawValue = singleCompute(dto, dataMap, listGood, listBad);
+                    BigDecimal coefficient = dto.getUnittransfactor() == null ? BigDecimal.ONE : dto.getUnittransfactor();
+                    BigDecimal calValue = new BigDecimal(rawValue.toString()).multiply(coefficient);
+                    InfluxPointValuePOJO pojo = GenInfluxPointValueUtils.getByPoint(dto, calValue);
                     pojo.setTimestamp(collectTime.toInstant());
                     result.add(pojo);
                 } catch (Exception ex) {
@@ -76,14 +83,24 @@
         return result;
     }
 
-    private Object singleCompute(DaPointDTO dto, Map<String, Object> dataMap) {
+    private Object singleCompute(DaPointDTO dto, Map<String, Object> dataMap, List<String> listGood, List<String> listBad) {
         String expression = dto.getExpression();
+        log.info("PointNo=" + dto.getPointNo() + ";SourceExpression=" + expression);
         String[] arr = expression.split(regex);
+        // 去掉arr中的空格
+        arr = Stream.of(arr).filter(StringUtils::isNotBlank).toArray(String[]::new);
+        // 判断arr都在dataMap中包含
+        if (!Arrays.stream(arr).allMatch(dataMap::containsKey)) {
+            log.info("dataMap not contains key");
+            listBad.add(dto.getPointNo());
+            return CommonConstant.BAD_VALUE;
+        }
 
         for (int i = 0; i < arr.length; i++) {
             String s = arr[i];
             if (StringUtils.isNotBlank(s) && dataMap.containsKey(s)) {
-                expression = expression.replace(s, dataMap.get(s).toString());
+                // 对每个数加(),否则负值报错
+                expression = expression.replace(s, "(" + dataMap.get(s).toString() + ")");
             }
         }
         expression = expression.replace("&", "&&");
@@ -93,22 +110,24 @@
         log.info("PointNo=" + dto.getPointNo() + ";expression=" + expression);
         String result = javaScriptHandler.eval(expression);
         log.info("result=" + result);
-        if (result == null) {
+        if (result == null || result.contains(JsErrorCode.Infinity.name()) || result.contains(JsErrorCode.NaN.name())) {
+            listBad.add(dto.getPointNo());
             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())) {
+                listGood.add(dto.getPointNo());
                 return new BigDecimal(result).intValue();
             } else if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType())) {
+                listGood.add(dto.getPointNo());
                 return new BigDecimal(result).setScale(4, BigDecimal.ROUND_UP).doubleValue();
             } else if (DataTypeEnum.BOOLEAN.getCode().equals(dto.getDataType())) {
+                listGood.add(dto.getPointNo());
                 return Boolean.parseBoolean(result);
+            } else {
+                listBad.add(dto.getPointNo());
+                throw new RuntimeException("计算异常,未知数据类型");
             }
         }
-        return result;
     }
 
     public Map<String, Object> getCurrent(List<String> pointNos) {
@@ -122,7 +141,9 @@
             if (redisTemplate.hasKey(PointCollector.PV + item.getPointNo())) {
                 value = redisTemplate.opsForValue().get(PointCollector.PV + item.getPointNo());
             } else {
-                value = singleCompute(item);
+                Object rawValue = singleCompute(item);
+                BigDecimal coefficient = item.getUnittransfactor() == null ? BigDecimal.ONE : item.getUnittransfactor();
+                value = new BigDecimal(rawValue.toString()).multiply(coefficient);
             }
             data.put(item.getPointNo(), value);
         });
@@ -143,7 +164,19 @@
             pointNos.add(s);
             dataMap.putAll(measureHandle.getCurrent(pointNos));
             dataMap.putAll(constantHandle.getCurrent(pointNos));
-            expression = expression.replace(s, dataMap.get(s).toString());
+            if (dataMap.get(s) == null) {
+                log.info("计算点数据异常");
+                log.info("pointNo=" + dto.getPointNo() + ";dataMap.key=" + s);
+                return CommonConstant.BAD_VALUE;
+            }
+            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("|", "||");

--
Gitblit v1.9.3