dengzedong
2024-12-24 aa0382e44311f9f7e62a688c8fcaa9c69a512e0f
提交 | 用户 | 时间
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<>();
92
93             log.info("读取常量点");
94             List<DaPointDTO> pointConstantList = daPointService.getConstantPoint(minfreq);
95             pointValues.addAll(constantHandle.handle(collectTime, pointConstantList, dataMap));
96
97             log.info("读取测量点");
98             List<DaPointDTO> pointMeasureList = daPointService.getMeasurePoint(minfreq);
99             pointValues.addAll(measureHandle.handle(collectTime, pointMeasureList, dataMap));
100
101             log.info("读取计算点");
102             List<DaPointDTO> pointCalculateList = daPointService.getMathPoint(minfreq);
103             pointValues.addAll(calculateHandle.handle(collectTime, pointCalculateList, dataMap));
104
56dba6 105             log.info("读取累计点");
106             List<DaPointDTO> pointCumulateList = daPointService.getCumulatePoint(minfreq);
107             pointValues.addAll(cumulateHandle.handle(collectTime, pointCumulateList));
108
781e72 109             log.info("存入时序库");
a6de49 110             influxDBService.asyncWritePointValues(pointValues);
H 111
781e72 112             log.info("存入缓存");
113             for (InfluxPointValuePOJO pointValue : pointValues) {
114                 if (pointValue instanceof InfluxPointValueSimPOJO) {
115                     InfluxPointValueSimPOJO simPOJO = (InfluxPointValueSimPOJO) pointValue;
140065 116                     redisTemplate.opsForValue().set(PV + simPOJO.getPoint(), simPOJO.getValue().doubleValue(), offset, TimeUnit.SECONDS);
781e72 117                 } else if (pointValue instanceof InfluxPointValueDigPOJO) {
118                     InfluxPointValueDigPOJO digPOJO = (InfluxPointValueDigPOJO) pointValue;
140065 119                     redisTemplate.opsForValue().set(PV + digPOJO.getPoint(), digPOJO.getValue().intValue(), offset, TimeUnit.SECONDS);
781e72 120                 } else if (pointValue instanceof InfluxPointValueBoolPOJO) {
121                     InfluxPointValueBoolPOJO boolPOJO = (InfluxPointValueBoolPOJO) pointValue;
140065 122                     redisTemplate.opsForValue().set(PV + boolPOJO.getPoint(), boolPOJO.getValue().booleanValue(), offset, TimeUnit.SECONDS);
781e72 123                 }
124             }
2228b6 125             log.info("更新采集状态");
161f55 126             daPointCollectStatusService.recordStatusList(pointValues, collectTime);
a6de49 127             log.info("采集完成");
2228b6 128         } catch (Exception ex) {
a6de49 129             log.info("采集异常!");
2228b6 130             ex.printStackTrace();
131         }
132     }
133
a6de49 134     public Map<String, Object> getCurrentValue(List<String> pointNos) {
H 135         try {
136             Map<String, Object> data = new HashMap<>();
137             if (CollectionUtils.isEmpty(pointNos)) {
138                 return data;
139             }
140             data.putAll(constantHandle.getCurrent(pointNos));
141             data.putAll(measureHandle.getCurrent(pointNos));
142             data.putAll(calculateHandle.getCurrent(pointNos));
5bf42a 143             data.putAll(cumulateHandle.getCurrent(pointNos));
a6de49 144             return data;
H 145         } catch (Exception ex) {
146             return R.error(ex.getMessage());
147         }
148
149     }
150
151     public void setValue(DaPointWriteValueDTO writeValue) throws Exception {
152         DaPointDTO daPointDTO = daPointService.getByNo(writeValue.getPointNo());
153         if (daPointDTO == null) {
154             throw new Exception("点位不存在");
155         }
156         if (PointTypeEnum.CONSTANT.getCode().equals(daPointDTO.getPointType())) {
157             daPointDTO.setDefaultValue(new BigDecimal(writeValue.getPointValue().toString()));
158             daPointService.updateDefaultValue(daPointDTO);
159         } else if (PointTypeEnum.MEASURE_POINT.getCode().equals(daPointDTO.getPointType())) {
160             DaPointDTO mPoint = daPointService.getMeasurePointByNo(daPointDTO.getPointNo());
161             if (DataSourceType.OPCUA.getCode().equals(mPoint.getSourceType())) {
162                 opcUaCollector.setTagData(mPoint.getSourceId(), mPoint.getTagNo(), writeValue.getPointValue().toString(), mPoint.getDataType());
163             } else if (DataSourceType.ModBus.getCode().equals(mPoint.getSourceType())) {
164                 modBusCollector.setTagValue(mPoint.getSourceId(), mPoint.getTagNo(), writeValue.getPointValue().toString());
165             } else if (DataSourceType.KIO.getCode().equals(mPoint.getSourceType())) {
166                 kingIOCollector.setTagValue(mPoint.getSourceId(), mPoint.getTagNo(), writeValue.getPointValue().toString(), mPoint.getDataType());
167             } else {
168                 log.info("没有匹配的TagNo=" + mPoint.getTagNo());
169             }
170         }
171
172     }
173 }