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