潘志宝
2025-01-14 48d2a226f4d6fdc812dbca0a0898772c769b67ff
iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/point/collection/PointCollector.java
@@ -3,6 +3,7 @@
import com.iailab.module.data.common.enums.DataSourceType;
import com.iailab.module.data.common.utils.R;
import com.iailab.module.data.channel.kio.collector.KingIOCollector;
import com.iailab.module.data.influxdb.pojo.InfluxPointValueBoolPOJO;
import com.iailab.module.data.influxdb.pojo.InfluxPointValueDigPOJO;
import com.iailab.module.data.influxdb.pojo.InfluxPointValueSimPOJO;
import com.iailab.module.data.point.collection.handler.CalculateHandle;
@@ -23,11 +24,14 @@
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.time.Duration;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
 * @author PanZhibao
@@ -68,6 +72,13 @@
    @Autowired
    private DaPointCollectStatusService daPointCollectStatusService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    public static final String PV = "point_value:";
    public static final long offset = 60 * 3L;
    /**
     * 采集
     *
@@ -78,47 +89,46 @@
        try {
            Map<String, Object> dataMap = new HashMap<>();
            List<InfluxPointValuePOJO> pointValues = new ArrayList<>();
            // 记录点位状态
            List<String> listGood = new ArrayList<>();
            List<String> listBad = new ArrayList<>();
            log.info("读取常量点");
            List<DaPointDTO> pointConstantList = daPointService.getConstantPoint(minfreq);
            pointValues.addAll(constantHandle.handle(collectTime, pointConstantList, dataMap));
            pointValues.addAll(constantHandle.handle(collectTime, pointConstantList, dataMap,listGood,listBad));
            log.info("读取测量点");
            List<DaPointDTO> pointMeasureList = daPointService.getMeasurePoint(minfreq);
            pointValues.addAll(measureHandle.handle(collectTime, pointMeasureList, dataMap));
            pointValues.addAll(measureHandle.handle(collectTime, pointMeasureList, dataMap,listGood,listBad));
            log.info("读取计算点");
            List<DaPointDTO> pointCalculateList = daPointService.getMathPoint(minfreq);
            pointValues.addAll(calculateHandle.handle(collectTime, pointCalculateList, dataMap));
            pointValues.addAll(calculateHandle.handle(collectTime, pointCalculateList, dataMap,listGood,listBad));
            log.info("读取累计点");
            List<DaPointDTO> pointCumulateList = daPointService.getCumulatePoint(minfreq);
            pointValues.addAll(cumulateHandle.handle(collectTime, pointCumulateList));
            pointValues.addAll(cumulateHandle.handle(collectTime, pointCumulateList,listGood,listBad));
            log.info("存入数据库");
            log.info("存入时序库");
            influxDBService.asyncWritePointValues(pointValues);
            log.info("存入缓存");
            for (InfluxPointValuePOJO pointValue : pointValues) {
                if (pointValue instanceof InfluxPointValueSimPOJO) {
                    InfluxPointValueSimPOJO simPOJO = (InfluxPointValueSimPOJO) pointValue;
                    redisTemplate.opsForValue().set(PV + simPOJO.getPoint(), simPOJO.getValue().doubleValue(), offset, TimeUnit.SECONDS);
                } else if (pointValue instanceof InfluxPointValueDigPOJO) {
                    InfluxPointValueDigPOJO digPOJO = (InfluxPointValueDigPOJO) pointValue;
                    redisTemplate.opsForValue().set(PV + digPOJO.getPoint(), digPOJO.getValue().intValue(), offset, TimeUnit.SECONDS);
                } else if (pointValue instanceof InfluxPointValueBoolPOJO) {
                    InfluxPointValueBoolPOJO boolPOJO = (InfluxPointValueBoolPOJO) pointValue;
                    redisTemplate.opsForValue().set(PV + boolPOJO.getPoint(), boolPOJO.getValue().booleanValue(), offset, TimeUnit.SECONDS);
                }
            }
            log.info("更新采集状态");
            updateCollectStatus(pointValues, collectTime);
            daPointCollectStatusService.recordStatusList(listGood,listBad, collectTime);
            log.info("采集完成");
        } catch (Exception ex) {
            log.info("采集异常!");
            ex.printStackTrace();
        }
    }
    private void updateCollectStatus(List<InfluxPointValuePOJO> pointValues, Date collectTime) {
        try {
            for (InfluxPointValuePOJO pointValue : pointValues) {
                if (pointValue instanceof InfluxPointValueSimPOJO) {
                    InfluxPointValueSimPOJO pvo = (InfluxPointValueSimPOJO) pointValue;
                    daPointCollectStatusService.recordStatus(pvo.getPoint(), pvo.getValue().toString(), collectTime);
                } else if (pointValue instanceof InfluxPointValueDigPOJO) {
                    InfluxPointValueDigPOJO pvo = (InfluxPointValueDigPOJO) pointValue;
                    daPointCollectStatusService.recordStatus(pvo.getPoint(), pvo.getValue().toString(), collectTime);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
@@ -132,8 +142,10 @@
            data.putAll(constantHandle.getCurrent(pointNos));
            data.putAll(measureHandle.getCurrent(pointNos));
            data.putAll(calculateHandle.getCurrent(pointNos));
            data.putAll(cumulateHandle.getCurrent(pointNos));
            return data;
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.error(ex.getMessage());
        }