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