潘志宝
2025-03-14 cd069965f32189683598ac1096dab66c992c517b
iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/CalculateHandle.java
@@ -13,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;
@@ -22,6 +23,7 @@
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Stream;
/**
 * 计算点处理
@@ -51,7 +53,7 @@
    public static final String regex = "[+\\-\\*/()\\&\\|\\>\\<]";
    public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> dataMap,List<String> listGood,List<String> listBad) {
    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("计算点处理开始");
@@ -61,8 +63,10 @@
            log.info(JSON.toJSONString(listBad));
            dtos.forEach(dto -> {
                try {
                    Object value = singleCompute(dto, dataMap, listGood, listBad);
                    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) {
@@ -79,10 +83,12 @@
        return result;
    }
    private Object singleCompute(DaPointDTO dto, Map<String, Object> dataMap,List<String> listGood,List<String> listBad) {
    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");
@@ -135,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);
        });
@@ -158,10 +166,10 @@
            dataMap.putAll(constantHandle.getCurrent(pointNos));
            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);
            }