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