From 06dcd31d599e24711d609dece7eb07a8b5f58dcd Mon Sep 17 00:00:00 2001 From: 潘志宝 <979469083@qq.com> Date: 星期三, 25 六月 2025 10:18:04 +0800 Subject: [PATCH] 测点当前值增加上下限制 --- iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/CalculateHandle.java | 135 ++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 120 insertions(+), 15 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 7731dc9..08a2ebb 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,10 +1,13 @@ package com.iailab.module.data.point.collection.handler; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; 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.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; @@ -12,7 +15,9 @@ import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO; import lombok.extern.slf4j.Slf4j; 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; @@ -21,7 +26,7 @@ import java.math.BigDecimal; import java.util.*; -import java.util.stream.Collectors; +import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Stream; /** @@ -45,56 +50,137 @@ 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 = "[+\\-\\*/()\\&\\|\\>\\<]"; - public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> dataMap,List<String> listGood,List<String> listBad) { + private final static String POINT_PREFIX = "C"; + + 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<>(); try { log.info("计算点处理开始"); if (CollectionUtils.isEmpty(dtos)) { return result; } + Map<String, DaPointDTO> pendingMap = new HashMap<>(); + log.info(JSON.toJSONString(listBad)); dtos.forEach(dto -> { try { - Object value = singleCompute(dto, dataMap, listGood, listBad); - InfluxPointValuePOJO pojo = GenInfluxPointValueUtils.getByPoint(dto, value); - pojo.setTimestamp(collectTime.toInstant()); - result.add(pojo); + Object rawValue = singleCompute(dto, dataMap, listGood, listBad); + 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) { - private Object singleCompute(DaPointDTO dto, Map<String, Object> dataMap,List<String> listGood,List<String> listBad) { + 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) { 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)) { + /*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() + ")"); + } else if(StringUtils.isNotBlank(s) && s.trim().startsWith(POINT_PREFIX)) { + log.info("包含计算点,先挂起"); + log.info("dataMap=" + JSONObject.toJSONString(dataMap)); + return PENDING_FLAG; + } else if(StringUtils.isNotBlank(s) && !dataMap.containsKey(s)) { + log.info("dataMap not contains key " + s); + listBad.add(dto.getPointNo()); + return CommonConstant.BAD_VALUE; } } expression = expression.replace("&", "&&"); @@ -102,6 +188,7 @@ expression = expression.replace("False", "false"); expression = expression.replace("True", "true"); log.info("PointNo=" + dto.getPointNo() + ";expression=" + expression); + String result = javaScriptHandler.eval(expression); log.info("result=" + result); if (result == null || result.contains(JsErrorCode.Infinity.name()) || result.contains(JsErrorCode.NaN.name())) { @@ -135,14 +222,16 @@ if (redisTemplate.hasKey(PointCollector.PV + item.getPointNo())) { value = redisTemplate.opsForValue().get(PointCollector.PV + item.getPointNo()); } else { - value = singleCompute(item); + Object rawValue = singleCompute(item, 1); + BigDecimal coefficient = item.getUnittransfactor() == null ? BigDecimal.ONE : item.getUnittransfactor(); + value = new BigDecimal(rawValue.toString()).multiply(coefficient); } data.put(item.getPointNo(), value); }); 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(); @@ -156,12 +245,28 @@ 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.trim().startsWith(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; + } + Object v = this.singleCompute(pointMathList.get(0), count); + dataMap.put(s, v); + count = count + 1; + } if (dataMap.get(s) == null) { log.info("计算点数据异常"); - log.info("pointNo=" + dto.getPointNo() +";dataMap.key=" + s); + log.info("pointNo=" + dto.getPointNo() + ";dataMap.key=" + s); return CommonConstant.BAD_VALUE; } - String valueStr = 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); } -- Gitblit v1.9.3