dengzedong
2024-12-24 aa0382e44311f9f7e62a688c8fcaa9c69a512e0f
iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/handler/MeasureHandle.java
@@ -1,5 +1,7 @@
package com.iailab.module.data.point.collection.handler;
import com.iailab.module.data.channel.http.collector.SourceApiEnum;
import com.iailab.module.data.channel.http.collector.ihdb.HttpCollectorForIhd;
import com.iailab.module.data.channel.opcda.collector.OpcDACollector;
import com.iailab.module.data.common.enums.CommonConstant;
import com.iailab.module.data.common.enums.DataSourceType;
@@ -8,21 +10,23 @@
import com.iailab.module.data.channel.kio.collector.KingIOCollector;
import com.iailab.module.data.channel.modbus.collector.ModBusCollector;
import com.iailab.module.data.channel.opcua.collector.OpcUaCollector;
import com.iailab.module.data.point.collection.PointCollector;
import com.iailab.module.data.point.collection.utils.GenInfluxPointValueUtils;
import com.iailab.module.data.point.common.PointDataTypeEnum;
import com.iailab.module.data.point.dto.DaPointDTO;
import com.iailab.module.data.point.service.DaPointService;
import com.iailab.module.data.channel.http.collector.HttpCollectorForZxzk;
import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO;
import lombok.extern.slf4j.Slf4j;
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;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
 * 测量点处理
@@ -51,11 +55,14 @@
    @Autowired
    private OpcDACollector opcDACollector;
    @Resource
    private HttpCollectorForZxzk httpCollectorForZxzk;
    @Autowired
    private HttpCollectorForIhd httpCollectorForIhd;
    @Resource
    private DaPointService daPointService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> dataMap) {
        log.info("测量点处理开始");
@@ -68,7 +75,7 @@
        List<String[]> opcDaTagIds = new ArrayList<>();
        List<String[]> modbusTagIds = new ArrayList<>();
        List<String[]> kioTagIds = new ArrayList<>();
        List<String[]> httpTagZxzk = new ArrayList<>();
        List<Object[]> httpTagIhd = new ArrayList<>();
        dtos.stream().forEach(item -> {
@@ -80,6 +87,12 @@
                modbusTagIds.add(new String[]{item.getSourceId(), item.getTagNo()});
            } else if (DataSourceType.KIO.getCode().equals(item.getSourceType())) {
                kioTagIds.add(new String[]{item.getSourceId(), item.getTagNo()});
            } else if (DataSourceType.HTTP.getCode().equals(item.getSourceType())) {
                if (SourceApiEnum.iHyperDB.getCode().equals(item.getSourceName())) {
                    if (item.getTagNo() != null && item.getDimension() != null && item.getValueType() != null) {
                        httpTagIhd.add(new Object[]{item.getSourceId(), item.getTagNo(), item.getDimension(), item.getValueType()});
                    }
                }
            }
        });
