dengzedong
2024-12-24 76743b009ca5ea67557fcab597b332f8d1947813
提交 | 用户 | 时间
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;
32 import java.util.*;
33
34 /**
35  * @author PanZhibao
36  * @Description
37  * @createTime 2023年04月25日 16:16:00
38  */
39 @Slf4j
40 @Component
41 public class PointCollector {
42
43     @Resource
44     private DaPointService daPointService;
45
46     @Resource
47     private ConstantHandle constantHandle;
48
49     @Resource
50     private MeasureHandle measureHandle;
51
52     @Resource
53     private CalculateHandle calculateHandle;
54
55     @Resource
56     private KingIOCollector kingIOCollector;
57
58     @Resource
59     private InfluxDBService influxDBService;
60
61     @Resource
62     private ModBusCollector modBusCollector;
63
64     @Resource
65     private OpcUaCollector opcUaCollector;
66
56dba6 67     @Resource
68     private CumulateHandle cumulateHandle;
69
2228b6 70     @Autowired
71     private DaPointCollectStatusService daPointCollectStatusService;
72
781e72 73     @Autowired
74     private RedisTemplate<String, Object> redisTemplate;
75
76     public static final String PV = "PV_";
77
78     public static final int offset = 60 * 3;
79
a6de49 80     /**
H 81      * 采集
82      *
83      * @param collectTime
84      * @param minfreq
85      */
86     public void collect(Date collectTime, String minfreq) {
87         try {
88             Map<String, Object> dataMap = new HashMap<>();
89             List<InfluxPointValuePOJO> pointValues = new ArrayList<>();
90
91             log.info("读取常量点");
92             List<DaPointDTO> pointConstantList = daPointService.getConstantPoint(minfreq);
93             pointValues.addAll(constantHandle.handle(collectTime, pointConstantList, dataMap));
94
95             log.info("读取测量点");
96             List<DaPointDTO> pointMeasureList = daPointService.getMeasurePoint(minfreq);
97             pointValues.addAll(measureHandle.handle(collectTime, pointMeasureList, dataMap));
98
99             log.info("读取计算点");
100             List<DaPointDTO> pointCalculateList = daPointService.getMathPoint(minfreq);
101             pointValues.addAll(calculateHandle.handle(collectTime, pointCalculateList, dataMap));
102
56dba6 103             log.info("读取累计点");
104             List<DaPointDTO> pointCumulateList = daPointService.getCumulatePoint(minfreq);
105             pointValues.addAll(cumulateHandle.handle(collectTime, pointCumulateList));
106
781e72 107             log.info("存入时序库");
a6de49 108             influxDBService.asyncWritePointValues(pointValues);
H 109
781e72 110             log.info("存入缓存");
111             for (InfluxPointValuePOJO pointValue : pointValues) {
112                 if (pointValue instanceof InfluxPointValueSimPOJO) {
113                     InfluxPointValueSimPOJO simPOJO = (InfluxPointValueSimPOJO) pointValue;
114                     redisTemplate.opsForValue().set(PV + simPOJO.getPoint(), simPOJO.getValue(), offset);
115                 } else if (pointValue instanceof InfluxPointValueDigPOJO) {
116                     InfluxPointValueDigPOJO digPOJO = (InfluxPointValueDigPOJO) pointValue;
117                     redisTemplate.opsForValue().set(PV + digPOJO.getPoint(), digPOJO.getValue(), offset);
118                 } else if (pointValue instanceof InfluxPointValueBoolPOJO) {
119                     InfluxPointValueBoolPOJO boolPOJO = (InfluxPointValueBoolPOJO) pointValue;
120                     redisTemplate.opsForValue().set(PV + boolPOJO.getPoint(), boolPOJO.getValue(), offset);
121                 }
122             }
2228b6 123             log.info("更新采集状态");
161f55 124             daPointCollectStatusService.recordStatusList(pointValues, collectTime);
a6de49 125             log.info("采集完成");
2228b6 126         } catch (Exception ex) {
a6de49 127             log.info("采集异常!");
2228b6 128             ex.printStackTrace();
129         }
130     }
131
a6de49 132     public Map<String, Object> getCurrentValue(List<String> pointNos) {
H 133         try {
134             Map<String, Object> data = new HashMap<>();
135             if (CollectionUtils.isEmpty(pointNos)) {
136                 return data;
137             }
138             data.putAll(constantHandle.getCurrent(pointNos));
139             data.putAll(measureHandle.getCurrent(pointNos));
140             data.putAll(calculateHandle.getCurrent(pointNos));
5bf42a 141             data.putAll(cumulateHandle.getCurrent(pointNos));
a6de49 142             return data;
H 143         } catch (Exception ex) {
144             return R.error(ex.getMessage());
145         }
146
147     }
148
149     public void setValue(DaPointWriteValueDTO writeValue) throws Exception {
150         DaPointDTO daPointDTO = daPointService.getByNo(writeValue.getPointNo());
151         if (daPointDTO == null) {
152             throw new Exception("点位不存在");
153         }
154         if (PointTypeEnum.CONSTANT.getCode().equals(daPointDTO.getPointType())) {
155             daPointDTO.setDefaultValue(new BigDecimal(writeValue.getPointValue().toString()));
156             daPointService.updateDefaultValue(daPointDTO);
157         } else if (PointTypeEnum.MEASURE_POINT.getCode().equals(daPointDTO.getPointType())) {
158             DaPointDTO mPoint = daPointService.getMeasurePointByNo(daPointDTO.getPointNo());
159             if (DataSourceType.OPCUA.getCode().equals(mPoint.getSourceType())) {
160                 opcUaCollector.setTagData(mPoint.getSourceId(), mPoint.getTagNo(), writeValue.getPointValue().toString(), mPoint.getDataType());
161             } else if (DataSourceType.ModBus.getCode().equals(mPoint.getSourceType())) {
162                 modBusCollector.setTagValue(mPoint.getSourceId(), mPoint.getTagNo(), writeValue.getPointValue().toString());
163             } else if (DataSourceType.KIO.getCode().equals(mPoint.getSourceType())) {
164                 kingIOCollector.setTagValue(mPoint.getSourceId(), mPoint.getTagNo(), writeValue.getPointValue().toString(), mPoint.getDataType());
165             } else {
166                 log.info("没有匹配的TagNo=" + mPoint.getTagNo());
167             }
168         }
169
170     }
171 }