潘志宝
2024-10-29 d41f14d2986b46da9dd7742f6df63d9725cd29f3
提交 | 用户 | 时间
a6de49 1 package com.iailab.module.data.point.collection.handler;
H 2
d41f14 3 import com.iailab.module.data.channel.http.collector.ihdb.HttpCollectorForIhd;
9d7e02 4 import com.iailab.module.data.channel.opcda.collector.OpcDACollector;
a6de49 5 import com.iailab.module.data.common.enums.CommonConstant;
H 6 import com.iailab.module.data.common.enums.DataSourceType;
7 import com.iailab.module.data.common.enums.DataTypeEnum;
8 import com.iailab.module.data.common.utils.TagUtils;
9 import com.iailab.module.data.channel.kio.collector.KingIOCollector;
10 import com.iailab.module.data.channel.modbus.collector.ModBusCollector;
11 import com.iailab.module.data.channel.opcua.collector.OpcUaCollector;
12 import com.iailab.module.data.point.collection.utils.GenInfluxPointValueUtils;
13 import com.iailab.module.data.point.common.PointDataTypeEnum;
14 import com.iailab.module.data.point.dto.DaPointDTO;
15 import com.iailab.module.data.point.service.DaPointService;
16 import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO;
17 import lombok.extern.slf4j.Slf4j;
18 import javax.annotation.Resource;
9d7e02 19
20 import org.springframework.beans.factory.annotation.Autowired;
a6de49 21 import org.springframework.stereotype.Component;
H 22 import org.springframework.util.CollectionUtils;
23
24 import java.math.BigDecimal;
25 import java.util.*;
26
27 /**
28  * 测量点处理
29  *
30  * @author PanZhibao
31  * @Description
32  * @createTime 2023年05月03日 22:36:00
33  */
34 @Slf4j
35 @Component
36 public class MeasureHandle {
37
38     private BigDecimal maxValue = new BigDecimal("1000000000");
39
40     private BigDecimal minValue = new BigDecimal("0");
41
42     @Resource
43     private ModBusCollector modBusCollector;
44
45     @Resource
46     private KingIOCollector kingIOCollector;
47
48     @Resource
49     private OpcUaCollector opcUaCollector;
50
9d7e02 51     @Autowired
52     private OpcDACollector opcDACollector;
a6de49 53
52487d 54     @Autowired
d41f14 55     private HttpCollectorForIhd httpCollectorForIhd;
52487d 56
a6de49 57     @Resource
H 58     private DaPointService daPointService;
59
60     public List<InfluxPointValuePOJO> handle(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> dataMap) {
61         log.info("测量点处理开始");
62         List<InfluxPointValuePOJO> result = new ArrayList<>();
63         if (CollectionUtils.isEmpty(dtos)) {
64             return result;
65         }
66
9d7e02 67         List<String[]> opcUaTagIds = new ArrayList<>();
a6de49 68         List<String[]> opcDaTagIds = new ArrayList<>();
H 69         List<String[]> modbusTagIds = new ArrayList<>();
70         List<String[]> kioTagIds = new ArrayList<>();
d41f14 71         List<Object[]> httpTagIhd = new ArrayList<>();
a6de49 72
H 73
74         dtos.stream().forEach(item -> {
75             if (DataSourceType.OPCUA.getCode().equals(item.getSourceType())) {
9d7e02 76                 opcUaTagIds.add(new String[]{item.getSourceId(), item.getTagNo()});
a6de49 77             } else if (DataSourceType.OPCDA.getCode().equals(item.getSourceType())) {
H 78                 opcDaTagIds.add(new String[]{item.getSourceId(), item.getTagNo()});
79             } else if (DataSourceType.ModBus.getCode().equals(item.getSourceType())) {
80                 modbusTagIds.add(new String[]{item.getSourceId(), item.getTagNo()});
81             } else if (DataSourceType.KIO.getCode().equals(item.getSourceType())) {
82                 kioTagIds.add(new String[]{item.getSourceId(), item.getTagNo()});
52487d 83             } else if (DataSourceType.HTTP.getCode().equals(item.getSourceType())) {
d41f14 84                 if (CommonConstant.iHyperDB.equals(item.getSourceName())) {
85                     httpTagIhd.add(new Object[]{item.getSourceId(), item.getTagNo(), item.getDimension(), item.getValueType()});
52487d 86                 }
a6de49 87             }
H 88         });
89
9d7e02 90         Map<String, Object> tagValues = new HashMap<>();
91         if (!CollectionUtils.isEmpty(opcUaTagIds)) {
92             tagValues.putAll(opcUaCollector.getTagValues(opcUaTagIds));
a6de49 93         }
H 94         if (!CollectionUtils.isEmpty(opcDaTagIds)) {
9d7e02 95             tagValues.putAll(opcDACollector.getTagValues(modbusTagIds));
a6de49 96         }
H 97         if (!CollectionUtils.isEmpty(modbusTagIds)) {
9d7e02 98             tagValues.putAll(modBusCollector.getTagValues(modbusTagIds));
a6de49 99         }
H 100         if (!CollectionUtils.isEmpty(kioTagIds)) {
9d7e02 101             tagValues.putAll(kingIOCollector.getTagValues(kioTagIds));
52487d 102         }
d41f14 103         if (!CollectionUtils.isEmpty(httpTagIhd)) {
104             tagValues.putAll(httpCollectorForIhd.getTagValues(httpTagIhd));
9d7e02 105         }
106         this.toCommonResult(collectTime, dtos, tagValues, dataMap, result);
a6de49 107         log.info("测量点处理结束");
H 108         return result;
109     }
110
111     private void toCommonResult(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> tagValues,
112                           Map<String, Object> dataMap, List<InfluxPointValuePOJO> result) {
113         if (!CollectionUtils.isEmpty(tagValues)) {
114             tagValues.forEach((k, v) -> {
115                 dataMap.put(k, v);
116             });
117             dtos.forEach(dto -> {
118                 String tagId = TagUtils.genTagId(dto.getSourceType(), dto.getSourceName(), dto.getTagNo());
119                 if (tagValues.get(tagId) != null) {
120                     Object value = handleData(dto, tagValues.get(tagId));
121                     InfluxPointValuePOJO pojo = GenInfluxPointValueUtils.getByPoint(dto, value);
122                     pojo.setTimestamp(collectTime.toInstant());
123                     dataMap.put(dto.getPointNo(), value);
124                     result.add(pojo);
125                 } else {
126                     System.out.println("值异常!TagId=" + tagId);
127                 }
128             });
129         }
130     }
131
132     private Object handleData(DaPointDTO dto, Object value) {
133         Object result = value;
134         try {
135             if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType()) || DataTypeEnum.INT.getCode().equals(dto.getDataType())) {
136                 BigDecimal rawValue = new BigDecimal(value.toString());
137
138                 // 异常值处理
139                 if (rawValue.compareTo(maxValue) > 0 || rawValue.compareTo(minValue) < 0) {
140                     rawValue = CommonConstant.BAD_VALUE;
141                 }
142                 BigDecimal coefficient = dto.getUnittransfactor() == null ? BigDecimal.ONE : dto.getUnittransfactor();
143                 BigDecimal calValue = rawValue.multiply(coefficient);
144                 if (dto.getMaxValue() != null && calValue.compareTo(dto.getMaxValue()) > 0) {
145                     result = dto.getMaxValue();
146                 } else if (dto.getMinValue() != null && calValue.compareTo(dto.getMinValue()) < 0) {
147                     result = dto.getMinValue();
148                 } else {
149                     result = calValue;
150                 }
151                 if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType())) {
152                     result = ((BigDecimal) result).doubleValue();
153                 } else {
154                     result = ((BigDecimal) result).intValue();
155                 }
156             } else if (DataTypeEnum.BOOLEAN.getCode().equals(dto.getDataType())) {
157                 result = Boolean.parseBoolean(value.toString());
158             }
159
160         } catch (Exception ex) {
161             log.warn("handleData异常,PointNo=" + dto.getPointNo());
162             ex.printStackTrace();
163         }
164         return result;
165     }
166
167     public Map<String, Object> getCurrent(List<String> pointNos) {
168         Map<String, Object> data = new HashMap<>();
169         List<DaPointDTO> pointMeasureList = daPointService.getMeasurePoint(pointNos);
170         pointMeasureList.forEach(
171                 item -> {
172                     try {
173                         Object value = CommonConstant.BAD_VALUE;
174                         if (DataSourceType.OPCUA.getCode().equals(item.getSourceType())) {
175                             value = opcUaCollector.getTagValue(item.getSourceId(), item.getTagNo());
176                         } else if (DataSourceType.ModBus.getCode().equals(item.getSourceType())) {
177                             value = modBusCollector.getTagValue(item.getSourceId(), item.getTagNo());
178                         } else if (DataSourceType.KIO.getCode().equals(item.getSourceType())) {
179                             value = kingIOCollector.getTagValue(item.getSourceId(), item.getTagNo());
180                         } else if (DataSourceType.HTTP.getCode().equals(item.getSourceType())) {
d41f14 181                             value = httpCollectorForIhd.getTagValue(item.getSourceId(), item.getTagNo(), item.getDimension(), item.getValueType());
a6de49 182                         } else {
H 183                             log.info("没有匹配的TagNo=" + item.getTagNo());
184                         }
185                         log.info("没有匹配的TagNo=" + item.getTagNo());
186                         log.info("valueStr=" + value.toString());
187                         log.info("DataType=" + item.getDataType());
188                         if (!PointDataTypeEnum.BOOLEAN.getCode().equals(item.getDataType())) {
189                             BigDecimal decValue =  new BigDecimal(value.toString());
190                             if (PointDataTypeEnum.FLOAT.getCode().equals(item.getDataType())) {
191                                 decValue = decValue.setScale(2, BigDecimal.ROUND_HALF_UP);
192                             } else if (PointDataTypeEnum.INT.getCode().equals(item.getDataType())) {
193                                 decValue = decValue.setScale(0, BigDecimal.ROUND_HALF_UP);
194                             }
195                             data.put(item.getPointNo(), decValue);
196                         } else {
197                             data.put(item.getPointNo(), value);
198                         }
199                     } catch (Exception ex) {
200                         ex.printStackTrace();
201                     }
202
203                 }
204         );
205         return data;
206     }
207 }