From 76743b009ca5ea67557fcab597b332f8d1947813 Mon Sep 17 00:00:00 2001 From: dengzedong <dengzedong@email> Date: 星期二, 24 十二月 2024 14:09:04 +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 | 30 ++++++++++++++++++++++-------- 1 files changed, 22 insertions(+), 8 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..699ea60 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,10 @@ package com.iailab.module.data.point.collection.handler; +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 +12,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 +43,9 @@ @Resource private JavaScriptHandler javaScriptHandler; + + @Autowired + private RedisTemplate<String, Object> redisTemplate; public static final String regex = "[+\\-\\*/()\\&\\|\\>\\<]"; @@ -86,7 +94,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 +103,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 +118,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); @@ -135,19 +150,18 @@ 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