@@ -96,9 +109,8 @@
        if (!CollectionUtils.isEmpty(kioTagIds)) {
            tagValues.putAll(kingIOCollector.getTagValues(kioTagIds));
        }
        if (!CollectionUtils.isEmpty(httpTagZxzk)) {
            tagValues.putAll(httpCollectorForZxzk.getTagValues(httpTagZxzk));
        if (!CollectionUtils.isEmpty(httpTagIhd)) {
            tagValues.putAll(httpCollectorForIhd.getTagValues(httpTagIhd, collectTime));
        }
        this.toCommonResult(collectTime, dtos, tagValues, dataMap, result);
        log.info("测量点处理结束");
@@ -129,12 +141,17 @@
    private Object handleData(DaPointDTO dto, Object value) {
        Object result = value;
        try {
            if (value == null) {
                return CommonConstant.BAD_VALUE;
            }
            if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType()) || DataTypeEnum.INT.getCode().equals(dto.getDataType())) {
                BigDecimal rawValue = new BigDecimal(value.toString());
                if(CommonConstant.BAD_VALUE.compareTo(rawValue) == 0) {
                    return CommonConstant.BAD_VALUE;
                }
                // 异常值处理
                if (rawValue.compareTo(maxValue) > 0 || rawValue.compareTo(minValue) < 0) {
                    rawValue = CommonConstant.BAD_VALUE;
                    return CommonConstant.BAD_VALUE;
                }
                BigDecimal coefficient = dto.getUnittransfactor() == null ? BigDecimal.ONE : dto.getUnittransfactor();
                BigDecimal calValue = rawValue.multiply(coefficient);
@@ -153,7 +170,6 @@
            } else if (DataTypeEnum.BOOLEAN.getCode().equals(dto.getDataType())) {
                result = Boolean.parseBoolean(value.toString());
            }
        } catch (Exception ex) {
            log.warn("handleData异常,PointNo=" + dto.getPointNo());
            ex.printStackTrace();
@@ -167,23 +183,22 @@
        pointMeasureList.forEach(
                item -> {
                    try {
                        boolean hasKey = redisTemplate.hasKey(PointCollector.PV + item.getPointNo());
                        Object value = CommonConstant.BAD_VALUE;
                        if (DataSourceType.OPCUA.getCode().equals(item.getSourceType())) {
                        if (hasKey) {
                            value = redisTemplate.opsForValue().get(PointCollector.PV + item.getPointNo());
                        } else if (DataSourceType.OPCUA.getCode().equals(item.getSourceType())) {
                            value = opcUaCollector.getTagValue(item.getSourceId(), item.getTagNo());
                        } else if (DataSourceType.ModBus.getCode().equals(item.getSourceType())) {
                            value = modBusCollector.getTagValue(item.getSourceId(), item.getTagNo());
                        } else if (DataSourceType.KIO.getCode().equals(item.getSourceType())) {
                            value = kingIOCollector.getTagValue(item.getSourceId(), item.getTagNo());
                        } else if (DataSourceType.HTTP.getCode().equals(item.getSourceType())) {
                            if (CommonConstant.HTTP_API_ZXZK_IH.equals(item.getSourceName())) {
                                value = httpCollectorForZxzk.getTagValue(item.getSourceId(), item.getTagNo());
                            }
                            value = httpCollectorForIhd.getTagValue(item.getSourceId(), item.getTagNo(), item.getDimension(), item.getValueType());
                        } else {
                            log.info("没有匹配的TagNo=" + item.getTagNo());
                        }
                        log.info("没有匹配的TagNo=" + item.getTagNo());
                        log.info("valueStr=" + value.toString());
                        log.info("DataType=" + item.getDataType());
                        log.info("TagNo=" + item.getTagNo() + ",value=" + value.toString());
                        if (!PointDataTypeEnum.BOOLEAN.getCode().equals(item.getDataType())) {
                            BigDecimal decValue =  new BigDecimal(value.toString());
                            if (PointDataTypeEnum.FLOAT.getCode().equals(item.getDataType())) {
@@ -195,6 +210,11 @@
                        } else {
                            data.put(item.getPointNo(), value);
                        }
                        if (!hasKey) {
                            // 存入缓存
                            toRedis(value, item);
                        }
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
@@ -203,4 +223,17 @@
        );
        return data;
    }
    public void toRedis(Object value, DaPointDTO point) {
        if (PointDataTypeEnum.FLOAT.getCode().equals(point.getDataType())) {
            redisTemplate.opsForValue().set(PointCollector.PV + point.getPointNo(), new BigDecimal(value.toString()).intValue(),
                    PointCollector.offset, TimeUnit.SECONDS);
        } else if (PointDataTypeEnum.INT.getCode().equals(point.getDataType())) {
            redisTemplate.opsForValue().set(PointCollector.PV + point.getPointNo(), new BigDecimal(value.toString()).doubleValue(),
                    PointCollector.offset, TimeUnit.SECONDS);
        } else if (PointDataTypeEnum.BOOLEAN.getCode().equals(point.getDataType())) {
            redisTemplate.opsForValue().set(PointCollector.PV + point.getPointNo(), Boolean.parseBoolean(value.toString()),
                    PointCollector.offset, TimeUnit.SECONDS);
        }
    }
}