潘志宝
7 天以前 bbe7acfbe5a4c08d6edc91eaf81dcecf9d630e18
提交 | 用户 | 时间
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("更新采集状态");
161f55 102             daPointCollectStatusService.recordStatusList(pointValues, collectTime);
a6de49 103             log.info("采集完成");
2228b6 104         } catch (Exception ex) {
a6de49 105             log.info("采集异常!");
2228b6 106             ex.printStackTrace();
107         }
108     }
109
a6de49 110     public Map<String, Object> getCurrentValue(List<String> pointNos) {
H 111         try {
112             Map<String, Object> data = new HashMap<>();
113             if (CollectionUtils.isEmpty(pointNos)) {
114                 return data;
115             }
116             data.putAll(constantHandle.getCurrent(pointNos));
117             data.putAll(measureHandle.getCurrent(pointNos));
118             data.putAll(calculateHandle.getCurrent(pointNos));
119             return data;
120         } catch (Exception ex) {
121             return R.error(ex.getMessage());
122         }
123
124     }
125
126     public void setValue(DaPointWriteValueDTO writeValue) throws Exception {
127         DaPointDTO daPointDTO = daPointService.getByNo(writeValue.getPointNo());
128         if (daPointDTO == null) {
129             throw new Exception("点位不存在");
130         }
131         if (PointTypeEnum.CONSTANT.getCode().equals(daPointDTO.getPointType())) {
132             daPointDTO.setDefaultValue(new BigDecimal(writeValue.getPointValue().toString()));
133             daPointService.updateDefaultValue(daPointDTO);
134         } else if (PointTypeEnum.MEASURE_POINT.getCode().equals(daPointDTO.getPointType())) {
135             DaPointDTO mPoint = daPointService.getMeasurePointByNo(daPointDTO.getPointNo());
136             if (DataSourceType.OPCUA.getCode().equals(mPoint.getSourceType())) {
137                 opcUaCollector.setTagData(mPoint.getSourceId(), mPoint.getTagNo(), writeValue.getPointValue().toString(), mPoint.getDataType());
138             } else if (DataSourceType.ModBus.getCode().equals(mPoint.getSourceType())) {
139                 modBusCollector.setTagValue(mPoint.getSourceId(), mPoint.getTagNo(), writeValue.getPointValue().toString());
140             } else if (DataSourceType.KIO.getCode().equals(mPoint.getSourceType())) {
141                 kingIOCollector.setTagValue(mPoint.getSourceId(), mPoint.getTagNo(), writeValue.getPointValue().toString(), mPoint.getDataType());
142             } else {
143                 log.info("没有匹配的TagNo=" + mPoint.getTagNo());
144             }
145         }
146
147     }
148 }