dongyukun
6 天以前 ed8fc5f674544d3af63c6f68093ffc038385c493
提交 | 用户 | 时间
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())) {
c4dc2a 86                     if (item.getTagNo() != null && item.getDimension() != null && item.getValueType() != null) {
87                         httpTagIhd.add(new Object[]{item.getSourceId(), item.getTagNo(), item.getDimension(), item.getValueType()});
88                     }
52487d 89                 }
a6de49 90             }
H 91         });
92
9d7e02 93         Map<String, Object> tagValues = new HashMap<>();
94         if (!CollectionUtils.isEmpty(opcUaTagIds)) {
95             tagValues.putAll(opcUaCollector.getTagValues(opcUaTagIds));
a6de49 96         }
H 97         if (!CollectionUtils.isEmpty(opcDaTagIds)) {
9d7e02 98             tagValues.putAll(opcDACollector.getTagValues(modbusTagIds));
a6de49 99         }
H 100         if (!CollectionUtils.isEmpty(modbusTagIds)) {
9d7e02 101             tagValues.putAll(modBusCollector.getTagValues(modbusTagIds));
a6de49 102         }
H 103         if (!CollectionUtils.isEmpty(kioTagIds)) {
9d7e02 104             tagValues.putAll(kingIOCollector.getTagValues(kioTagIds));
52487d 105         }
d41f14 106         if (!CollectionUtils.isEmpty(httpTagIhd)) {
0a2804 107             tagValues.putAll(httpCollectorForIhd.getTagValues(httpTagIhd, collectTime));
9d7e02 108         }
109         this.toCommonResult(collectTime, dtos, tagValues, dataMap, result);
a6de49 110         log.info("测量点处理结束");
H 111         return result;
112     }
113
114     private void toCommonResult(Date collectTime, List<DaPointDTO> dtos, Map<String, Object> tagValues,
115                           Map<String, Object> dataMap, List<InfluxPointValuePOJO> result) {
116         if (!CollectionUtils.isEmpty(tagValues)) {
117             tagValues.forEach((k, v) -> {
118                 dataMap.put(k, v);
119             });
120             dtos.forEach(dto -> {
121                 String tagId = TagUtils.genTagId(dto.getSourceType(), dto.getSourceName(), dto.getTagNo());
122                 if (tagValues.get(tagId) != null) {
123                     Object value = handleData(dto, tagValues.get(tagId));
124                     InfluxPointValuePOJO pojo = GenInfluxPointValueUtils.getByPoint(dto, value);
125                     pojo.setTimestamp(collectTime.toInstant());
126                     dataMap.put(dto.getPointNo(), value);
127                     result.add(pojo);
128                 } else {
129                     System.out.println("值异常!TagId=" + tagId);
130                 }
131             });
132         }
133     }
134
135     private Object handleData(DaPointDTO dto, Object value) {
136         Object result = value;
137         try {
2228b6 138             if (value == null) {
139                 return CommonConstant.BAD_VALUE;
140             }
a6de49 141             if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType()) || DataTypeEnum.INT.getCode().equals(dto.getDataType())) {
H 142                 BigDecimal rawValue = new BigDecimal(value.toString());
c4dc2a 143                 if(CommonConstant.BAD_VALUE.compareTo(rawValue) == 0) {
144                     return CommonConstant.BAD_VALUE;
145                 }
a6de49 146                 // 异常值处理
H 147                 if (rawValue.compareTo(maxValue) > 0 || rawValue.compareTo(minValue) < 0) {
c4dc2a 148                     return CommonConstant.BAD_VALUE;
a6de49 149                 }
H 150                 BigDecimal coefficient = dto.getUnittransfactor() == null ? BigDecimal.ONE : dto.getUnittransfactor();
151                 BigDecimal calValue = rawValue.multiply(coefficient);
152                 if (dto.getMaxValue() != null && calValue.compareTo(dto.getMaxValue()) > 0) {
153                     result = dto.getMaxValue();
154                 } else if (dto.getMinValue() != null && calValue.compareTo(dto.getMinValue()) < 0) {
155                     result = dto.getMinValue();
156                 } else {
157                     result = calValue;
158                 }
159                 if (DataTypeEnum.FLOAT.getCode().equals(dto.getDataType())) {
160                     result = ((BigDecimal) result).doubleValue();
161                 } else {
162                     result = ((BigDecimal) result).intValue();
163                 }
164             } else if (DataTypeEnum.BOOLEAN.getCode().equals(dto.getDataType())) {
165                 result = Boolean.parseBoolean(value.toString());
166             }
167         } catch (Exception ex) {
168             log.warn("handleData异常,PointNo=" + dto.getPointNo());
169             ex.printStackTrace();
170         }
171         return result;
172     }
173
174     public Map<String, Object> getCurrent(List<String> pointNos) {
175         Map<String, Object> data = new HashMap<>();
176         List<DaPointDTO> pointMeasureList = daPointService.getMeasurePoint(pointNos);
177         pointMeasureList.forEach(
178                 item -> {
179                     try {
180                         Object value = CommonConstant.BAD_VALUE;
181                         if (DataSourceType.OPCUA.getCode().equals(item.getSourceType())) {
182                             value = opcUaCollector.getTagValue(item.getSourceId(), item.getTagNo());
183                         } else if (DataSourceType.ModBus.getCode().equals(item.getSourceType())) {
184                             value = modBusCollector.getTagValue(item.getSourceId(), item.getTagNo());
185                         } else if (DataSourceType.KIO.getCode().equals(item.getSourceType())) {
186                             value = kingIOCollector.getTagValue(item.getSourceId(), item.getTagNo());
187                         } else if (DataSourceType.HTTP.getCode().equals(item.getSourceType())) {
d41f14 188                             value = httpCollectorForIhd.getTagValue(item.getSourceId(), item.getTagNo(), item.getDimension(), item.getValueType());
a6de49 189                         } else {
H 190                             log.info("没有匹配的TagNo=" + item.getTagNo());
191                         }
192                         log.info("没有匹配的TagNo=" + item.getTagNo());
193                         log.info("valueStr=" + value.toString());
194                         log.info("DataType=" + item.getDataType());
195                         if (!PointDataTypeEnum.BOOLEAN.getCode().equals(item.getDataType())) {
196                             BigDecimal decValue =  new BigDecimal(value.toString());
197                             if (PointDataTypeEnum.FLOAT.getCode().equals(item.getDataType())) {
198                                 decValue = decValue.setScale(2, BigDecimal.ROUND_HALF_UP);
199                             } else if (PointDataTypeEnum.INT.getCode().equals(item.getDataType())) {
200                                 decValue = decValue.setScale(0, BigDecimal.ROUND_HALF_UP);
201                             }
202                             data.put(item.getPointNo(), decValue);
203                         } else {
204                             data.put(item.getPointNo(), value);
205                         }
206                     } catch (Exception ex) {
207                         ex.printStackTrace();
208                     }
209
210                 }
211         );
212         return data;
213     }
214 }