From 88c781e970e813c6cdc34c67f2060e7ca9caee78 Mon Sep 17 00:00:00 2001 From: dongyukun <1208714201@qq.com> Date: 星期二, 03 六月 2025 18:07:32 +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 | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 100 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 8b1376a..406382e 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 @@ -6,6 +6,7 @@ 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.enums.DataPointFreqEnum; 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; @@ -15,6 +16,7 @@ import org.apache.commons.lang3.StringUtils; import javax.annotation.Resource; +import javax.validation.constraints.Max; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; @@ -23,6 +25,7 @@ import java.math.BigDecimal; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Stream; /** @@ -46,12 +49,24 @@ private ConstantHandle constantHandle; @Resource + private CumulateHandle cumulateHandle; + + @Resource + private ExtremalHandle extremalHandle; + + @Resource private JavaScriptHandler javaScriptHandler; @Autowired private RedisTemplate<String, Object> redisTemplate; - public static final String regex = "[+\\-\\*/()\\&\\|\\>\\<]"; + public final static String regex = "[+\\-\\*/()\\&\\|\\>\\<]"; + + private final static String POINT_PREFIX = "M"; + + private final static String PENDING_FLAG = "pending"; + + private final static int MAX_RECURSION = 10; public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> dataMap, List<String> listGood, List<String> listBad) { List<InfluxPointValuePOJO> result = new ArrayList<>(); @@ -60,27 +75,83 @@ if (CollectionUtils.isEmpty(dtos)) { return result; } + Map<String, DaPointDTO> pendingMap = new HashMap<>(); log.info(JSON.toJSONString(listBad)); dtos.forEach(dto -> { try { 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); + if (PENDING_FLAG.equals(rawValue.toString())) { + pendingMap.put(dto.getPointNo(), dto); + } else { + BigDecimal coefficient = dto.getUnittransfactor() == null ? BigDecimal.ONE : dto.getUnittransfactor(); + BigDecimal calValue = new BigDecimal(rawValue.toString()).multiply(coefficient); + if (dto.getMaxValue() != null && calValue.compareTo(dto.getMaxValue()) > 0) { + calValue = dto.getMaxValue(); + } else if (dto.getMinValue() != null && calValue.compareTo(dto.getMinValue()) < 0) { + calValue = dto.getMinValue(); + } + dataMap.put(dto.getPointNo(), calValue); + InfluxPointValuePOJO pojo = GenInfluxPointValueUtils.getByPoint(dto, calValue); + pojo.setTimestamp(GenInfluxPointValueUtils.getByMin(collectTime, DataPointFreqEnum.getEumByCode(dto.getMinfreqid()))); + result.add(pojo); + } } catch (Exception ex) { ex.printStackTrace(); log.info("计算点异常!PointNo=" + dto.getPointNo()); } }); - log.info("计算点处理结束"); + + Map<DaPointDTO, Object> valueResult = new HashMap<>(); + handPending(pendingMap, dataMap, listGood, listBad, valueResult, 1); + log.info("valueResult size=" + valueResult.size()); + valueResult.forEach((key, value) -> { + InfluxPointValuePOJO pojo = GenInfluxPointValueUtils.getByPoint(key, value); + pojo.setTimestamp(GenInfluxPointValueUtils.getByMin(collectTime, DataPointFreqEnum.getEumByCode(key.getMinfreqid()))); + result.add(pojo); + }); + log.info("计算点处理结束"); } catch (Exception ex) { ex.printStackTrace(); log.info("计算点处理异常!"); } return result; + } + private void handPending(Map<String, DaPointDTO> pendingMap, Map<String, Object> dataMap, + List<String> listGood, List<String> listBad, Map<DaPointDTO, Object> valueResult, int count) { + + Map<String, DaPointDTO> tempMap = new HashMap<>(); + if (CollectionUtils.isEmpty(pendingMap)) { + log.info("pendingMap is empty"); + return; + } + log.info("处理包含计算点的"); + log.info("handPending count=" + count); + if (count > MAX_RECURSION) { + log.info("最多递归10次"); + return; + } + count = count + 1; + for(String key : pendingMap.keySet()) { + DaPointDTO dto = pendingMap.get(key); + Object rawValue = singleCompute(dto, dataMap, listGood, listBad); + if (PENDING_FLAG.equals(rawValue.toString())) { + tempMap.put(key, dto); + } else { + BigDecimal coefficient = dto.getUnittransfactor() == null ? BigDecimal.ONE : dto.getUnittransfactor(); + BigDecimal calValue = new BigDecimal(rawValue.toString()).multiply(coefficient); + if (dto.getMaxValue() != null && calValue.compareTo(dto.getMaxValue()) > 0) { + calValue = dto.getMaxValue(); + } else if (dto.getMinValue() != null && calValue.compareTo(dto.getMinValue()) < 0) { + calValue = dto.getMinValue(); + } + dataMap.put(dto.getPointNo(), calValue); + valueResult.put(dto, calValue); + } + } + if (!CollectionUtils.isEmpty(tempMap)) { + this.handPending(tempMap, dataMap, listGood, listBad, valueResult, count); + } } private Object singleCompute(DaPointDTO dto, Map<String, Object> dataMap, List<String> listGood, List<String> listBad) { @@ -108,6 +179,11 @@ expression = expression.replace("False", "false"); expression = expression.replace("True", "true"); log.info("PointNo=" + dto.getPointNo() + ";expression=" + expression); + if(expression.contains(POINT_PREFIX)) { + // 包含计算点,先挂起 + return PENDING_FLAG; + } + String result = javaScriptHandler.eval(expression); log.info("result=" + result); if (result == null || result.contains(JsErrorCode.Infinity.name()) || result.contains(JsErrorCode.NaN.name())) { @@ -141,7 +217,7 @@ if (redisTemplate.hasKey(PointCollector.PV + item.getPointNo())) { value = redisTemplate.opsForValue().get(PointCollector.PV + item.getPointNo()); } else { - Object rawValue = singleCompute(item); + Object rawValue = singleCompute(item, 1); BigDecimal coefficient = item.getUnittransfactor() == null ? BigDecimal.ONE : item.getUnittransfactor(); value = new BigDecimal(rawValue.toString()).multiply(coefficient); } @@ -150,7 +226,7 @@ return data; } - private Object singleCompute(DaPointDTO dto) { + private Object singleCompute(DaPointDTO dto, int count) { String result = CommonConstant.BAD_VALUE.toString(); Map<String, Object> dataMap = new HashMap<>(); String expression = dto.getExpression(); @@ -164,6 +240,21 @@ pointNos.add(s); dataMap.putAll(measureHandle.getCurrent(pointNos)); dataMap.putAll(constantHandle.getCurrent(pointNos)); + dataMap.putAll(cumulateHandle.getCurrent(pointNos)); + dataMap.putAll(extremalHandle.getCurrent(pointNos)); + if (s.contains(POINT_PREFIX)) { + log.info("计算点递归查询"); + List<DaPointDTO> pointMathList = daPointService.getMathPoint(pointNos); + if (CollectionUtils.isEmpty(pointMathList)) { + return result; + } + log.info("count = " + count); + if (count > MAX_RECURSION) { + return result; + } + this.singleCompute(pointMathList.get(0), count); + count = count + 1; + } if (dataMap.get(s) == null) { log.info("计算点数据异常"); log.info("pointNo=" + dto.getPointNo() + ";dataMap.key=" + s); -- Gitblit v1.9.3