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