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