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