dengzedong
2025-01-18 3c511d4ef08ffece38891435a9d16ee38319345b
提交 | 用户 | 时间
a6de49 1 package com.iailab.module.data.point.collection;
H 2
3 import com.iailab.module.data.common.enums.DataSourceType;
4 import com.iailab.module.data.common.utils.R;
5 import com.iailab.module.data.channel.kio.collector.KingIOCollector;
781e72 6 import com.iailab.module.data.influxdb.pojo.InfluxPointValueBoolPOJO;
2228b6 7 import com.iailab.module.data.influxdb.pojo.InfluxPointValueDigPOJO;
8 import com.iailab.module.data.influxdb.pojo.InfluxPointValueSimPOJO;
a6de49 9 import com.iailab.module.data.point.collection.handler.CalculateHandle;
56dba6 10 import com.iailab.module.data.point.collection.handler.CumulateHandle;
a6de49 11 import com.iailab.module.data.point.common.PointTypeEnum;
H 12 import com.iailab.module.data.point.dto.DaPointDTO;
2228b6 13 import com.iailab.module.data.point.service.DaPointCollectStatusService;
a6de49 14 import com.iailab.module.data.point.service.DaPointService;
H 15 import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO;
16 import com.iailab.module.data.channel.modbus.collector.ModBusCollector;
17 import com.iailab.module.data.channel.opcua.collector.OpcUaCollector;
18 import com.iailab.module.data.point.collection.handler.ConstantHandle;
19 import com.iailab.module.data.point.collection.handler.MeasureHandle;
20 import com.iailab.module.data.point.dto.DaPointWriteValueDTO;
21 import com.iailab.module.data.influxdb.service.InfluxDBService;
22 import lombok.extern.slf4j.Slf4j;
2228b6 23
a6de49 24 import javax.annotation.Resource;
2228b6 25
26 import org.springframework.beans.factory.annotation.Autowired;
781e72 27 import org.springframework.data.redis.core.RedisTemplate;
a6de49 28 import org.springframework.stereotype.Component;
H 29 import org.springframework.util.CollectionUtils;
30
31 import java.math.BigDecimal;
140065 32 import java.time.Duration;
a6de49 33 import java.util.*;
140065 34 import java.util.concurrent.TimeUnit;
a6de49 35
H 36 /**
37  * @author PanZhibao
38  * @Description
39  * @createTime 2023年04月25日 16:16:00
40  */
41 @Slf4j
42 @Component
43 public class PointCollector {
44
45     @Resource
46     private DaPointService daPointService;
47
48     @Resource
49     private ConstantHandle constantHandle;
50
51     @Resource
52     private MeasureHandle measureHandle;
53
54     @Resource
55     private CalculateHandle calculateHandle;
56
57     @Resource
58     private KingIOCollector kingIOCollector;
59
60     @Resource
61     private InfluxDBService influxDBService;
62
63     @Resource
64     private ModBusCollector modBusCollector;
65
66     @Resource
67     private OpcUaCollector opcUaCollector;
68
56dba6 69     @Resource
70     private CumulateHandle cumulateHandle;
71
2228b6 72     @Autowired
73     private DaPointCollectStatusService daPointCollectStatusService;
74
781e72 75     @Autowired
76     private RedisTemplate<String, Object> redisTemplate;
77
140065 78     public static final String PV = "point_value:";
781e72 79
140065 80     public static final long offset = 60 * 3L;
781e72 81
a6de49 82     /**
H 83      * 采集
84      *
85      * @param collectTime
86      * @param minfreq
87      */
88     public void collect(Date collectTime, String minfreq) {
89         try {
90             Map<String, Object> dataMap = new HashMap<>();
91             List<InfluxPointValuePOJO> pointValues = new ArrayList<>();
2fcc1a 92             // 记录点位状态
D 93             List<String> listGood = new ArrayList<>();
94             List<String> listBad = new ArrayList<>();
a6de49 95             log.info("读取常量点");
H 96             List<DaPointDTO> pointConstantList = daPointService.getConstantPoint(minfreq);
2fcc1a 97             pointValues.addAll(constantHandle.handle(collectTime, pointConstantList, dataMap,listGood,listBad));
a6de49 98
H 99             log.info("读取测量点");
100             List<DaPointDTO> pointMeasureList = daPointService.getMeasurePoint(minfreq);
2fcc1a 101             pointValues.addAll(measureHandle.handle(collectTime, pointMeasureList, dataMap,listGood,listBad));
a6de49 102
H 103             log.info("读取计算点");
104             List<DaPointDTO> pointCalculateList = daPointService.getMathPoint(minfreq);
2fcc1a 105             pointValues.addAll(calculateHandle.handle(collectTime, pointCalculateList, dataMap,listGood,listBad));
a6de49 106
56dba6 107             log.info("读取累计点");
108             List<DaPointDTO> pointCumulateList = daPointService.getCumulatePoint(minfreq);
2fcc1a 109             pointValues.addAll(cumulateHandle.handle(collectTime, pointCumulateList,listGood,listBad));
56dba6 110
781e72 111             log.info("存入时序库");
a6de49 112             influxDBService.asyncWritePointValues(pointValues);
H 113
781e72 114             log.info("存入缓存");
115             for (InfluxPointValuePOJO pointValue : pointValues) {
116                 if (pointValue instanceof InfluxPointValueSimPOJO) {
117                     InfluxPointValueSimPOJO simPOJO = (InfluxPointValueSimPOJO) pointValue;
140065 118                     redisTemplate.opsForValue().set(PV + simPOJO.getPoint(), simPOJO.getValue().doubleValue(), offset, TimeUnit.SECONDS);
781e72 119                 } else if (pointValue instanceof InfluxPointValueDigPOJO) {
120                     InfluxPointValueDigPOJO digPOJO = (InfluxPointValueDigPOJO) pointValue;
140065 121                     redisTemplate.opsForValue().set(PV + digPOJO.getPoint(), digPOJO.getValue().intValue(), offset, TimeUnit.SECONDS);
781e72 122                 } else if (pointValue instanceof InfluxPointValueBoolPOJO) {
123                     InfluxPointValueBoolPOJO boolPOJO = (InfluxPointValueBoolPOJO) pointValue;
140065 124                     redisTemplate.opsForValue().set(PV + boolPOJO.getPoint(), boolPOJO.getValue().booleanValue(), offset, TimeUnit.SECONDS);
781e72 125                 }
126             }
2228b6 127             log.info("更新采集状态");
2fcc1a 128             daPointCollectStatusService.recordStatusList(listGood,listBad, collectTime);
a6de49 129             log.info("采集完成");
2228b6 130         } catch (Exception ex) {
a6de49 131             log.info("采集异常!");
2228b6 132             ex.printStackTrace();
133         }
134     }
135
a6de49 136     public Map<String, Object> getCurrentValue(List<String> pointNos) {
H 137         try {
138             Map<String, Object> data = new HashMap<>();
139             if (CollectionUtils.isEmpty(pointNos)) {
140                 return data;
141             }
142             data.putAll(constantHandle.getCurrent(pointNos));
143             data.putAll(measureHandle.getCurrent(pointNos));
144             data.putAll(calculateHandle.getCurrent(pointNos));
5bf42a 145             data.putAll(cumulateHandle.getCurrent(pointNos));
a6de49 146             return data;
H 147         } catch (Exception ex) {
48d2a2 148             ex.printStackTrace();
a6de49 149             return R.error(ex.getMessage());
H 150         }
151
152     }
153
154     public void setValue(DaPointWriteValueDTO writeValue) throws Exception {
155         DaPointDTO daPointDTO = daPointService.getByNo(writeValue.getPointNo());
156         if (daPointDTO == null) {
157             throw new Exception("点位不存在");
158         }
159         if (PointTypeEnum.CONSTANT.getCode().equals(daPointDTO.getPointType())) {
160             daPointDTO.setDefaultValue(new BigDecimal(writeValue.getPointValue().toString()));
161             daPointService.updateDefaultValue(daPointDTO);
162         } else if (PointTypeEnum.MEASURE_POINT.getCode().equals(daPointDTO.getPointType())) {
163             DaPointDTO mPoint = daPointService.getMeasurePointByNo(daPointDTO.getPointNo());
164             if (DataSourceType.OPCUA.getCode().equals(mPoint.getSourceType())) {
165                 opcUaCollector.setTagData(mPoint.getSourceId(), mPoint.getTagNo(), writeValue.getPointValue().toString(), mPoint.getDataType());
166             } else if (DataSourceType.ModBus.getCode().equals(mPoint.getSourceType())) {
167                 modBusCollector.setTagValue(mPoint.getSourceId(), mPoint.getTagNo(), writeValue.getPointValue().toString());
168             } else if (DataSourceType.KIO.getCode().equals(mPoint.getSourceType())) {
169                 kingIOCollector.setTagValue(mPoint.getSourceId(), mPoint.getTagNo(), writeValue.getPointValue().toString(), mPoint.getDataType());
170             } else {
171                 log.info("没有匹配的TagNo=" + mPoint.getTagNo());
172             }
173         }
174
175     }
176 }